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