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