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