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