]> asedeno.scripts.mit.edu Git - linux.git/blob - kernel/trace/trace_events_hist.c
4f4759c6e9726465aa6f573ec667f65a620413ed
[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->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 static int synth_event_create(int argc, const char **argv)
1388 {
1389         const char *name = argv[0];
1390         int len;
1391
1392         if (name[0] != 's' || name[1] != ':')
1393                 return -ECANCELED;
1394         name += 2;
1395
1396         /* This interface accepts group name prefix */
1397         if (strchr(name, '/')) {
1398                 len = str_has_prefix(name, SYNTH_SYSTEM "/");
1399                 if (len == 0)
1400                         return -EINVAL;
1401                 name += len;
1402         }
1403         return __create_synth_event(argc - 1, name, argv + 1);
1404 }
1405
1406 static int synth_event_release(struct dyn_event *ev)
1407 {
1408         struct synth_event *event = to_synth_event(ev);
1409         int ret;
1410
1411         if (event->ref)
1412                 return -EBUSY;
1413
1414         ret = unregister_synth_event(event);
1415         if (ret)
1416                 return ret;
1417
1418         dyn_event_remove(ev);
1419         free_synth_event(event);
1420         return 0;
1421 }
1422
1423 static int __synth_event_show(struct seq_file *m, struct synth_event *event)
1424 {
1425         struct synth_field *field;
1426         unsigned int i;
1427
1428         seq_printf(m, "%s\t", event->name);
1429
1430         for (i = 0; i < event->n_fields; i++) {
1431                 field = event->fields[i];
1432
1433                 /* parameter values */
1434                 seq_printf(m, "%s %s%s", field->type, field->name,
1435                            i == event->n_fields - 1 ? "" : "; ");
1436         }
1437
1438         seq_putc(m, '\n');
1439
1440         return 0;
1441 }
1442
1443 static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
1444 {
1445         struct synth_event *event = to_synth_event(ev);
1446
1447         seq_printf(m, "s:%s/", event->class.system);
1448
1449         return __synth_event_show(m, event);
1450 }
1451
1452 static int synth_events_seq_show(struct seq_file *m, void *v)
1453 {
1454         struct dyn_event *ev = v;
1455
1456         if (!is_synth_event(ev))
1457                 return 0;
1458
1459         return __synth_event_show(m, to_synth_event(ev));
1460 }
1461
1462 static const struct seq_operations synth_events_seq_op = {
1463         .start  = dyn_event_seq_start,
1464         .next   = dyn_event_seq_next,
1465         .stop   = dyn_event_seq_stop,
1466         .show   = synth_events_seq_show,
1467 };
1468
1469 static int synth_events_open(struct inode *inode, struct file *file)
1470 {
1471         int ret;
1472
1473         ret = security_locked_down(LOCKDOWN_TRACEFS);
1474         if (ret)
1475                 return ret;
1476
1477         if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
1478                 ret = dyn_events_release_all(&synth_event_ops);
1479                 if (ret < 0)
1480                         return ret;
1481         }
1482
1483         return seq_open(file, &synth_events_seq_op);
1484 }
1485
1486 static ssize_t synth_events_write(struct file *file,
1487                                   const char __user *buffer,
1488                                   size_t count, loff_t *ppos)
1489 {
1490         return trace_parse_run_command(file, buffer, count, ppos,
1491                                        create_or_delete_synth_event);
1492 }
1493
1494 static const struct file_operations synth_events_fops = {
1495         .open           = synth_events_open,
1496         .write          = synth_events_write,
1497         .read           = seq_read,
1498         .llseek         = seq_lseek,
1499         .release        = seq_release,
1500 };
1501
1502 static u64 hist_field_timestamp(struct hist_field *hist_field,
1503                                 struct tracing_map_elt *elt,
1504                                 struct ring_buffer_event *rbe,
1505                                 void *event)
1506 {
1507         struct hist_trigger_data *hist_data = hist_field->hist_data;
1508         struct trace_array *tr = hist_data->event_file->tr;
1509
1510         u64 ts = ring_buffer_event_time_stamp(rbe);
1511
1512         if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
1513                 ts = ns2usecs(ts);
1514
1515         return ts;
1516 }
1517
1518 static u64 hist_field_cpu(struct hist_field *hist_field,
1519                           struct tracing_map_elt *elt,
1520                           struct ring_buffer_event *rbe,
1521                           void *event)
1522 {
1523         int cpu = smp_processor_id();
1524
1525         return cpu;
1526 }
1527
1528 /**
1529  * check_field_for_var_ref - Check if a VAR_REF field references a variable
1530  * @hist_field: The VAR_REF field to check
1531  * @var_data: The hist trigger that owns the variable
1532  * @var_idx: The trigger variable identifier
1533  *
1534  * Check the given VAR_REF field to see whether or not it references
1535  * the given variable associated with the given trigger.
1536  *
1537  * Return: The VAR_REF field if it does reference the variable, NULL if not
1538  */
1539 static struct hist_field *
1540 check_field_for_var_ref(struct hist_field *hist_field,
1541                         struct hist_trigger_data *var_data,
1542                         unsigned int var_idx)
1543 {
1544         WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
1545
1546         if (hist_field && hist_field->var.idx == var_idx &&
1547             hist_field->var.hist_data == var_data)
1548                 return hist_field;
1549
1550         return NULL;
1551 }
1552
1553 /**
1554  * find_var_ref - Check if a trigger has a reference to a trigger variable
1555  * @hist_data: The hist trigger that might have a reference to the variable
1556  * @var_data: The hist trigger that owns the variable
1557  * @var_idx: The trigger variable identifier
1558  *
1559  * Check the list of var_refs[] on the first hist trigger to see
1560  * whether any of them are references to the variable on the second
1561  * trigger.
1562  *
1563  * Return: The VAR_REF field referencing the variable if so, NULL if not
1564  */
1565 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
1566                                        struct hist_trigger_data *var_data,
1567                                        unsigned int var_idx)
1568 {
1569         struct hist_field *hist_field;
1570         unsigned int i;
1571
1572         for (i = 0; i < hist_data->n_var_refs; i++) {
1573                 hist_field = hist_data->var_refs[i];
1574                 if (check_field_for_var_ref(hist_field, var_data, var_idx))
1575                         return hist_field;
1576         }
1577
1578         return NULL;
1579 }
1580
1581 /**
1582  * find_any_var_ref - Check if there is a reference to a given trigger variable
1583  * @hist_data: The hist trigger
1584  * @var_idx: The trigger variable identifier
1585  *
1586  * Check to see whether the given variable is currently referenced by
1587  * any other trigger.
1588  *
1589  * The trigger the variable is defined on is explicitly excluded - the
1590  * assumption being that a self-reference doesn't prevent a trigger
1591  * from being removed.
1592  *
1593  * Return: The VAR_REF field referencing the variable if so, NULL if not
1594  */
1595 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
1596                                            unsigned int var_idx)
1597 {
1598         struct trace_array *tr = hist_data->event_file->tr;
1599         struct hist_field *found = NULL;
1600         struct hist_var_data *var_data;
1601
1602         list_for_each_entry(var_data, &tr->hist_vars, list) {
1603                 if (var_data->hist_data == hist_data)
1604                         continue;
1605                 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
1606                 if (found)
1607                         break;
1608         }
1609
1610         return found;
1611 }
1612
1613 /**
1614  * check_var_refs - Check if there is a reference to any of trigger's variables
1615  * @hist_data: The hist trigger
1616  *
1617  * A trigger can define one or more variables.  If any one of them is
1618  * currently referenced by any other trigger, this function will
1619  * determine that.
1620
1621  * Typically used to determine whether or not a trigger can be removed
1622  * - if there are any references to a trigger's variables, it cannot.
1623  *
1624  * Return: True if there is a reference to any of trigger's variables
1625  */
1626 static bool check_var_refs(struct hist_trigger_data *hist_data)
1627 {
1628         struct hist_field *field;
1629         bool found = false;
1630         int i;
1631
1632         for_each_hist_field(i, hist_data) {
1633                 field = hist_data->fields[i];
1634                 if (field && field->flags & HIST_FIELD_FL_VAR) {
1635                         if (find_any_var_ref(hist_data, field->var.idx)) {
1636                                 found = true;
1637                                 break;
1638                         }
1639                 }
1640         }
1641
1642         return found;
1643 }
1644
1645 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
1646 {
1647         struct trace_array *tr = hist_data->event_file->tr;
1648         struct hist_var_data *var_data, *found = NULL;
1649
1650         list_for_each_entry(var_data, &tr->hist_vars, list) {
1651                 if (var_data->hist_data == hist_data) {
1652                         found = var_data;
1653                         break;
1654                 }
1655         }
1656
1657         return found;
1658 }
1659
1660 static bool field_has_hist_vars(struct hist_field *hist_field,
1661                                 unsigned int level)
1662 {
1663         int i;
1664
1665         if (level > 3)
1666                 return false;
1667
1668         if (!hist_field)
1669                 return false;
1670
1671         if (hist_field->flags & HIST_FIELD_FL_VAR ||
1672             hist_field->flags & HIST_FIELD_FL_VAR_REF)
1673                 return true;
1674
1675         for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
1676                 struct hist_field *operand;
1677
1678                 operand = hist_field->operands[i];
1679                 if (field_has_hist_vars(operand, level + 1))
1680                         return true;
1681         }
1682
1683         return false;
1684 }
1685
1686 static bool has_hist_vars(struct hist_trigger_data *hist_data)
1687 {
1688         struct hist_field *hist_field;
1689         int i;
1690
1691         for_each_hist_field(i, hist_data) {
1692                 hist_field = hist_data->fields[i];
1693                 if (field_has_hist_vars(hist_field, 0))
1694                         return true;
1695         }
1696
1697         return false;
1698 }
1699
1700 static int save_hist_vars(struct hist_trigger_data *hist_data)
1701 {
1702         struct trace_array *tr = hist_data->event_file->tr;
1703         struct hist_var_data *var_data;
1704
1705         var_data = find_hist_vars(hist_data);
1706         if (var_data)
1707                 return 0;
1708
1709         if (tracing_check_open_get_tr(tr))
1710                 return -ENODEV;
1711
1712         var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
1713         if (!var_data) {
1714                 trace_array_put(tr);
1715                 return -ENOMEM;
1716         }
1717
1718         var_data->hist_data = hist_data;
1719         list_add(&var_data->list, &tr->hist_vars);
1720
1721         return 0;
1722 }
1723
1724 static void remove_hist_vars(struct hist_trigger_data *hist_data)
1725 {
1726         struct trace_array *tr = hist_data->event_file->tr;
1727         struct hist_var_data *var_data;
1728
1729         var_data = find_hist_vars(hist_data);
1730         if (!var_data)
1731                 return;
1732
1733         if (WARN_ON(check_var_refs(hist_data)))
1734                 return;
1735
1736         list_del(&var_data->list);
1737
1738         kfree(var_data);
1739
1740         trace_array_put(tr);
1741 }
1742
1743 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
1744                                          const char *var_name)
1745 {
1746         struct hist_field *hist_field, *found = NULL;
1747         int i;
1748
1749         for_each_hist_field(i, hist_data) {
1750                 hist_field = hist_data->fields[i];
1751                 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
1752                     strcmp(hist_field->var.name, var_name) == 0) {
1753                         found = hist_field;
1754                         break;
1755                 }
1756         }
1757
1758         return found;
1759 }
1760
1761 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
1762                                    struct trace_event_file *file,
1763                                    const char *var_name)
1764 {
1765         struct hist_trigger_data *test_data;
1766         struct event_trigger_data *test;
1767         struct hist_field *hist_field;
1768
1769         hist_field = find_var_field(hist_data, var_name);
1770         if (hist_field)
1771                 return hist_field;
1772
1773         list_for_each_entry_rcu(test, &file->triggers, list) {
1774                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1775                         test_data = test->private_data;
1776                         hist_field = find_var_field(test_data, var_name);
1777                         if (hist_field)
1778                                 return hist_field;
1779                 }
1780         }
1781
1782         return NULL;
1783 }
1784
1785 static struct trace_event_file *find_var_file(struct trace_array *tr,
1786                                               char *system,
1787                                               char *event_name,
1788                                               char *var_name)
1789 {
1790         struct hist_trigger_data *var_hist_data;
1791         struct hist_var_data *var_data;
1792         struct trace_event_file *file, *found = NULL;
1793
1794         if (system)
1795                 return find_event_file(tr, system, event_name);
1796
1797         list_for_each_entry(var_data, &tr->hist_vars, list) {
1798                 var_hist_data = var_data->hist_data;
1799                 file = var_hist_data->event_file;
1800                 if (file == found)
1801                         continue;
1802
1803                 if (find_var_field(var_hist_data, var_name)) {
1804                         if (found) {
1805                                 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name));
1806                                 return NULL;
1807                         }
1808
1809                         found = file;
1810                 }
1811         }
1812
1813         return found;
1814 }
1815
1816 static struct hist_field *find_file_var(struct trace_event_file *file,
1817                                         const char *var_name)
1818 {
1819         struct hist_trigger_data *test_data;
1820         struct event_trigger_data *test;
1821         struct hist_field *hist_field;
1822
1823         list_for_each_entry_rcu(test, &file->triggers, list) {
1824                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1825                         test_data = test->private_data;
1826                         hist_field = find_var_field(test_data, var_name);
1827                         if (hist_field)
1828                                 return hist_field;
1829                 }
1830         }
1831
1832         return NULL;
1833 }
1834
1835 static struct hist_field *
1836 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
1837 {
1838         struct trace_array *tr = hist_data->event_file->tr;
1839         struct hist_field *hist_field, *found = NULL;
1840         struct trace_event_file *file;
1841         unsigned int i;
1842
1843         for (i = 0; i < hist_data->n_actions; i++) {
1844                 struct action_data *data = hist_data->actions[i];
1845
1846                 if (data->handler == HANDLER_ONMATCH) {
1847                         char *system = data->match_data.event_system;
1848                         char *event_name = data->match_data.event;
1849
1850                         file = find_var_file(tr, system, event_name, var_name);
1851                         if (!file)
1852                                 continue;
1853                         hist_field = find_file_var(file, var_name);
1854                         if (hist_field) {
1855                                 if (found) {
1856                                         hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE,
1857                                                  errpos(var_name));
1858                                         return ERR_PTR(-EINVAL);
1859                                 }
1860
1861                                 found = hist_field;
1862                         }
1863                 }
1864         }
1865         return found;
1866 }
1867
1868 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
1869                                          char *system,
1870                                          char *event_name,
1871                                          char *var_name)
1872 {
1873         struct trace_array *tr = hist_data->event_file->tr;
1874         struct hist_field *hist_field = NULL;
1875         struct trace_event_file *file;
1876
1877         if (!system || !event_name) {
1878                 hist_field = find_match_var(hist_data, var_name);
1879                 if (IS_ERR(hist_field))
1880                         return NULL;
1881                 if (hist_field)
1882                         return hist_field;
1883         }
1884
1885         file = find_var_file(tr, system, event_name, var_name);
1886         if (!file)
1887                 return NULL;
1888
1889         hist_field = find_file_var(file, var_name);
1890
1891         return hist_field;
1892 }
1893
1894 static u64 hist_field_var_ref(struct hist_field *hist_field,
1895                               struct tracing_map_elt *elt,
1896                               struct ring_buffer_event *rbe,
1897                               void *event)
1898 {
1899         struct hist_elt_data *elt_data;
1900         u64 var_val = 0;
1901
1902         if (WARN_ON_ONCE(!elt))
1903                 return var_val;
1904
1905         elt_data = elt->private_data;
1906         var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
1907
1908         return var_val;
1909 }
1910
1911 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
1912                              u64 *var_ref_vals, bool self)
1913 {
1914         struct hist_trigger_data *var_data;
1915         struct tracing_map_elt *var_elt;
1916         struct hist_field *hist_field;
1917         unsigned int i, var_idx;
1918         bool resolved = true;
1919         u64 var_val = 0;
1920
1921         for (i = 0; i < hist_data->n_var_refs; i++) {
1922                 hist_field = hist_data->var_refs[i];
1923                 var_idx = hist_field->var.idx;
1924                 var_data = hist_field->var.hist_data;
1925
1926                 if (var_data == NULL) {
1927                         resolved = false;
1928                         break;
1929                 }
1930
1931                 if ((self && var_data != hist_data) ||
1932                     (!self && var_data == hist_data))
1933                         continue;
1934
1935                 var_elt = tracing_map_lookup(var_data->map, key);
1936                 if (!var_elt) {
1937                         resolved = false;
1938                         break;
1939                 }
1940
1941                 if (!tracing_map_var_set(var_elt, var_idx)) {
1942                         resolved = false;
1943                         break;
1944                 }
1945
1946                 if (self || !hist_field->read_once)
1947                         var_val = tracing_map_read_var(var_elt, var_idx);
1948                 else
1949                         var_val = tracing_map_read_var_once(var_elt, var_idx);
1950
1951                 var_ref_vals[i] = var_val;
1952         }
1953
1954         return resolved;
1955 }
1956
1957 static const char *hist_field_name(struct hist_field *field,
1958                                    unsigned int level)
1959 {
1960         const char *field_name = "";
1961
1962         if (level > 1)
1963                 return field_name;
1964
1965         if (field->field)
1966                 field_name = field->field->name;
1967         else if (field->flags & HIST_FIELD_FL_LOG2 ||
1968                  field->flags & HIST_FIELD_FL_ALIAS)
1969                 field_name = hist_field_name(field->operands[0], ++level);
1970         else if (field->flags & HIST_FIELD_FL_CPU)
1971                 field_name = "cpu";
1972         else if (field->flags & HIST_FIELD_FL_EXPR ||
1973                  field->flags & HIST_FIELD_FL_VAR_REF) {
1974                 if (field->system) {
1975                         static char full_name[MAX_FILTER_STR_VAL];
1976
1977                         strcat(full_name, field->system);
1978                         strcat(full_name, ".");
1979                         strcat(full_name, field->event_name);
1980                         strcat(full_name, ".");
1981                         strcat(full_name, field->name);
1982                         field_name = full_name;
1983                 } else
1984                         field_name = field->name;
1985         } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1986                 field_name = "common_timestamp";
1987
1988         if (field_name == NULL)
1989                 field_name = "";
1990
1991         return field_name;
1992 }
1993
1994 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
1995 {
1996         hist_field_fn_t fn = NULL;
1997
1998         switch (field_size) {
1999         case 8:
2000                 if (field_is_signed)
2001                         fn = hist_field_s64;
2002                 else
2003                         fn = hist_field_u64;
2004                 break;
2005         case 4:
2006                 if (field_is_signed)
2007                         fn = hist_field_s32;
2008                 else
2009                         fn = hist_field_u32;
2010                 break;
2011         case 2:
2012                 if (field_is_signed)
2013                         fn = hist_field_s16;
2014                 else
2015                         fn = hist_field_u16;
2016                 break;
2017         case 1:
2018                 if (field_is_signed)
2019                         fn = hist_field_s8;
2020                 else
2021                         fn = hist_field_u8;
2022                 break;
2023         }
2024
2025         return fn;
2026 }
2027
2028 static int parse_map_size(char *str)
2029 {
2030         unsigned long size, map_bits;
2031         int ret;
2032
2033         strsep(&str, "=");
2034         if (!str) {
2035                 ret = -EINVAL;
2036                 goto out;
2037         }
2038
2039         ret = kstrtoul(str, 0, &size);
2040         if (ret)
2041                 goto out;
2042
2043         map_bits = ilog2(roundup_pow_of_two(size));
2044         if (map_bits < TRACING_MAP_BITS_MIN ||
2045             map_bits > TRACING_MAP_BITS_MAX)
2046                 ret = -EINVAL;
2047         else
2048                 ret = map_bits;
2049  out:
2050         return ret;
2051 }
2052
2053 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
2054 {
2055         unsigned int i;
2056
2057         if (!attrs)
2058                 return;
2059
2060         for (i = 0; i < attrs->n_assignments; i++)
2061                 kfree(attrs->assignment_str[i]);
2062
2063         for (i = 0; i < attrs->n_actions; i++)
2064                 kfree(attrs->action_str[i]);
2065
2066         kfree(attrs->name);
2067         kfree(attrs->sort_key_str);
2068         kfree(attrs->keys_str);
2069         kfree(attrs->vals_str);
2070         kfree(attrs->clock);
2071         kfree(attrs);
2072 }
2073
2074 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
2075 {
2076         int ret = -EINVAL;
2077
2078         if (attrs->n_actions >= HIST_ACTIONS_MAX)
2079                 return ret;
2080
2081         if ((str_has_prefix(str, "onmatch(")) ||
2082             (str_has_prefix(str, "onmax(")) ||
2083             (str_has_prefix(str, "onchange("))) {
2084                 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
2085                 if (!attrs->action_str[attrs->n_actions]) {
2086                         ret = -ENOMEM;
2087                         return ret;
2088                 }
2089                 attrs->n_actions++;
2090                 ret = 0;
2091         }
2092         return ret;
2093 }
2094
2095 static int parse_assignment(struct trace_array *tr,
2096                             char *str, struct hist_trigger_attrs *attrs)
2097 {
2098         int ret = 0;
2099
2100         if ((str_has_prefix(str, "key=")) ||
2101             (str_has_prefix(str, "keys="))) {
2102                 attrs->keys_str = kstrdup(str, GFP_KERNEL);
2103                 if (!attrs->keys_str) {
2104                         ret = -ENOMEM;
2105                         goto out;
2106                 }
2107         } else if ((str_has_prefix(str, "val=")) ||
2108                    (str_has_prefix(str, "vals=")) ||
2109                    (str_has_prefix(str, "values="))) {
2110                 attrs->vals_str = kstrdup(str, GFP_KERNEL);
2111                 if (!attrs->vals_str) {
2112                         ret = -ENOMEM;
2113                         goto out;
2114                 }
2115         } else if (str_has_prefix(str, "sort=")) {
2116                 attrs->sort_key_str = kstrdup(str, GFP_KERNEL);
2117                 if (!attrs->sort_key_str) {
2118                         ret = -ENOMEM;
2119                         goto out;
2120                 }
2121         } else if (str_has_prefix(str, "name=")) {
2122                 attrs->name = kstrdup(str, GFP_KERNEL);
2123                 if (!attrs->name) {
2124                         ret = -ENOMEM;
2125                         goto out;
2126                 }
2127         } else if (str_has_prefix(str, "clock=")) {
2128                 strsep(&str, "=");
2129                 if (!str) {
2130                         ret = -EINVAL;
2131                         goto out;
2132                 }
2133
2134                 str = strstrip(str);
2135                 attrs->clock = kstrdup(str, GFP_KERNEL);
2136                 if (!attrs->clock) {
2137                         ret = -ENOMEM;
2138                         goto out;
2139                 }
2140         } else if (str_has_prefix(str, "size=")) {
2141                 int map_bits = parse_map_size(str);
2142
2143                 if (map_bits < 0) {
2144                         ret = map_bits;
2145                         goto out;
2146                 }
2147                 attrs->map_bits = map_bits;
2148         } else {
2149                 char *assignment;
2150
2151                 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
2152                         hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str));
2153                         ret = -EINVAL;
2154                         goto out;
2155                 }
2156
2157                 assignment = kstrdup(str, GFP_KERNEL);
2158                 if (!assignment) {
2159                         ret = -ENOMEM;
2160                         goto out;
2161                 }
2162
2163                 attrs->assignment_str[attrs->n_assignments++] = assignment;
2164         }
2165  out:
2166         return ret;
2167 }
2168
2169 static struct hist_trigger_attrs *
2170 parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str)
2171 {
2172         struct hist_trigger_attrs *attrs;
2173         int ret = 0;
2174
2175         attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
2176         if (!attrs)
2177                 return ERR_PTR(-ENOMEM);
2178
2179         while (trigger_str) {
2180                 char *str = strsep(&trigger_str, ":");
2181
2182                 if (strchr(str, '=')) {
2183                         ret = parse_assignment(tr, str, attrs);
2184                         if (ret)
2185                                 goto free;
2186                 } else if (strcmp(str, "pause") == 0)
2187                         attrs->pause = true;
2188                 else if ((strcmp(str, "cont") == 0) ||
2189                          (strcmp(str, "continue") == 0))
2190                         attrs->cont = true;
2191                 else if (strcmp(str, "clear") == 0)
2192                         attrs->clear = true;
2193                 else {
2194                         ret = parse_action(str, attrs);
2195                         if (ret)
2196                                 goto free;
2197                 }
2198         }
2199
2200         if (!attrs->keys_str) {
2201                 ret = -EINVAL;
2202                 goto free;
2203         }
2204
2205         if (!attrs->clock) {
2206                 attrs->clock = kstrdup("global", GFP_KERNEL);
2207                 if (!attrs->clock) {
2208                         ret = -ENOMEM;
2209                         goto free;
2210                 }
2211         }
2212
2213         return attrs;
2214  free:
2215         destroy_hist_trigger_attrs(attrs);
2216
2217         return ERR_PTR(ret);
2218 }
2219
2220 static inline void save_comm(char *comm, struct task_struct *task)
2221 {
2222         if (!task->pid) {
2223                 strcpy(comm, "<idle>");
2224                 return;
2225         }
2226
2227         if (WARN_ON_ONCE(task->pid < 0)) {
2228                 strcpy(comm, "<XXX>");
2229                 return;
2230         }
2231
2232         strncpy(comm, task->comm, TASK_COMM_LEN);
2233 }
2234
2235 static void hist_elt_data_free(struct hist_elt_data *elt_data)
2236 {
2237         unsigned int i;
2238
2239         for (i = 0; i < SYNTH_FIELDS_MAX; i++)
2240                 kfree(elt_data->field_var_str[i]);
2241
2242         kfree(elt_data->comm);
2243         kfree(elt_data);
2244 }
2245
2246 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
2247 {
2248         struct hist_elt_data *elt_data = elt->private_data;
2249
2250         hist_elt_data_free(elt_data);
2251 }
2252
2253 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
2254 {
2255         struct hist_trigger_data *hist_data = elt->map->private_data;
2256         unsigned int size = TASK_COMM_LEN;
2257         struct hist_elt_data *elt_data;
2258         struct hist_field *key_field;
2259         unsigned int i, n_str;
2260
2261         elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
2262         if (!elt_data)
2263                 return -ENOMEM;
2264
2265         for_each_hist_key_field(i, hist_data) {
2266                 key_field = hist_data->fields[i];
2267
2268                 if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
2269                         elt_data->comm = kzalloc(size, GFP_KERNEL);
2270                         if (!elt_data->comm) {
2271                                 kfree(elt_data);
2272                                 return -ENOMEM;
2273                         }
2274                         break;
2275                 }
2276         }
2277
2278         n_str = hist_data->n_field_var_str + hist_data->n_save_var_str;
2279
2280         size = STR_VAR_LEN_MAX;
2281
2282         for (i = 0; i < n_str; i++) {
2283                 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
2284                 if (!elt_data->field_var_str[i]) {
2285                         hist_elt_data_free(elt_data);
2286                         return -ENOMEM;
2287                 }
2288         }
2289
2290         elt->private_data = elt_data;
2291
2292         return 0;
2293 }
2294
2295 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
2296 {
2297         struct hist_elt_data *elt_data = elt->private_data;
2298
2299         if (elt_data->comm)
2300                 save_comm(elt_data->comm, current);
2301 }
2302
2303 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
2304         .elt_alloc      = hist_trigger_elt_data_alloc,
2305         .elt_free       = hist_trigger_elt_data_free,
2306         .elt_init       = hist_trigger_elt_data_init,
2307 };
2308
2309 static const char *get_hist_field_flags(struct hist_field *hist_field)
2310 {
2311         const char *flags_str = NULL;
2312
2313         if (hist_field->flags & HIST_FIELD_FL_HEX)
2314                 flags_str = "hex";
2315         else if (hist_field->flags & HIST_FIELD_FL_SYM)
2316                 flags_str = "sym";
2317         else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
2318                 flags_str = "sym-offset";
2319         else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
2320                 flags_str = "execname";
2321         else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
2322                 flags_str = "syscall";
2323         else if (hist_field->flags & HIST_FIELD_FL_LOG2)
2324                 flags_str = "log2";
2325         else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2326                 flags_str = "usecs";
2327
2328         return flags_str;
2329 }
2330
2331 static void expr_field_str(struct hist_field *field, char *expr)
2332 {
2333         if (field->flags & HIST_FIELD_FL_VAR_REF)
2334                 strcat(expr, "$");
2335
2336         strcat(expr, hist_field_name(field, 0));
2337
2338         if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
2339                 const char *flags_str = get_hist_field_flags(field);
2340
2341                 if (flags_str) {
2342                         strcat(expr, ".");
2343                         strcat(expr, flags_str);
2344                 }
2345         }
2346 }
2347
2348 static char *expr_str(struct hist_field *field, unsigned int level)
2349 {
2350         char *expr;
2351
2352         if (level > 1)
2353                 return NULL;
2354
2355         expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2356         if (!expr)
2357                 return NULL;
2358
2359         if (!field->operands[0]) {
2360                 expr_field_str(field, expr);
2361                 return expr;
2362         }
2363
2364         if (field->operator == FIELD_OP_UNARY_MINUS) {
2365                 char *subexpr;
2366
2367                 strcat(expr, "-(");
2368                 subexpr = expr_str(field->operands[0], ++level);
2369                 if (!subexpr) {
2370                         kfree(expr);
2371                         return NULL;
2372                 }
2373                 strcat(expr, subexpr);
2374                 strcat(expr, ")");
2375
2376                 kfree(subexpr);
2377
2378                 return expr;
2379         }
2380
2381         expr_field_str(field->operands[0], expr);
2382
2383         switch (field->operator) {
2384         case FIELD_OP_MINUS:
2385                 strcat(expr, "-");
2386                 break;
2387         case FIELD_OP_PLUS:
2388                 strcat(expr, "+");
2389                 break;
2390         default:
2391                 kfree(expr);
2392                 return NULL;
2393         }
2394
2395         expr_field_str(field->operands[1], expr);
2396
2397         return expr;
2398 }
2399
2400 static int contains_operator(char *str)
2401 {
2402         enum field_op_id field_op = FIELD_OP_NONE;
2403         char *op;
2404
2405         op = strpbrk(str, "+-");
2406         if (!op)
2407                 return FIELD_OP_NONE;
2408
2409         switch (*op) {
2410         case '-':
2411                 if (*str == '-')
2412                         field_op = FIELD_OP_UNARY_MINUS;
2413                 else
2414                         field_op = FIELD_OP_MINUS;
2415                 break;
2416         case '+':
2417                 field_op = FIELD_OP_PLUS;
2418                 break;
2419         default:
2420                 break;
2421         }
2422
2423         return field_op;
2424 }
2425
2426 static void __destroy_hist_field(struct hist_field *hist_field)
2427 {
2428         kfree(hist_field->var.name);
2429         kfree(hist_field->name);
2430         kfree(hist_field->type);
2431
2432         kfree(hist_field);
2433 }
2434
2435 static void destroy_hist_field(struct hist_field *hist_field,
2436                                unsigned int level)
2437 {
2438         unsigned int i;
2439
2440         if (level > 3)
2441                 return;
2442
2443         if (!hist_field)
2444                 return;
2445
2446         if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
2447                 return; /* var refs will be destroyed separately */
2448
2449         for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
2450                 destroy_hist_field(hist_field->operands[i], level + 1);
2451
2452         __destroy_hist_field(hist_field);
2453 }
2454
2455 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
2456                                             struct ftrace_event_field *field,
2457                                             unsigned long flags,
2458                                             char *var_name)
2459 {
2460         struct hist_field *hist_field;
2461
2462         if (field && is_function_field(field))
2463                 return NULL;
2464
2465         hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
2466         if (!hist_field)
2467                 return NULL;
2468
2469         hist_field->hist_data = hist_data;
2470
2471         if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
2472                 goto out; /* caller will populate */
2473
2474         if (flags & HIST_FIELD_FL_VAR_REF) {
2475                 hist_field->fn = hist_field_var_ref;
2476                 goto out;
2477         }
2478
2479         if (flags & HIST_FIELD_FL_HITCOUNT) {
2480                 hist_field->fn = hist_field_counter;
2481                 hist_field->size = sizeof(u64);
2482                 hist_field->type = kstrdup("u64", GFP_KERNEL);
2483                 if (!hist_field->type)
2484                         goto free;
2485                 goto out;
2486         }
2487
2488         if (flags & HIST_FIELD_FL_STACKTRACE) {
2489                 hist_field->fn = hist_field_none;
2490                 goto out;
2491         }
2492
2493         if (flags & HIST_FIELD_FL_LOG2) {
2494                 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2;
2495                 hist_field->fn = hist_field_log2;
2496                 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
2497                 hist_field->size = hist_field->operands[0]->size;
2498                 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL);
2499                 if (!hist_field->type)
2500                         goto free;
2501                 goto out;
2502         }
2503
2504         if (flags & HIST_FIELD_FL_TIMESTAMP) {
2505                 hist_field->fn = hist_field_timestamp;
2506                 hist_field->size = sizeof(u64);
2507                 hist_field->type = kstrdup("u64", GFP_KERNEL);
2508                 if (!hist_field->type)
2509                         goto free;
2510                 goto out;
2511         }
2512
2513         if (flags & HIST_FIELD_FL_CPU) {
2514                 hist_field->fn = hist_field_cpu;
2515                 hist_field->size = sizeof(int);
2516                 hist_field->type = kstrdup("unsigned int", GFP_KERNEL);
2517                 if (!hist_field->type)
2518                         goto free;
2519                 goto out;
2520         }
2521
2522         if (WARN_ON_ONCE(!field))
2523                 goto out;
2524
2525         if (is_string_field(field)) {
2526                 flags |= HIST_FIELD_FL_STRING;
2527
2528                 hist_field->size = MAX_FILTER_STR_VAL;
2529                 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2530                 if (!hist_field->type)
2531                         goto free;
2532
2533                 if (field->filter_type == FILTER_STATIC_STRING)
2534                         hist_field->fn = hist_field_string;
2535                 else if (field->filter_type == FILTER_DYN_STRING)
2536                         hist_field->fn = hist_field_dynstring;
2537                 else
2538                         hist_field->fn = hist_field_pstring;
2539         } else {
2540                 hist_field->size = field->size;
2541                 hist_field->is_signed = field->is_signed;
2542                 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2543                 if (!hist_field->type)
2544                         goto free;
2545
2546                 hist_field->fn = select_value_fn(field->size,
2547                                                  field->is_signed);
2548                 if (!hist_field->fn) {
2549                         destroy_hist_field(hist_field, 0);
2550                         return NULL;
2551                 }
2552         }
2553  out:
2554         hist_field->field = field;
2555         hist_field->flags = flags;
2556
2557         if (var_name) {
2558                 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
2559                 if (!hist_field->var.name)
2560                         goto free;
2561         }
2562
2563         return hist_field;
2564  free:
2565         destroy_hist_field(hist_field, 0);
2566         return NULL;
2567 }
2568
2569 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
2570 {
2571         unsigned int i;
2572
2573         for (i = 0; i < HIST_FIELDS_MAX; i++) {
2574                 if (hist_data->fields[i]) {
2575                         destroy_hist_field(hist_data->fields[i], 0);
2576                         hist_data->fields[i] = NULL;
2577                 }
2578         }
2579
2580         for (i = 0; i < hist_data->n_var_refs; i++) {
2581                 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
2582                 __destroy_hist_field(hist_data->var_refs[i]);
2583                 hist_data->var_refs[i] = NULL;
2584         }
2585 }
2586
2587 static int init_var_ref(struct hist_field *ref_field,
2588                         struct hist_field *var_field,
2589                         char *system, char *event_name)
2590 {
2591         int err = 0;
2592
2593         ref_field->var.idx = var_field->var.idx;
2594         ref_field->var.hist_data = var_field->hist_data;
2595         ref_field->size = var_field->size;
2596         ref_field->is_signed = var_field->is_signed;
2597         ref_field->flags |= var_field->flags &
2598                 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2599
2600         if (system) {
2601                 ref_field->system = kstrdup(system, GFP_KERNEL);
2602                 if (!ref_field->system)
2603                         return -ENOMEM;
2604         }
2605
2606         if (event_name) {
2607                 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
2608                 if (!ref_field->event_name) {
2609                         err = -ENOMEM;
2610                         goto free;
2611                 }
2612         }
2613
2614         if (var_field->var.name) {
2615                 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
2616                 if (!ref_field->name) {
2617                         err = -ENOMEM;
2618                         goto free;
2619                 }
2620         } else if (var_field->name) {
2621                 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
2622                 if (!ref_field->name) {
2623                         err = -ENOMEM;
2624                         goto free;
2625                 }
2626         }
2627
2628         ref_field->type = kstrdup(var_field->type, GFP_KERNEL);
2629         if (!ref_field->type) {
2630                 err = -ENOMEM;
2631                 goto free;
2632         }
2633  out:
2634         return err;
2635  free:
2636         kfree(ref_field->system);
2637         kfree(ref_field->event_name);
2638         kfree(ref_field->name);
2639
2640         goto out;
2641 }
2642
2643 /**
2644  * create_var_ref - Create a variable reference and attach it to trigger
2645  * @hist_data: The trigger that will be referencing the variable
2646  * @var_field: The VAR field to create a reference to
2647  * @system: The optional system string
2648  * @event_name: The optional event_name string
2649  *
2650  * Given a variable hist_field, create a VAR_REF hist_field that
2651  * represents a reference to it.
2652  *
2653  * This function also adds the reference to the trigger that
2654  * now references the variable.
2655  *
2656  * Return: The VAR_REF field if successful, NULL if not
2657  */
2658 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
2659                                          struct hist_field *var_field,
2660                                          char *system, char *event_name)
2661 {
2662         unsigned long flags = HIST_FIELD_FL_VAR_REF;
2663         struct hist_field *ref_field;
2664
2665         ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
2666         if (ref_field) {
2667                 if (init_var_ref(ref_field, var_field, system, event_name)) {
2668                         destroy_hist_field(ref_field, 0);
2669                         return NULL;
2670                 }
2671
2672                 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
2673                 ref_field->var_ref_idx = hist_data->n_var_refs++;
2674         }
2675
2676         return ref_field;
2677 }
2678
2679 static bool is_var_ref(char *var_name)
2680 {
2681         if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
2682                 return false;
2683
2684         return true;
2685 }
2686
2687 static char *field_name_from_var(struct hist_trigger_data *hist_data,
2688                                  char *var_name)
2689 {
2690         char *name, *field;
2691         unsigned int i;
2692
2693         for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
2694                 name = hist_data->attrs->var_defs.name[i];
2695
2696                 if (strcmp(var_name, name) == 0) {
2697                         field = hist_data->attrs->var_defs.expr[i];
2698                         if (contains_operator(field) || is_var_ref(field))
2699                                 continue;
2700                         return field;
2701                 }
2702         }
2703
2704         return NULL;
2705 }
2706
2707 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
2708                                  char *system, char *event_name,
2709                                  char *var_name)
2710 {
2711         struct trace_event_call *call;
2712
2713         if (system && event_name) {
2714                 call = hist_data->event_file->event_call;
2715
2716                 if (strcmp(system, call->class->system) != 0)
2717                         return NULL;
2718
2719                 if (strcmp(event_name, trace_event_name(call)) != 0)
2720                         return NULL;
2721         }
2722
2723         if (!!system != !!event_name)
2724                 return NULL;
2725
2726         if (!is_var_ref(var_name))
2727                 return NULL;
2728
2729         var_name++;
2730
2731         return field_name_from_var(hist_data, var_name);
2732 }
2733
2734 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
2735                                         char *system, char *event_name,
2736                                         char *var_name)
2737 {
2738         struct hist_field *var_field = NULL, *ref_field = NULL;
2739         struct trace_array *tr = hist_data->event_file->tr;
2740
2741         if (!is_var_ref(var_name))
2742                 return NULL;
2743
2744         var_name++;
2745
2746         var_field = find_event_var(hist_data, system, event_name, var_name);
2747         if (var_field)
2748                 ref_field = create_var_ref(hist_data, var_field,
2749                                            system, event_name);
2750
2751         if (!ref_field)
2752                 hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name));
2753
2754         return ref_field;
2755 }
2756
2757 static struct ftrace_event_field *
2758 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
2759             char *field_str, unsigned long *flags)
2760 {
2761         struct ftrace_event_field *field = NULL;
2762         char *field_name, *modifier, *str;
2763         struct trace_array *tr = file->tr;
2764
2765         modifier = str = kstrdup(field_str, GFP_KERNEL);
2766         if (!modifier)
2767                 return ERR_PTR(-ENOMEM);
2768
2769         field_name = strsep(&modifier, ".");
2770         if (modifier) {
2771                 if (strcmp(modifier, "hex") == 0)
2772                         *flags |= HIST_FIELD_FL_HEX;
2773                 else if (strcmp(modifier, "sym") == 0)
2774                         *flags |= HIST_FIELD_FL_SYM;
2775                 else if (strcmp(modifier, "sym-offset") == 0)
2776                         *flags |= HIST_FIELD_FL_SYM_OFFSET;
2777                 else if ((strcmp(modifier, "execname") == 0) &&
2778                          (strcmp(field_name, "common_pid") == 0))
2779                         *flags |= HIST_FIELD_FL_EXECNAME;
2780                 else if (strcmp(modifier, "syscall") == 0)
2781                         *flags |= HIST_FIELD_FL_SYSCALL;
2782                 else if (strcmp(modifier, "log2") == 0)
2783                         *flags |= HIST_FIELD_FL_LOG2;
2784                 else if (strcmp(modifier, "usecs") == 0)
2785                         *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
2786                 else {
2787                         hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier));
2788                         field = ERR_PTR(-EINVAL);
2789                         goto out;
2790                 }
2791         }
2792
2793         if (strcmp(field_name, "common_timestamp") == 0) {
2794                 *flags |= HIST_FIELD_FL_TIMESTAMP;
2795                 hist_data->enable_timestamps = true;
2796                 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2797                         hist_data->attrs->ts_in_usecs = true;
2798         } else if (strcmp(field_name, "cpu") == 0)
2799                 *flags |= HIST_FIELD_FL_CPU;
2800         else {
2801                 field = trace_find_event_field(file->event_call, field_name);
2802                 if (!field || !field->size) {
2803                         hist_err(tr, HIST_ERR_FIELD_NOT_FOUND, errpos(field_name));
2804                         field = ERR_PTR(-EINVAL);
2805                         goto out;
2806                 }
2807         }
2808  out:
2809         kfree(str);
2810
2811         return field;
2812 }
2813
2814 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2815                                        struct hist_field *var_ref,
2816                                        char *var_name)
2817 {
2818         struct hist_field *alias = NULL;
2819         unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2820
2821         alias = create_hist_field(hist_data, NULL, flags, var_name);
2822         if (!alias)
2823                 return NULL;
2824
2825         alias->fn = var_ref->fn;
2826         alias->operands[0] = var_ref;
2827
2828         if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2829                 destroy_hist_field(alias, 0);
2830                 return NULL;
2831         }
2832
2833         alias->var_ref_idx = var_ref->var_ref_idx;
2834
2835         return alias;
2836 }
2837
2838 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2839                                      struct trace_event_file *file, char *str,
2840                                      unsigned long *flags, char *var_name)
2841 {
2842         char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
2843         struct ftrace_event_field *field = NULL;
2844         struct hist_field *hist_field = NULL;
2845         int ret = 0;
2846
2847         s = strchr(str, '.');
2848         if (s) {
2849                 s = strchr(++s, '.');
2850                 if (s) {
2851                         ref_system = strsep(&str, ".");
2852                         if (!str) {
2853                                 ret = -EINVAL;
2854                                 goto out;
2855                         }
2856                         ref_event = strsep(&str, ".");
2857                         if (!str) {
2858                                 ret = -EINVAL;
2859                                 goto out;
2860                         }
2861                         ref_var = str;
2862                 }
2863         }
2864
2865         s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2866         if (!s) {
2867                 hist_field = parse_var_ref(hist_data, ref_system,
2868                                            ref_event, ref_var);
2869                 if (hist_field) {
2870                         if (var_name) {
2871                                 hist_field = create_alias(hist_data, hist_field, var_name);
2872                                 if (!hist_field) {
2873                                         ret = -ENOMEM;
2874                                         goto out;
2875                                 }
2876                         }
2877                         return hist_field;
2878                 }
2879         } else
2880                 str = s;
2881
2882         field = parse_field(hist_data, file, str, flags);
2883         if (IS_ERR(field)) {
2884                 ret = PTR_ERR(field);
2885                 goto out;
2886         }
2887
2888         hist_field = create_hist_field(hist_data, field, *flags, var_name);
2889         if (!hist_field) {
2890                 ret = -ENOMEM;
2891                 goto out;
2892         }
2893
2894         return hist_field;
2895  out:
2896         return ERR_PTR(ret);
2897 }
2898
2899 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2900                                      struct trace_event_file *file,
2901                                      char *str, unsigned long flags,
2902                                      char *var_name, unsigned int level);
2903
2904 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2905                                       struct trace_event_file *file,
2906                                       char *str, unsigned long flags,
2907                                       char *var_name, unsigned int level)
2908 {
2909         struct hist_field *operand1, *expr = NULL;
2910         unsigned long operand_flags;
2911         int ret = 0;
2912         char *s;
2913
2914         /* we support only -(xxx) i.e. explicit parens required */
2915
2916         if (level > 3) {
2917                 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2918                 ret = -EINVAL;
2919                 goto free;
2920         }
2921
2922         str++; /* skip leading '-' */
2923
2924         s = strchr(str, '(');
2925         if (s)
2926                 str++;
2927         else {
2928                 ret = -EINVAL;
2929                 goto free;
2930         }
2931
2932         s = strrchr(str, ')');
2933         if (s)
2934                 *s = '\0';
2935         else {
2936                 ret = -EINVAL; /* no closing ')' */
2937                 goto free;
2938         }
2939
2940         flags |= HIST_FIELD_FL_EXPR;
2941         expr = create_hist_field(hist_data, NULL, flags, var_name);
2942         if (!expr) {
2943                 ret = -ENOMEM;
2944                 goto free;
2945         }
2946
2947         operand_flags = 0;
2948         operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2949         if (IS_ERR(operand1)) {
2950                 ret = PTR_ERR(operand1);
2951                 goto free;
2952         }
2953
2954         expr->flags |= operand1->flags &
2955                 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2956         expr->fn = hist_field_unary_minus;
2957         expr->operands[0] = operand1;
2958         expr->operator = FIELD_OP_UNARY_MINUS;
2959         expr->name = expr_str(expr, 0);
2960         expr->type = kstrdup(operand1->type, GFP_KERNEL);
2961         if (!expr->type) {
2962                 ret = -ENOMEM;
2963                 goto free;
2964         }
2965
2966         return expr;
2967  free:
2968         destroy_hist_field(expr, 0);
2969         return ERR_PTR(ret);
2970 }
2971
2972 static int check_expr_operands(struct trace_array *tr,
2973                                struct hist_field *operand1,
2974                                struct hist_field *operand2)
2975 {
2976         unsigned long operand1_flags = operand1->flags;
2977         unsigned long operand2_flags = operand2->flags;
2978
2979         if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2980             (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2981                 struct hist_field *var;
2982
2983                 var = find_var_field(operand1->var.hist_data, operand1->name);
2984                 if (!var)
2985                         return -EINVAL;
2986                 operand1_flags = var->flags;
2987         }
2988
2989         if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2990             (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2991                 struct hist_field *var;
2992
2993                 var = find_var_field(operand2->var.hist_data, operand2->name);
2994                 if (!var)
2995                         return -EINVAL;
2996                 operand2_flags = var->flags;
2997         }
2998
2999         if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
3000             (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
3001                 hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0);
3002                 return -EINVAL;
3003         }
3004
3005         return 0;
3006 }
3007
3008 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
3009                                      struct trace_event_file *file,
3010                                      char *str, unsigned long flags,
3011                                      char *var_name, unsigned int level)
3012 {
3013         struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
3014         unsigned long operand_flags;
3015         int field_op, ret = -EINVAL;
3016         char *sep, *operand1_str;
3017
3018         if (level > 3) {
3019                 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
3020                 return ERR_PTR(-EINVAL);
3021         }
3022
3023         field_op = contains_operator(str);
3024
3025         if (field_op == FIELD_OP_NONE)
3026                 return parse_atom(hist_data, file, str, &flags, var_name);
3027
3028         if (field_op == FIELD_OP_UNARY_MINUS)
3029                 return parse_unary(hist_data, file, str, flags, var_name, ++level);
3030
3031         switch (field_op) {
3032         case FIELD_OP_MINUS:
3033                 sep = "-";
3034                 break;
3035         case FIELD_OP_PLUS:
3036                 sep = "+";
3037                 break;
3038         default:
3039                 goto free;
3040         }
3041
3042         operand1_str = strsep(&str, sep);
3043         if (!operand1_str || !str)
3044                 goto free;
3045
3046         operand_flags = 0;
3047         operand1 = parse_atom(hist_data, file, operand1_str,
3048                               &operand_flags, NULL);
3049         if (IS_ERR(operand1)) {
3050                 ret = PTR_ERR(operand1);
3051                 operand1 = NULL;
3052                 goto free;
3053         }
3054
3055         /* rest of string could be another expression e.g. b+c in a+b+c */
3056         operand_flags = 0;
3057         operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
3058         if (IS_ERR(operand2)) {
3059                 ret = PTR_ERR(operand2);
3060                 operand2 = NULL;
3061                 goto free;
3062         }
3063
3064         ret = check_expr_operands(file->tr, operand1, operand2);
3065         if (ret)
3066                 goto free;
3067
3068         flags |= HIST_FIELD_FL_EXPR;
3069
3070         flags |= operand1->flags &
3071                 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
3072
3073         expr = create_hist_field(hist_data, NULL, flags, var_name);
3074         if (!expr) {
3075                 ret = -ENOMEM;
3076                 goto free;
3077         }
3078
3079         operand1->read_once = true;
3080         operand2->read_once = true;
3081
3082         expr->operands[0] = operand1;
3083         expr->operands[1] = operand2;
3084         expr->operator = field_op;
3085         expr->name = expr_str(expr, 0);
3086         expr->type = kstrdup(operand1->type, GFP_KERNEL);
3087         if (!expr->type) {
3088                 ret = -ENOMEM;
3089                 goto free;
3090         }
3091
3092         switch (field_op) {
3093         case FIELD_OP_MINUS:
3094                 expr->fn = hist_field_minus;
3095                 break;
3096         case FIELD_OP_PLUS:
3097                 expr->fn = hist_field_plus;
3098                 break;
3099         default:
3100                 ret = -EINVAL;
3101                 goto free;
3102         }
3103
3104         return expr;
3105  free:
3106         destroy_hist_field(operand1, 0);
3107         destroy_hist_field(operand2, 0);
3108         destroy_hist_field(expr, 0);
3109
3110         return ERR_PTR(ret);
3111 }
3112
3113 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
3114                                  struct trace_event_file *file)
3115 {
3116         struct event_trigger_data *test;
3117
3118         list_for_each_entry_rcu(test, &file->triggers, list) {
3119                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
3120                         if (test->private_data == hist_data)
3121                                 return test->filter_str;
3122                 }
3123         }
3124
3125         return NULL;
3126 }
3127
3128 static struct event_command trigger_hist_cmd;
3129 static int event_hist_trigger_func(struct event_command *cmd_ops,
3130                                    struct trace_event_file *file,
3131                                    char *glob, char *cmd, char *param);
3132
3133 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
3134                             struct hist_trigger_data *hist_data,
3135                             unsigned int n_keys)
3136 {
3137         struct hist_field *target_hist_field, *hist_field;
3138         unsigned int n, i, j;
3139
3140         if (hist_data->n_fields - hist_data->n_vals != n_keys)
3141                 return false;
3142
3143         i = hist_data->n_vals;
3144         j = target_hist_data->n_vals;
3145
3146         for (n = 0; n < n_keys; n++) {
3147                 hist_field = hist_data->fields[i + n];
3148                 target_hist_field = target_hist_data->fields[j + n];
3149
3150                 if (strcmp(hist_field->type, target_hist_field->type) != 0)
3151                         return false;
3152                 if (hist_field->size != target_hist_field->size)
3153                         return false;
3154                 if (hist_field->is_signed != target_hist_field->is_signed)
3155                         return false;
3156         }
3157
3158         return true;
3159 }
3160
3161 static struct hist_trigger_data *
3162 find_compatible_hist(struct hist_trigger_data *target_hist_data,
3163                      struct trace_event_file *file)
3164 {
3165         struct hist_trigger_data *hist_data;
3166         struct event_trigger_data *test;
3167         unsigned int n_keys;
3168
3169         n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
3170
3171         list_for_each_entry_rcu(test, &file->triggers, list) {
3172                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
3173                         hist_data = test->private_data;
3174
3175                         if (compatible_keys(target_hist_data, hist_data, n_keys))
3176                                 return hist_data;
3177                 }
3178         }
3179
3180         return NULL;
3181 }
3182
3183 static struct trace_event_file *event_file(struct trace_array *tr,
3184                                            char *system, char *event_name)
3185 {
3186         struct trace_event_file *file;
3187
3188         file = __find_event_file(tr, system, event_name);
3189         if (!file)
3190                 return ERR_PTR(-EINVAL);
3191
3192         return file;
3193 }
3194
3195 static struct hist_field *
3196 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
3197                          char *system, char *event_name, char *field_name)
3198 {
3199         struct hist_field *event_var;
3200         char *synthetic_name;
3201
3202         synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3203         if (!synthetic_name)
3204                 return ERR_PTR(-ENOMEM);
3205
3206         strcpy(synthetic_name, "synthetic_");
3207         strcat(synthetic_name, field_name);
3208
3209         event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
3210
3211         kfree(synthetic_name);
3212
3213         return event_var;
3214 }
3215
3216 /**
3217  * create_field_var_hist - Automatically create a histogram and var for a field
3218  * @target_hist_data: The target hist trigger
3219  * @subsys_name: Optional subsystem name
3220  * @event_name: Optional event name
3221  * @field_name: The name of the field (and the resulting variable)
3222  *
3223  * Hist trigger actions fetch data from variables, not directly from
3224  * events.  However, for convenience, users are allowed to directly
3225  * specify an event field in an action, which will be automatically
3226  * converted into a variable on their behalf.
3227
3228  * If a user specifies a field on an event that isn't the event the
3229  * histogram currently being defined (the target event histogram), the
3230  * only way that can be accomplished is if a new hist trigger is
3231  * created and the field variable defined on that.
3232  *
3233  * This function creates a new histogram compatible with the target
3234  * event (meaning a histogram with the same key as the target
3235  * histogram), and creates a variable for the specified field, but
3236  * with 'synthetic_' prepended to the variable name in order to avoid
3237  * collision with normal field variables.
3238  *
3239  * Return: The variable created for the field.
3240  */
3241 static struct hist_field *
3242 create_field_var_hist(struct hist_trigger_data *target_hist_data,
3243                       char *subsys_name, char *event_name, char *field_name)
3244 {
3245         struct trace_array *tr = target_hist_data->event_file->tr;
3246         struct hist_field *event_var = ERR_PTR(-EINVAL);
3247         struct hist_trigger_data *hist_data;
3248         unsigned int i, n, first = true;
3249         struct field_var_hist *var_hist;
3250         struct trace_event_file *file;
3251         struct hist_field *key_field;
3252         char *saved_filter;
3253         char *cmd;
3254         int ret;
3255
3256         if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
3257                 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
3258                 return ERR_PTR(-EINVAL);
3259         }
3260
3261         file = event_file(tr, subsys_name, event_name);
3262
3263         if (IS_ERR(file)) {
3264                 hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name));
3265                 ret = PTR_ERR(file);
3266                 return ERR_PTR(ret);
3267         }
3268
3269         /*
3270          * Look for a histogram compatible with target.  We'll use the
3271          * found histogram specification to create a new matching
3272          * histogram with our variable on it.  target_hist_data is not
3273          * yet a registered histogram so we can't use that.
3274          */
3275         hist_data = find_compatible_hist(target_hist_data, file);
3276         if (!hist_data) {
3277                 hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name));
3278                 return ERR_PTR(-EINVAL);
3279         }
3280
3281         /* See if a synthetic field variable has already been created */
3282         event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3283                                              event_name, field_name);
3284         if (!IS_ERR_OR_NULL(event_var))
3285                 return event_var;
3286
3287         var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
3288         if (!var_hist)
3289                 return ERR_PTR(-ENOMEM);
3290
3291         cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3292         if (!cmd) {
3293                 kfree(var_hist);
3294                 return ERR_PTR(-ENOMEM);
3295         }
3296
3297         /* Use the same keys as the compatible histogram */
3298         strcat(cmd, "keys=");
3299
3300         for_each_hist_key_field(i, hist_data) {
3301                 key_field = hist_data->fields[i];
3302                 if (!first)
3303                         strcat(cmd, ",");
3304                 strcat(cmd, key_field->field->name);
3305                 first = false;
3306         }
3307
3308         /* Create the synthetic field variable specification */
3309         strcat(cmd, ":synthetic_");
3310         strcat(cmd, field_name);
3311         strcat(cmd, "=");
3312         strcat(cmd, field_name);
3313
3314         /* Use the same filter as the compatible histogram */
3315         saved_filter = find_trigger_filter(hist_data, file);
3316         if (saved_filter) {
3317                 strcat(cmd, " if ");
3318                 strcat(cmd, saved_filter);
3319         }
3320
3321         var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
3322         if (!var_hist->cmd) {
3323                 kfree(cmd);
3324                 kfree(var_hist);
3325                 return ERR_PTR(-ENOMEM);
3326         }
3327
3328         /* Save the compatible histogram information */
3329         var_hist->hist_data = hist_data;
3330
3331         /* Create the new histogram with our variable */
3332         ret = event_hist_trigger_func(&trigger_hist_cmd, file,
3333                                       "", "hist", cmd);
3334         if (ret) {
3335                 kfree(cmd);
3336                 kfree(var_hist->cmd);
3337                 kfree(var_hist);
3338                 hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name));
3339                 return ERR_PTR(ret);
3340         }
3341
3342         kfree(cmd);
3343
3344         /* If we can't find the variable, something went wrong */
3345         event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3346                                              event_name, field_name);
3347         if (IS_ERR_OR_NULL(event_var)) {
3348                 kfree(var_hist->cmd);
3349                 kfree(var_hist);
3350                 hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
3351                 return ERR_PTR(-EINVAL);
3352         }
3353
3354         n = target_hist_data->n_field_var_hists;
3355         target_hist_data->field_var_hists[n] = var_hist;
3356         target_hist_data->n_field_var_hists++;
3357
3358         return event_var;
3359 }
3360
3361 static struct hist_field *
3362 find_target_event_var(struct hist_trigger_data *hist_data,
3363                       char *subsys_name, char *event_name, char *var_name)
3364 {
3365         struct trace_event_file *file = hist_data->event_file;
3366         struct hist_field *hist_field = NULL;
3367
3368         if (subsys_name) {
3369                 struct trace_event_call *call;
3370
3371                 if (!event_name)
3372                         return NULL;
3373
3374                 call = file->event_call;
3375
3376                 if (strcmp(subsys_name, call->class->system) != 0)
3377                         return NULL;
3378
3379                 if (strcmp(event_name, trace_event_name(call)) != 0)
3380                         return NULL;
3381         }
3382
3383         hist_field = find_var_field(hist_data, var_name);
3384
3385         return hist_field;
3386 }
3387
3388 static inline void __update_field_vars(struct tracing_map_elt *elt,
3389                                        struct ring_buffer_event *rbe,
3390                                        void *rec,
3391                                        struct field_var **field_vars,
3392                                        unsigned int n_field_vars,
3393                                        unsigned int field_var_str_start)
3394 {
3395         struct hist_elt_data *elt_data = elt->private_data;
3396         unsigned int i, j, var_idx;
3397         u64 var_val;
3398
3399         for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
3400                 struct field_var *field_var = field_vars[i];
3401                 struct hist_field *var = field_var->var;
3402                 struct hist_field *val = field_var->val;
3403
3404                 var_val = val->fn(val, elt, rbe, rec);
3405                 var_idx = var->var.idx;
3406
3407                 if (val->flags & HIST_FIELD_FL_STRING) {
3408                         char *str = elt_data->field_var_str[j++];
3409                         char *val_str = (char *)(uintptr_t)var_val;
3410
3411                         strscpy(str, val_str, STR_VAR_LEN_MAX);
3412                         var_val = (u64)(uintptr_t)str;
3413                 }
3414                 tracing_map_set_var(elt, var_idx, var_val);
3415         }
3416 }
3417
3418 static void update_field_vars(struct hist_trigger_data *hist_data,
3419                               struct tracing_map_elt *elt,
3420                               struct ring_buffer_event *rbe,
3421                               void *rec)
3422 {
3423         __update_field_vars(elt, rbe, rec, hist_data->field_vars,
3424                             hist_data->n_field_vars, 0);
3425 }
3426
3427 static void save_track_data_vars(struct hist_trigger_data *hist_data,
3428                                  struct tracing_map_elt *elt, void *rec,
3429                                  struct ring_buffer_event *rbe, void *key,
3430                                  struct action_data *data, u64 *var_ref_vals)
3431 {
3432         __update_field_vars(elt, rbe, rec, hist_data->save_vars,
3433                             hist_data->n_save_vars, hist_data->n_field_var_str);
3434 }
3435
3436 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
3437                                      struct trace_event_file *file,
3438                                      char *name, int size, const char *type)
3439 {
3440         struct hist_field *var;
3441         int idx;
3442
3443         if (find_var(hist_data, file, name) && !hist_data->remove) {
3444                 var = ERR_PTR(-EINVAL);
3445                 goto out;
3446         }
3447
3448         var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3449         if (!var) {
3450                 var = ERR_PTR(-ENOMEM);
3451                 goto out;
3452         }
3453
3454         idx = tracing_map_add_var(hist_data->map);
3455         if (idx < 0) {
3456                 kfree(var);
3457                 var = ERR_PTR(-EINVAL);
3458                 goto out;
3459         }
3460
3461         var->flags = HIST_FIELD_FL_VAR;
3462         var->var.idx = idx;
3463         var->var.hist_data = var->hist_data = hist_data;
3464         var->size = size;
3465         var->var.name = kstrdup(name, GFP_KERNEL);
3466         var->type = kstrdup(type, GFP_KERNEL);
3467         if (!var->var.name || !var->type) {
3468                 kfree(var->var.name);
3469                 kfree(var->type);
3470                 kfree(var);
3471                 var = ERR_PTR(-ENOMEM);
3472         }
3473  out:
3474         return var;
3475 }
3476
3477 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
3478                                           struct trace_event_file *file,
3479                                           char *field_name)
3480 {
3481         struct hist_field *val = NULL, *var = NULL;
3482         unsigned long flags = HIST_FIELD_FL_VAR;
3483         struct trace_array *tr = file->tr;
3484         struct field_var *field_var;
3485         int ret = 0;
3486
3487         if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
3488                 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
3489                 ret = -EINVAL;
3490                 goto err;
3491         }
3492
3493         val = parse_atom(hist_data, file, field_name, &flags, NULL);
3494         if (IS_ERR(val)) {
3495                 hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name));
3496                 ret = PTR_ERR(val);
3497                 goto err;
3498         }
3499
3500         var = create_var(hist_data, file, field_name, val->size, val->type);
3501         if (IS_ERR(var)) {
3502                 hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name));
3503                 kfree(val);
3504                 ret = PTR_ERR(var);
3505                 goto err;
3506         }
3507
3508         field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
3509         if (!field_var) {
3510                 kfree(val);
3511                 kfree(var);
3512                 ret =  -ENOMEM;
3513                 goto err;
3514         }
3515
3516         field_var->var = var;
3517         field_var->val = val;
3518  out:
3519         return field_var;
3520  err:
3521         field_var = ERR_PTR(ret);
3522         goto out;
3523 }
3524
3525 /**
3526  * create_target_field_var - Automatically create a variable for a field
3527  * @target_hist_data: The target hist trigger
3528  * @subsys_name: Optional subsystem name
3529  * @event_name: Optional event name
3530  * @var_name: The name of the field (and the resulting variable)
3531  *
3532  * Hist trigger actions fetch data from variables, not directly from
3533  * events.  However, for convenience, users are allowed to directly
3534  * specify an event field in an action, which will be automatically
3535  * converted into a variable on their behalf.
3536
3537  * This function creates a field variable with the name var_name on
3538  * the hist trigger currently being defined on the target event.  If
3539  * subsys_name and event_name are specified, this function simply
3540  * verifies that they do in fact match the target event subsystem and
3541  * event name.
3542  *
3543  * Return: The variable created for the field.
3544  */
3545 static struct field_var *
3546 create_target_field_var(struct hist_trigger_data *target_hist_data,
3547                         char *subsys_name, char *event_name, char *var_name)
3548 {
3549         struct trace_event_file *file = target_hist_data->event_file;
3550
3551         if (subsys_name) {
3552                 struct trace_event_call *call;
3553
3554                 if (!event_name)
3555                         return NULL;
3556
3557                 call = file->event_call;
3558
3559                 if (strcmp(subsys_name, call->class->system) != 0)
3560                         return NULL;
3561
3562                 if (strcmp(event_name, trace_event_name(call)) != 0)
3563                         return NULL;
3564         }
3565
3566         return create_field_var(target_hist_data, file, var_name);
3567 }
3568
3569 static bool check_track_val_max(u64 track_val, u64 var_val)
3570 {
3571         if (var_val <= track_val)
3572                 return false;
3573
3574         return true;
3575 }
3576
3577 static bool check_track_val_changed(u64 track_val, u64 var_val)
3578 {
3579         if (var_val == track_val)
3580                 return false;
3581
3582         return true;
3583 }
3584
3585 static u64 get_track_val(struct hist_trigger_data *hist_data,
3586                          struct tracing_map_elt *elt,
3587                          struct action_data *data)
3588 {
3589         unsigned int track_var_idx = data->track_data.track_var->var.idx;
3590         u64 track_val;
3591
3592         track_val = tracing_map_read_var(elt, track_var_idx);
3593
3594         return track_val;
3595 }
3596
3597 static void save_track_val(struct hist_trigger_data *hist_data,
3598                            struct tracing_map_elt *elt,
3599                            struct action_data *data, u64 var_val)
3600 {
3601         unsigned int track_var_idx = data->track_data.track_var->var.idx;
3602
3603         tracing_map_set_var(elt, track_var_idx, var_val);
3604 }
3605
3606 static void save_track_data(struct hist_trigger_data *hist_data,
3607                             struct tracing_map_elt *elt, void *rec,
3608                             struct ring_buffer_event *rbe, void *key,
3609                             struct action_data *data, u64 *var_ref_vals)
3610 {
3611         if (data->track_data.save_data)
3612                 data->track_data.save_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
3613 }
3614
3615 static bool check_track_val(struct tracing_map_elt *elt,
3616                             struct action_data *data,
3617                             u64 var_val)
3618 {
3619         struct hist_trigger_data *hist_data;
3620         u64 track_val;
3621
3622         hist_data = data->track_data.track_var->hist_data;
3623         track_val = get_track_val(hist_data, elt, data);
3624
3625         return data->track_data.check_val(track_val, var_val);
3626 }
3627
3628 #ifdef CONFIG_TRACER_SNAPSHOT
3629 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3630 {
3631         /* called with tr->max_lock held */
3632         struct track_data *track_data = tr->cond_snapshot->cond_data;
3633         struct hist_elt_data *elt_data, *track_elt_data;
3634         struct snapshot_context *context = cond_data;
3635         struct action_data *action;
3636         u64 track_val;
3637
3638         if (!track_data)
3639                 return false;
3640
3641         action = track_data->action_data;
3642
3643         track_val = get_track_val(track_data->hist_data, context->elt,
3644                                   track_data->action_data);
3645
3646         if (!action->track_data.check_val(track_data->track_val, track_val))
3647                 return false;
3648
3649         track_data->track_val = track_val;
3650         memcpy(track_data->key, context->key, track_data->key_len);
3651
3652         elt_data = context->elt->private_data;
3653         track_elt_data = track_data->elt.private_data;
3654         if (elt_data->comm)
3655                 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
3656
3657         track_data->updated = true;
3658
3659         return true;
3660 }
3661
3662 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3663                                      struct tracing_map_elt *elt, void *rec,
3664                                      struct ring_buffer_event *rbe, void *key,
3665                                      struct action_data *data,
3666                                      u64 *var_ref_vals)
3667 {
3668         struct trace_event_file *file = hist_data->event_file;
3669         struct snapshot_context context;
3670
3671         context.elt = elt;
3672         context.key = key;
3673
3674         tracing_snapshot_cond(file->tr, &context);
3675 }
3676
3677 static void hist_trigger_print_key(struct seq_file *m,
3678                                    struct hist_trigger_data *hist_data,
3679                                    void *key,
3680                                    struct tracing_map_elt *elt);
3681
3682 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
3683 {
3684         unsigned int i;
3685
3686         if (!hist_data->n_actions)
3687                 return NULL;
3688
3689         for (i = 0; i < hist_data->n_actions; i++) {
3690                 struct action_data *data = hist_data->actions[i];
3691
3692                 if (data->action == ACTION_SNAPSHOT)
3693                         return data;
3694         }
3695
3696         return NULL;
3697 }
3698
3699 static void track_data_snapshot_print(struct seq_file *m,
3700                                       struct hist_trigger_data *hist_data)
3701 {
3702         struct trace_event_file *file = hist_data->event_file;
3703         struct track_data *track_data;
3704         struct action_data *action;
3705
3706         track_data = tracing_cond_snapshot_data(file->tr);
3707         if (!track_data)
3708                 return;
3709
3710         if (!track_data->updated)
3711                 return;
3712
3713         action = snapshot_action(hist_data);
3714         if (!action)
3715                 return;
3716
3717         seq_puts(m, "\nSnapshot taken (see tracing/snapshot).  Details:\n");
3718         seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
3719                    action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
3720                    action->track_data.var_str, track_data->track_val);
3721
3722         seq_puts(m, "\ttriggered by event with key: ");
3723         hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
3724         seq_putc(m, '\n');
3725 }
3726 #else
3727 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3728 {
3729         return false;
3730 }
3731 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3732                                      struct tracing_map_elt *elt, void *rec,
3733                                      struct ring_buffer_event *rbe, void *key,
3734                                      struct action_data *data,
3735                                      u64 *var_ref_vals) {}
3736 static void track_data_snapshot_print(struct seq_file *m,
3737                                       struct hist_trigger_data *hist_data) {}
3738 #endif /* CONFIG_TRACER_SNAPSHOT */
3739
3740 static void track_data_print(struct seq_file *m,
3741                              struct hist_trigger_data *hist_data,
3742                              struct tracing_map_elt *elt,
3743                              struct action_data *data)
3744 {
3745         u64 track_val = get_track_val(hist_data, elt, data);
3746         unsigned int i, save_var_idx;
3747
3748         if (data->handler == HANDLER_ONMAX)
3749                 seq_printf(m, "\n\tmax: %10llu", track_val);
3750         else if (data->handler == HANDLER_ONCHANGE)
3751                 seq_printf(m, "\n\tchanged: %10llu", track_val);
3752
3753         if (data->action == ACTION_SNAPSHOT)
3754                 return;
3755
3756         for (i = 0; i < hist_data->n_save_vars; i++) {
3757                 struct hist_field *save_val = hist_data->save_vars[i]->val;
3758                 struct hist_field *save_var = hist_data->save_vars[i]->var;
3759                 u64 val;
3760
3761                 save_var_idx = save_var->var.idx;
3762
3763                 val = tracing_map_read_var(elt, save_var_idx);
3764
3765                 if (save_val->flags & HIST_FIELD_FL_STRING) {
3766                         seq_printf(m, "  %s: %-32s", save_var->var.name,
3767                                    (char *)(uintptr_t)(val));
3768                 } else
3769                         seq_printf(m, "  %s: %10llu", save_var->var.name, val);
3770         }
3771 }
3772
3773 static void ontrack_action(struct hist_trigger_data *hist_data,
3774                            struct tracing_map_elt *elt, void *rec,
3775                            struct ring_buffer_event *rbe, void *key,
3776                            struct action_data *data, u64 *var_ref_vals)
3777 {
3778         u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
3779
3780         if (check_track_val(elt, data, var_val)) {
3781                 save_track_val(hist_data, elt, data, var_val);
3782                 save_track_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
3783         }
3784 }
3785
3786 static void action_data_destroy(struct action_data *data)
3787 {
3788         unsigned int i;
3789
3790         lockdep_assert_held(&event_mutex);
3791
3792         kfree(data->action_name);
3793
3794         for (i = 0; i < data->n_params; i++)
3795                 kfree(data->params[i]);
3796
3797         if (data->synth_event)
3798                 data->synth_event->ref--;
3799
3800         kfree(data->synth_event_name);
3801
3802         kfree(data);
3803 }
3804
3805 static void track_data_destroy(struct hist_trigger_data *hist_data,
3806                                struct action_data *data)
3807 {
3808         struct trace_event_file *file = hist_data->event_file;
3809
3810         destroy_hist_field(data->track_data.track_var, 0);
3811
3812         if (data->action == ACTION_SNAPSHOT) {
3813                 struct track_data *track_data;
3814
3815                 track_data = tracing_cond_snapshot_data(file->tr);
3816                 if (track_data && track_data->hist_data == hist_data) {
3817                         tracing_snapshot_cond_disable(file->tr);
3818                         track_data_free(track_data);
3819                 }
3820         }
3821
3822         kfree(data->track_data.var_str);
3823
3824         action_data_destroy(data);
3825 }
3826
3827 static int action_create(struct hist_trigger_data *hist_data,
3828                          struct action_data *data);
3829
3830 static int track_data_create(struct hist_trigger_data *hist_data,
3831                              struct action_data *data)
3832 {
3833         struct hist_field *var_field, *ref_field, *track_var = NULL;
3834         struct trace_event_file *file = hist_data->event_file;
3835         struct trace_array *tr = file->tr;
3836         char *track_data_var_str;
3837         int ret = 0;
3838
3839         track_data_var_str = data->track_data.var_str;
3840         if (track_data_var_str[0] != '$') {
3841                 hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str));
3842                 return -EINVAL;
3843         }
3844         track_data_var_str++;
3845
3846         var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
3847         if (!var_field) {
3848                 hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str));
3849                 return -EINVAL;
3850         }
3851
3852         ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
3853         if (!ref_field)
3854                 return -ENOMEM;
3855
3856         data->track_data.var_ref = ref_field;
3857
3858         if (data->handler == HANDLER_ONMAX)
3859                 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
3860         if (IS_ERR(track_var)) {
3861                 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3862                 ret = PTR_ERR(track_var);
3863                 goto out;
3864         }
3865
3866         if (data->handler == HANDLER_ONCHANGE)
3867                 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
3868         if (IS_ERR(track_var)) {
3869                 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3870                 ret = PTR_ERR(track_var);
3871                 goto out;
3872         }
3873         data->track_data.track_var = track_var;
3874
3875         ret = action_create(hist_data, data);
3876  out:
3877         return ret;
3878 }
3879
3880 static int parse_action_params(struct trace_array *tr, char *params,
3881                                struct action_data *data)
3882 {
3883         char *param, *saved_param;
3884         bool first_param = true;
3885         int ret = 0;
3886
3887         while (params) {
3888                 if (data->n_params >= SYNTH_FIELDS_MAX) {
3889                         hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0);
3890                         goto out;
3891                 }
3892
3893                 param = strsep(&params, ",");
3894                 if (!param) {
3895                         hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0);
3896                         ret = -EINVAL;
3897                         goto out;
3898                 }
3899
3900                 param = strstrip(param);
3901                 if (strlen(param) < 2) {
3902                         hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param));
3903                         ret = -EINVAL;
3904                         goto out;
3905                 }
3906
3907                 saved_param = kstrdup(param, GFP_KERNEL);
3908                 if (!saved_param) {
3909                         ret = -ENOMEM;
3910                         goto out;
3911                 }
3912
3913                 if (first_param && data->use_trace_keyword) {
3914                         data->synth_event_name = saved_param;
3915                         first_param = false;
3916                         continue;
3917                 }
3918                 first_param = false;
3919
3920                 data->params[data->n_params++] = saved_param;
3921         }
3922  out:
3923         return ret;
3924 }
3925
3926 static int action_parse(struct trace_array *tr, char *str, struct action_data *data,
3927                         enum handler_id handler)
3928 {
3929         char *action_name;
3930         int ret = 0;
3931
3932         strsep(&str, ".");
3933         if (!str) {
3934                 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3935                 ret = -EINVAL;
3936                 goto out;
3937         }
3938
3939         action_name = strsep(&str, "(");
3940         if (!action_name || !str) {
3941                 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3942                 ret = -EINVAL;
3943                 goto out;
3944         }
3945
3946         if (str_has_prefix(action_name, "save")) {
3947                 char *params = strsep(&str, ")");
3948
3949                 if (!params) {
3950                         hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0);
3951                         ret = -EINVAL;
3952                         goto out;
3953                 }
3954
3955                 ret = parse_action_params(tr, params, data);
3956                 if (ret)
3957                         goto out;
3958
3959                 if (handler == HANDLER_ONMAX)
3960                         data->track_data.check_val = check_track_val_max;
3961                 else if (handler == HANDLER_ONCHANGE)
3962                         data->track_data.check_val = check_track_val_changed;
3963                 else {
3964                         hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3965                         ret = -EINVAL;
3966                         goto out;
3967                 }
3968
3969                 data->track_data.save_data = save_track_data_vars;
3970                 data->fn = ontrack_action;
3971                 data->action = ACTION_SAVE;
3972         } else if (str_has_prefix(action_name, "snapshot")) {
3973                 char *params = strsep(&str, ")");
3974
3975                 if (!str) {
3976                         hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params));
3977                         ret = -EINVAL;
3978                         goto out;
3979                 }
3980
3981                 if (handler == HANDLER_ONMAX)
3982                         data->track_data.check_val = check_track_val_max;
3983                 else if (handler == HANDLER_ONCHANGE)
3984                         data->track_data.check_val = check_track_val_changed;
3985                 else {
3986                         hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3987                         ret = -EINVAL;
3988                         goto out;
3989                 }
3990
3991                 data->track_data.save_data = save_track_data_snapshot;
3992                 data->fn = ontrack_action;
3993                 data->action = ACTION_SNAPSHOT;
3994         } else {
3995                 char *params = strsep(&str, ")");
3996
3997                 if (str_has_prefix(action_name, "trace"))
3998                         data->use_trace_keyword = true;
3999
4000                 if (params) {
4001                         ret = parse_action_params(tr, params, data);
4002                         if (ret)
4003                                 goto out;
4004                 }
4005
4006                 if (handler == HANDLER_ONMAX)
4007                         data->track_data.check_val = check_track_val_max;
4008                 else if (handler == HANDLER_ONCHANGE)
4009                         data->track_data.check_val = check_track_val_changed;
4010
4011                 if (handler != HANDLER_ONMATCH) {
4012                         data->track_data.save_data = action_trace;
4013                         data->fn = ontrack_action;
4014                 } else
4015                         data->fn = action_trace;
4016
4017                 data->action = ACTION_TRACE;
4018         }
4019
4020         data->action_name = kstrdup(action_name, GFP_KERNEL);
4021         if (!data->action_name) {
4022                 ret = -ENOMEM;
4023                 goto out;
4024         }
4025
4026         data->handler = handler;
4027  out:
4028         return ret;
4029 }
4030
4031 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
4032                                             char *str, enum handler_id handler)
4033 {
4034         struct action_data *data;
4035         int ret = -EINVAL;
4036         char *var_str;
4037
4038         data = kzalloc(sizeof(*data), GFP_KERNEL);
4039         if (!data)
4040                 return ERR_PTR(-ENOMEM);
4041
4042         var_str = strsep(&str, ")");
4043         if (!var_str || !str) {
4044                 ret = -EINVAL;
4045                 goto free;
4046         }
4047
4048         data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
4049         if (!data->track_data.var_str) {
4050                 ret = -ENOMEM;
4051                 goto free;
4052         }
4053
4054         ret = action_parse(hist_data->event_file->tr, str, data, handler);
4055         if (ret)
4056                 goto free;
4057  out:
4058         return data;
4059  free:
4060         track_data_destroy(hist_data, data);
4061         data = ERR_PTR(ret);
4062         goto out;
4063 }
4064
4065 static void onmatch_destroy(struct action_data *data)
4066 {
4067         kfree(data->match_data.event);
4068         kfree(data->match_data.event_system);
4069
4070         action_data_destroy(data);
4071 }
4072
4073 static void destroy_field_var(struct field_var *field_var)
4074 {
4075         if (!field_var)
4076                 return;
4077
4078         destroy_hist_field(field_var->var, 0);
4079         destroy_hist_field(field_var->val, 0);
4080
4081         kfree(field_var);
4082 }
4083
4084 static void destroy_field_vars(struct hist_trigger_data *hist_data)
4085 {
4086         unsigned int i;
4087
4088         for (i = 0; i < hist_data->n_field_vars; i++)
4089                 destroy_field_var(hist_data->field_vars[i]);
4090 }
4091
4092 static void save_field_var(struct hist_trigger_data *hist_data,
4093                            struct field_var *field_var)
4094 {
4095         hist_data->field_vars[hist_data->n_field_vars++] = field_var;
4096
4097         if (field_var->val->flags & HIST_FIELD_FL_STRING)
4098                 hist_data->n_field_var_str++;
4099 }
4100
4101
4102 static int check_synth_field(struct synth_event *event,
4103                              struct hist_field *hist_field,
4104                              unsigned int field_pos)
4105 {
4106         struct synth_field *field;
4107
4108         if (field_pos >= event->n_fields)
4109                 return -EINVAL;
4110
4111         field = event->fields[field_pos];
4112
4113         if (strcmp(field->type, hist_field->type) != 0) {
4114                 if (field->size != hist_field->size ||
4115                     field->is_signed != hist_field->is_signed)
4116                         return -EINVAL;
4117         }
4118
4119         return 0;
4120 }
4121
4122 static struct hist_field *
4123 trace_action_find_var(struct hist_trigger_data *hist_data,
4124                       struct action_data *data,
4125                       char *system, char *event, char *var)
4126 {
4127         struct trace_array *tr = hist_data->event_file->tr;
4128         struct hist_field *hist_field;
4129
4130         var++; /* skip '$' */
4131
4132         hist_field = find_target_event_var(hist_data, system, event, var);
4133         if (!hist_field) {
4134                 if (!system && data->handler == HANDLER_ONMATCH) {
4135                         system = data->match_data.event_system;
4136                         event = data->match_data.event;
4137                 }
4138
4139                 hist_field = find_event_var(hist_data, system, event, var);
4140         }
4141
4142         if (!hist_field)
4143                 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var));
4144
4145         return hist_field;
4146 }
4147
4148 static struct hist_field *
4149 trace_action_create_field_var(struct hist_trigger_data *hist_data,
4150                               struct action_data *data, char *system,
4151                               char *event, char *var)
4152 {
4153         struct hist_field *hist_field = NULL;
4154         struct field_var *field_var;
4155
4156         /*
4157          * First try to create a field var on the target event (the
4158          * currently being defined).  This will create a variable for
4159          * unqualified fields on the target event, or if qualified,
4160          * target fields that have qualified names matching the target.
4161          */
4162         field_var = create_target_field_var(hist_data, system, event, var);
4163
4164         if (field_var && !IS_ERR(field_var)) {
4165                 save_field_var(hist_data, field_var);
4166                 hist_field = field_var->var;
4167         } else {
4168                 field_var = NULL;
4169                 /*
4170                  * If no explicit system.event is specfied, default to
4171                  * looking for fields on the onmatch(system.event.xxx)
4172                  * event.
4173                  */
4174                 if (!system && data->handler == HANDLER_ONMATCH) {
4175                         system = data->match_data.event_system;
4176                         event = data->match_data.event;
4177                 }
4178
4179                 /*
4180                  * At this point, we're looking at a field on another
4181                  * event.  Because we can't modify a hist trigger on
4182                  * another event to add a variable for a field, we need
4183                  * to create a new trigger on that event and create the
4184                  * variable at the same time.
4185                  */
4186                 hist_field = create_field_var_hist(hist_data, system, event, var);
4187                 if (IS_ERR(hist_field))
4188                         goto free;
4189         }
4190  out:
4191         return hist_field;
4192  free:
4193         destroy_field_var(field_var);
4194         hist_field = NULL;
4195         goto out;
4196 }
4197
4198 static int trace_action_create(struct hist_trigger_data *hist_data,
4199                                struct action_data *data)
4200 {
4201         struct trace_array *tr = hist_data->event_file->tr;
4202         char *event_name, *param, *system = NULL;
4203         struct hist_field *hist_field, *var_ref;
4204         unsigned int i, var_ref_idx;
4205         unsigned int field_pos = 0;
4206         struct synth_event *event;
4207         char *synth_event_name;
4208         int ret = 0;
4209
4210         lockdep_assert_held(&event_mutex);
4211
4212         if (data->use_trace_keyword)
4213                 synth_event_name = data->synth_event_name;
4214         else
4215                 synth_event_name = data->action_name;
4216
4217         event = find_synth_event(synth_event_name);
4218         if (!event) {
4219                 hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name));
4220                 return -EINVAL;
4221         }
4222
4223         event->ref++;
4224
4225         var_ref_idx = hist_data->n_var_refs;
4226
4227         for (i = 0; i < data->n_params; i++) {
4228                 char *p;
4229
4230                 p = param = kstrdup(data->params[i], GFP_KERNEL);
4231                 if (!param) {
4232                         ret = -ENOMEM;
4233                         goto err;
4234                 }
4235
4236                 system = strsep(&param, ".");
4237                 if (!param) {
4238                         param = (char *)system;
4239                         system = event_name = NULL;
4240                 } else {
4241                         event_name = strsep(&param, ".");
4242                         if (!param) {
4243                                 kfree(p);
4244                                 ret = -EINVAL;
4245                                 goto err;
4246                         }
4247                 }
4248
4249                 if (param[0] == '$')
4250                         hist_field = trace_action_find_var(hist_data, data,
4251                                                            system, event_name,
4252                                                            param);
4253                 else
4254                         hist_field = trace_action_create_field_var(hist_data,
4255                                                                    data,
4256                                                                    system,
4257                                                                    event_name,
4258                                                                    param);
4259
4260                 if (!hist_field) {
4261                         kfree(p);
4262                         ret = -EINVAL;
4263                         goto err;
4264                 }
4265
4266                 if (check_synth_field(event, hist_field, field_pos) == 0) {
4267                         var_ref = create_var_ref(hist_data, hist_field,
4268                                                  system, event_name);
4269                         if (!var_ref) {
4270                                 kfree(p);
4271                                 ret = -ENOMEM;
4272                                 goto err;
4273                         }
4274
4275                         field_pos++;
4276                         kfree(p);
4277                         continue;
4278                 }
4279
4280                 hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param));
4281                 kfree(p);
4282                 ret = -EINVAL;
4283                 goto err;
4284         }
4285
4286         if (field_pos != event->n_fields) {
4287                 hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name));
4288                 ret = -EINVAL;
4289                 goto err;
4290         }
4291
4292         data->synth_event = event;
4293         data->var_ref_idx = var_ref_idx;
4294  out:
4295         return ret;
4296  err:
4297         event->ref--;
4298
4299         goto out;
4300 }
4301
4302 static int action_create(struct hist_trigger_data *hist_data,
4303                          struct action_data *data)
4304 {
4305         struct trace_event_file *file = hist_data->event_file;
4306         struct trace_array *tr = file->tr;
4307         struct track_data *track_data;
4308         struct field_var *field_var;
4309         unsigned int i;
4310         char *param;
4311         int ret = 0;
4312
4313         if (data->action == ACTION_TRACE)
4314                 return trace_action_create(hist_data, data);
4315
4316         if (data->action == ACTION_SNAPSHOT) {
4317                 track_data = track_data_alloc(hist_data->key_size, data, hist_data);
4318                 if (IS_ERR(track_data)) {
4319                         ret = PTR_ERR(track_data);
4320                         goto out;
4321                 }
4322
4323                 ret = tracing_snapshot_cond_enable(file->tr, track_data,
4324                                                    cond_snapshot_update);
4325                 if (ret)
4326                         track_data_free(track_data);
4327
4328                 goto out;
4329         }
4330
4331         if (data->action == ACTION_SAVE) {
4332                 if (hist_data->n_save_vars) {
4333                         ret = -EEXIST;
4334                         hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0);
4335                         goto out;
4336                 }
4337
4338                 for (i = 0; i < data->n_params; i++) {
4339                         param = kstrdup(data->params[i], GFP_KERNEL);
4340                         if (!param) {
4341                                 ret = -ENOMEM;
4342                                 goto out;
4343                         }
4344
4345                         field_var = create_target_field_var(hist_data, NULL, NULL, param);
4346                         if (IS_ERR(field_var)) {
4347                                 hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL,
4348                                          errpos(param));
4349                                 ret = PTR_ERR(field_var);
4350                                 kfree(param);
4351                                 goto out;
4352                         }
4353
4354                         hist_data->save_vars[hist_data->n_save_vars++] = field_var;
4355                         if (field_var->val->flags & HIST_FIELD_FL_STRING)
4356                                 hist_data->n_save_var_str++;
4357                         kfree(param);
4358                 }
4359         }
4360  out:
4361         return ret;
4362 }
4363
4364 static int onmatch_create(struct hist_trigger_data *hist_data,
4365                           struct action_data *data)
4366 {
4367         return action_create(hist_data, data);
4368 }
4369
4370 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
4371 {
4372         char *match_event, *match_event_system;
4373         struct action_data *data;
4374         int ret = -EINVAL;
4375
4376         data = kzalloc(sizeof(*data), GFP_KERNEL);
4377         if (!data)
4378                 return ERR_PTR(-ENOMEM);
4379
4380         match_event = strsep(&str, ")");
4381         if (!match_event || !str) {
4382                 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event));
4383                 goto free;
4384         }
4385
4386         match_event_system = strsep(&match_event, ".");
4387         if (!match_event) {
4388                 hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system));
4389                 goto free;
4390         }
4391
4392         if (IS_ERR(event_file(tr, match_event_system, match_event))) {
4393                 hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event));
4394                 goto free;
4395         }
4396
4397         data->match_data.event = kstrdup(match_event, GFP_KERNEL);
4398         if (!data->match_data.event) {
4399                 ret = -ENOMEM;
4400                 goto free;
4401         }
4402
4403         data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
4404         if (!data->match_data.event_system) {
4405                 ret = -ENOMEM;
4406                 goto free;
4407         }
4408
4409         ret = action_parse(tr, str, data, HANDLER_ONMATCH);
4410         if (ret)
4411                 goto free;
4412  out:
4413         return data;
4414  free:
4415         onmatch_destroy(data);
4416         data = ERR_PTR(ret);
4417         goto out;
4418 }
4419
4420 static int create_hitcount_val(struct hist_trigger_data *hist_data)
4421 {
4422         hist_data->fields[HITCOUNT_IDX] =
4423                 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
4424         if (!hist_data->fields[HITCOUNT_IDX])
4425                 return -ENOMEM;
4426
4427         hist_data->n_vals++;
4428         hist_data->n_fields++;
4429
4430         if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
4431                 return -EINVAL;
4432
4433         return 0;
4434 }
4435
4436 static int __create_val_field(struct hist_trigger_data *hist_data,
4437                               unsigned int val_idx,
4438                               struct trace_event_file *file,
4439                               char *var_name, char *field_str,
4440                               unsigned long flags)
4441 {
4442         struct hist_field *hist_field;
4443         int ret = 0;
4444
4445         hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
4446         if (IS_ERR(hist_field)) {
4447                 ret = PTR_ERR(hist_field);
4448                 goto out;
4449         }
4450
4451         hist_data->fields[val_idx] = hist_field;
4452
4453         ++hist_data->n_vals;
4454         ++hist_data->n_fields;
4455
4456         if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4457                 ret = -EINVAL;
4458  out:
4459         return ret;
4460 }
4461
4462 static int create_val_field(struct hist_trigger_data *hist_data,
4463                             unsigned int val_idx,
4464                             struct trace_event_file *file,
4465                             char *field_str)
4466 {
4467         if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
4468                 return -EINVAL;
4469
4470         return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
4471 }
4472
4473 static int create_var_field(struct hist_trigger_data *hist_data,
4474                             unsigned int val_idx,
4475                             struct trace_event_file *file,
4476                             char *var_name, char *expr_str)
4477 {
4478         struct trace_array *tr = hist_data->event_file->tr;
4479         unsigned long flags = 0;
4480
4481         if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4482                 return -EINVAL;
4483
4484         if (find_var(hist_data, file, var_name) && !hist_data->remove) {
4485                 hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name));
4486                 return -EINVAL;
4487         }
4488
4489         flags |= HIST_FIELD_FL_VAR;
4490         hist_data->n_vars++;
4491         if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
4492                 return -EINVAL;
4493
4494         return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
4495 }
4496
4497 static int create_val_fields(struct hist_trigger_data *hist_data,
4498                              struct trace_event_file *file)
4499 {
4500         char *fields_str, *field_str;
4501         unsigned int i, j = 1;
4502         int ret;
4503
4504         ret = create_hitcount_val(hist_data);
4505         if (ret)
4506                 goto out;
4507
4508         fields_str = hist_data->attrs->vals_str;
4509         if (!fields_str)
4510                 goto out;
4511
4512         strsep(&fields_str, "=");
4513         if (!fields_str)
4514                 goto out;
4515
4516         for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
4517                      j < TRACING_MAP_VALS_MAX; i++) {
4518                 field_str = strsep(&fields_str, ",");
4519                 if (!field_str)
4520                         break;
4521
4522                 if (strcmp(field_str, "hitcount") == 0)
4523                         continue;
4524
4525                 ret = create_val_field(hist_data, j++, file, field_str);
4526                 if (ret)
4527                         goto out;
4528         }
4529
4530         if (fields_str && (strcmp(fields_str, "hitcount") != 0))
4531                 ret = -EINVAL;
4532  out:
4533         return ret;
4534 }
4535
4536 static int create_key_field(struct hist_trigger_data *hist_data,
4537                             unsigned int key_idx,
4538                             unsigned int key_offset,
4539                             struct trace_event_file *file,
4540                             char *field_str)
4541 {
4542         struct trace_array *tr = hist_data->event_file->tr;
4543         struct hist_field *hist_field = NULL;
4544         unsigned long flags = 0;
4545         unsigned int key_size;
4546         int ret = 0;
4547
4548         if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
4549                 return -EINVAL;
4550
4551         flags |= HIST_FIELD_FL_KEY;
4552
4553         if (strcmp(field_str, "stacktrace") == 0) {
4554                 flags |= HIST_FIELD_FL_STACKTRACE;
4555                 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
4556                 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
4557         } else {
4558                 hist_field = parse_expr(hist_data, file, field_str, flags,
4559                                         NULL, 0);
4560                 if (IS_ERR(hist_field)) {
4561                         ret = PTR_ERR(hist_field);
4562                         goto out;
4563                 }
4564
4565                 if (field_has_hist_vars(hist_field, 0)) {
4566                         hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str));
4567                         destroy_hist_field(hist_field, 0);
4568                         ret = -EINVAL;
4569                         goto out;
4570                 }
4571
4572                 key_size = hist_field->size;
4573         }
4574
4575         hist_data->fields[key_idx] = hist_field;
4576
4577         key_size = ALIGN(key_size, sizeof(u64));
4578         hist_data->fields[key_idx]->size = key_size;
4579         hist_data->fields[key_idx]->offset = key_offset;
4580
4581         hist_data->key_size += key_size;
4582
4583         if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
4584                 ret = -EINVAL;
4585                 goto out;
4586         }
4587
4588         hist_data->n_keys++;
4589         hist_data->n_fields++;
4590
4591         if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
4592                 return -EINVAL;
4593
4594         ret = key_size;
4595  out:
4596         return ret;
4597 }
4598
4599 static int create_key_fields(struct hist_trigger_data *hist_data,
4600                              struct trace_event_file *file)
4601 {
4602         unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
4603         char *fields_str, *field_str;
4604         int ret = -EINVAL;
4605
4606         fields_str = hist_data->attrs->keys_str;
4607         if (!fields_str)
4608                 goto out;
4609
4610         strsep(&fields_str, "=");
4611         if (!fields_str)
4612                 goto out;
4613
4614         for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
4615                 field_str = strsep(&fields_str, ",");
4616                 if (!field_str)
4617                         break;
4618                 ret = create_key_field(hist_data, i, key_offset,
4619                                        file, field_str);
4620                 if (ret < 0)
4621                         goto out;
4622                 key_offset += ret;
4623         }
4624         if (fields_str) {
4625                 ret = -EINVAL;
4626                 goto out;
4627         }
4628         ret = 0;
4629  out:
4630         return ret;
4631 }
4632
4633 static int create_var_fields(struct hist_trigger_data *hist_data,
4634                              struct trace_event_file *file)
4635 {
4636         unsigned int i, j = hist_data->n_vals;
4637         int ret = 0;
4638
4639         unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
4640
4641         for (i = 0; i < n_vars; i++) {
4642                 char *var_name = hist_data->attrs->var_defs.name[i];
4643                 char *expr = hist_data->attrs->var_defs.expr[i];
4644
4645                 ret = create_var_field(hist_data, j++, file, var_name, expr);
4646                 if (ret)
4647                         goto out;
4648         }
4649  out:
4650         return ret;
4651 }
4652
4653 static void free_var_defs(struct hist_trigger_data *hist_data)
4654 {
4655         unsigned int i;
4656
4657         for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
4658                 kfree(hist_data->attrs->var_defs.name[i]);
4659                 kfree(hist_data->attrs->var_defs.expr[i]);
4660         }
4661
4662         hist_data->attrs->var_defs.n_vars = 0;
4663 }
4664
4665 static int parse_var_defs(struct hist_trigger_data *hist_data)
4666 {
4667         struct trace_array *tr = hist_data->event_file->tr;
4668         char *s, *str, *var_name, *field_str;
4669         unsigned int i, j, n_vars = 0;
4670         int ret = 0;
4671
4672         for (i = 0; i < hist_data->attrs->n_assignments; i++) {
4673                 str = hist_data->attrs->assignment_str[i];
4674                 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
4675                         field_str = strsep(&str, ",");
4676                         if (!field_str)
4677                                 break;
4678
4679                         var_name = strsep(&field_str, "=");
4680                         if (!var_name || !field_str) {
4681                                 hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT,
4682                                          errpos(var_name));
4683                                 ret = -EINVAL;
4684                                 goto free;
4685                         }
4686
4687                         if (n_vars == TRACING_MAP_VARS_MAX) {
4688                                 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name));
4689                                 ret = -EINVAL;
4690                                 goto free;
4691                         }
4692
4693                         s = kstrdup(var_name, GFP_KERNEL);
4694                         if (!s) {
4695                                 ret = -ENOMEM;
4696                                 goto free;
4697                         }
4698                         hist_data->attrs->var_defs.name[n_vars] = s;
4699
4700                         s = kstrdup(field_str, GFP_KERNEL);
4701                         if (!s) {
4702                                 kfree(hist_data->attrs->var_defs.name[n_vars]);
4703                                 ret = -ENOMEM;
4704                                 goto free;
4705                         }
4706                         hist_data->attrs->var_defs.expr[n_vars++] = s;
4707
4708                         hist_data->attrs->var_defs.n_vars = n_vars;
4709                 }
4710         }
4711
4712         return ret;
4713  free:
4714         free_var_defs(hist_data);
4715
4716         return ret;
4717 }
4718
4719 static int create_hist_fields(struct hist_trigger_data *hist_data,
4720                               struct trace_event_file *file)
4721 {
4722         int ret;
4723
4724         ret = parse_var_defs(hist_data);
4725         if (ret)
4726                 goto out;
4727
4728         ret = create_val_fields(hist_data, file);
4729         if (ret)
4730                 goto out;
4731
4732         ret = create_var_fields(hist_data, file);
4733         if (ret)
4734                 goto out;
4735
4736         ret = create_key_fields(hist_data, file);
4737         if (ret)
4738                 goto out;
4739  out:
4740         free_var_defs(hist_data);
4741
4742         return ret;
4743 }
4744
4745 static int is_descending(const char *str)
4746 {
4747         if (!str)
4748                 return 0;
4749
4750         if (strcmp(str, "descending") == 0)
4751                 return 1;
4752
4753         if (strcmp(str, "ascending") == 0)
4754                 return 0;
4755
4756         return -EINVAL;
4757 }
4758
4759 static int create_sort_keys(struct hist_trigger_data *hist_data)
4760 {
4761         char *fields_str = hist_data->attrs->sort_key_str;
4762         struct tracing_map_sort_key *sort_key;
4763         int descending, ret = 0;
4764         unsigned int i, j, k;
4765
4766         hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
4767
4768         if (!fields_str)
4769                 goto out;
4770
4771         strsep(&fields_str, "=");
4772         if (!fields_str) {
4773                 ret = -EINVAL;
4774                 goto out;
4775         }
4776
4777         for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
4778                 struct hist_field *hist_field;
4779                 char *field_str, *field_name;
4780                 const char *test_name;
4781
4782                 sort_key = &hist_data->sort_keys[i];
4783
4784                 field_str = strsep(&fields_str, ",");
4785                 if (!field_str) {
4786                         if (i == 0)
4787                                 ret = -EINVAL;
4788                         break;
4789                 }
4790
4791                 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4792                         ret = -EINVAL;
4793                         break;
4794                 }
4795
4796                 field_name = strsep(&field_str, ".");
4797                 if (!field_name) {
4798                         ret = -EINVAL;
4799                         break;
4800                 }
4801
4802                 if (strcmp(field_name, "hitcount") == 0) {
4803                         descending = is_descending(field_str);
4804                         if (descending < 0) {
4805                                 ret = descending;
4806                                 break;
4807                         }
4808                         sort_key->descending = descending;
4809                         continue;
4810                 }
4811
4812                 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4813                         unsigned int idx;
4814
4815                         hist_field = hist_data->fields[j];
4816                         if (hist_field->flags & HIST_FIELD_FL_VAR)
4817                                 continue;
4818
4819                         idx = k++;
4820
4821                         test_name = hist_field_name(hist_field, 0);
4822
4823                         if (strcmp(field_name, test_name) == 0) {
4824                                 sort_key->field_idx = idx;
4825                                 descending = is_descending(field_str);
4826                                 if (descending < 0) {
4827                                         ret = descending;
4828                                         goto out;
4829                                 }
4830                                 sort_key->descending = descending;
4831                                 break;
4832                         }
4833                 }
4834                 if (j == hist_data->n_fields) {
4835                         ret = -EINVAL;
4836                         break;
4837                 }
4838         }
4839
4840         hist_data->n_sort_keys = i;
4841  out:
4842         return ret;
4843 }
4844
4845 static void destroy_actions(struct hist_trigger_data *hist_data)
4846 {
4847         unsigned int i;
4848
4849         for (i = 0; i < hist_data->n_actions; i++) {
4850                 struct action_data *data = hist_data->actions[i];
4851
4852                 if (data->handler == HANDLER_ONMATCH)
4853                         onmatch_destroy(data);
4854                 else if (data->handler == HANDLER_ONMAX ||
4855                          data->handler == HANDLER_ONCHANGE)
4856                         track_data_destroy(hist_data, data);
4857                 else
4858                         kfree(data);
4859         }
4860 }
4861
4862 static int parse_actions(struct hist_trigger_data *hist_data)
4863 {
4864         struct trace_array *tr = hist_data->event_file->tr;
4865         struct action_data *data;
4866         unsigned int i;
4867         int ret = 0;
4868         char *str;
4869         int len;
4870
4871         for (i = 0; i < hist_data->attrs->n_actions; i++) {
4872                 str = hist_data->attrs->action_str[i];
4873
4874                 if ((len = str_has_prefix(str, "onmatch("))) {
4875                         char *action_str = str + len;
4876
4877                         data = onmatch_parse(tr, action_str);
4878                         if (IS_ERR(data)) {
4879                                 ret = PTR_ERR(data);
4880                                 break;
4881                         }
4882                 } else if ((len = str_has_prefix(str, "onmax("))) {
4883                         char *action_str = str + len;
4884
4885                         data = track_data_parse(hist_data, action_str,
4886                                                 HANDLER_ONMAX);
4887                         if (IS_ERR(data)) {
4888                                 ret = PTR_ERR(data);
4889                                 break;
4890                         }
4891                 } else if ((len = str_has_prefix(str, "onchange("))) {
4892                         char *action_str = str + len;
4893
4894                         data = track_data_parse(hist_data, action_str,
4895                                                 HANDLER_ONCHANGE);
4896                         if (IS_ERR(data)) {
4897                                 ret = PTR_ERR(data);
4898                                 break;
4899                         }
4900                 } else {
4901                         ret = -EINVAL;
4902                         break;
4903                 }
4904
4905                 hist_data->actions[hist_data->n_actions++] = data;
4906         }
4907
4908         return ret;
4909 }
4910
4911 static int create_actions(struct hist_trigger_data *hist_data)
4912 {
4913         struct action_data *data;
4914         unsigned int i;
4915         int ret = 0;
4916
4917         for (i = 0; i < hist_data->attrs->n_actions; i++) {
4918                 data = hist_data->actions[i];
4919
4920                 if (data->handler == HANDLER_ONMATCH) {
4921                         ret = onmatch_create(hist_data, data);
4922                         if (ret)
4923                                 break;
4924                 } else if (data->handler == HANDLER_ONMAX ||
4925                            data->handler == HANDLER_ONCHANGE) {
4926                         ret = track_data_create(hist_data, data);
4927                         if (ret)
4928                                 break;
4929                 } else {
4930                         ret = -EINVAL;
4931                         break;
4932                 }
4933         }
4934
4935         return ret;
4936 }
4937
4938 static void print_actions(struct seq_file *m,
4939                           struct hist_trigger_data *hist_data,
4940                           struct tracing_map_elt *elt)
4941 {
4942         unsigned int i;
4943
4944         for (i = 0; i < hist_data->n_actions; i++) {
4945                 struct action_data *data = hist_data->actions[i];
4946
4947                 if (data->action == ACTION_SNAPSHOT)
4948                         continue;
4949
4950                 if (data->handler == HANDLER_ONMAX ||
4951                     data->handler == HANDLER_ONCHANGE)
4952                         track_data_print(m, hist_data, elt, data);
4953         }
4954 }
4955
4956 static void print_action_spec(struct seq_file *m,
4957                               struct hist_trigger_data *hist_data,
4958                               struct action_data *data)
4959 {
4960         unsigned int i;
4961
4962         if (data->action == ACTION_SAVE) {
4963                 for (i = 0; i < hist_data->n_save_vars; i++) {
4964                         seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
4965                         if (i < hist_data->n_save_vars - 1)
4966                                 seq_puts(m, ",");
4967                 }
4968         } else if (data->action == ACTION_TRACE) {
4969                 if (data->use_trace_keyword)
4970                         seq_printf(m, "%s", data->synth_event_name);
4971                 for (i = 0; i < data->n_params; i++) {
4972                         if (i || data->use_trace_keyword)
4973                                 seq_puts(m, ",");
4974                         seq_printf(m, "%s", data->params[i]);
4975                 }
4976         }
4977 }
4978
4979 static void print_track_data_spec(struct seq_file *m,
4980                                   struct hist_trigger_data *hist_data,
4981                                   struct action_data *data)
4982 {
4983         if (data->handler == HANDLER_ONMAX)
4984                 seq_puts(m, ":onmax(");
4985         else if (data->handler == HANDLER_ONCHANGE)
4986                 seq_puts(m, ":onchange(");
4987         seq_printf(m, "%s", data->track_data.var_str);
4988         seq_printf(m, ").%s(", data->action_name);
4989
4990         print_action_spec(m, hist_data, data);
4991
4992         seq_puts(m, ")");
4993 }
4994
4995 static void print_onmatch_spec(struct seq_file *m,
4996                                struct hist_trigger_data *hist_data,
4997                                struct action_data *data)
4998 {
4999         seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
5000                    data->match_data.event);
5001
5002         seq_printf(m, "%s(", data->action_name);
5003
5004         print_action_spec(m, hist_data, data);
5005
5006         seq_puts(m, ")");
5007 }
5008
5009 static bool actions_match(struct hist_trigger_data *hist_data,
5010                           struct hist_trigger_data *hist_data_test)
5011 {
5012         unsigned int i, j;
5013
5014         if (hist_data->n_actions != hist_data_test->n_actions)
5015                 return false;
5016
5017         for (i = 0; i < hist_data->n_actions; i++) {
5018                 struct action_data *data = hist_data->actions[i];
5019                 struct action_data *data_test = hist_data_test->actions[i];
5020                 char *action_name, *action_name_test;
5021
5022                 if (data->handler != data_test->handler)
5023                         return false;
5024                 if (data->action != data_test->action)
5025                         return false;
5026
5027                 if (data->n_params != data_test->n_params)
5028                         return false;
5029
5030                 for (j = 0; j < data->n_params; j++) {
5031                         if (strcmp(data->params[j], data_test->params[j]) != 0)
5032                                 return false;
5033                 }
5034
5035                 if (data->use_trace_keyword)
5036                         action_name = data->synth_event_name;
5037                 else
5038                         action_name = data->action_name;
5039
5040                 if (data_test->use_trace_keyword)
5041                         action_name_test = data_test->synth_event_name;
5042                 else
5043                         action_name_test = data_test->action_name;
5044
5045                 if (strcmp(action_name, action_name_test) != 0)
5046                         return false;
5047
5048                 if (data->handler == HANDLER_ONMATCH) {
5049                         if (strcmp(data->match_data.event_system,
5050                                    data_test->match_data.event_system) != 0)
5051                                 return false;
5052                         if (strcmp(data->match_data.event,
5053                                    data_test->match_data.event) != 0)
5054                                 return false;
5055                 } else if (data->handler == HANDLER_ONMAX ||
5056                            data->handler == HANDLER_ONCHANGE) {
5057                         if (strcmp(data->track_data.var_str,
5058                                    data_test->track_data.var_str) != 0)
5059                                 return false;
5060                 }
5061         }
5062
5063         return true;
5064 }
5065
5066
5067 static void print_actions_spec(struct seq_file *m,
5068                                struct hist_trigger_data *hist_data)
5069 {
5070         unsigned int i;
5071
5072         for (i = 0; i < hist_data->n_actions; i++) {
5073                 struct action_data *data = hist_data->actions[i];
5074
5075                 if (data->handler == HANDLER_ONMATCH)
5076                         print_onmatch_spec(m, hist_data, data);
5077                 else if (data->handler == HANDLER_ONMAX ||
5078                          data->handler == HANDLER_ONCHANGE)
5079                         print_track_data_spec(m, hist_data, data);
5080         }
5081 }
5082
5083 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
5084 {
5085         unsigned int i;
5086
5087         for (i = 0; i < hist_data->n_field_var_hists; i++) {
5088                 kfree(hist_data->field_var_hists[i]->cmd);
5089                 kfree(hist_data->field_var_hists[i]);
5090         }
5091 }
5092
5093 static void destroy_hist_data(struct hist_trigger_data *hist_data)
5094 {
5095         if (!hist_data)
5096                 return;
5097
5098         destroy_hist_trigger_attrs(hist_data->attrs);
5099         destroy_hist_fields(hist_data);
5100         tracing_map_destroy(hist_data->map);
5101
5102         destroy_actions(hist_data);
5103         destroy_field_vars(hist_data);
5104         destroy_field_var_hists(hist_data);
5105
5106         kfree(hist_data);
5107 }
5108
5109 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
5110 {
5111         struct tracing_map *map = hist_data->map;
5112         struct ftrace_event_field *field;
5113         struct hist_field *hist_field;
5114         int i, idx = 0;
5115
5116         for_each_hist_field(i, hist_data) {
5117                 hist_field = hist_data->fields[i];
5118                 if (hist_field->flags & HIST_FIELD_FL_KEY) {
5119                         tracing_map_cmp_fn_t cmp_fn;
5120
5121                         field = hist_field->field;
5122
5123                         if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
5124                                 cmp_fn = tracing_map_cmp_none;
5125                         else if (!field)
5126                                 cmp_fn = tracing_map_cmp_num(hist_field->size,
5127                                                              hist_field->is_signed);
5128                         else if (is_string_field(field))
5129                                 cmp_fn = tracing_map_cmp_string;
5130                         else
5131                                 cmp_fn = tracing_map_cmp_num(field->size,
5132                                                              field->is_signed);
5133                         idx = tracing_map_add_key_field(map,
5134                                                         hist_field->offset,
5135                                                         cmp_fn);
5136                 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
5137                         idx = tracing_map_add_sum_field(map);
5138
5139                 if (idx < 0)
5140                         return idx;
5141
5142                 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5143                         idx = tracing_map_add_var(map);
5144                         if (idx < 0)
5145                                 return idx;
5146                         hist_field->var.idx = idx;
5147                         hist_field->var.hist_data = hist_data;
5148                 }
5149         }
5150
5151         return 0;
5152 }
5153
5154 static struct hist_trigger_data *
5155 create_hist_data(unsigned int map_bits,
5156                  struct hist_trigger_attrs *attrs,
5157                  struct trace_event_file *file,
5158                  bool remove)
5159 {
5160         const struct tracing_map_ops *map_ops = NULL;
5161         struct hist_trigger_data *hist_data;
5162         int ret = 0;
5163
5164         hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
5165         if (!hist_data)
5166                 return ERR_PTR(-ENOMEM);
5167
5168         hist_data->attrs = attrs;
5169         hist_data->remove = remove;
5170         hist_data->event_file = file;
5171
5172         ret = parse_actions(hist_data);
5173         if (ret)
5174                 goto free;
5175
5176         ret = create_hist_fields(hist_data, file);
5177         if (ret)
5178                 goto free;
5179
5180         ret = create_sort_keys(hist_data);
5181         if (ret)
5182                 goto free;
5183
5184         map_ops = &hist_trigger_elt_data_ops;
5185
5186         hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
5187                                             map_ops, hist_data);
5188         if (IS_ERR(hist_data->map)) {
5189                 ret = PTR_ERR(hist_data->map);
5190                 hist_data->map = NULL;
5191                 goto free;
5192         }
5193
5194         ret = create_tracing_map_fields(hist_data);
5195         if (ret)
5196                 goto free;
5197  out:
5198         return hist_data;
5199  free:
5200         hist_data->attrs = NULL;
5201
5202         destroy_hist_data(hist_data);
5203
5204         hist_data = ERR_PTR(ret);
5205
5206         goto out;
5207 }
5208
5209 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
5210                                     struct tracing_map_elt *elt, void *rec,
5211                                     struct ring_buffer_event *rbe,
5212                                     u64 *var_ref_vals)
5213 {
5214         struct hist_elt_data *elt_data;
5215         struct hist_field *hist_field;
5216         unsigned int i, var_idx;
5217         u64 hist_val;
5218
5219         elt_data = elt->private_data;
5220         elt_data->var_ref_vals = var_ref_vals;
5221
5222         for_each_hist_val_field(i, hist_data) {
5223                 hist_field = hist_data->fields[i];
5224                 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
5225                 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5226                         var_idx = hist_field->var.idx;
5227                         tracing_map_set_var(elt, var_idx, hist_val);
5228                         continue;
5229                 }
5230                 tracing_map_update_sum(elt, i, hist_val);
5231         }
5232
5233         for_each_hist_key_field(i, hist_data) {
5234                 hist_field = hist_data->fields[i];
5235                 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5236                         hist_val = hist_field->fn(hist_field, elt, rbe, rec);
5237                         var_idx = hist_field->var.idx;
5238                         tracing_map_set_var(elt, var_idx, hist_val);
5239                 }
5240         }
5241
5242         update_field_vars(hist_data, elt, rbe, rec);
5243 }
5244
5245 static inline void add_to_key(char *compound_key, void *key,
5246                               struct hist_field *key_field, void *rec)
5247 {
5248         size_t size = key_field->size;
5249
5250         if (key_field->flags & HIST_FIELD_FL_STRING) {
5251                 struct ftrace_event_field *field;
5252
5253                 field = key_field->field;
5254                 if (field->filter_type == FILTER_DYN_STRING)
5255                         size = *(u32 *)(rec + field->offset) >> 16;
5256                 else if (field->filter_type == FILTER_PTR_STRING)
5257                         size = strlen(key);
5258                 else if (field->filter_type == FILTER_STATIC_STRING)
5259                         size = field->size;
5260
5261                 /* ensure NULL-termination */
5262                 if (size > key_field->size - 1)
5263                         size = key_field->size - 1;
5264
5265                 strncpy(compound_key + key_field->offset, (char *)key, size);
5266         } else
5267                 memcpy(compound_key + key_field->offset, key, size);
5268 }
5269
5270 static void
5271 hist_trigger_actions(struct hist_trigger_data *hist_data,
5272                      struct tracing_map_elt *elt, void *rec,
5273                      struct ring_buffer_event *rbe, void *key,
5274                      u64 *var_ref_vals)
5275 {
5276         struct action_data *data;
5277         unsigned int i;
5278
5279         for (i = 0; i < hist_data->n_actions; i++) {
5280                 data = hist_data->actions[i];
5281                 data->fn(hist_data, elt, rec, rbe, key, data, var_ref_vals);
5282         }
5283 }
5284
5285 static void event_hist_trigger(struct event_trigger_data *data, void *rec,
5286                                struct ring_buffer_event *rbe)
5287 {
5288         struct hist_trigger_data *hist_data = data->private_data;
5289         bool use_compound_key = (hist_data->n_keys > 1);
5290         unsigned long entries[HIST_STACKTRACE_DEPTH];
5291         u64 var_ref_vals[TRACING_MAP_VARS_MAX];
5292         char compound_key[HIST_KEY_SIZE_MAX];
5293         struct tracing_map_elt *elt = NULL;
5294         struct hist_field *key_field;
5295         u64 field_contents;
5296         void *key = NULL;
5297         unsigned int i;
5298
5299         memset(compound_key, 0, hist_data->key_size);
5300
5301         for_each_hist_key_field(i, hist_data) {
5302                 key_field = hist_data->fields[i];
5303
5304                 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5305                         memset(entries, 0, HIST_STACKTRACE_SIZE);
5306                         stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
5307                                          HIST_STACKTRACE_SKIP);
5308                         key = entries;
5309                 } else {
5310                         field_contents = key_field->fn(key_field, elt, rbe, rec);
5311                         if (key_field->flags & HIST_FIELD_FL_STRING) {
5312                                 key = (void *)(unsigned long)field_contents;
5313                                 use_compound_key = true;
5314                         } else
5315                                 key = (void *)&field_contents;
5316                 }
5317
5318                 if (use_compound_key)
5319                         add_to_key(compound_key, key, key_field, rec);
5320         }
5321
5322         if (use_compound_key)
5323                 key = compound_key;
5324
5325         if (hist_data->n_var_refs &&
5326             !resolve_var_refs(hist_data, key, var_ref_vals, false))
5327                 return;
5328
5329         elt = tracing_map_insert(hist_data->map, key);
5330         if (!elt)
5331                 return;
5332
5333         hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals);
5334
5335         if (resolve_var_refs(hist_data, key, var_ref_vals, true))
5336                 hist_trigger_actions(hist_data, elt, rec, rbe, key, var_ref_vals);
5337 }
5338
5339 static void hist_trigger_stacktrace_print(struct seq_file *m,
5340                                           unsigned long *stacktrace_entries,
5341                                           unsigned int max_entries)
5342 {
5343         char str[KSYM_SYMBOL_LEN];
5344         unsigned int spaces = 8;
5345         unsigned int i;
5346
5347         for (i = 0; i < max_entries; i++) {
5348                 if (!stacktrace_entries[i])
5349                         return;
5350
5351                 seq_printf(m, "%*c", 1 + spaces, ' ');
5352                 sprint_symbol(str, stacktrace_entries[i]);
5353                 seq_printf(m, "%s\n", str);
5354         }
5355 }
5356
5357 static void hist_trigger_print_key(struct seq_file *m,
5358                                    struct hist_trigger_data *hist_data,
5359                                    void *key,
5360                                    struct tracing_map_elt *elt)
5361 {
5362         struct hist_field *key_field;
5363         char str[KSYM_SYMBOL_LEN];
5364         bool multiline = false;
5365         const char *field_name;
5366         unsigned int i;
5367         u64 uval;
5368
5369         seq_puts(m, "{ ");
5370
5371         for_each_hist_key_field(i, hist_data) {
5372                 key_field = hist_data->fields[i];
5373
5374                 if (i > hist_data->n_vals)
5375                         seq_puts(m, ", ");
5376
5377                 field_name = hist_field_name(key_field, 0);
5378
5379                 if (key_field->flags & HIST_FIELD_FL_HEX) {
5380                         uval = *(u64 *)(key + key_field->offset);
5381                         seq_printf(m, "%s: %llx", field_name, uval);
5382                 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
5383                         uval = *(u64 *)(key + key_field->offset);
5384                         sprint_symbol_no_offset(str, uval);
5385                         seq_printf(m, "%s: [%llx] %-45s", field_name,
5386                                    uval, str);
5387                 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
5388                         uval = *(u64 *)(key + key_field->offset);
5389                         sprint_symbol(str, uval);
5390                         seq_printf(m, "%s: [%llx] %-55s", field_name,
5391                                    uval, str);
5392                 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
5393                         struct hist_elt_data *elt_data = elt->private_data;
5394                         char *comm;
5395
5396                         if (WARN_ON_ONCE(!elt_data))
5397                                 return;
5398
5399                         comm = elt_data->comm;
5400
5401                         uval = *(u64 *)(key + key_field->offset);
5402                         seq_printf(m, "%s: %-16s[%10llu]", field_name,
5403                                    comm, uval);
5404                 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
5405                         const char *syscall_name;
5406
5407                         uval = *(u64 *)(key + key_field->offset);
5408                         syscall_name = get_syscall_name(uval);
5409                         if (!syscall_name)
5410                                 syscall_name = "unknown_syscall";
5411
5412                         seq_printf(m, "%s: %-30s[%3llu]", field_name,
5413                                    syscall_name, uval);
5414                 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5415                         seq_puts(m, "stacktrace:\n");
5416                         hist_trigger_stacktrace_print(m,
5417                                                       key + key_field->offset,
5418                                                       HIST_STACKTRACE_DEPTH);
5419                         multiline = true;
5420                 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
5421                         seq_printf(m, "%s: ~ 2^%-2llu", field_name,
5422                                    *(u64 *)(key + key_field->offset));
5423                 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
5424                         seq_printf(m, "%s: %-50s", field_name,
5425                                    (char *)(key + key_field->offset));
5426                 } else {
5427                         uval = *(u64 *)(key + key_field->offset);
5428                         seq_printf(m, "%s: %10llu", field_name, uval);
5429                 }
5430         }
5431
5432         if (!multiline)
5433                 seq_puts(m, " ");
5434
5435         seq_puts(m, "}");
5436 }
5437
5438 static void hist_trigger_entry_print(struct seq_file *m,
5439                                      struct hist_trigger_data *hist_data,
5440                                      void *key,
5441                                      struct tracing_map_elt *elt)
5442 {
5443         const char *field_name;
5444         unsigned int i;
5445
5446         hist_trigger_print_key(m, hist_data, key, elt);
5447
5448         seq_printf(m, " hitcount: %10llu",
5449                    tracing_map_read_sum(elt, HITCOUNT_IDX));
5450
5451         for (i = 1; i < hist_data->n_vals; i++) {
5452                 field_name = hist_field_name(hist_data->fields[i], 0);
5453
5454                 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
5455                     hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
5456                         continue;
5457
5458                 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
5459                         seq_printf(m, "  %s: %10llx", field_name,
5460                                    tracing_map_read_sum(elt, i));
5461                 } else {
5462                         seq_printf(m, "  %s: %10llu", field_name,
5463                                    tracing_map_read_sum(elt, i));
5464                 }
5465         }
5466
5467         print_actions(m, hist_data, elt);
5468
5469         seq_puts(m, "\n");
5470 }
5471
5472 static int print_entries(struct seq_file *m,
5473                          struct hist_trigger_data *hist_data)
5474 {
5475         struct tracing_map_sort_entry **sort_entries = NULL;
5476         struct tracing_map *map = hist_data->map;
5477         int i, n_entries;
5478
5479         n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
5480                                              hist_data->n_sort_keys,
5481                                              &sort_entries);
5482         if (n_entries < 0)
5483                 return n_entries;
5484
5485         for (i = 0; i < n_entries; i++)
5486                 hist_trigger_entry_print(m, hist_data,
5487                                          sort_entries[i]->key,
5488                                          sort_entries[i]->elt);
5489
5490         tracing_map_destroy_sort_entries(sort_entries, n_entries);
5491
5492         return n_entries;
5493 }
5494
5495 static void hist_trigger_show(struct seq_file *m,
5496                               struct event_trigger_data *data, int n)
5497 {
5498         struct hist_trigger_data *hist_data;
5499         int n_entries;
5500
5501         if (n > 0)
5502                 seq_puts(m, "\n\n");
5503
5504         seq_puts(m, "# event histogram\n#\n# trigger info: ");
5505         data->ops->print(m, data->ops, data);
5506         seq_puts(m, "#\n\n");
5507
5508         hist_data = data->private_data;
5509         n_entries = print_entries(m, hist_data);
5510         if (n_entries < 0)
5511                 n_entries = 0;
5512
5513         track_data_snapshot_print(m, hist_data);
5514
5515         seq_printf(m, "\nTotals:\n    Hits: %llu\n    Entries: %u\n    Dropped: %llu\n",
5516                    (u64)atomic64_read(&hist_data->map->hits),
5517                    n_entries, (u64)atomic64_read(&hist_data->map->drops));
5518 }
5519
5520 static int hist_show(struct seq_file *m, void *v)
5521 {
5522         struct event_trigger_data *data;
5523         struct trace_event_file *event_file;
5524         int n = 0, ret = 0;
5525
5526         mutex_lock(&event_mutex);
5527
5528         event_file = event_file_data(m->private);
5529         if (unlikely(!event_file)) {
5530                 ret = -ENODEV;
5531                 goto out_unlock;
5532         }
5533
5534         list_for_each_entry_rcu(data, &event_file->triggers, list) {
5535                 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5536                         hist_trigger_show(m, data, n++);
5537         }
5538
5539  out_unlock:
5540         mutex_unlock(&event_mutex);
5541
5542         return ret;
5543 }
5544
5545 static int event_hist_open(struct inode *inode, struct file *file)
5546 {
5547         int ret;
5548
5549         ret = security_locked_down(LOCKDOWN_TRACEFS);
5550         if (ret)
5551                 return ret;
5552
5553         return single_open(file, hist_show, file);
5554 }
5555
5556 const struct file_operations event_hist_fops = {
5557         .open = event_hist_open,
5558         .read = seq_read,
5559         .llseek = seq_lseek,
5560         .release = single_release,
5561 };
5562
5563 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
5564 {
5565         const char *field_name = hist_field_name(hist_field, 0);
5566
5567         if (hist_field->var.name)
5568                 seq_printf(m, "%s=", hist_field->var.name);
5569
5570         if (hist_field->flags & HIST_FIELD_FL_CPU)
5571                 seq_puts(m, "cpu");
5572         else if (field_name) {
5573                 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
5574                     hist_field->flags & HIST_FIELD_FL_ALIAS)
5575                         seq_putc(m, '$');
5576                 seq_printf(m, "%s", field_name);
5577         } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
5578                 seq_puts(m, "common_timestamp");
5579
5580         if (hist_field->flags) {
5581                 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
5582                     !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
5583                         const char *flags = get_hist_field_flags(hist_field);
5584
5585                         if (flags)
5586                                 seq_printf(m, ".%s", flags);
5587                 }
5588         }
5589 }
5590
5591 static int event_hist_trigger_print(struct seq_file *m,
5592                                     struct event_trigger_ops *ops,
5593                                     struct event_trigger_data *data)
5594 {
5595         struct hist_trigger_data *hist_data = data->private_data;
5596         struct hist_field *field;
5597         bool have_var = false;
5598         unsigned int i;
5599
5600         seq_puts(m, "hist:");
5601
5602         if (data->name)
5603                 seq_printf(m, "%s:", data->name);
5604
5605         seq_puts(m, "keys=");
5606
5607         for_each_hist_key_field(i, hist_data) {
5608                 field = hist_data->fields[i];
5609
5610                 if (i > hist_data->n_vals)
5611                         seq_puts(m, ",");
5612
5613                 if (field->flags & HIST_FIELD_FL_STACKTRACE)
5614                         seq_puts(m, "stacktrace");
5615                 else
5616                         hist_field_print(m, field);
5617         }
5618
5619         seq_puts(m, ":vals=");
5620
5621         for_each_hist_val_field(i, hist_data) {
5622                 field = hist_data->fields[i];
5623                 if (field->flags & HIST_FIELD_FL_VAR) {
5624                         have_var = true;
5625                         continue;
5626                 }
5627
5628                 if (i == HITCOUNT_IDX)
5629                         seq_puts(m, "hitcount");
5630                 else {
5631                         seq_puts(m, ",");
5632                         hist_field_print(m, field);
5633                 }
5634         }
5635
5636         if (have_var) {
5637                 unsigned int n = 0;
5638
5639                 seq_puts(m, ":");
5640
5641                 for_each_hist_val_field(i, hist_data) {
5642                         field = hist_data->fields[i];
5643
5644                         if (field->flags & HIST_FIELD_FL_VAR) {
5645                                 if (n++)
5646                                         seq_puts(m, ",");
5647                                 hist_field_print(m, field);
5648                         }
5649                 }
5650         }
5651
5652         seq_puts(m, ":sort=");
5653
5654         for (i = 0; i < hist_data->n_sort_keys; i++) {
5655                 struct tracing_map_sort_key *sort_key;
5656                 unsigned int idx, first_key_idx;
5657
5658                 /* skip VAR vals */
5659                 first_key_idx = hist_data->n_vals - hist_data->n_vars;
5660
5661                 sort_key = &hist_data->sort_keys[i];
5662                 idx = sort_key->field_idx;
5663
5664                 if (WARN_ON(idx >= HIST_FIELDS_MAX))
5665                         return -EINVAL;
5666
5667                 if (i > 0)
5668                         seq_puts(m, ",");
5669
5670                 if (idx == HITCOUNT_IDX)
5671                         seq_puts(m, "hitcount");
5672                 else {
5673                         if (idx >= first_key_idx)
5674                                 idx += hist_data->n_vars;
5675                         hist_field_print(m, hist_data->fields[idx]);
5676                 }
5677
5678                 if (sort_key->descending)
5679                         seq_puts(m, ".descending");
5680         }
5681         seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
5682         if (hist_data->enable_timestamps)
5683                 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
5684
5685         print_actions_spec(m, hist_data);
5686
5687         if (data->filter_str)
5688                 seq_printf(m, " if %s", data->filter_str);
5689
5690         if (data->paused)
5691                 seq_puts(m, " [paused]");
5692         else
5693                 seq_puts(m, " [active]");
5694
5695         seq_putc(m, '\n');
5696
5697         return 0;
5698 }
5699
5700 static int event_hist_trigger_init(struct event_trigger_ops *ops,
5701                                    struct event_trigger_data *data)
5702 {
5703         struct hist_trigger_data *hist_data = data->private_data;
5704
5705         if (!data->ref && hist_data->attrs->name)
5706                 save_named_trigger(hist_data->attrs->name, data);
5707
5708         data->ref++;
5709
5710         return 0;
5711 }
5712
5713 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
5714 {
5715         struct trace_event_file *file;
5716         unsigned int i;
5717         char *cmd;
5718         int ret;
5719
5720         for (i = 0; i < hist_data->n_field_var_hists; i++) {
5721                 file = hist_data->field_var_hists[i]->hist_data->event_file;
5722                 cmd = hist_data->field_var_hists[i]->cmd;
5723                 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
5724                                               "!hist", "hist", cmd);
5725         }
5726 }
5727
5728 static void event_hist_trigger_free(struct event_trigger_ops *ops,
5729                                     struct event_trigger_data *data)
5730 {
5731         struct hist_trigger_data *hist_data = data->private_data;
5732
5733         if (WARN_ON_ONCE(data->ref <= 0))
5734                 return;
5735
5736         data->ref--;
5737         if (!data->ref) {
5738                 if (data->name)
5739                         del_named_trigger(data);
5740
5741                 trigger_data_free(data);
5742
5743                 remove_hist_vars(hist_data);
5744
5745                 unregister_field_var_hists(hist_data);
5746
5747                 destroy_hist_data(hist_data);
5748         }
5749 }
5750
5751 static struct event_trigger_ops event_hist_trigger_ops = {
5752         .func                   = event_hist_trigger,
5753         .print                  = event_hist_trigger_print,
5754         .init                   = event_hist_trigger_init,
5755         .free                   = event_hist_trigger_free,
5756 };
5757
5758 static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
5759                                          struct event_trigger_data *data)
5760 {
5761         data->ref++;
5762
5763         save_named_trigger(data->named_data->name, data);
5764
5765         event_hist_trigger_init(ops, data->named_data);
5766
5767         return 0;
5768 }
5769
5770 static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
5771                                           struct event_trigger_data *data)
5772 {
5773         if (WARN_ON_ONCE(data->ref <= 0))
5774                 return;
5775
5776         event_hist_trigger_free(ops, data->named_data);
5777
5778         data->ref--;
5779         if (!data->ref) {
5780                 del_named_trigger(data);
5781                 trigger_data_free(data);
5782         }
5783 }
5784
5785 static struct event_trigger_ops event_hist_trigger_named_ops = {
5786         .func                   = event_hist_trigger,
5787         .print                  = event_hist_trigger_print,
5788         .init                   = event_hist_trigger_named_init,
5789         .free                   = event_hist_trigger_named_free,
5790 };
5791
5792 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
5793                                                             char *param)
5794 {
5795         return &event_hist_trigger_ops;
5796 }
5797
5798 static void hist_clear(struct event_trigger_data *data)
5799 {
5800         struct hist_trigger_data *hist_data = data->private_data;
5801
5802         if (data->name)
5803                 pause_named_trigger(data);
5804
5805         tracepoint_synchronize_unregister();
5806
5807         tracing_map_clear(hist_data->map);
5808
5809         if (data->name)
5810                 unpause_named_trigger(data);
5811 }
5812
5813 static bool compatible_field(struct ftrace_event_field *field,
5814                              struct ftrace_event_field *test_field)
5815 {
5816         if (field == test_field)
5817                 return true;
5818         if (field == NULL || test_field == NULL)
5819                 return false;
5820         if (strcmp(field->name, test_field->name) != 0)
5821                 return false;
5822         if (strcmp(field->type, test_field->type) != 0)
5823                 return false;
5824         if (field->size != test_field->size)
5825                 return false;
5826         if (field->is_signed != test_field->is_signed)
5827                 return false;
5828
5829         return true;
5830 }
5831
5832 static bool hist_trigger_match(struct event_trigger_data *data,
5833                                struct event_trigger_data *data_test,
5834                                struct event_trigger_data *named_data,
5835                                bool ignore_filter)
5836 {
5837         struct tracing_map_sort_key *sort_key, *sort_key_test;
5838         struct hist_trigger_data *hist_data, *hist_data_test;
5839         struct hist_field *key_field, *key_field_test;
5840         unsigned int i;
5841
5842         if (named_data && (named_data != data_test) &&
5843             (named_data != data_test->named_data))
5844                 return false;
5845
5846         if (!named_data && is_named_trigger(data_test))
5847                 return false;
5848
5849         hist_data = data->private_data;
5850         hist_data_test = data_test->private_data;
5851
5852         if (hist_data->n_vals != hist_data_test->n_vals ||
5853             hist_data->n_fields != hist_data_test->n_fields ||
5854             hist_data->n_sort_keys != hist_data_test->n_sort_keys)
5855                 return false;
5856
5857         if (!ignore_filter) {
5858                 if ((data->filter_str && !data_test->filter_str) ||
5859                    (!data->filter_str && data_test->filter_str))
5860                         return false;
5861         }
5862
5863         for_each_hist_field(i, hist_data) {
5864                 key_field = hist_data->fields[i];
5865                 key_field_test = hist_data_test->fields[i];
5866
5867                 if (key_field->flags != key_field_test->flags)
5868                         return false;
5869                 if (!compatible_field(key_field->field, key_field_test->field))
5870                         return false;
5871                 if (key_field->offset != key_field_test->offset)
5872                         return false;
5873                 if (key_field->size != key_field_test->size)
5874                         return false;
5875                 if (key_field->is_signed != key_field_test->is_signed)
5876                         return false;
5877                 if (!!key_field->var.name != !!key_field_test->var.name)
5878                         return false;
5879                 if (key_field->var.name &&
5880                     strcmp(key_field->var.name, key_field_test->var.name) != 0)
5881                         return false;
5882         }
5883
5884         for (i = 0; i < hist_data->n_sort_keys; i++) {
5885                 sort_key = &hist_data->sort_keys[i];
5886                 sort_key_test = &hist_data_test->sort_keys[i];
5887
5888                 if (sort_key->field_idx != sort_key_test->field_idx ||
5889                     sort_key->descending != sort_key_test->descending)
5890                         return false;
5891         }
5892
5893         if (!ignore_filter && data->filter_str &&
5894             (strcmp(data->filter_str, data_test->filter_str) != 0))
5895                 return false;
5896
5897         if (!actions_match(hist_data, hist_data_test))
5898                 return false;
5899
5900         return true;
5901 }
5902
5903 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
5904                                  struct event_trigger_data *data,
5905                                  struct trace_event_file *file)
5906 {
5907         struct hist_trigger_data *hist_data = data->private_data;
5908         struct event_trigger_data *test, *named_data = NULL;
5909         struct trace_array *tr = file->tr;
5910         int ret = 0;
5911
5912         if (hist_data->attrs->name) {
5913                 named_data = find_named_trigger(hist_data->attrs->name);
5914                 if (named_data) {
5915                         if (!hist_trigger_match(data, named_data, named_data,
5916                                                 true)) {
5917                                 hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name));
5918                                 ret = -EINVAL;
5919                                 goto out;
5920                         }
5921                 }
5922         }
5923
5924         if (hist_data->attrs->name && !named_data)
5925                 goto new;
5926
5927         list_for_each_entry_rcu(test, &file->triggers, list) {
5928                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5929                         if (!hist_trigger_match(data, test, named_data, false))
5930                                 continue;
5931                         if (hist_data->attrs->pause)
5932                                 test->paused = true;
5933                         else if (hist_data->attrs->cont)
5934                                 test->paused = false;
5935                         else if (hist_data->attrs->clear)
5936                                 hist_clear(test);
5937                         else {
5938                                 hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
5939                                 ret = -EEXIST;
5940                         }
5941                         goto out;
5942                 }
5943         }
5944  new:
5945         if (hist_data->attrs->cont || hist_data->attrs->clear) {
5946                 hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0);
5947                 ret = -ENOENT;
5948                 goto out;
5949         }
5950
5951         if (hist_data->attrs->pause)
5952                 data->paused = true;
5953
5954         if (named_data) {
5955                 data->private_data = named_data->private_data;
5956                 set_named_trigger_data(data, named_data);
5957                 data->ops = &event_hist_trigger_named_ops;
5958         }
5959
5960         if (data->ops->init) {
5961                 ret = data->ops->init(data->ops, data);
5962                 if (ret < 0)
5963                         goto out;
5964         }
5965
5966         if (hist_data->enable_timestamps) {
5967                 char *clock = hist_data->attrs->clock;
5968
5969                 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
5970                 if (ret) {
5971                         hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock));
5972                         goto out;
5973                 }
5974
5975                 tracing_set_time_stamp_abs(file->tr, true);
5976         }
5977
5978         if (named_data)
5979                 destroy_hist_data(hist_data);
5980
5981         ret++;
5982  out:
5983         return ret;
5984 }
5985
5986 static int hist_trigger_enable(struct event_trigger_data *data,
5987                                struct trace_event_file *file)
5988 {
5989         int ret = 0;
5990
5991         list_add_tail_rcu(&data->list, &file->triggers);
5992
5993         update_cond_flag(file);
5994
5995         if (trace_event_trigger_enable_disable(file, 1) < 0) {
5996                 list_del_rcu(&data->list);
5997                 update_cond_flag(file);
5998                 ret--;
5999         }
6000
6001         return ret;
6002 }
6003
6004 static bool have_hist_trigger_match(struct event_trigger_data *data,
6005                                     struct trace_event_file *file)
6006 {
6007         struct hist_trigger_data *hist_data = data->private_data;
6008         struct event_trigger_data *test, *named_data = NULL;
6009         bool match = false;
6010
6011         if (hist_data->attrs->name)
6012                 named_data = find_named_trigger(hist_data->attrs->name);
6013
6014         list_for_each_entry_rcu(test, &file->triggers, list) {
6015                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6016                         if (hist_trigger_match(data, test, named_data, false)) {
6017                                 match = true;
6018                                 break;
6019                         }
6020                 }
6021         }
6022
6023         return match;
6024 }
6025
6026 static bool hist_trigger_check_refs(struct event_trigger_data *data,
6027                                     struct trace_event_file *file)
6028 {
6029         struct hist_trigger_data *hist_data = data->private_data;
6030         struct event_trigger_data *test, *named_data = NULL;
6031
6032         if (hist_data->attrs->name)
6033                 named_data = find_named_trigger(hist_data->attrs->name);
6034
6035         list_for_each_entry_rcu(test, &file->triggers, list) {
6036                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6037                         if (!hist_trigger_match(data, test, named_data, false))
6038                                 continue;
6039                         hist_data = test->private_data;
6040                         if (check_var_refs(hist_data))
6041                                 return true;
6042                         break;
6043                 }
6044         }
6045
6046         return false;
6047 }
6048
6049 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
6050                                     struct event_trigger_data *data,
6051                                     struct trace_event_file *file)
6052 {
6053         struct hist_trigger_data *hist_data = data->private_data;
6054         struct event_trigger_data *test, *named_data = NULL;
6055         bool unregistered = false;
6056
6057         if (hist_data->attrs->name)
6058                 named_data = find_named_trigger(hist_data->attrs->name);
6059
6060         list_for_each_entry_rcu(test, &file->triggers, list) {
6061                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6062                         if (!hist_trigger_match(data, test, named_data, false))
6063                                 continue;
6064                         unregistered = true;
6065                         list_del_rcu(&test->list);
6066                         trace_event_trigger_enable_disable(file, 0);
6067                         update_cond_flag(file);
6068                         break;
6069                 }
6070         }
6071
6072         if (unregistered && test->ops->free)
6073                 test->ops->free(test->ops, test);
6074
6075         if (hist_data->enable_timestamps) {
6076                 if (!hist_data->remove || unregistered)
6077                         tracing_set_time_stamp_abs(file->tr, false);
6078         }
6079 }
6080
6081 static bool hist_file_check_refs(struct trace_event_file *file)
6082 {
6083         struct hist_trigger_data *hist_data;
6084         struct event_trigger_data *test;
6085
6086         list_for_each_entry_rcu(test, &file->triggers, list) {
6087                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6088                         hist_data = test->private_data;
6089                         if (check_var_refs(hist_data))
6090                                 return true;
6091                 }
6092         }
6093
6094         return false;
6095 }
6096
6097 static void hist_unreg_all(struct trace_event_file *file)
6098 {
6099         struct event_trigger_data *test, *n;
6100         struct hist_trigger_data *hist_data;
6101         struct synth_event *se;
6102         const char *se_name;
6103
6104         lockdep_assert_held(&event_mutex);
6105
6106         if (hist_file_check_refs(file))
6107                 return;
6108
6109         list_for_each_entry_safe(test, n, &file->triggers, list) {
6110                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6111                         hist_data = test->private_data;
6112                         list_del_rcu(&test->list);
6113                         trace_event_trigger_enable_disable(file, 0);
6114
6115                         se_name = trace_event_name(file->event_call);
6116                         se = find_synth_event(se_name);
6117                         if (se)
6118                                 se->ref--;
6119
6120                         update_cond_flag(file);
6121                         if (hist_data->enable_timestamps)
6122                                 tracing_set_time_stamp_abs(file->tr, false);
6123                         if (test->ops->free)
6124                                 test->ops->free(test->ops, test);
6125                 }
6126         }
6127 }
6128
6129 static int event_hist_trigger_func(struct event_command *cmd_ops,
6130                                    struct trace_event_file *file,
6131                                    char *glob, char *cmd, char *param)
6132 {
6133         unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
6134         struct event_trigger_data *trigger_data;
6135         struct hist_trigger_attrs *attrs;
6136         struct event_trigger_ops *trigger_ops;
6137         struct hist_trigger_data *hist_data;
6138         struct synth_event *se;
6139         const char *se_name;
6140         bool remove = false;
6141         char *trigger, *p;
6142         int ret = 0;
6143
6144         lockdep_assert_held(&event_mutex);
6145
6146         if (glob && strlen(glob)) {
6147                 hist_err_clear();
6148                 last_cmd_set(file, param);
6149         }
6150
6151         if (!param)
6152                 return -EINVAL;
6153
6154         if (glob[0] == '!')
6155                 remove = true;
6156
6157         /*
6158          * separate the trigger from the filter (k:v [if filter])
6159          * allowing for whitespace in the trigger
6160          */
6161         p = trigger = param;
6162         do {
6163                 p = strstr(p, "if");
6164                 if (!p)
6165                         break;
6166                 if (p == param)
6167                         return -EINVAL;
6168                 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
6169                         p++;
6170                         continue;
6171                 }
6172                 if (p >= param + strlen(param) - (sizeof("if") - 1) - 1)
6173                         return -EINVAL;
6174                 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
6175                         p++;
6176                         continue;
6177                 }
6178                 break;
6179         } while (p);
6180
6181         if (!p)
6182                 param = NULL;
6183         else {
6184                 *(p - 1) = '\0';
6185                 param = strstrip(p);
6186                 trigger = strstrip(trigger);
6187         }
6188
6189         attrs = parse_hist_trigger_attrs(file->tr, trigger);
6190         if (IS_ERR(attrs))
6191                 return PTR_ERR(attrs);
6192
6193         if (attrs->map_bits)
6194                 hist_trigger_bits = attrs->map_bits;
6195
6196         hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
6197         if (IS_ERR(hist_data)) {
6198                 destroy_hist_trigger_attrs(attrs);
6199                 return PTR_ERR(hist_data);
6200         }
6201
6202         trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
6203
6204         trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
6205         if (!trigger_data) {
6206                 ret = -ENOMEM;
6207                 goto out_free;
6208         }
6209
6210         trigger_data->count = -1;
6211         trigger_data->ops = trigger_ops;
6212         trigger_data->cmd_ops = cmd_ops;
6213
6214         INIT_LIST_HEAD(&trigger_data->list);
6215         RCU_INIT_POINTER(trigger_data->filter, NULL);
6216
6217         trigger_data->private_data = hist_data;
6218
6219         /* if param is non-empty, it's supposed to be a filter */
6220         if (param && cmd_ops->set_filter) {
6221                 ret = cmd_ops->set_filter(param, trigger_data, file);
6222                 if (ret < 0)
6223                         goto out_free;
6224         }
6225
6226         if (remove) {
6227                 if (!have_hist_trigger_match(trigger_data, file))
6228                         goto out_free;
6229
6230                 if (hist_trigger_check_refs(trigger_data, file)) {
6231                         ret = -EBUSY;
6232                         goto out_free;
6233                 }
6234
6235                 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
6236                 se_name = trace_event_name(file->event_call);
6237                 se = find_synth_event(se_name);
6238                 if (se)
6239                         se->ref--;
6240                 ret = 0;
6241                 goto out_free;
6242         }
6243
6244         ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
6245         /*
6246          * The above returns on success the # of triggers registered,
6247          * but if it didn't register any it returns zero.  Consider no
6248          * triggers registered a failure too.
6249          */
6250         if (!ret) {
6251                 if (!(attrs->pause || attrs->cont || attrs->clear))
6252                         ret = -ENOENT;
6253                 goto out_free;
6254         } else if (ret < 0)
6255                 goto out_free;
6256
6257         if (get_named_trigger_data(trigger_data))
6258                 goto enable;
6259
6260         if (has_hist_vars(hist_data))
6261                 save_hist_vars(hist_data);
6262
6263         ret = create_actions(hist_data);
6264         if (ret)
6265                 goto out_unreg;
6266
6267         ret = tracing_map_init(hist_data->map);
6268         if (ret)
6269                 goto out_unreg;
6270 enable:
6271         ret = hist_trigger_enable(trigger_data, file);
6272         if (ret)
6273                 goto out_unreg;
6274
6275         se_name = trace_event_name(file->event_call);
6276         se = find_synth_event(se_name);
6277         if (se)
6278                 se->ref++;
6279         /* Just return zero, not the number of registered triggers */
6280         ret = 0;
6281  out:
6282         if (ret == 0)
6283                 hist_err_clear();
6284
6285         return ret;
6286  out_unreg:
6287         cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
6288  out_free:
6289         if (cmd_ops->set_filter)
6290                 cmd_ops->set_filter(NULL, trigger_data, NULL);
6291
6292         remove_hist_vars(hist_data);
6293
6294         kfree(trigger_data);
6295
6296         destroy_hist_data(hist_data);
6297         goto out;
6298 }
6299
6300 static struct event_command trigger_hist_cmd = {
6301         .name                   = "hist",
6302         .trigger_type           = ETT_EVENT_HIST,
6303         .flags                  = EVENT_CMD_FL_NEEDS_REC,
6304         .func                   = event_hist_trigger_func,
6305         .reg                    = hist_register_trigger,
6306         .unreg                  = hist_unregister_trigger,
6307         .unreg_all              = hist_unreg_all,
6308         .get_trigger_ops        = event_hist_get_trigger_ops,
6309         .set_filter             = set_trigger_filter,
6310 };
6311
6312 __init int register_trigger_hist_cmd(void)
6313 {
6314         int ret;
6315
6316         ret = register_event_command(&trigger_hist_cmd);
6317         WARN_ON(ret < 0);
6318
6319         return ret;
6320 }
6321
6322 static void
6323 hist_enable_trigger(struct event_trigger_data *data, void *rec,
6324                     struct ring_buffer_event *event)
6325 {
6326         struct enable_trigger_data *enable_data = data->private_data;
6327         struct event_trigger_data *test;
6328
6329         list_for_each_entry_rcu(test, &enable_data->file->triggers, list) {
6330                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6331                         if (enable_data->enable)
6332                                 test->paused = false;
6333                         else
6334                                 test->paused = true;
6335                 }
6336         }
6337 }
6338
6339 static void
6340 hist_enable_count_trigger(struct event_trigger_data *data, void *rec,
6341                           struct ring_buffer_event *event)
6342 {
6343         if (!data->count)
6344                 return;
6345
6346         if (data->count != -1)
6347                 (data->count)--;
6348
6349         hist_enable_trigger(data, rec, event);
6350 }
6351
6352 static struct event_trigger_ops hist_enable_trigger_ops = {
6353         .func                   = hist_enable_trigger,
6354         .print                  = event_enable_trigger_print,
6355         .init                   = event_trigger_init,
6356         .free                   = event_enable_trigger_free,
6357 };
6358
6359 static struct event_trigger_ops hist_enable_count_trigger_ops = {
6360         .func                   = hist_enable_count_trigger,
6361         .print                  = event_enable_trigger_print,
6362         .init                   = event_trigger_init,
6363         .free                   = event_enable_trigger_free,
6364 };
6365
6366 static struct event_trigger_ops hist_disable_trigger_ops = {
6367         .func                   = hist_enable_trigger,
6368         .print                  = event_enable_trigger_print,
6369         .init                   = event_trigger_init,
6370         .free                   = event_enable_trigger_free,
6371 };
6372
6373 static struct event_trigger_ops hist_disable_count_trigger_ops = {
6374         .func                   = hist_enable_count_trigger,
6375         .print                  = event_enable_trigger_print,
6376         .init                   = event_trigger_init,
6377         .free                   = event_enable_trigger_free,
6378 };
6379
6380 static struct event_trigger_ops *
6381 hist_enable_get_trigger_ops(char *cmd, char *param)
6382 {
6383         struct event_trigger_ops *ops;
6384         bool enable;
6385
6386         enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
6387
6388         if (enable)
6389                 ops = param ? &hist_enable_count_trigger_ops :
6390                         &hist_enable_trigger_ops;
6391         else
6392                 ops = param ? &hist_disable_count_trigger_ops :
6393                         &hist_disable_trigger_ops;
6394
6395         return ops;
6396 }
6397
6398 static void hist_enable_unreg_all(struct trace_event_file *file)
6399 {
6400         struct event_trigger_data *test, *n;
6401
6402         list_for_each_entry_safe(test, n, &file->triggers, list) {
6403                 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
6404                         list_del_rcu(&test->list);
6405                         update_cond_flag(file);
6406                         trace_event_trigger_enable_disable(file, 0);
6407                         if (test->ops->free)
6408                                 test->ops->free(test->ops, test);
6409                 }
6410         }
6411 }
6412
6413 static struct event_command trigger_hist_enable_cmd = {
6414         .name                   = ENABLE_HIST_STR,
6415         .trigger_type           = ETT_HIST_ENABLE,
6416         .func                   = event_enable_trigger_func,
6417         .reg                    = event_enable_register_trigger,
6418         .unreg                  = event_enable_unregister_trigger,
6419         .unreg_all              = hist_enable_unreg_all,
6420         .get_trigger_ops        = hist_enable_get_trigger_ops,
6421         .set_filter             = set_trigger_filter,
6422 };
6423
6424 static struct event_command trigger_hist_disable_cmd = {
6425         .name                   = DISABLE_HIST_STR,
6426         .trigger_type           = ETT_HIST_ENABLE,
6427         .func                   = event_enable_trigger_func,
6428         .reg                    = event_enable_register_trigger,
6429         .unreg                  = event_enable_unregister_trigger,
6430         .unreg_all              = hist_enable_unreg_all,
6431         .get_trigger_ops        = hist_enable_get_trigger_ops,
6432         .set_filter             = set_trigger_filter,
6433 };
6434
6435 static __init void unregister_trigger_hist_enable_disable_cmds(void)
6436 {
6437         unregister_event_command(&trigger_hist_enable_cmd);
6438         unregister_event_command(&trigger_hist_disable_cmd);
6439 }
6440
6441 __init int register_trigger_hist_enable_disable_cmds(void)
6442 {
6443         int ret;
6444
6445         ret = register_event_command(&trigger_hist_enable_cmd);
6446         if (WARN_ON(ret < 0))
6447                 return ret;
6448         ret = register_event_command(&trigger_hist_disable_cmd);
6449         if (WARN_ON(ret < 0))
6450                 unregister_trigger_hist_enable_disable_cmds();
6451
6452         return ret;
6453 }
6454
6455 static __init int trace_events_hist_init(void)
6456 {
6457         struct dentry *entry = NULL;
6458         struct dentry *d_tracer;
6459         int err = 0;
6460
6461         err = dyn_event_register(&synth_event_ops);
6462         if (err) {
6463                 pr_warn("Could not register synth_event_ops\n");
6464                 return err;
6465         }
6466
6467         d_tracer = tracing_init_dentry();
6468         if (IS_ERR(d_tracer)) {
6469                 err = PTR_ERR(d_tracer);
6470                 goto err;
6471         }
6472
6473         entry = tracefs_create_file("synthetic_events", 0644, d_tracer,
6474                                     NULL, &synth_events_fops);
6475         if (!entry) {
6476                 err = -ENODEV;
6477                 goto err;
6478         }
6479
6480         return err;
6481  err:
6482         pr_warn("Could not create tracefs 'synthetic_events' entry\n");
6483
6484         return err;
6485 }
6486
6487 fs_initcall(trace_events_hist_init);