tracing: Add hist trigger 'log2' modifier
[cascardo/linux.git] / kernel / trace / trace_events_hist.c
1 /*
2  * trace_events_hist - trace event hist triggers
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * Copyright (C) 2015 Tom Zanussi <tom.zanussi@linux.intel.com>
15  */
16
17 #include <linux/module.h>
18 #include <linux/kallsyms.h>
19 #include <linux/mutex.h>
20 #include <linux/slab.h>
21 #include <linux/stacktrace.h>
22
23 #include "tracing_map.h"
24 #include "trace.h"
25
26 struct hist_field;
27
28 typedef u64 (*hist_field_fn_t) (struct hist_field *field, void *event);
29
30 struct hist_field {
31         struct ftrace_event_field       *field;
32         unsigned long                   flags;
33         hist_field_fn_t                 fn;
34         unsigned int                    size;
35         unsigned int                    offset;
36 };
37
38 static u64 hist_field_none(struct hist_field *field, void *event)
39 {
40         return 0;
41 }
42
43 static u64 hist_field_counter(struct hist_field *field, void *event)
44 {
45         return 1;
46 }
47
48 static u64 hist_field_string(struct hist_field *hist_field, void *event)
49 {
50         char *addr = (char *)(event + hist_field->field->offset);
51
52         return (u64)(unsigned long)addr;
53 }
54
55 static u64 hist_field_dynstring(struct hist_field *hist_field, void *event)
56 {
57         u32 str_item = *(u32 *)(event + hist_field->field->offset);
58         int str_loc = str_item & 0xffff;
59         char *addr = (char *)(event + str_loc);
60
61         return (u64)(unsigned long)addr;
62 }
63
64 static u64 hist_field_pstring(struct hist_field *hist_field, void *event)
65 {
66         char **addr = (char **)(event + hist_field->field->offset);
67
68         return (u64)(unsigned long)*addr;
69 }
70
71 static u64 hist_field_log2(struct hist_field *hist_field, void *event)
72 {
73         u64 val = *(u64 *)(event + hist_field->field->offset);
74
75         return (u64) ilog2(roundup_pow_of_two(val));
76 }
77
78 #define DEFINE_HIST_FIELD_FN(type)                                      \
79 static u64 hist_field_##type(struct hist_field *hist_field, void *event)\
80 {                                                                       \
81         type *addr = (type *)(event + hist_field->field->offset);       \
82                                                                         \
83         return (u64)(unsigned long)*addr;                               \
84 }
85
86 DEFINE_HIST_FIELD_FN(s64);
87 DEFINE_HIST_FIELD_FN(u64);
88 DEFINE_HIST_FIELD_FN(s32);
89 DEFINE_HIST_FIELD_FN(u32);
90 DEFINE_HIST_FIELD_FN(s16);
91 DEFINE_HIST_FIELD_FN(u16);
92 DEFINE_HIST_FIELD_FN(s8);
93 DEFINE_HIST_FIELD_FN(u8);
94
95 #define for_each_hist_field(i, hist_data)       \
96         for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
97
98 #define for_each_hist_val_field(i, hist_data)   \
99         for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
100
101 #define for_each_hist_key_field(i, hist_data)   \
102         for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
103
104 #define HIST_STACKTRACE_DEPTH   16
105 #define HIST_STACKTRACE_SIZE    (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
106 #define HIST_STACKTRACE_SKIP    5
107
108 #define HITCOUNT_IDX            0
109 #define HIST_KEY_SIZE_MAX       (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
110
111 enum hist_field_flags {
112         HIST_FIELD_FL_HITCOUNT          = 1,
113         HIST_FIELD_FL_KEY               = 2,
114         HIST_FIELD_FL_STRING            = 4,
115         HIST_FIELD_FL_HEX               = 8,
116         HIST_FIELD_FL_SYM               = 16,
117         HIST_FIELD_FL_SYM_OFFSET        = 32,
118         HIST_FIELD_FL_EXECNAME          = 64,
119         HIST_FIELD_FL_SYSCALL           = 128,
120         HIST_FIELD_FL_STACKTRACE        = 256,
121         HIST_FIELD_FL_LOG2              = 512,
122 };
123
124 struct hist_trigger_attrs {
125         char            *keys_str;
126         char            *vals_str;
127         char            *sort_key_str;
128         char            *name;
129         bool            pause;
130         bool            cont;
131         bool            clear;
132         unsigned int    map_bits;
133 };
134
135 struct hist_trigger_data {
136         struct hist_field               *fields[TRACING_MAP_FIELDS_MAX];
137         unsigned int                    n_vals;
138         unsigned int                    n_keys;
139         unsigned int                    n_fields;
140         unsigned int                    key_size;
141         struct tracing_map_sort_key     sort_keys[TRACING_MAP_SORT_KEYS_MAX];
142         unsigned int                    n_sort_keys;
143         struct trace_event_file         *event_file;
144         struct hist_trigger_attrs       *attrs;
145         struct tracing_map              *map;
146 };
147
148 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
149 {
150         hist_field_fn_t fn = NULL;
151
152         switch (field_size) {
153         case 8:
154                 if (field_is_signed)
155                         fn = hist_field_s64;
156                 else
157                         fn = hist_field_u64;
158                 break;
159         case 4:
160                 if (field_is_signed)
161                         fn = hist_field_s32;
162                 else
163                         fn = hist_field_u32;
164                 break;
165         case 2:
166                 if (field_is_signed)
167                         fn = hist_field_s16;
168                 else
169                         fn = hist_field_u16;
170                 break;
171         case 1:
172                 if (field_is_signed)
173                         fn = hist_field_s8;
174                 else
175                         fn = hist_field_u8;
176                 break;
177         }
178
179         return fn;
180 }
181
182 static int parse_map_size(char *str)
183 {
184         unsigned long size, map_bits;
185         int ret;
186
187         strsep(&str, "=");
188         if (!str) {
189                 ret = -EINVAL;
190                 goto out;
191         }
192
193         ret = kstrtoul(str, 0, &size);
194         if (ret)
195                 goto out;
196
197         map_bits = ilog2(roundup_pow_of_two(size));
198         if (map_bits < TRACING_MAP_BITS_MIN ||
199             map_bits > TRACING_MAP_BITS_MAX)
200                 ret = -EINVAL;
201         else
202                 ret = map_bits;
203  out:
204         return ret;
205 }
206
207 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
208 {
209         if (!attrs)
210                 return;
211
212         kfree(attrs->name);
213         kfree(attrs->sort_key_str);
214         kfree(attrs->keys_str);
215         kfree(attrs->vals_str);
216         kfree(attrs);
217 }
218
219 static struct hist_trigger_attrs *parse_hist_trigger_attrs(char *trigger_str)
220 {
221         struct hist_trigger_attrs *attrs;
222         int ret = 0;
223
224         attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
225         if (!attrs)
226                 return ERR_PTR(-ENOMEM);
227
228         while (trigger_str) {
229                 char *str = strsep(&trigger_str, ":");
230
231                 if ((strncmp(str, "key=", strlen("key=")) == 0) ||
232                     (strncmp(str, "keys=", strlen("keys=")) == 0))
233                         attrs->keys_str = kstrdup(str, GFP_KERNEL);
234                 else if ((strncmp(str, "val=", strlen("val=")) == 0) ||
235                          (strncmp(str, "vals=", strlen("vals=")) == 0) ||
236                          (strncmp(str, "values=", strlen("values=")) == 0))
237                         attrs->vals_str = kstrdup(str, GFP_KERNEL);
238                 else if (strncmp(str, "sort=", strlen("sort=")) == 0)
239                         attrs->sort_key_str = kstrdup(str, GFP_KERNEL);
240                 else if (strncmp(str, "name=", strlen("name=")) == 0)
241                         attrs->name = kstrdup(str, GFP_KERNEL);
242                 else if (strcmp(str, "pause") == 0)
243                         attrs->pause = true;
244                 else if ((strcmp(str, "cont") == 0) ||
245                          (strcmp(str, "continue") == 0))
246                         attrs->cont = true;
247                 else if (strcmp(str, "clear") == 0)
248                         attrs->clear = true;
249                 else if (strncmp(str, "size=", strlen("size=")) == 0) {
250                         int map_bits = parse_map_size(str);
251
252                         if (map_bits < 0) {
253                                 ret = map_bits;
254                                 goto free;
255                         }
256                         attrs->map_bits = map_bits;
257                 } else {
258                         ret = -EINVAL;
259                         goto free;
260                 }
261         }
262
263         if (!attrs->keys_str) {
264                 ret = -EINVAL;
265                 goto free;
266         }
267
268         return attrs;
269  free:
270         destroy_hist_trigger_attrs(attrs);
271
272         return ERR_PTR(ret);
273 }
274
275 static inline void save_comm(char *comm, struct task_struct *task)
276 {
277         if (!task->pid) {
278                 strcpy(comm, "<idle>");
279                 return;
280         }
281
282         if (WARN_ON_ONCE(task->pid < 0)) {
283                 strcpy(comm, "<XXX>");
284                 return;
285         }
286
287         memcpy(comm, task->comm, TASK_COMM_LEN);
288 }
289
290 static void hist_trigger_elt_comm_free(struct tracing_map_elt *elt)
291 {
292         kfree((char *)elt->private_data);
293 }
294
295 static int hist_trigger_elt_comm_alloc(struct tracing_map_elt *elt)
296 {
297         struct hist_trigger_data *hist_data = elt->map->private_data;
298         struct hist_field *key_field;
299         unsigned int i;
300
301         for_each_hist_key_field(i, hist_data) {
302                 key_field = hist_data->fields[i];
303
304                 if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
305                         unsigned int size = TASK_COMM_LEN + 1;
306
307                         elt->private_data = kzalloc(size, GFP_KERNEL);
308                         if (!elt->private_data)
309                                 return -ENOMEM;
310                         break;
311                 }
312         }
313
314         return 0;
315 }
316
317 static void hist_trigger_elt_comm_copy(struct tracing_map_elt *to,
318                                        struct tracing_map_elt *from)
319 {
320         char *comm_from = from->private_data;
321         char *comm_to = to->private_data;
322
323         if (comm_from)
324                 memcpy(comm_to, comm_from, TASK_COMM_LEN + 1);
325 }
326
327 static void hist_trigger_elt_comm_init(struct tracing_map_elt *elt)
328 {
329         char *comm = elt->private_data;
330
331         if (comm)
332                 save_comm(comm, current);
333 }
334
335 static const struct tracing_map_ops hist_trigger_elt_comm_ops = {
336         .elt_alloc      = hist_trigger_elt_comm_alloc,
337         .elt_copy       = hist_trigger_elt_comm_copy,
338         .elt_free       = hist_trigger_elt_comm_free,
339         .elt_init       = hist_trigger_elt_comm_init,
340 };
341
342 static void destroy_hist_field(struct hist_field *hist_field)
343 {
344         kfree(hist_field);
345 }
346
347 static struct hist_field *create_hist_field(struct ftrace_event_field *field,
348                                             unsigned long flags)
349 {
350         struct hist_field *hist_field;
351
352         if (field && is_function_field(field))
353                 return NULL;
354
355         hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
356         if (!hist_field)
357                 return NULL;
358
359         if (flags & HIST_FIELD_FL_HITCOUNT) {
360                 hist_field->fn = hist_field_counter;
361                 goto out;
362         }
363
364         if (flags & HIST_FIELD_FL_STACKTRACE) {
365                 hist_field->fn = hist_field_none;
366                 goto out;
367         }
368
369         if (flags & HIST_FIELD_FL_LOG2) {
370                 hist_field->fn = hist_field_log2;
371                 goto out;
372         }
373
374         if (is_string_field(field)) {
375                 flags |= HIST_FIELD_FL_STRING;
376
377                 if (field->filter_type == FILTER_STATIC_STRING)
378                         hist_field->fn = hist_field_string;
379                 else if (field->filter_type == FILTER_DYN_STRING)
380                         hist_field->fn = hist_field_dynstring;
381                 else
382                         hist_field->fn = hist_field_pstring;
383         } else {
384                 hist_field->fn = select_value_fn(field->size,
385                                                  field->is_signed);
386                 if (!hist_field->fn) {
387                         destroy_hist_field(hist_field);
388                         return NULL;
389                 }
390         }
391  out:
392         hist_field->field = field;
393         hist_field->flags = flags;
394
395         return hist_field;
396 }
397
398 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
399 {
400         unsigned int i;
401
402         for (i = 0; i < TRACING_MAP_FIELDS_MAX; i++) {
403                 if (hist_data->fields[i]) {
404                         destroy_hist_field(hist_data->fields[i]);
405                         hist_data->fields[i] = NULL;
406                 }
407         }
408 }
409
410 static int create_hitcount_val(struct hist_trigger_data *hist_data)
411 {
412         hist_data->fields[HITCOUNT_IDX] =
413                 create_hist_field(NULL, HIST_FIELD_FL_HITCOUNT);
414         if (!hist_data->fields[HITCOUNT_IDX])
415                 return -ENOMEM;
416
417         hist_data->n_vals++;
418
419         if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
420                 return -EINVAL;
421
422         return 0;
423 }
424
425 static int create_val_field(struct hist_trigger_data *hist_data,
426                             unsigned int val_idx,
427                             struct trace_event_file *file,
428                             char *field_str)
429 {
430         struct ftrace_event_field *field = NULL;
431         unsigned long flags = 0;
432         char *field_name;
433         int ret = 0;
434
435         if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
436                 return -EINVAL;
437
438         field_name = strsep(&field_str, ".");
439         if (field_str) {
440                 if (strcmp(field_str, "hex") == 0)
441                         flags |= HIST_FIELD_FL_HEX;
442                 else {
443                         ret = -EINVAL;
444                         goto out;
445                 }
446         }
447
448         field = trace_find_event_field(file->event_call, field_name);
449         if (!field) {
450                 ret = -EINVAL;
451                 goto out;
452         }
453
454         hist_data->fields[val_idx] = create_hist_field(field, flags);
455         if (!hist_data->fields[val_idx]) {
456                 ret = -ENOMEM;
457                 goto out;
458         }
459
460         ++hist_data->n_vals;
461
462         if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
463                 ret = -EINVAL;
464  out:
465         return ret;
466 }
467
468 static int create_val_fields(struct hist_trigger_data *hist_data,
469                              struct trace_event_file *file)
470 {
471         char *fields_str, *field_str;
472         unsigned int i, j;
473         int ret;
474
475         ret = create_hitcount_val(hist_data);
476         if (ret)
477                 goto out;
478
479         fields_str = hist_data->attrs->vals_str;
480         if (!fields_str)
481                 goto out;
482
483         strsep(&fields_str, "=");
484         if (!fields_str)
485                 goto out;
486
487         for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
488                      j < TRACING_MAP_VALS_MAX; i++) {
489                 field_str = strsep(&fields_str, ",");
490                 if (!field_str)
491                         break;
492                 if (strcmp(field_str, "hitcount") == 0)
493                         continue;
494                 ret = create_val_field(hist_data, j++, file, field_str);
495                 if (ret)
496                         goto out;
497         }
498         if (fields_str && (strcmp(fields_str, "hitcount") != 0))
499                 ret = -EINVAL;
500  out:
501         return ret;
502 }
503
504 static int create_key_field(struct hist_trigger_data *hist_data,
505                             unsigned int key_idx,
506                             unsigned int key_offset,
507                             struct trace_event_file *file,
508                             char *field_str)
509 {
510         struct ftrace_event_field *field = NULL;
511         unsigned long flags = 0;
512         unsigned int key_size;
513         int ret = 0;
514
515         if (WARN_ON(key_idx >= TRACING_MAP_FIELDS_MAX))
516                 return -EINVAL;
517
518         flags |= HIST_FIELD_FL_KEY;
519
520         if (strcmp(field_str, "stacktrace") == 0) {
521                 flags |= HIST_FIELD_FL_STACKTRACE;
522                 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
523         } else {
524                 char *field_name = strsep(&field_str, ".");
525
526                 if (field_str) {
527                         if (strcmp(field_str, "hex") == 0)
528                                 flags |= HIST_FIELD_FL_HEX;
529                         else if (strcmp(field_str, "sym") == 0)
530                                 flags |= HIST_FIELD_FL_SYM;
531                         else if (strcmp(field_str, "sym-offset") == 0)
532                                 flags |= HIST_FIELD_FL_SYM_OFFSET;
533                         else if ((strcmp(field_str, "execname") == 0) &&
534                                  (strcmp(field_name, "common_pid") == 0))
535                                 flags |= HIST_FIELD_FL_EXECNAME;
536                         else if (strcmp(field_str, "syscall") == 0)
537                                 flags |= HIST_FIELD_FL_SYSCALL;
538                         else if (strcmp(field_str, "log2") == 0)
539                                 flags |= HIST_FIELD_FL_LOG2;
540                         else {
541                                 ret = -EINVAL;
542                                 goto out;
543                         }
544                 }
545
546                 field = trace_find_event_field(file->event_call, field_name);
547                 if (!field) {
548                         ret = -EINVAL;
549                         goto out;
550                 }
551
552                 if (is_string_field(field))
553                         key_size = MAX_FILTER_STR_VAL;
554                 else
555                         key_size = field->size;
556         }
557
558         hist_data->fields[key_idx] = create_hist_field(field, flags);
559         if (!hist_data->fields[key_idx]) {
560                 ret = -ENOMEM;
561                 goto out;
562         }
563
564         key_size = ALIGN(key_size, sizeof(u64));
565         hist_data->fields[key_idx]->size = key_size;
566         hist_data->fields[key_idx]->offset = key_offset;
567         hist_data->key_size += key_size;
568         if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
569                 ret = -EINVAL;
570                 goto out;
571         }
572
573         hist_data->n_keys++;
574
575         if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
576                 return -EINVAL;
577
578         ret = key_size;
579  out:
580         return ret;
581 }
582
583 static int create_key_fields(struct hist_trigger_data *hist_data,
584                              struct trace_event_file *file)
585 {
586         unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
587         char *fields_str, *field_str;
588         int ret = -EINVAL;
589
590         fields_str = hist_data->attrs->keys_str;
591         if (!fields_str)
592                 goto out;
593
594         strsep(&fields_str, "=");
595         if (!fields_str)
596                 goto out;
597
598         for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
599                 field_str = strsep(&fields_str, ",");
600                 if (!field_str)
601                         break;
602                 ret = create_key_field(hist_data, i, key_offset,
603                                        file, field_str);
604                 if (ret < 0)
605                         goto out;
606                 key_offset += ret;
607         }
608         if (fields_str) {
609                 ret = -EINVAL;
610                 goto out;
611         }
612         ret = 0;
613  out:
614         return ret;
615 }
616
617 static int create_hist_fields(struct hist_trigger_data *hist_data,
618                               struct trace_event_file *file)
619 {
620         int ret;
621
622         ret = create_val_fields(hist_data, file);
623         if (ret)
624                 goto out;
625
626         ret = create_key_fields(hist_data, file);
627         if (ret)
628                 goto out;
629
630         hist_data->n_fields = hist_data->n_vals + hist_data->n_keys;
631  out:
632         return ret;
633 }
634
635 static int is_descending(const char *str)
636 {
637         if (!str)
638                 return 0;
639
640         if (strcmp(str, "descending") == 0)
641                 return 1;
642
643         if (strcmp(str, "ascending") == 0)
644                 return 0;
645
646         return -EINVAL;
647 }
648
649 static int create_sort_keys(struct hist_trigger_data *hist_data)
650 {
651         char *fields_str = hist_data->attrs->sort_key_str;
652         struct ftrace_event_field *field = NULL;
653         struct tracing_map_sort_key *sort_key;
654         int descending, ret = 0;
655         unsigned int i, j;
656
657         hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
658
659         if (!fields_str)
660                 goto out;
661
662         strsep(&fields_str, "=");
663         if (!fields_str) {
664                 ret = -EINVAL;
665                 goto out;
666         }
667
668         for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
669                 char *field_str, *field_name;
670
671                 sort_key = &hist_data->sort_keys[i];
672
673                 field_str = strsep(&fields_str, ",");
674                 if (!field_str) {
675                         if (i == 0)
676                                 ret = -EINVAL;
677                         break;
678                 }
679
680                 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
681                         ret = -EINVAL;
682                         break;
683                 }
684
685                 field_name = strsep(&field_str, ".");
686                 if (!field_name) {
687                         ret = -EINVAL;
688                         break;
689                 }
690
691                 if (strcmp(field_name, "hitcount") == 0) {
692                         descending = is_descending(field_str);
693                         if (descending < 0) {
694                                 ret = descending;
695                                 break;
696                         }
697                         sort_key->descending = descending;
698                         continue;
699                 }
700
701                 for (j = 1; j < hist_data->n_fields; j++) {
702                         field = hist_data->fields[j]->field;
703                         if (field && (strcmp(field_name, field->name) == 0)) {
704                                 sort_key->field_idx = j;
705                                 descending = is_descending(field_str);
706                                 if (descending < 0) {
707                                         ret = descending;
708                                         goto out;
709                                 }
710                                 sort_key->descending = descending;
711                                 break;
712                         }
713                 }
714                 if (j == hist_data->n_fields) {
715                         ret = -EINVAL;
716                         break;
717                 }
718         }
719         hist_data->n_sort_keys = i;
720  out:
721         return ret;
722 }
723
724 static void destroy_hist_data(struct hist_trigger_data *hist_data)
725 {
726         destroy_hist_trigger_attrs(hist_data->attrs);
727         destroy_hist_fields(hist_data);
728         tracing_map_destroy(hist_data->map);
729         kfree(hist_data);
730 }
731
732 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
733 {
734         struct tracing_map *map = hist_data->map;
735         struct ftrace_event_field *field;
736         struct hist_field *hist_field;
737         unsigned int i, idx;
738
739         for_each_hist_field(i, hist_data) {
740                 hist_field = hist_data->fields[i];
741                 if (hist_field->flags & HIST_FIELD_FL_KEY) {
742                         tracing_map_cmp_fn_t cmp_fn;
743
744                         field = hist_field->field;
745
746                         if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
747                                 cmp_fn = tracing_map_cmp_none;
748                         else if (is_string_field(field))
749                                 cmp_fn = tracing_map_cmp_string;
750                         else
751                                 cmp_fn = tracing_map_cmp_num(field->size,
752                                                              field->is_signed);
753                         idx = tracing_map_add_key_field(map,
754                                                         hist_field->offset,
755                                                         cmp_fn);
756
757                 } else
758                         idx = tracing_map_add_sum_field(map);
759
760                 if (idx < 0)
761                         return idx;
762         }
763
764         return 0;
765 }
766
767 static bool need_tracing_map_ops(struct hist_trigger_data *hist_data)
768 {
769         struct hist_field *key_field;
770         unsigned int i;
771
772         for_each_hist_key_field(i, hist_data) {
773                 key_field = hist_data->fields[i];
774
775                 if (key_field->flags & HIST_FIELD_FL_EXECNAME)
776                         return true;
777         }
778
779         return false;
780 }
781
782 static struct hist_trigger_data *
783 create_hist_data(unsigned int map_bits,
784                  struct hist_trigger_attrs *attrs,
785                  struct trace_event_file *file)
786 {
787         const struct tracing_map_ops *map_ops = NULL;
788         struct hist_trigger_data *hist_data;
789         int ret = 0;
790
791         hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
792         if (!hist_data)
793                 return ERR_PTR(-ENOMEM);
794
795         hist_data->attrs = attrs;
796
797         ret = create_hist_fields(hist_data, file);
798         if (ret)
799                 goto free;
800
801         ret = create_sort_keys(hist_data);
802         if (ret)
803                 goto free;
804
805         if (need_tracing_map_ops(hist_data))
806                 map_ops = &hist_trigger_elt_comm_ops;
807
808         hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
809                                             map_ops, hist_data);
810         if (IS_ERR(hist_data->map)) {
811                 ret = PTR_ERR(hist_data->map);
812                 hist_data->map = NULL;
813                 goto free;
814         }
815
816         ret = create_tracing_map_fields(hist_data);
817         if (ret)
818                 goto free;
819
820         ret = tracing_map_init(hist_data->map);
821         if (ret)
822                 goto free;
823
824         hist_data->event_file = file;
825  out:
826         return hist_data;
827  free:
828         hist_data->attrs = NULL;
829
830         destroy_hist_data(hist_data);
831
832         hist_data = ERR_PTR(ret);
833
834         goto out;
835 }
836
837 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
838                                     struct tracing_map_elt *elt,
839                                     void *rec)
840 {
841         struct hist_field *hist_field;
842         unsigned int i;
843         u64 hist_val;
844
845         for_each_hist_val_field(i, hist_data) {
846                 hist_field = hist_data->fields[i];
847                 hist_val = hist_field->fn(hist_field, rec);
848                 tracing_map_update_sum(elt, i, hist_val);
849         }
850 }
851
852 static inline void add_to_key(char *compound_key, void *key,
853                               struct hist_field *key_field, void *rec)
854 {
855         size_t size = key_field->size;
856
857         if (key_field->flags & HIST_FIELD_FL_STRING) {
858                 struct ftrace_event_field *field;
859
860                 field = key_field->field;
861                 if (field->filter_type == FILTER_DYN_STRING)
862                         size = *(u32 *)(rec + field->offset) >> 16;
863                 else if (field->filter_type == FILTER_PTR_STRING)
864                         size = strlen(key);
865                 else if (field->filter_type == FILTER_STATIC_STRING)
866                         size = field->size;
867
868                 /* ensure NULL-termination */
869                 if (size > key_field->size - 1)
870                         size = key_field->size - 1;
871         }
872
873         memcpy(compound_key + key_field->offset, key, size);
874 }
875
876 static void event_hist_trigger(struct event_trigger_data *data, void *rec)
877 {
878         struct hist_trigger_data *hist_data = data->private_data;
879         bool use_compound_key = (hist_data->n_keys > 1);
880         unsigned long entries[HIST_STACKTRACE_DEPTH];
881         char compound_key[HIST_KEY_SIZE_MAX];
882         struct stack_trace stacktrace;
883         struct hist_field *key_field;
884         struct tracing_map_elt *elt;
885         u64 field_contents;
886         void *key = NULL;
887         unsigned int i;
888
889         memset(compound_key, 0, hist_data->key_size);
890
891         for_each_hist_key_field(i, hist_data) {
892                 key_field = hist_data->fields[i];
893
894                 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
895                         stacktrace.max_entries = HIST_STACKTRACE_DEPTH;
896                         stacktrace.entries = entries;
897                         stacktrace.nr_entries = 0;
898                         stacktrace.skip = HIST_STACKTRACE_SKIP;
899
900                         memset(stacktrace.entries, 0, HIST_STACKTRACE_SIZE);
901                         save_stack_trace(&stacktrace);
902
903                         key = entries;
904                 } else {
905                         field_contents = key_field->fn(key_field, rec);
906                         if (key_field->flags & HIST_FIELD_FL_STRING) {
907                                 key = (void *)(unsigned long)field_contents;
908                                 use_compound_key = true;
909                         } else
910                                 key = (void *)&field_contents;
911                 }
912
913                 if (use_compound_key)
914                         add_to_key(compound_key, key, key_field, rec);
915         }
916
917         if (use_compound_key)
918                 key = compound_key;
919
920         elt = tracing_map_insert(hist_data->map, key);
921         if (elt)
922                 hist_trigger_elt_update(hist_data, elt, rec);
923 }
924
925 static void hist_trigger_stacktrace_print(struct seq_file *m,
926                                           unsigned long *stacktrace_entries,
927                                           unsigned int max_entries)
928 {
929         char str[KSYM_SYMBOL_LEN];
930         unsigned int spaces = 8;
931         unsigned int i;
932
933         for (i = 0; i < max_entries; i++) {
934                 if (stacktrace_entries[i] == ULONG_MAX)
935                         return;
936
937                 seq_printf(m, "%*c", 1 + spaces, ' ');
938                 sprint_symbol(str, stacktrace_entries[i]);
939                 seq_printf(m, "%s\n", str);
940         }
941 }
942
943 static void
944 hist_trigger_entry_print(struct seq_file *m,
945                          struct hist_trigger_data *hist_data, void *key,
946                          struct tracing_map_elt *elt)
947 {
948         struct hist_field *key_field;
949         char str[KSYM_SYMBOL_LEN];
950         bool multiline = false;
951         unsigned int i;
952         u64 uval;
953
954         seq_puts(m, "{ ");
955
956         for_each_hist_key_field(i, hist_data) {
957                 key_field = hist_data->fields[i];
958
959                 if (i > hist_data->n_vals)
960                         seq_puts(m, ", ");
961
962                 if (key_field->flags & HIST_FIELD_FL_HEX) {
963                         uval = *(u64 *)(key + key_field->offset);
964                         seq_printf(m, "%s: %llx",
965                                    key_field->field->name, uval);
966                 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
967                         uval = *(u64 *)(key + key_field->offset);
968                         sprint_symbol_no_offset(str, uval);
969                         seq_printf(m, "%s: [%llx] %-45s",
970                                    key_field->field->name, uval, str);
971                 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
972                         uval = *(u64 *)(key + key_field->offset);
973                         sprint_symbol(str, uval);
974                         seq_printf(m, "%s: [%llx] %-55s",
975                                    key_field->field->name, uval, str);
976                 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
977                         char *comm = elt->private_data;
978
979                         uval = *(u64 *)(key + key_field->offset);
980                         seq_printf(m, "%s: %-16s[%10llu]",
981                                    key_field->field->name, comm, uval);
982                 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
983                         const char *syscall_name;
984
985                         uval = *(u64 *)(key + key_field->offset);
986                         syscall_name = get_syscall_name(uval);
987                         if (!syscall_name)
988                                 syscall_name = "unknown_syscall";
989
990                         seq_printf(m, "%s: %-30s[%3llu]",
991                                    key_field->field->name, syscall_name, uval);
992                 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
993                         seq_puts(m, "stacktrace:\n");
994                         hist_trigger_stacktrace_print(m,
995                                                       key + key_field->offset,
996                                                       HIST_STACKTRACE_DEPTH);
997                         multiline = true;
998                 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
999                         seq_printf(m, "%s: ~ 2^%-2llu", key_field->field->name,
1000                                    *(u64 *)(key + key_field->offset));
1001                 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
1002                         seq_printf(m, "%s: %-50s", key_field->field->name,
1003                                    (char *)(key + key_field->offset));
1004                 } else {
1005                         uval = *(u64 *)(key + key_field->offset);
1006                         seq_printf(m, "%s: %10llu", key_field->field->name,
1007                                    uval);
1008                 }
1009         }
1010
1011         if (!multiline)
1012                 seq_puts(m, " ");
1013
1014         seq_puts(m, "}");
1015
1016         seq_printf(m, " hitcount: %10llu",
1017                    tracing_map_read_sum(elt, HITCOUNT_IDX));
1018
1019         for (i = 1; i < hist_data->n_vals; i++) {
1020                 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
1021                         seq_printf(m, "  %s: %10llx",
1022                                    hist_data->fields[i]->field->name,
1023                                    tracing_map_read_sum(elt, i));
1024                 } else {
1025                         seq_printf(m, "  %s: %10llu",
1026                                    hist_data->fields[i]->field->name,
1027                                    tracing_map_read_sum(elt, i));
1028                 }
1029         }
1030
1031         seq_puts(m, "\n");
1032 }
1033
1034 static int print_entries(struct seq_file *m,
1035                          struct hist_trigger_data *hist_data)
1036 {
1037         struct tracing_map_sort_entry **sort_entries = NULL;
1038         struct tracing_map *map = hist_data->map;
1039         unsigned int i, n_entries;
1040
1041         n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
1042                                              hist_data->n_sort_keys,
1043                                              &sort_entries);
1044         if (n_entries < 0)
1045                 return n_entries;
1046
1047         for (i = 0; i < n_entries; i++)
1048                 hist_trigger_entry_print(m, hist_data,
1049                                          sort_entries[i]->key,
1050                                          sort_entries[i]->elt);
1051
1052         tracing_map_destroy_sort_entries(sort_entries, n_entries);
1053
1054         return n_entries;
1055 }
1056
1057 static void hist_trigger_show(struct seq_file *m,
1058                               struct event_trigger_data *data, int n)
1059 {
1060         struct hist_trigger_data *hist_data;
1061         int n_entries, ret = 0;
1062
1063         if (n > 0)
1064                 seq_puts(m, "\n\n");
1065
1066         seq_puts(m, "# event histogram\n#\n# trigger info: ");
1067         data->ops->print(m, data->ops, data);
1068         seq_puts(m, "#\n\n");
1069
1070         hist_data = data->private_data;
1071         n_entries = print_entries(m, hist_data);
1072         if (n_entries < 0) {
1073                 ret = n_entries;
1074                 n_entries = 0;
1075         }
1076
1077         seq_printf(m, "\nTotals:\n    Hits: %llu\n    Entries: %u\n    Dropped: %llu\n",
1078                    (u64)atomic64_read(&hist_data->map->hits),
1079                    n_entries, (u64)atomic64_read(&hist_data->map->drops));
1080 }
1081
1082 static int hist_show(struct seq_file *m, void *v)
1083 {
1084         struct event_trigger_data *data;
1085         struct trace_event_file *event_file;
1086         int n = 0, ret = 0;
1087
1088         mutex_lock(&event_mutex);
1089
1090         event_file = event_file_data(m->private);
1091         if (unlikely(!event_file)) {
1092                 ret = -ENODEV;
1093                 goto out_unlock;
1094         }
1095
1096         list_for_each_entry_rcu(data, &event_file->triggers, list) {
1097                 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
1098                         hist_trigger_show(m, data, n++);
1099         }
1100
1101  out_unlock:
1102         mutex_unlock(&event_mutex);
1103
1104         return ret;
1105 }
1106
1107 static int event_hist_open(struct inode *inode, struct file *file)
1108 {
1109         return single_open(file, hist_show, file);
1110 }
1111
1112 const struct file_operations event_hist_fops = {
1113         .open = event_hist_open,
1114         .read = seq_read,
1115         .llseek = seq_lseek,
1116         .release = single_release,
1117 };
1118
1119 static const char *get_hist_field_flags(struct hist_field *hist_field)
1120 {
1121         const char *flags_str = NULL;
1122
1123         if (hist_field->flags & HIST_FIELD_FL_HEX)
1124                 flags_str = "hex";
1125         else if (hist_field->flags & HIST_FIELD_FL_SYM)
1126                 flags_str = "sym";
1127         else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
1128                 flags_str = "sym-offset";
1129         else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
1130                 flags_str = "execname";
1131         else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
1132                 flags_str = "syscall";
1133         else if (hist_field->flags & HIST_FIELD_FL_LOG2)
1134                 flags_str = "log2";
1135
1136         return flags_str;
1137 }
1138
1139 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
1140 {
1141         seq_printf(m, "%s", hist_field->field->name);
1142         if (hist_field->flags) {
1143                 const char *flags_str = get_hist_field_flags(hist_field);
1144
1145                 if (flags_str)
1146                         seq_printf(m, ".%s", flags_str);
1147         }
1148 }
1149
1150 static int event_hist_trigger_print(struct seq_file *m,
1151                                     struct event_trigger_ops *ops,
1152                                     struct event_trigger_data *data)
1153 {
1154         struct hist_trigger_data *hist_data = data->private_data;
1155         struct hist_field *key_field;
1156         unsigned int i;
1157
1158         seq_puts(m, "hist:");
1159
1160         if (data->name)
1161                 seq_printf(m, "%s:", data->name);
1162
1163         seq_puts(m, "keys=");
1164
1165         for_each_hist_key_field(i, hist_data) {
1166                 key_field = hist_data->fields[i];
1167
1168                 if (i > hist_data->n_vals)
1169                         seq_puts(m, ",");
1170
1171                 if (key_field->flags & HIST_FIELD_FL_STACKTRACE)
1172                         seq_puts(m, "stacktrace");
1173                 else
1174                         hist_field_print(m, key_field);
1175         }
1176
1177         seq_puts(m, ":vals=");
1178
1179         for_each_hist_val_field(i, hist_data) {
1180                 if (i == HITCOUNT_IDX)
1181                         seq_puts(m, "hitcount");
1182                 else {
1183                         seq_puts(m, ",");
1184                         hist_field_print(m, hist_data->fields[i]);
1185                 }
1186         }
1187
1188         seq_puts(m, ":sort=");
1189
1190         for (i = 0; i < hist_data->n_sort_keys; i++) {
1191                 struct tracing_map_sort_key *sort_key;
1192
1193                 sort_key = &hist_data->sort_keys[i];
1194
1195                 if (i > 0)
1196                         seq_puts(m, ",");
1197
1198                 if (sort_key->field_idx == HITCOUNT_IDX)
1199                         seq_puts(m, "hitcount");
1200                 else {
1201                         unsigned int idx = sort_key->field_idx;
1202
1203                         if (WARN_ON(idx >= TRACING_MAP_FIELDS_MAX))
1204                                 return -EINVAL;
1205
1206                         hist_field_print(m, hist_data->fields[idx]);
1207                 }
1208
1209                 if (sort_key->descending)
1210                         seq_puts(m, ".descending");
1211         }
1212
1213         seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
1214
1215         if (data->filter_str)
1216                 seq_printf(m, " if %s", data->filter_str);
1217
1218         if (data->paused)
1219                 seq_puts(m, " [paused]");
1220         else
1221                 seq_puts(m, " [active]");
1222
1223         seq_putc(m, '\n');
1224
1225         return 0;
1226 }
1227
1228 static int event_hist_trigger_init(struct event_trigger_ops *ops,
1229                                    struct event_trigger_data *data)
1230 {
1231         struct hist_trigger_data *hist_data = data->private_data;
1232
1233         if (!data->ref && hist_data->attrs->name)
1234                 save_named_trigger(hist_data->attrs->name, data);
1235
1236         data->ref++;
1237
1238         return 0;
1239 }
1240
1241 static void event_hist_trigger_free(struct event_trigger_ops *ops,
1242                                     struct event_trigger_data *data)
1243 {
1244         struct hist_trigger_data *hist_data = data->private_data;
1245
1246         if (WARN_ON_ONCE(data->ref <= 0))
1247                 return;
1248
1249         data->ref--;
1250         if (!data->ref) {
1251                 if (data->name)
1252                         del_named_trigger(data);
1253                 trigger_data_free(data);
1254                 destroy_hist_data(hist_data);
1255         }
1256 }
1257
1258 static struct event_trigger_ops event_hist_trigger_ops = {
1259         .func                   = event_hist_trigger,
1260         .print                  = event_hist_trigger_print,
1261         .init                   = event_hist_trigger_init,
1262         .free                   = event_hist_trigger_free,
1263 };
1264
1265 static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
1266                                          struct event_trigger_data *data)
1267 {
1268         data->ref++;
1269
1270         save_named_trigger(data->named_data->name, data);
1271
1272         event_hist_trigger_init(ops, data->named_data);
1273
1274         return 0;
1275 }
1276
1277 static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
1278                                           struct event_trigger_data *data)
1279 {
1280         if (WARN_ON_ONCE(data->ref <= 0))
1281                 return;
1282
1283         event_hist_trigger_free(ops, data->named_data);
1284
1285         data->ref--;
1286         if (!data->ref) {
1287                 del_named_trigger(data);
1288                 trigger_data_free(data);
1289         }
1290 }
1291
1292 static struct event_trigger_ops event_hist_trigger_named_ops = {
1293         .func                   = event_hist_trigger,
1294         .print                  = event_hist_trigger_print,
1295         .init                   = event_hist_trigger_named_init,
1296         .free                   = event_hist_trigger_named_free,
1297 };
1298
1299 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
1300                                                             char *param)
1301 {
1302         return &event_hist_trigger_ops;
1303 }
1304
1305 static void hist_clear(struct event_trigger_data *data)
1306 {
1307         struct hist_trigger_data *hist_data = data->private_data;
1308
1309         if (data->name)
1310                 pause_named_trigger(data);
1311
1312         synchronize_sched();
1313
1314         tracing_map_clear(hist_data->map);
1315
1316         if (data->name)
1317                 unpause_named_trigger(data);
1318 }
1319
1320 static bool compatible_field(struct ftrace_event_field *field,
1321                              struct ftrace_event_field *test_field)
1322 {
1323         if (field == test_field)
1324                 return true;
1325         if (field == NULL || test_field == NULL)
1326                 return false;
1327         if (strcmp(field->name, test_field->name) != 0)
1328                 return false;
1329         if (strcmp(field->type, test_field->type) != 0)
1330                 return false;
1331         if (field->size != test_field->size)
1332                 return false;
1333         if (field->is_signed != test_field->is_signed)
1334                 return false;
1335
1336         return true;
1337 }
1338
1339 static bool hist_trigger_match(struct event_trigger_data *data,
1340                                struct event_trigger_data *data_test,
1341                                struct event_trigger_data *named_data,
1342                                bool ignore_filter)
1343 {
1344         struct tracing_map_sort_key *sort_key, *sort_key_test;
1345         struct hist_trigger_data *hist_data, *hist_data_test;
1346         struct hist_field *key_field, *key_field_test;
1347         unsigned int i;
1348
1349         if (named_data && (named_data != data_test) &&
1350             (named_data != data_test->named_data))
1351                 return false;
1352
1353         if (!named_data && is_named_trigger(data_test))
1354                 return false;
1355
1356         hist_data = data->private_data;
1357         hist_data_test = data_test->private_data;
1358
1359         if (hist_data->n_vals != hist_data_test->n_vals ||
1360             hist_data->n_fields != hist_data_test->n_fields ||
1361             hist_data->n_sort_keys != hist_data_test->n_sort_keys)
1362                 return false;
1363
1364         if (!ignore_filter) {
1365                 if ((data->filter_str && !data_test->filter_str) ||
1366                    (!data->filter_str && data_test->filter_str))
1367                         return false;
1368         }
1369
1370         for_each_hist_field(i, hist_data) {
1371                 key_field = hist_data->fields[i];
1372                 key_field_test = hist_data_test->fields[i];
1373
1374                 if (key_field->flags != key_field_test->flags)
1375                         return false;
1376                 if (!compatible_field(key_field->field, key_field_test->field))
1377                         return false;
1378                 if (key_field->offset != key_field_test->offset)
1379                         return false;
1380         }
1381
1382         for (i = 0; i < hist_data->n_sort_keys; i++) {
1383                 sort_key = &hist_data->sort_keys[i];
1384                 sort_key_test = &hist_data_test->sort_keys[i];
1385
1386                 if (sort_key->field_idx != sort_key_test->field_idx ||
1387                     sort_key->descending != sort_key_test->descending)
1388                         return false;
1389         }
1390
1391         if (!ignore_filter && data->filter_str &&
1392             (strcmp(data->filter_str, data_test->filter_str) != 0))
1393                 return false;
1394
1395         return true;
1396 }
1397
1398 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
1399                                  struct event_trigger_data *data,
1400                                  struct trace_event_file *file)
1401 {
1402         struct hist_trigger_data *hist_data = data->private_data;
1403         struct event_trigger_data *test, *named_data = NULL;
1404         int ret = 0;
1405
1406         if (hist_data->attrs->name) {
1407                 named_data = find_named_trigger(hist_data->attrs->name);
1408                 if (named_data) {
1409                         if (!hist_trigger_match(data, named_data, named_data,
1410                                                 true)) {
1411                                 ret = -EINVAL;
1412                                 goto out;
1413                         }
1414                 }
1415         }
1416
1417         if (hist_data->attrs->name && !named_data)
1418                 goto new;
1419
1420         list_for_each_entry_rcu(test, &file->triggers, list) {
1421                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1422                         if (!hist_trigger_match(data, test, named_data, false))
1423                                 continue;
1424                         if (hist_data->attrs->pause)
1425                                 test->paused = true;
1426                         else if (hist_data->attrs->cont)
1427                                 test->paused = false;
1428                         else if (hist_data->attrs->clear)
1429                                 hist_clear(test);
1430                         else
1431                                 ret = -EEXIST;
1432                         goto out;
1433                 }
1434         }
1435  new:
1436         if (hist_data->attrs->cont || hist_data->attrs->clear) {
1437                 ret = -ENOENT;
1438                 goto out;
1439         }
1440
1441         if (named_data) {
1442                 destroy_hist_data(data->private_data);
1443                 data->private_data = named_data->private_data;
1444                 set_named_trigger_data(data, named_data);
1445                 data->ops = &event_hist_trigger_named_ops;
1446         }
1447
1448         if (hist_data->attrs->pause)
1449                 data->paused = true;
1450
1451         if (data->ops->init) {
1452                 ret = data->ops->init(data->ops, data);
1453                 if (ret < 0)
1454                         goto out;
1455         }
1456
1457         list_add_rcu(&data->list, &file->triggers);
1458         ret++;
1459
1460         update_cond_flag(file);
1461
1462         if (trace_event_trigger_enable_disable(file, 1) < 0) {
1463                 list_del_rcu(&data->list);
1464                 update_cond_flag(file);
1465                 ret--;
1466         }
1467  out:
1468         return ret;
1469 }
1470
1471 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
1472                                     struct event_trigger_data *data,
1473                                     struct trace_event_file *file)
1474 {
1475         struct hist_trigger_data *hist_data = data->private_data;
1476         struct event_trigger_data *test, *named_data = NULL;
1477         bool unregistered = false;
1478
1479         if (hist_data->attrs->name)
1480                 named_data = find_named_trigger(hist_data->attrs->name);
1481
1482         list_for_each_entry_rcu(test, &file->triggers, list) {
1483                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1484                         if (!hist_trigger_match(data, test, named_data, false))
1485                                 continue;
1486                         unregistered = true;
1487                         list_del_rcu(&test->list);
1488                         trace_event_trigger_enable_disable(file, 0);
1489                         update_cond_flag(file);
1490                         break;
1491                 }
1492         }
1493
1494         if (unregistered && test->ops->free)
1495                 test->ops->free(test->ops, test);
1496 }
1497
1498 static void hist_unreg_all(struct trace_event_file *file)
1499 {
1500         struct event_trigger_data *test;
1501
1502         list_for_each_entry_rcu(test, &file->triggers, list) {
1503                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1504                         list_del_rcu(&test->list);
1505                         trace_event_trigger_enable_disable(file, 0);
1506                         update_cond_flag(file);
1507                         if (test->ops->free)
1508                                 test->ops->free(test->ops, test);
1509                 }
1510         }
1511 }
1512
1513 static int event_hist_trigger_func(struct event_command *cmd_ops,
1514                                    struct trace_event_file *file,
1515                                    char *glob, char *cmd, char *param)
1516 {
1517         unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
1518         struct event_trigger_data *trigger_data;
1519         struct hist_trigger_attrs *attrs;
1520         struct event_trigger_ops *trigger_ops;
1521         struct hist_trigger_data *hist_data;
1522         char *trigger;
1523         int ret = 0;
1524
1525         if (!param)
1526                 return -EINVAL;
1527
1528         /* separate the trigger from the filter (k:v [if filter]) */
1529         trigger = strsep(&param, " \t");
1530         if (!trigger)
1531                 return -EINVAL;
1532
1533         attrs = parse_hist_trigger_attrs(trigger);
1534         if (IS_ERR(attrs))
1535                 return PTR_ERR(attrs);
1536
1537         if (attrs->map_bits)
1538                 hist_trigger_bits = attrs->map_bits;
1539
1540         hist_data = create_hist_data(hist_trigger_bits, attrs, file);
1541         if (IS_ERR(hist_data)) {
1542                 destroy_hist_trigger_attrs(attrs);
1543                 return PTR_ERR(hist_data);
1544         }
1545
1546         trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
1547
1548         ret = -ENOMEM;
1549         trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
1550         if (!trigger_data)
1551                 goto out_free;
1552
1553         trigger_data->count = -1;
1554         trigger_data->ops = trigger_ops;
1555         trigger_data->cmd_ops = cmd_ops;
1556
1557         INIT_LIST_HEAD(&trigger_data->list);
1558         RCU_INIT_POINTER(trigger_data->filter, NULL);
1559
1560         trigger_data->private_data = hist_data;
1561
1562         /* if param is non-empty, it's supposed to be a filter */
1563         if (param && cmd_ops->set_filter) {
1564                 ret = cmd_ops->set_filter(param, trigger_data, file);
1565                 if (ret < 0)
1566                         goto out_free;
1567         }
1568
1569         if (glob[0] == '!') {
1570                 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
1571                 ret = 0;
1572                 goto out_free;
1573         }
1574
1575         ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
1576         /*
1577          * The above returns on success the # of triggers registered,
1578          * but if it didn't register any it returns zero.  Consider no
1579          * triggers registered a failure too.
1580          */
1581         if (!ret) {
1582                 if (!(attrs->pause || attrs->cont || attrs->clear))
1583                         ret = -ENOENT;
1584                 goto out_free;
1585         } else if (ret < 0)
1586                 goto out_free;
1587         /* Just return zero, not the number of registered triggers */
1588         ret = 0;
1589  out:
1590         return ret;
1591  out_free:
1592         if (cmd_ops->set_filter)
1593                 cmd_ops->set_filter(NULL, trigger_data, NULL);
1594
1595         kfree(trigger_data);
1596
1597         destroy_hist_data(hist_data);
1598         goto out;
1599 }
1600
1601 static struct event_command trigger_hist_cmd = {
1602         .name                   = "hist",
1603         .trigger_type           = ETT_EVENT_HIST,
1604         .flags                  = EVENT_CMD_FL_NEEDS_REC,
1605         .func                   = event_hist_trigger_func,
1606         .reg                    = hist_register_trigger,
1607         .unreg                  = hist_unregister_trigger,
1608         .unreg_all              = hist_unreg_all,
1609         .get_trigger_ops        = event_hist_get_trigger_ops,
1610         .set_filter             = set_trigger_filter,
1611 };
1612
1613 __init int register_trigger_hist_cmd(void)
1614 {
1615         int ret;
1616
1617         ret = register_event_command(&trigger_hist_cmd);
1618         WARN_ON(ret < 0);
1619
1620         return ret;
1621 }
1622
1623 static void
1624 hist_enable_trigger(struct event_trigger_data *data, void *rec)
1625 {
1626         struct enable_trigger_data *enable_data = data->private_data;
1627         struct event_trigger_data *test;
1628
1629         list_for_each_entry_rcu(test, &enable_data->file->triggers, list) {
1630                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1631                         if (enable_data->enable)
1632                                 test->paused = false;
1633                         else
1634                                 test->paused = true;
1635                 }
1636         }
1637 }
1638
1639 static void
1640 hist_enable_count_trigger(struct event_trigger_data *data, void *rec)
1641 {
1642         if (!data->count)
1643                 return;
1644
1645         if (data->count != -1)
1646                 (data->count)--;
1647
1648         hist_enable_trigger(data, rec);
1649 }
1650
1651 static struct event_trigger_ops hist_enable_trigger_ops = {
1652         .func                   = hist_enable_trigger,
1653         .print                  = event_enable_trigger_print,
1654         .init                   = event_trigger_init,
1655         .free                   = event_enable_trigger_free,
1656 };
1657
1658 static struct event_trigger_ops hist_enable_count_trigger_ops = {
1659         .func                   = hist_enable_count_trigger,
1660         .print                  = event_enable_trigger_print,
1661         .init                   = event_trigger_init,
1662         .free                   = event_enable_trigger_free,
1663 };
1664
1665 static struct event_trigger_ops hist_disable_trigger_ops = {
1666         .func                   = hist_enable_trigger,
1667         .print                  = event_enable_trigger_print,
1668         .init                   = event_trigger_init,
1669         .free                   = event_enable_trigger_free,
1670 };
1671
1672 static struct event_trigger_ops hist_disable_count_trigger_ops = {
1673         .func                   = hist_enable_count_trigger,
1674         .print                  = event_enable_trigger_print,
1675         .init                   = event_trigger_init,
1676         .free                   = event_enable_trigger_free,
1677 };
1678
1679 static struct event_trigger_ops *
1680 hist_enable_get_trigger_ops(char *cmd, char *param)
1681 {
1682         struct event_trigger_ops *ops;
1683         bool enable;
1684
1685         enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
1686
1687         if (enable)
1688                 ops = param ? &hist_enable_count_trigger_ops :
1689                         &hist_enable_trigger_ops;
1690         else
1691                 ops = param ? &hist_disable_count_trigger_ops :
1692                         &hist_disable_trigger_ops;
1693
1694         return ops;
1695 }
1696
1697 static void hist_enable_unreg_all(struct trace_event_file *file)
1698 {
1699         struct event_trigger_data *test;
1700
1701         list_for_each_entry_rcu(test, &file->triggers, list) {
1702                 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
1703                         list_del_rcu(&test->list);
1704                         update_cond_flag(file);
1705                         trace_event_trigger_enable_disable(file, 0);
1706                         if (test->ops->free)
1707                                 test->ops->free(test->ops, test);
1708                 }
1709         }
1710 }
1711
1712 static struct event_command trigger_hist_enable_cmd = {
1713         .name                   = ENABLE_HIST_STR,
1714         .trigger_type           = ETT_HIST_ENABLE,
1715         .func                   = event_enable_trigger_func,
1716         .reg                    = event_enable_register_trigger,
1717         .unreg                  = event_enable_unregister_trigger,
1718         .unreg_all              = hist_enable_unreg_all,
1719         .get_trigger_ops        = hist_enable_get_trigger_ops,
1720         .set_filter             = set_trigger_filter,
1721 };
1722
1723 static struct event_command trigger_hist_disable_cmd = {
1724         .name                   = DISABLE_HIST_STR,
1725         .trigger_type           = ETT_HIST_ENABLE,
1726         .func                   = event_enable_trigger_func,
1727         .reg                    = event_enable_register_trigger,
1728         .unreg                  = event_enable_unregister_trigger,
1729         .unreg_all              = hist_enable_unreg_all,
1730         .get_trigger_ops        = hist_enable_get_trigger_ops,
1731         .set_filter             = set_trigger_filter,
1732 };
1733
1734 static __init void unregister_trigger_hist_enable_disable_cmds(void)
1735 {
1736         unregister_event_command(&trigger_hist_enable_cmd);
1737         unregister_event_command(&trigger_hist_disable_cmd);
1738 }
1739
1740 __init int register_trigger_hist_enable_disable_cmds(void)
1741 {
1742         int ret;
1743
1744         ret = register_event_command(&trigger_hist_enable_cmd);
1745         if (WARN_ON(ret < 0))
1746                 return ret;
1747         ret = register_event_command(&trigger_hist_disable_cmd);
1748         if (WARN_ON(ret < 0))
1749                 unregister_trigger_hist_enable_disable_cmds();
1750
1751         return ret;
1752 }