blob: f231fa2a3dcde15d0d749668416587690cfb7310 [file] [log] [blame]
Tom Zanussi7ef224d2016-03-03 12:54:42 -06001/*
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>
Ingo Molnarb2d09102017-02-04 01:27:20 +010022#include <linux/rculist.h>
Tom Zanussi4b147932018-01-15 20:51:58 -060023#include <linux/tracefs.h>
Tom Zanussi7ef224d2016-03-03 12:54:42 -060024
25#include "tracing_map.h"
26#include "trace.h"
27
Tom Zanussi4b147932018-01-15 20:51:58 -060028#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
Tom Zanussi7ef224d2016-03-03 12:54:42 -060033struct hist_field;
34
Tom Zanussidf35d932018-01-15 20:51:54 -060035typedef u64 (*hist_field_fn_t) (struct hist_field *field,
36 struct tracing_map_elt *elt,
37 struct ring_buffer_event *rbe,
38 void *event);
Tom Zanussi7ef224d2016-03-03 12:54:42 -060039
Tom Zanussi5819ead2017-09-22 14:58:23 -050040#define HIST_FIELD_OPERANDS_MAX 2
Tom Zanussi30350d62018-01-15 20:51:49 -060041#define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
Tom Zanussi0212e2a2018-01-15 20:51:57 -060042#define HIST_ACTIONS_MAX 8
Tom Zanussi30350d62018-01-15 20:51:49 -060043
Tom Zanussi100719d2018-01-15 20:51:52 -060044enum field_op_id {
45 FIELD_OP_NONE,
46 FIELD_OP_PLUS,
47 FIELD_OP_MINUS,
48 FIELD_OP_UNARY_MINUS,
49};
50
Tom Zanussi30350d62018-01-15 20:51:49 -060051struct hist_var {
52 char *name;
53 struct hist_trigger_data *hist_data;
54 unsigned int idx;
55};
Tom Zanussi5819ead2017-09-22 14:58:23 -050056
Tom Zanussi7ef224d2016-03-03 12:54:42 -060057struct hist_field {
58 struct ftrace_event_field *field;
59 unsigned long flags;
60 hist_field_fn_t fn;
61 unsigned int size;
Tom Zanussi76a3b0c2016-03-03 12:54:44 -060062 unsigned int offset;
Tom Zanussi5819ead2017-09-22 14:58:23 -050063 unsigned int is_signed;
Tom Zanussi19a9fac2018-01-15 20:51:55 -060064 const char *type;
Tom Zanussi5819ead2017-09-22 14:58:23 -050065 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
Tom Zanussib559d003a2018-01-15 20:51:47 -060066 struct hist_trigger_data *hist_data;
Tom Zanussi30350d62018-01-15 20:51:49 -060067 struct hist_var var;
Tom Zanussi100719d2018-01-15 20:51:52 -060068 enum field_op_id operator;
Tom Zanussi067fe032018-01-15 20:51:56 -060069 char *system;
70 char *event_name;
Tom Zanussi100719d2018-01-15 20:51:52 -060071 char *name;
Tom Zanussi067fe032018-01-15 20:51:56 -060072 unsigned int var_idx;
73 unsigned int var_ref_idx;
74 bool read_once;
Tom Zanussi7ef224d2016-03-03 12:54:42 -060075};
76
Tom Zanussidf35d932018-01-15 20:51:54 -060077static u64 hist_field_none(struct hist_field *field,
78 struct tracing_map_elt *elt,
79 struct ring_buffer_event *rbe,
80 void *event)
Tom Zanussi69a02002016-03-03 12:54:52 -060081{
82 return 0;
83}
84
Tom Zanussidf35d932018-01-15 20:51:54 -060085static u64 hist_field_counter(struct hist_field *field,
86 struct tracing_map_elt *elt,
87 struct ring_buffer_event *rbe,
88 void *event)
Tom Zanussi7ef224d2016-03-03 12:54:42 -060089{
90 return 1;
91}
92
Tom Zanussidf35d932018-01-15 20:51:54 -060093static u64 hist_field_string(struct hist_field *hist_field,
94 struct tracing_map_elt *elt,
95 struct ring_buffer_event *rbe,
96 void *event)
Tom Zanussi7ef224d2016-03-03 12:54:42 -060097{
98 char *addr = (char *)(event + hist_field->field->offset);
99
100 return (u64)(unsigned long)addr;
101}
102
Tom Zanussidf35d932018-01-15 20:51:54 -0600103static u64 hist_field_dynstring(struct hist_field *hist_field,
104 struct tracing_map_elt *elt,
105 struct ring_buffer_event *rbe,
106 void *event)
Namhyung Kim79e577c2016-03-03 12:54:53 -0600107{
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
Tom Zanussidf35d932018-01-15 20:51:54 -0600115static u64 hist_field_pstring(struct hist_field *hist_field,
116 struct tracing_map_elt *elt,
117 struct ring_buffer_event *rbe,
118 void *event)
Namhyung Kim79e577c2016-03-03 12:54:53 -0600119{
120 char **addr = (char **)(event + hist_field->field->offset);
121
122 return (u64)(unsigned long)*addr;
123}
124
Tom Zanussidf35d932018-01-15 20:51:54 -0600125static u64 hist_field_log2(struct hist_field *hist_field,
126 struct tracing_map_elt *elt,
127 struct ring_buffer_event *rbe,
128 void *event)
Namhyung Kim4b94f5b2016-03-03 12:55:02 -0600129{
Tom Zanussi5819ead2017-09-22 14:58:23 -0500130 struct hist_field *operand = hist_field->operands[0];
131
Tom Zanussidf35d932018-01-15 20:51:54 -0600132 u64 val = operand->fn(operand, elt, rbe, event);
Namhyung Kim4b94f5b2016-03-03 12:55:02 -0600133
134 return (u64) ilog2(roundup_pow_of_two(val));
135}
136
Tom Zanussidf35d932018-01-15 20:51:54 -0600137static u64 hist_field_plus(struct hist_field *hist_field,
138 struct tracing_map_elt *elt,
139 struct ring_buffer_event *rbe,
140 void *event)
Tom Zanussi100719d2018-01-15 20:51:52 -0600141{
142 struct hist_field *operand1 = hist_field->operands[0];
143 struct hist_field *operand2 = hist_field->operands[1];
144
Tom Zanussidf35d932018-01-15 20:51:54 -0600145 u64 val1 = operand1->fn(operand1, elt, rbe, event);
146 u64 val2 = operand2->fn(operand2, elt, rbe, event);
Tom Zanussi100719d2018-01-15 20:51:52 -0600147
148 return val1 + val2;
149}
150
Tom Zanussidf35d932018-01-15 20:51:54 -0600151static u64 hist_field_minus(struct hist_field *hist_field,
152 struct tracing_map_elt *elt,
153 struct ring_buffer_event *rbe,
154 void *event)
Tom Zanussi100719d2018-01-15 20:51:52 -0600155{
156 struct hist_field *operand1 = hist_field->operands[0];
157 struct hist_field *operand2 = hist_field->operands[1];
158
Tom Zanussidf35d932018-01-15 20:51:54 -0600159 u64 val1 = operand1->fn(operand1, elt, rbe, event);
160 u64 val2 = operand2->fn(operand2, elt, rbe, event);
Tom Zanussi100719d2018-01-15 20:51:52 -0600161
162 return val1 - val2;
163}
164
Tom Zanussidf35d932018-01-15 20:51:54 -0600165static 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)
Tom Zanussi100719d2018-01-15 20:51:52 -0600169{
170 struct hist_field *operand = hist_field->operands[0];
171
Tom Zanussidf35d932018-01-15 20:51:54 -0600172 s64 sval = (s64)operand->fn(operand, elt, rbe, event);
Tom Zanussi100719d2018-01-15 20:51:52 -0600173 u64 val = (u64)-sval;
174
175 return val;
176}
177
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600178#define DEFINE_HIST_FIELD_FN(type) \
Tom Zanussifbd302c2018-01-15 20:51:43 -0600179 static u64 hist_field_##type(struct hist_field *hist_field, \
Tom Zanussidf35d932018-01-15 20:51:54 -0600180 struct tracing_map_elt *elt, \
181 struct ring_buffer_event *rbe, \
182 void *event) \
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600183{ \
184 type *addr = (type *)(event + hist_field->field->offset); \
185 \
Namhyung Kim79e577c2016-03-03 12:54:53 -0600186 return (u64)(unsigned long)*addr; \
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600187}
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
Tom Zanussi69a02002016-03-03 12:54:52 -0600207#define HIST_STACKTRACE_DEPTH 16
208#define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
209#define HIST_STACKTRACE_SKIP 5
210
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600211#define HITCOUNT_IDX 0
Tom Zanussi69a02002016-03-03 12:54:52 -0600212#define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600213
214enum hist_field_flags {
Tom Zanussi0d7a8322017-09-22 14:58:21 -0500215 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,
Tom Zanussiad42feb2018-01-15 20:51:45 -0600225 HIST_FIELD_FL_TIMESTAMP = 1 << 10,
Tom Zanussi860f9f62018-01-15 20:51:48 -0600226 HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11,
Tom Zanussi30350d62018-01-15 20:51:49 -0600227 HIST_FIELD_FL_VAR = 1 << 12,
Tom Zanussi100719d2018-01-15 20:51:52 -0600228 HIST_FIELD_FL_EXPR = 1 << 13,
Tom Zanussi067fe032018-01-15 20:51:56 -0600229 HIST_FIELD_FL_VAR_REF = 1 << 14,
Tom Zanussi8b7622b2018-01-15 20:52:03 -0600230 HIST_FIELD_FL_CPU = 1 << 15,
Tom Zanussi7e8b88a2018-01-15 20:52:04 -0600231 HIST_FIELD_FL_ALIAS = 1 << 16,
Tom Zanussi30350d62018-01-15 20:51:49 -0600232};
233
234struct var_defs {
235 unsigned int n_vars;
236 char *name[TRACING_MAP_VARS_MAX];
237 char *expr[TRACING_MAP_VARS_MAX];
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600238};
239
240struct hist_trigger_attrs {
241 char *keys_str;
Tom Zanussif2606832016-03-03 12:54:43 -0600242 char *vals_str;
Tom Zanussie62347d2016-03-03 12:54:45 -0600243 char *sort_key_str;
Tom Zanussi5463bfd2016-03-03 12:54:59 -0600244 char *name;
Tom Zanussia4072fe2018-01-15 20:52:08 -0600245 char *clock;
Tom Zanussi83e99912016-03-03 12:54:46 -0600246 bool pause;
247 bool cont;
Tom Zanussie86ae9b2016-03-03 12:54:47 -0600248 bool clear;
Tom Zanussi860f9f62018-01-15 20:51:48 -0600249 bool ts_in_usecs;
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600250 unsigned int map_bits;
Tom Zanussi30350d62018-01-15 20:51:49 -0600251
252 char *assignment_str[TRACING_MAP_VARS_MAX];
253 unsigned int n_assignments;
254
Tom Zanussi0212e2a2018-01-15 20:51:57 -0600255 char *action_str[HIST_ACTIONS_MAX];
256 unsigned int n_actions;
257
Tom Zanussi30350d62018-01-15 20:51:49 -0600258 struct var_defs var_defs;
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600259};
260
Tom Zanussi02205a62018-01-15 20:51:59 -0600261struct 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
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600271struct hist_trigger_data {
Tom Zanussi30350d62018-01-15 20:51:49 -0600272 struct hist_field *fields[HIST_FIELDS_MAX];
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600273 unsigned int n_vals;
274 unsigned int n_keys;
275 unsigned int n_fields;
Tom Zanussi30350d62018-01-15 20:51:49 -0600276 unsigned int n_vars;
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600277 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;
Tom Zanussiad42feb2018-01-15 20:51:45 -0600283 bool enable_timestamps;
Tom Zanussi30350d62018-01-15 20:51:49 -0600284 bool remove;
Tom Zanussi067fe032018-01-15 20:51:56 -0600285 struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
286 unsigned int n_var_refs;
Tom Zanussi0212e2a2018-01-15 20:51:57 -0600287
288 struct action_data *actions[HIST_ACTIONS_MAX];
289 unsigned int n_actions;
Tom Zanussi02205a62018-01-15 20:51:59 -0600290
Tom Zanussic282a382018-01-15 20:52:00 -0600291 struct hist_field *synth_var_refs[SYNTH_FIELDS_MAX];
292 unsigned int n_synth_var_refs;
Tom Zanussi02205a62018-01-15 20:51:59 -0600293 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;
Tom Zanussi50450602018-01-15 20:52:01 -0600298
299 struct field_var *max_vars[SYNTH_FIELDS_MAX];
300 unsigned int n_max_vars;
301 unsigned int n_max_var_str;
Tom Zanussi0212e2a2018-01-15 20:51:57 -0600302};
303
Tom Zanussi4b147932018-01-15 20:51:58 -0600304struct 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
Tom Zanussi0212e2a2018-01-15 20:51:57 -0600324struct 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;
Tom Zanussic282a382018-01-15 20:52:00 -0600333 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;
Tom Zanussi50450602018-01-15 20:52:01 -0600344
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;
Tom Zanussic282a382018-01-15 20:52:00 -0600352 };
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600353};
354
Tom Zanussif404da62018-01-15 20:52:05 -0600355
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
Tom Zanussi4b147932018-01-15 20:51:58 -0600414static 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;
Steven Rostedt (VMware)4708abc2018-02-07 17:29:46 -0500643 struct ring_buffer *buffer;
Tom Zanussi4b147932018-01-15 20:51:58 -0600644 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
Steven Rostedt (VMware)4708abc2018-02-07 17:29:46 -0500655 /*
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
Tom Zanussi4b147932018-01-15 20:51:58 -0600662 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
663 sizeof(*entry) + fields_size);
664 if (!entry)
Steven Rostedt (VMware)4708abc2018-02-07 17:29:46 -0500665 goto out;
Tom Zanussi4b147932018-01-15 20:51:58 -0600666
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
Tom Zanussiad452872018-03-28 15:10:56 -0500672 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
Tom Zanussi4b147932018-01-15 20:51:58 -0600673 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);
Steven Rostedt (VMware)4708abc2018-02-07 17:29:46 -0500681out:
682 ring_buffer_nest_end(buffer);
Tom Zanussi4b147932018-01-15 20:51:58 -0600683}
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
Tom Zanussic282a382018-01-15 20:52:00 -0600987static 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
Tom Zanussi4b147932018-01-15 20:51:58 -06001002static 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
Tom Zanussidf35d932018-01-15 20:51:54 -06001223static u64 hist_field_timestamp(struct hist_field *hist_field,
1224 struct tracing_map_elt *elt,
1225 struct ring_buffer_event *rbe,
1226 void *event)
Tom Zanussi860f9f62018-01-15 20:51:48 -06001227{
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
Tom Zanussi8b7622b2018-01-15 20:52:03 -06001239static 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
Tom Zanussi067fe032018-01-15 20:51:56 -06001249static 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
Tom Zanussic282a382018-01-15 20:52:00 -06001314 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
Tom Zanussi067fe032018-01-15 20:51:56 -06001322 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
Tom Zanussi30350d62018-01-15 20:51:49 -06001460static 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
Tom Zanussi067fe032018-01-15 20:51:56 -06001502static 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)) {
Tom Zanussif404da62018-01-15 20:52:05 -06001521 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);
Tom Zanussi067fe032018-01-15 20:51:56 -06001523 return NULL;
Tom Zanussif404da62018-01-15 20:52:05 -06001524 }
Tom Zanussi067fe032018-01-15 20:51:56 -06001525
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
Tom Zanussic282a382018-01-15 20:52:00 -06001552static 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) {
Tom Zanussif404da62018-01-15 20:52:05 -06001573 hist_err_event("Variable name not unique, need to use fully qualified name (subsys.event.var) for variable: ", system, event_name, var_name);
Tom Zanussic282a382018-01-15 20:52:00 -06001574 return ERR_PTR(-EINVAL);
1575 }
1576
1577 found = hist_field;
1578 }
1579 }
1580 }
1581 return found;
1582}
1583
Tom Zanussi067fe032018-01-15 20:51:56 -06001584static 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
Tom Zanussic282a382018-01-15 20:52:00 -06001593 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
Tom Zanussi067fe032018-01-15 20:51:56 -06001601 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
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06001610struct hist_elt_data {
1611 char *comm;
Tom Zanussi067fe032018-01-15 20:51:56 -06001612 u64 *var_ref_vals;
Tom Zanussi02205a62018-01-15 20:51:59 -06001613 char *field_var_str[SYNTH_FIELDS_MAX];
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06001614};
1615
Tom Zanussi067fe032018-01-15 20:51:56 -06001616static 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
Tom Zanussi85013252017-09-22 14:58:22 -05001676static 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;
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06001686 else if (field->flags & HIST_FIELD_FL_LOG2 ||
1687 field->flags & HIST_FIELD_FL_ALIAS)
Tom Zanussi5819ead2017-09-22 14:58:23 -05001688 field_name = hist_field_name(field->operands[0], ++level);
Tom Zanussi8b7622b2018-01-15 20:52:03 -06001689 else if (field->flags & HIST_FIELD_FL_CPU)
1690 field_name = "cpu";
Tom Zanussi067fe032018-01-15 20:51:56 -06001691 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;
Tom Zanussi0ae79612018-03-28 15:10:53 -05001704 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1705 field_name = "common_timestamp";
Tom Zanussi85013252017-09-22 14:58:22 -05001706
1707 if (field_name == NULL)
1708 field_name = "";
1709
1710 return field_name;
1711}
1712
Tom Zanussi7ef224d2016-03-03 12:54:42 -06001713static 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{
Tom Zanussi30350d62018-01-15 20:51:49 -06001774 unsigned int i;
1775
Tom Zanussi7ef224d2016-03-03 12:54:42 -06001776 if (!attrs)
1777 return;
1778
Tom Zanussi30350d62018-01-15 20:51:49 -06001779 for (i = 0; i < attrs->n_assignments; i++)
1780 kfree(attrs->assignment_str[i]);
1781
Tom Zanussi0212e2a2018-01-15 20:51:57 -06001782 for (i = 0; i < attrs->n_actions; i++)
1783 kfree(attrs->action_str[i]);
1784
Tom Zanussi5463bfd2016-03-03 12:54:59 -06001785 kfree(attrs->name);
Tom Zanussie62347d2016-03-03 12:54:45 -06001786 kfree(attrs->sort_key_str);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06001787 kfree(attrs->keys_str);
Tom Zanussif2606832016-03-03 12:54:43 -06001788 kfree(attrs->vals_str);
Tom Zanussia4072fe2018-01-15 20:52:08 -06001789 kfree(attrs->clock);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06001790 kfree(attrs);
1791}
1792
Tom Zanussi0212e2a2018-01-15 20:51:57 -06001793static int parse_action(char *str, struct hist_trigger_attrs *attrs)
1794{
Tom Zanussic282a382018-01-15 20:52:00 -06001795 int ret = -EINVAL;
Tom Zanussi0212e2a2018-01-15 20:51:57 -06001796
1797 if (attrs->n_actions >= HIST_ACTIONS_MAX)
1798 return ret;
1799
Tom Zanussi50450602018-01-15 20:52:01 -06001800 if ((strncmp(str, "onmatch(", strlen("onmatch(")) == 0) ||
1801 (strncmp(str, "onmax(", strlen("onmax(")) == 0)) {
Tom Zanussic282a382018-01-15 20:52:00 -06001802 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
Tom Zanussi0212e2a2018-01-15 20:51:57 -06001811 return ret;
1812}
1813
Tom Zanussi9b1ae032018-01-15 20:51:44 -06001814static 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 }
Tom Zanussia4072fe2018-01-15 20:52:08 -06001845 } 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 }
Tom Zanussi9b1ae032018-01-15 20:51:44 -06001858 } 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;
Tom Zanussi30350d62018-01-15 20:51:49 -06001866 } else {
1867 char *assignment;
1868
1869 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
Tom Zanussif404da62018-01-15 20:52:05 -06001870 hist_err("Too many variables defined: ", str);
Tom Zanussi30350d62018-01-15 20:51:49 -06001871 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 }
Tom Zanussi9b1ae032018-01-15 20:51:44 -06001883 out:
1884 return ret;
1885}
1886
Tom Zanussi7ef224d2016-03-03 12:54:42 -06001887static 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
Tom Zanussi9b1ae032018-01-15 20:51:44 -06001899 if (strchr(str, '=')) {
1900 ret = parse_assignment(str, attrs);
1901 if (ret)
1902 goto free;
1903 } else if (strcmp(str, "pause") == 0)
Tom Zanussi83e99912016-03-03 12:54:46 -06001904 attrs->pause = true;
1905 else if ((strcmp(str, "cont") == 0) ||
1906 (strcmp(str, "continue") == 0))
1907 attrs->cont = true;
Tom Zanussie86ae9b2016-03-03 12:54:47 -06001908 else if (strcmp(str, "clear") == 0)
1909 attrs->clear = true;
Tom Zanussi9b1ae032018-01-15 20:51:44 -06001910 else {
Tom Zanussi0212e2a2018-01-15 20:51:57 -06001911 ret = parse_action(str, attrs);
1912 if (ret)
1913 goto free;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06001914 }
1915 }
1916
1917 if (!attrs->keys_str) {
1918 ret = -EINVAL;
1919 goto free;
1920 }
1921
Tom Zanussia4072fe2018-01-15 20:52:08 -06001922 if (!attrs->clock) {
1923 attrs->clock = kstrdup("global", GFP_KERNEL);
1924 if (!attrs->clock) {
1925 ret = -ENOMEM;
1926 goto free;
1927 }
1928 }
1929
Tom Zanussi7ef224d2016-03-03 12:54:42 -06001930 return attrs;
1931 free:
1932 destroy_hist_trigger_attrs(attrs);
1933
1934 return ERR_PTR(ret);
1935}
1936
Tom Zanussi6b4827a2016-03-03 12:54:50 -06001937static 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
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06001952static void hist_elt_data_free(struct hist_elt_data *elt_data)
Tom Zanussi6b4827a2016-03-03 12:54:50 -06001953{
Tom Zanussi02205a62018-01-15 20:51:59 -06001954 unsigned int i;
1955
1956 for (i = 0; i < SYNTH_FIELDS_MAX; i++)
1957 kfree(elt_data->field_var_str[i]);
1958
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06001959 kfree(elt_data->comm);
1960 kfree(elt_data);
Tom Zanussi6b4827a2016-03-03 12:54:50 -06001961}
1962
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06001963static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
1964{
1965 struct hist_elt_data *elt_data = elt->private_data;
1966
1967 hist_elt_data_free(elt_data);
1968}
1969
1970static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
Tom Zanussi6b4827a2016-03-03 12:54:50 -06001971{
1972 struct hist_trigger_data *hist_data = elt->map->private_data;
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06001973 unsigned int size = TASK_COMM_LEN;
1974 struct hist_elt_data *elt_data;
Tom Zanussi6b4827a2016-03-03 12:54:50 -06001975 struct hist_field *key_field;
Tom Zanussi02205a62018-01-15 20:51:59 -06001976 unsigned int i, n_str;
Tom Zanussi6b4827a2016-03-03 12:54:50 -06001977
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06001978 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
1979 if (!elt_data)
1980 return -ENOMEM;
1981
Tom Zanussi6b4827a2016-03-03 12:54:50 -06001982 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) {
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06001986 elt_data->comm = kzalloc(size, GFP_KERNEL);
1987 if (!elt_data->comm) {
1988 kfree(elt_data);
Tom Zanussi6b4827a2016-03-03 12:54:50 -06001989 return -ENOMEM;
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06001990 }
Tom Zanussi6b4827a2016-03-03 12:54:50 -06001991 break;
1992 }
1993 }
1994
Tom Zanussi50450602018-01-15 20:52:01 -06001995 n_str = hist_data->n_field_var_str + hist_data->n_max_var_str;
Tom Zanussi02205a62018-01-15 20:51:59 -06001996
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
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002007 elt->private_data = elt_data;
2008
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002009 return 0;
2010}
2011
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002012static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002013{
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002014 struct hist_elt_data *elt_data = elt->private_data;
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002015
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002016 if (elt_data->comm)
2017 save_comm(elt_data->comm, current);
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002018}
2019
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002020static 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,
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002024};
2025
Tom Zanussi2ece94f2018-01-15 20:51:51 -06002026static 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
Tom Zanussi100719d2018-01-15 20:51:52 -06002048static void expr_field_str(struct hist_field *field, char *expr)
2049{
Tom Zanussi067fe032018-01-15 20:51:56 -06002050 if (field->flags & HIST_FIELD_FL_VAR_REF)
2051 strcat(expr, "$");
2052
Tom Zanussi100719d2018-01-15 20:51:52 -06002053 strcat(expr, hist_field_name(field, 0));
2054
Tom Zanussi76690942018-03-28 15:10:54 -05002055 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
Tom Zanussi100719d2018-01-15 20:51:52 -06002056 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
Tom Zanussi5819ead2017-09-22 14:58:23 -05002143static void destroy_hist_field(struct hist_field *hist_field,
2144 unsigned int level)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002145{
Tom Zanussi5819ead2017-09-22 14:58:23 -05002146 unsigned int i;
2147
Tom Zanussi100719d2018-01-15 20:51:52 -06002148 if (level > 3)
Tom Zanussi5819ead2017-09-22 14:58:23 -05002149 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
Tom Zanussi30350d62018-01-15 20:51:49 -06002157 kfree(hist_field->var.name);
Tom Zanussi100719d2018-01-15 20:51:52 -06002158 kfree(hist_field->name);
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002159 kfree(hist_field->type);
Tom Zanussi30350d62018-01-15 20:51:49 -06002160
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002161 kfree(hist_field);
2162}
2163
Tom Zanussib559d003a2018-01-15 20:51:47 -06002164static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
2165 struct ftrace_event_field *field,
Tom Zanussi30350d62018-01-15 20:51:49 -06002166 unsigned long flags,
2167 char *var_name)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002168{
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
Tom Zanussib559d003a2018-01-15 20:51:47 -06002178 hist_field->hist_data = hist_data;
2179
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06002180 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
Tom Zanussi100719d2018-01-15 20:51:52 -06002181 goto out; /* caller will populate */
2182
Tom Zanussi067fe032018-01-15 20:51:56 -06002183 if (flags & HIST_FIELD_FL_VAR_REF) {
2184 hist_field->fn = hist_field_var_ref;
2185 goto out;
2186 }
2187
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002188 if (flags & HIST_FIELD_FL_HITCOUNT) {
2189 hist_field->fn = hist_field_counter;
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002190 hist_field->size = sizeof(u64);
2191 hist_field->type = kstrdup("u64", GFP_KERNEL);
2192 if (!hist_field->type)
2193 goto free;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002194 goto out;
2195 }
2196
Tom Zanussi69a02002016-03-03 12:54:52 -06002197 if (flags & HIST_FIELD_FL_STACKTRACE) {
2198 hist_field->fn = hist_field_none;
2199 goto out;
2200 }
2201
Namhyung Kim4b94f5b2016-03-03 12:55:02 -06002202 if (flags & HIST_FIELD_FL_LOG2) {
Tom Zanussi5819ead2017-09-22 14:58:23 -05002203 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2;
Namhyung Kim4b94f5b2016-03-03 12:55:02 -06002204 hist_field->fn = hist_field_log2;
Tom Zanussi30350d62018-01-15 20:51:49 -06002205 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
Tom Zanussi5819ead2017-09-22 14:58:23 -05002206 hist_field->size = hist_field->operands[0]->size;
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002207 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL);
2208 if (!hist_field->type)
2209 goto free;
Namhyung Kim4b94f5b2016-03-03 12:55:02 -06002210 goto out;
2211 }
2212
Tom Zanussiad42feb2018-01-15 20:51:45 -06002213 if (flags & HIST_FIELD_FL_TIMESTAMP) {
2214 hist_field->fn = hist_field_timestamp;
2215 hist_field->size = sizeof(u64);
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002216 hist_field->type = kstrdup("u64", GFP_KERNEL);
2217 if (!hist_field->type)
2218 goto free;
Tom Zanussiad42feb2018-01-15 20:51:45 -06002219 goto out;
2220 }
2221
Tom Zanussi8b7622b2018-01-15 20:52:03 -06002222 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
Tom Zanussi432480c2016-04-25 14:01:27 -05002231 if (WARN_ON_ONCE(!field))
2232 goto out;
2233
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002234 if (is_string_field(field)) {
2235 flags |= HIST_FIELD_FL_STRING;
Namhyung Kim79e577c2016-03-03 12:54:53 -06002236
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002237 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
Namhyung Kim79e577c2016-03-03 12:54:53 -06002242 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;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002248 } else {
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002249 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
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002255 hist_field->fn = select_value_fn(field->size,
2256 field->is_signed);
2257 if (!hist_field->fn) {
Tom Zanussi5819ead2017-09-22 14:58:23 -05002258 destroy_hist_field(hist_field, 0);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002259 return NULL;
2260 }
2261 }
2262 out:
2263 hist_field->field = field;
2264 hist_field->flags = flags;
2265
Tom Zanussi30350d62018-01-15 20:51:49 -06002266 if (var_name) {
2267 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
2268 if (!hist_field->var.name)
2269 goto free;
2270 }
2271
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002272 return hist_field;
Tom Zanussi30350d62018-01-15 20:51:49 -06002273 free:
2274 destroy_hist_field(hist_field, 0);
2275 return NULL;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002276}
2277
2278static void destroy_hist_fields(struct hist_trigger_data *hist_data)
2279{
2280 unsigned int i;
2281
Tom Zanussi30350d62018-01-15 20:51:49 -06002282 for (i = 0; i < HIST_FIELDS_MAX; i++) {
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002283 if (hist_data->fields[i]) {
Tom Zanussi5819ead2017-09-22 14:58:23 -05002284 destroy_hist_field(hist_data->fields[i], 0);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002285 hist_data->fields[i] = NULL;
2286 }
2287 }
2288}
2289
Tom Zanussi067fe032018-01-15 20:51:56 -06002290static 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
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06002317 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 }
Tom Zanussi067fe032018-01-15 20:51:56 -06002329 }
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
Tom Zanussif404da62018-01-15 20:52:05 -06002433 if (!ref_field)
2434 hist_err_event("Couldn't find variable: $",
2435 system, event_name, var_name);
2436
Tom Zanussi067fe032018-01-15 20:51:56 -06002437 return ref_field;
2438}
2439
Tom Zanussi100719d2018-01-15 20:51:52 -06002440static 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;
Tom Zanussi8b7622b2018-01-15 20:52:03 -06002479 } else if (strcmp(field_name, "cpu") == 0)
2480 *flags |= HIST_FIELD_FL_CPU;
2481 else {
Tom Zanussi100719d2018-01-15 20:51:52 -06002482 field = trace_find_event_field(file->event_call, field_name);
2483 if (!field || !field->size) {
Tom Zanussi5ec432d2018-04-26 20:04:48 -05002484 hist_err("Couldn't find field: ", field_name);
Tom Zanussi100719d2018-01-15 20:51:52 -06002485 field = ERR_PTR(-EINVAL);
2486 goto out;
2487 }
2488 }
2489 out:
2490 kfree(str);
2491
2492 return field;
2493}
2494
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06002495static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2496 struct hist_field *var_ref,
2497 char *var_name)
2498{
2499 struct hist_field *alias = NULL;
2500 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2501
2502 alias = create_hist_field(hist_data, NULL, flags, var_name);
2503 if (!alias)
2504 return NULL;
2505
2506 alias->fn = var_ref->fn;
2507 alias->operands[0] = var_ref;
2508
2509 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2510 destroy_hist_field(alias, 0);
2511 return NULL;
2512 }
2513
2514 return alias;
2515}
2516
Tom Zanussi100719d2018-01-15 20:51:52 -06002517static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2518 struct trace_event_file *file, char *str,
2519 unsigned long *flags, char *var_name)
2520{
Tom Zanussi067fe032018-01-15 20:51:56 -06002521 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
Tom Zanussi100719d2018-01-15 20:51:52 -06002522 struct ftrace_event_field *field = NULL;
2523 struct hist_field *hist_field = NULL;
2524 int ret = 0;
2525
Tom Zanussi067fe032018-01-15 20:51:56 -06002526 s = strchr(str, '.');
2527 if (s) {
2528 s = strchr(++s, '.');
2529 if (s) {
2530 ref_system = strsep(&str, ".");
2531 if (!str) {
2532 ret = -EINVAL;
2533 goto out;
2534 }
2535 ref_event = strsep(&str, ".");
2536 if (!str) {
2537 ret = -EINVAL;
2538 goto out;
2539 }
2540 ref_var = str;
2541 }
2542 }
2543
2544 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2545 if (!s) {
2546 hist_field = parse_var_ref(hist_data, ref_system, ref_event, ref_var);
2547 if (hist_field) {
2548 hist_data->var_refs[hist_data->n_var_refs] = hist_field;
2549 hist_field->var_ref_idx = hist_data->n_var_refs++;
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06002550 if (var_name) {
2551 hist_field = create_alias(hist_data, hist_field, var_name);
2552 if (!hist_field) {
2553 ret = -ENOMEM;
2554 goto out;
2555 }
2556 }
Tom Zanussi067fe032018-01-15 20:51:56 -06002557 return hist_field;
2558 }
2559 } else
2560 str = s;
2561
Tom Zanussi100719d2018-01-15 20:51:52 -06002562 field = parse_field(hist_data, file, str, flags);
2563 if (IS_ERR(field)) {
2564 ret = PTR_ERR(field);
2565 goto out;
2566 }
2567
2568 hist_field = create_hist_field(hist_data, field, *flags, var_name);
2569 if (!hist_field) {
2570 ret = -ENOMEM;
2571 goto out;
2572 }
2573
2574 return hist_field;
2575 out:
2576 return ERR_PTR(ret);
2577}
2578
2579static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2580 struct trace_event_file *file,
2581 char *str, unsigned long flags,
2582 char *var_name, unsigned int level);
2583
2584static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2585 struct trace_event_file *file,
2586 char *str, unsigned long flags,
2587 char *var_name, unsigned int level)
2588{
2589 struct hist_field *operand1, *expr = NULL;
2590 unsigned long operand_flags;
2591 int ret = 0;
2592 char *s;
2593
2594 /* we support only -(xxx) i.e. explicit parens required */
2595
2596 if (level > 3) {
Tom Zanussif404da62018-01-15 20:52:05 -06002597 hist_err("Too many subexpressions (3 max): ", str);
Tom Zanussi100719d2018-01-15 20:51:52 -06002598 ret = -EINVAL;
2599 goto free;
2600 }
2601
2602 str++; /* skip leading '-' */
2603
2604 s = strchr(str, '(');
2605 if (s)
2606 str++;
2607 else {
2608 ret = -EINVAL;
2609 goto free;
2610 }
2611
2612 s = strrchr(str, ')');
2613 if (s)
2614 *s = '\0';
2615 else {
2616 ret = -EINVAL; /* no closing ')' */
2617 goto free;
2618 }
2619
2620 flags |= HIST_FIELD_FL_EXPR;
2621 expr = create_hist_field(hist_data, NULL, flags, var_name);
2622 if (!expr) {
2623 ret = -ENOMEM;
2624 goto free;
2625 }
2626
2627 operand_flags = 0;
2628 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2629 if (IS_ERR(operand1)) {
2630 ret = PTR_ERR(operand1);
2631 goto free;
2632 }
2633
2634 expr->flags |= operand1->flags &
2635 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2636 expr->fn = hist_field_unary_minus;
2637 expr->operands[0] = operand1;
2638 expr->operator = FIELD_OP_UNARY_MINUS;
2639 expr->name = expr_str(expr, 0);
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002640 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2641 if (!expr->type) {
2642 ret = -ENOMEM;
2643 goto free;
2644 }
Tom Zanussi100719d2018-01-15 20:51:52 -06002645
2646 return expr;
2647 free:
2648 destroy_hist_field(expr, 0);
2649 return ERR_PTR(ret);
2650}
2651
2652static int check_expr_operands(struct hist_field *operand1,
2653 struct hist_field *operand2)
2654{
2655 unsigned long operand1_flags = operand1->flags;
2656 unsigned long operand2_flags = operand2->flags;
2657
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06002658 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2659 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2660 struct hist_field *var;
2661
2662 var = find_var_field(operand1->var.hist_data, operand1->name);
2663 if (!var)
2664 return -EINVAL;
2665 operand1_flags = var->flags;
2666 }
2667
2668 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2669 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2670 struct hist_field *var;
2671
2672 var = find_var_field(operand2->var.hist_data, operand2->name);
2673 if (!var)
2674 return -EINVAL;
2675 operand2_flags = var->flags;
2676 }
2677
Tom Zanussi100719d2018-01-15 20:51:52 -06002678 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
Tom Zanussif404da62018-01-15 20:52:05 -06002679 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
2680 hist_err("Timestamp units in expression don't match", NULL);
Tom Zanussi100719d2018-01-15 20:51:52 -06002681 return -EINVAL;
Tom Zanussif404da62018-01-15 20:52:05 -06002682 }
Tom Zanussi100719d2018-01-15 20:51:52 -06002683
2684 return 0;
2685}
2686
2687static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2688 struct trace_event_file *file,
2689 char *str, unsigned long flags,
2690 char *var_name, unsigned int level)
2691{
2692 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2693 unsigned long operand_flags;
2694 int field_op, ret = -EINVAL;
2695 char *sep, *operand1_str;
2696
Tom Zanussif404da62018-01-15 20:52:05 -06002697 if (level > 3) {
2698 hist_err("Too many subexpressions (3 max): ", str);
Tom Zanussi100719d2018-01-15 20:51:52 -06002699 return ERR_PTR(-EINVAL);
Tom Zanussif404da62018-01-15 20:52:05 -06002700 }
Tom Zanussi100719d2018-01-15 20:51:52 -06002701
2702 field_op = contains_operator(str);
2703
2704 if (field_op == FIELD_OP_NONE)
2705 return parse_atom(hist_data, file, str, &flags, var_name);
2706
2707 if (field_op == FIELD_OP_UNARY_MINUS)
2708 return parse_unary(hist_data, file, str, flags, var_name, ++level);
2709
2710 switch (field_op) {
2711 case FIELD_OP_MINUS:
2712 sep = "-";
2713 break;
2714 case FIELD_OP_PLUS:
2715 sep = "+";
2716 break;
2717 default:
2718 goto free;
2719 }
2720
2721 operand1_str = strsep(&str, sep);
2722 if (!operand1_str || !str)
2723 goto free;
2724
2725 operand_flags = 0;
2726 operand1 = parse_atom(hist_data, file, operand1_str,
2727 &operand_flags, NULL);
2728 if (IS_ERR(operand1)) {
2729 ret = PTR_ERR(operand1);
2730 operand1 = NULL;
2731 goto free;
2732 }
2733
2734 /* rest of string could be another expression e.g. b+c in a+b+c */
2735 operand_flags = 0;
2736 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2737 if (IS_ERR(operand2)) {
2738 ret = PTR_ERR(operand2);
2739 operand2 = NULL;
2740 goto free;
2741 }
2742
2743 ret = check_expr_operands(operand1, operand2);
2744 if (ret)
2745 goto free;
2746
2747 flags |= HIST_FIELD_FL_EXPR;
2748
2749 flags |= operand1->flags &
2750 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2751
2752 expr = create_hist_field(hist_data, NULL, flags, var_name);
2753 if (!expr) {
2754 ret = -ENOMEM;
2755 goto free;
2756 }
2757
Tom Zanussi067fe032018-01-15 20:51:56 -06002758 operand1->read_once = true;
2759 operand2->read_once = true;
2760
Tom Zanussi100719d2018-01-15 20:51:52 -06002761 expr->operands[0] = operand1;
2762 expr->operands[1] = operand2;
2763 expr->operator = field_op;
2764 expr->name = expr_str(expr, 0);
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002765 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2766 if (!expr->type) {
2767 ret = -ENOMEM;
2768 goto free;
2769 }
Tom Zanussi100719d2018-01-15 20:51:52 -06002770
2771 switch (field_op) {
2772 case FIELD_OP_MINUS:
2773 expr->fn = hist_field_minus;
2774 break;
2775 case FIELD_OP_PLUS:
2776 expr->fn = hist_field_plus;
2777 break;
2778 default:
Dan Carpenter5e4cf2b2018-03-23 14:37:36 +03002779 ret = -EINVAL;
Tom Zanussi100719d2018-01-15 20:51:52 -06002780 goto free;
2781 }
2782
2783 return expr;
2784 free:
2785 destroy_hist_field(operand1, 0);
2786 destroy_hist_field(operand2, 0);
2787 destroy_hist_field(expr, 0);
2788
2789 return ERR_PTR(ret);
2790}
2791
Tom Zanussi02205a62018-01-15 20:51:59 -06002792static char *find_trigger_filter(struct hist_trigger_data *hist_data,
2793 struct trace_event_file *file)
2794{
2795 struct event_trigger_data *test;
2796
2797 list_for_each_entry_rcu(test, &file->triggers, list) {
2798 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2799 if (test->private_data == hist_data)
2800 return test->filter_str;
2801 }
2802 }
2803
2804 return NULL;
2805}
2806
2807static struct event_command trigger_hist_cmd;
2808static int event_hist_trigger_func(struct event_command *cmd_ops,
2809 struct trace_event_file *file,
2810 char *glob, char *cmd, char *param);
2811
2812static bool compatible_keys(struct hist_trigger_data *target_hist_data,
2813 struct hist_trigger_data *hist_data,
2814 unsigned int n_keys)
2815{
2816 struct hist_field *target_hist_field, *hist_field;
2817 unsigned int n, i, j;
2818
2819 if (hist_data->n_fields - hist_data->n_vals != n_keys)
2820 return false;
2821
2822 i = hist_data->n_vals;
2823 j = target_hist_data->n_vals;
2824
2825 for (n = 0; n < n_keys; n++) {
2826 hist_field = hist_data->fields[i + n];
2827 target_hist_field = target_hist_data->fields[j + n];
2828
2829 if (strcmp(hist_field->type, target_hist_field->type) != 0)
2830 return false;
2831 if (hist_field->size != target_hist_field->size)
2832 return false;
2833 if (hist_field->is_signed != target_hist_field->is_signed)
2834 return false;
2835 }
2836
2837 return true;
2838}
2839
2840static struct hist_trigger_data *
2841find_compatible_hist(struct hist_trigger_data *target_hist_data,
2842 struct trace_event_file *file)
2843{
2844 struct hist_trigger_data *hist_data;
2845 struct event_trigger_data *test;
2846 unsigned int n_keys;
2847
2848 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
2849
2850 list_for_each_entry_rcu(test, &file->triggers, list) {
2851 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2852 hist_data = test->private_data;
2853
2854 if (compatible_keys(target_hist_data, hist_data, n_keys))
2855 return hist_data;
2856 }
2857 }
2858
2859 return NULL;
2860}
2861
2862static struct trace_event_file *event_file(struct trace_array *tr,
2863 char *system, char *event_name)
2864{
2865 struct trace_event_file *file;
2866
2867 file = find_event_file(tr, system, event_name);
2868 if (!file)
2869 return ERR_PTR(-EINVAL);
2870
2871 return file;
2872}
2873
2874static struct hist_field *
2875find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
2876 char *system, char *event_name, char *field_name)
2877{
2878 struct hist_field *event_var;
2879 char *synthetic_name;
2880
2881 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2882 if (!synthetic_name)
2883 return ERR_PTR(-ENOMEM);
2884
2885 strcpy(synthetic_name, "synthetic_");
2886 strcat(synthetic_name, field_name);
2887
2888 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
2889
2890 kfree(synthetic_name);
2891
2892 return event_var;
2893}
2894
2895/**
2896 * create_field_var_hist - Automatically create a histogram and var for a field
2897 * @target_hist_data: The target hist trigger
2898 * @subsys_name: Optional subsystem name
2899 * @event_name: Optional event name
2900 * @field_name: The name of the field (and the resulting variable)
2901 *
2902 * Hist trigger actions fetch data from variables, not directly from
2903 * events. However, for convenience, users are allowed to directly
2904 * specify an event field in an action, which will be automatically
2905 * converted into a variable on their behalf.
2906
2907 * If a user specifies a field on an event that isn't the event the
2908 * histogram currently being defined (the target event histogram), the
2909 * only way that can be accomplished is if a new hist trigger is
2910 * created and the field variable defined on that.
2911 *
2912 * This function creates a new histogram compatible with the target
2913 * event (meaning a histogram with the same key as the target
2914 * histogram), and creates a variable for the specified field, but
2915 * with 'synthetic_' prepended to the variable name in order to avoid
2916 * collision with normal field variables.
2917 *
2918 * Return: The variable created for the field.
2919 */
Tom Zanussic282a382018-01-15 20:52:00 -06002920static struct hist_field *
Tom Zanussi02205a62018-01-15 20:51:59 -06002921create_field_var_hist(struct hist_trigger_data *target_hist_data,
2922 char *subsys_name, char *event_name, char *field_name)
2923{
2924 struct trace_array *tr = target_hist_data->event_file->tr;
2925 struct hist_field *event_var = ERR_PTR(-EINVAL);
2926 struct hist_trigger_data *hist_data;
2927 unsigned int i, n, first = true;
2928 struct field_var_hist *var_hist;
2929 struct trace_event_file *file;
2930 struct hist_field *key_field;
2931 char *saved_filter;
2932 char *cmd;
2933 int ret;
2934
Tom Zanussif404da62018-01-15 20:52:05 -06002935 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
2936 hist_err_event("onmatch: Too many field variables defined: ",
2937 subsys_name, event_name, field_name);
Tom Zanussi02205a62018-01-15 20:51:59 -06002938 return ERR_PTR(-EINVAL);
Tom Zanussif404da62018-01-15 20:52:05 -06002939 }
Tom Zanussi02205a62018-01-15 20:51:59 -06002940
2941 file = event_file(tr, subsys_name, event_name);
2942
2943 if (IS_ERR(file)) {
Tom Zanussif404da62018-01-15 20:52:05 -06002944 hist_err_event("onmatch: Event file not found: ",
2945 subsys_name, event_name, field_name);
Tom Zanussi02205a62018-01-15 20:51:59 -06002946 ret = PTR_ERR(file);
2947 return ERR_PTR(ret);
2948 }
2949
2950 /*
2951 * Look for a histogram compatible with target. We'll use the
2952 * found histogram specification to create a new matching
2953 * histogram with our variable on it. target_hist_data is not
2954 * yet a registered histogram so we can't use that.
2955 */
2956 hist_data = find_compatible_hist(target_hist_data, file);
Tom Zanussif404da62018-01-15 20:52:05 -06002957 if (!hist_data) {
2958 hist_err_event("onmatch: Matching event histogram not found: ",
2959 subsys_name, event_name, field_name);
Tom Zanussi02205a62018-01-15 20:51:59 -06002960 return ERR_PTR(-EINVAL);
Tom Zanussif404da62018-01-15 20:52:05 -06002961 }
Tom Zanussi02205a62018-01-15 20:51:59 -06002962
2963 /* See if a synthetic field variable has already been created */
2964 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
2965 event_name, field_name);
2966 if (!IS_ERR_OR_NULL(event_var))
2967 return event_var;
2968
2969 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
2970 if (!var_hist)
2971 return ERR_PTR(-ENOMEM);
2972
2973 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2974 if (!cmd) {
2975 kfree(var_hist);
2976 return ERR_PTR(-ENOMEM);
2977 }
2978
2979 /* Use the same keys as the compatible histogram */
2980 strcat(cmd, "keys=");
2981
2982 for_each_hist_key_field(i, hist_data) {
2983 key_field = hist_data->fields[i];
2984 if (!first)
2985 strcat(cmd, ",");
2986 strcat(cmd, key_field->field->name);
2987 first = false;
2988 }
2989
2990 /* Create the synthetic field variable specification */
2991 strcat(cmd, ":synthetic_");
2992 strcat(cmd, field_name);
2993 strcat(cmd, "=");
2994 strcat(cmd, field_name);
2995
2996 /* Use the same filter as the compatible histogram */
2997 saved_filter = find_trigger_filter(hist_data, file);
2998 if (saved_filter) {
2999 strcat(cmd, " if ");
3000 strcat(cmd, saved_filter);
3001 }
3002
3003 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
3004 if (!var_hist->cmd) {
3005 kfree(cmd);
3006 kfree(var_hist);
3007 return ERR_PTR(-ENOMEM);
3008 }
3009
3010 /* Save the compatible histogram information */
3011 var_hist->hist_data = hist_data;
3012
3013 /* Create the new histogram with our variable */
3014 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
3015 "", "hist", cmd);
3016 if (ret) {
3017 kfree(cmd);
3018 kfree(var_hist->cmd);
3019 kfree(var_hist);
Tom Zanussif404da62018-01-15 20:52:05 -06003020 hist_err_event("onmatch: Couldn't create histogram for field: ",
3021 subsys_name, event_name, field_name);
Tom Zanussi02205a62018-01-15 20:51:59 -06003022 return ERR_PTR(ret);
3023 }
3024
3025 kfree(cmd);
3026
3027 /* If we can't find the variable, something went wrong */
3028 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3029 event_name, field_name);
3030 if (IS_ERR_OR_NULL(event_var)) {
3031 kfree(var_hist->cmd);
3032 kfree(var_hist);
Tom Zanussif404da62018-01-15 20:52:05 -06003033 hist_err_event("onmatch: Couldn't find synthetic variable: ",
3034 subsys_name, event_name, field_name);
Tom Zanussi02205a62018-01-15 20:51:59 -06003035 return ERR_PTR(-EINVAL);
3036 }
3037
3038 n = target_hist_data->n_field_var_hists;
3039 target_hist_data->field_var_hists[n] = var_hist;
3040 target_hist_data->n_field_var_hists++;
3041
3042 return event_var;
3043}
3044
Tom Zanussic282a382018-01-15 20:52:00 -06003045static struct hist_field *
Tom Zanussi02205a62018-01-15 20:51:59 -06003046find_target_event_var(struct hist_trigger_data *hist_data,
3047 char *subsys_name, char *event_name, char *var_name)
3048{
3049 struct trace_event_file *file = hist_data->event_file;
3050 struct hist_field *hist_field = NULL;
3051
3052 if (subsys_name) {
3053 struct trace_event_call *call;
3054
3055 if (!event_name)
3056 return NULL;
3057
3058 call = file->event_call;
3059
3060 if (strcmp(subsys_name, call->class->system) != 0)
3061 return NULL;
3062
3063 if (strcmp(event_name, trace_event_name(call)) != 0)
3064 return NULL;
3065 }
3066
3067 hist_field = find_var_field(hist_data, var_name);
3068
3069 return hist_field;
3070}
3071
3072static inline void __update_field_vars(struct tracing_map_elt *elt,
3073 struct ring_buffer_event *rbe,
3074 void *rec,
3075 struct field_var **field_vars,
3076 unsigned int n_field_vars,
3077 unsigned int field_var_str_start)
3078{
3079 struct hist_elt_data *elt_data = elt->private_data;
3080 unsigned int i, j, var_idx;
3081 u64 var_val;
3082
3083 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
3084 struct field_var *field_var = field_vars[i];
3085 struct hist_field *var = field_var->var;
3086 struct hist_field *val = field_var->val;
3087
3088 var_val = val->fn(val, elt, rbe, rec);
3089 var_idx = var->var.idx;
3090
3091 if (val->flags & HIST_FIELD_FL_STRING) {
3092 char *str = elt_data->field_var_str[j++];
3093 char *val_str = (char *)(uintptr_t)var_val;
3094
Tom Zanussiad452872018-03-28 15:10:56 -05003095 strscpy(str, val_str, STR_VAR_LEN_MAX);
Tom Zanussi02205a62018-01-15 20:51:59 -06003096 var_val = (u64)(uintptr_t)str;
3097 }
3098 tracing_map_set_var(elt, var_idx, var_val);
3099 }
3100}
3101
3102static void update_field_vars(struct hist_trigger_data *hist_data,
3103 struct tracing_map_elt *elt,
3104 struct ring_buffer_event *rbe,
3105 void *rec)
3106{
3107 __update_field_vars(elt, rbe, rec, hist_data->field_vars,
3108 hist_data->n_field_vars, 0);
3109}
3110
Tom Zanussi50450602018-01-15 20:52:01 -06003111static void update_max_vars(struct hist_trigger_data *hist_data,
3112 struct tracing_map_elt *elt,
3113 struct ring_buffer_event *rbe,
3114 void *rec)
3115{
3116 __update_field_vars(elt, rbe, rec, hist_data->max_vars,
3117 hist_data->n_max_vars, hist_data->n_field_var_str);
3118}
3119
Tom Zanussi02205a62018-01-15 20:51:59 -06003120static struct hist_field *create_var(struct hist_trigger_data *hist_data,
3121 struct trace_event_file *file,
3122 char *name, int size, const char *type)
3123{
3124 struct hist_field *var;
3125 int idx;
3126
3127 if (find_var(hist_data, file, name) && !hist_data->remove) {
3128 var = ERR_PTR(-EINVAL);
3129 goto out;
3130 }
3131
3132 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3133 if (!var) {
3134 var = ERR_PTR(-ENOMEM);
3135 goto out;
3136 }
3137
3138 idx = tracing_map_add_var(hist_data->map);
3139 if (idx < 0) {
3140 kfree(var);
3141 var = ERR_PTR(-EINVAL);
3142 goto out;
3143 }
3144
3145 var->flags = HIST_FIELD_FL_VAR;
3146 var->var.idx = idx;
3147 var->var.hist_data = var->hist_data = hist_data;
3148 var->size = size;
3149 var->var.name = kstrdup(name, GFP_KERNEL);
3150 var->type = kstrdup(type, GFP_KERNEL);
3151 if (!var->var.name || !var->type) {
3152 kfree(var->var.name);
3153 kfree(var->type);
3154 kfree(var);
3155 var = ERR_PTR(-ENOMEM);
3156 }
3157 out:
3158 return var;
3159}
3160
3161static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
3162 struct trace_event_file *file,
3163 char *field_name)
3164{
3165 struct hist_field *val = NULL, *var = NULL;
3166 unsigned long flags = HIST_FIELD_FL_VAR;
3167 struct field_var *field_var;
3168 int ret = 0;
3169
3170 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
Tom Zanussif404da62018-01-15 20:52:05 -06003171 hist_err("Too many field variables defined: ", field_name);
Tom Zanussi02205a62018-01-15 20:51:59 -06003172 ret = -EINVAL;
3173 goto err;
3174 }
3175
3176 val = parse_atom(hist_data, file, field_name, &flags, NULL);
3177 if (IS_ERR(val)) {
Tom Zanussif404da62018-01-15 20:52:05 -06003178 hist_err("Couldn't parse field variable: ", field_name);
Tom Zanussi02205a62018-01-15 20:51:59 -06003179 ret = PTR_ERR(val);
3180 goto err;
3181 }
3182
3183 var = create_var(hist_data, file, field_name, val->size, val->type);
3184 if (IS_ERR(var)) {
Tom Zanussif404da62018-01-15 20:52:05 -06003185 hist_err("Couldn't create or find variable: ", field_name);
Tom Zanussi02205a62018-01-15 20:51:59 -06003186 kfree(val);
3187 ret = PTR_ERR(var);
3188 goto err;
3189 }
3190
3191 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
3192 if (!field_var) {
3193 kfree(val);
3194 kfree(var);
3195 ret = -ENOMEM;
3196 goto err;
3197 }
3198
3199 field_var->var = var;
3200 field_var->val = val;
3201 out:
3202 return field_var;
3203 err:
3204 field_var = ERR_PTR(ret);
3205 goto out;
3206}
3207
3208/**
3209 * create_target_field_var - Automatically create a variable for a field
3210 * @target_hist_data: The target hist trigger
3211 * @subsys_name: Optional subsystem name
3212 * @event_name: Optional event name
3213 * @var_name: The name of the field (and the resulting variable)
3214 *
3215 * Hist trigger actions fetch data from variables, not directly from
3216 * events. However, for convenience, users are allowed to directly
3217 * specify an event field in an action, which will be automatically
3218 * converted into a variable on their behalf.
3219
3220 * This function creates a field variable with the name var_name on
3221 * the hist trigger currently being defined on the target event. If
3222 * subsys_name and event_name are specified, this function simply
3223 * verifies that they do in fact match the target event subsystem and
3224 * event name.
3225 *
3226 * Return: The variable created for the field.
3227 */
Tom Zanussic282a382018-01-15 20:52:00 -06003228static struct field_var *
Tom Zanussi02205a62018-01-15 20:51:59 -06003229create_target_field_var(struct hist_trigger_data *target_hist_data,
3230 char *subsys_name, char *event_name, char *var_name)
3231{
3232 struct trace_event_file *file = target_hist_data->event_file;
3233
3234 if (subsys_name) {
3235 struct trace_event_call *call;
3236
3237 if (!event_name)
3238 return NULL;
3239
3240 call = file->event_call;
3241
3242 if (strcmp(subsys_name, call->class->system) != 0)
3243 return NULL;
3244
3245 if (strcmp(event_name, trace_event_name(call)) != 0)
3246 return NULL;
3247 }
3248
3249 return create_field_var(target_hist_data, file, var_name);
3250}
3251
Tom Zanussi50450602018-01-15 20:52:01 -06003252static void onmax_print(struct seq_file *m,
3253 struct hist_trigger_data *hist_data,
3254 struct tracing_map_elt *elt,
3255 struct action_data *data)
3256{
3257 unsigned int i, save_var_idx, max_idx = data->onmax.max_var->var.idx;
3258
3259 seq_printf(m, "\n\tmax: %10llu", tracing_map_read_var(elt, max_idx));
3260
3261 for (i = 0; i < hist_data->n_max_vars; i++) {
3262 struct hist_field *save_val = hist_data->max_vars[i]->val;
3263 struct hist_field *save_var = hist_data->max_vars[i]->var;
3264 u64 val;
3265
3266 save_var_idx = save_var->var.idx;
3267
3268 val = tracing_map_read_var(elt, save_var_idx);
3269
3270 if (save_val->flags & HIST_FIELD_FL_STRING) {
3271 seq_printf(m, " %s: %-32s", save_var->var.name,
3272 (char *)(uintptr_t)(val));
3273 } else
3274 seq_printf(m, " %s: %10llu", save_var->var.name, val);
3275 }
3276}
3277
3278static void onmax_save(struct hist_trigger_data *hist_data,
3279 struct tracing_map_elt *elt, void *rec,
3280 struct ring_buffer_event *rbe,
3281 struct action_data *data, u64 *var_ref_vals)
3282{
3283 unsigned int max_idx = data->onmax.max_var->var.idx;
3284 unsigned int max_var_ref_idx = data->onmax.max_var_ref_idx;
3285
3286 u64 var_val, max_val;
3287
3288 var_val = var_ref_vals[max_var_ref_idx];
3289 max_val = tracing_map_read_var(elt, max_idx);
3290
3291 if (var_val <= max_val)
3292 return;
3293
3294 tracing_map_set_var(elt, max_idx, var_val);
3295
3296 update_max_vars(hist_data, elt, rbe, rec);
3297}
3298
3299static void onmax_destroy(struct action_data *data)
3300{
3301 unsigned int i;
3302
3303 destroy_hist_field(data->onmax.max_var, 0);
3304 destroy_hist_field(data->onmax.var, 0);
3305
3306 kfree(data->onmax.var_str);
3307 kfree(data->onmax.fn_name);
3308
3309 for (i = 0; i < data->n_params; i++)
3310 kfree(data->params[i]);
3311
3312 kfree(data);
3313}
3314
3315static int onmax_create(struct hist_trigger_data *hist_data,
3316 struct action_data *data)
3317{
3318 struct trace_event_file *file = hist_data->event_file;
3319 struct hist_field *var_field, *ref_field, *max_var;
3320 unsigned int var_ref_idx = hist_data->n_var_refs;
3321 struct field_var *field_var;
3322 char *onmax_var_str, *param;
3323 unsigned long flags;
3324 unsigned int i;
3325 int ret = 0;
3326
3327 onmax_var_str = data->onmax.var_str;
Tom Zanussif404da62018-01-15 20:52:05 -06003328 if (onmax_var_str[0] != '$') {
3329 hist_err("onmax: For onmax(x), x must be a variable: ", onmax_var_str);
Tom Zanussi50450602018-01-15 20:52:01 -06003330 return -EINVAL;
Tom Zanussif404da62018-01-15 20:52:05 -06003331 }
Tom Zanussi50450602018-01-15 20:52:01 -06003332 onmax_var_str++;
3333
3334 var_field = find_target_event_var(hist_data, NULL, NULL, onmax_var_str);
Tom Zanussif404da62018-01-15 20:52:05 -06003335 if (!var_field) {
3336 hist_err("onmax: Couldn't find onmax variable: ", onmax_var_str);
Tom Zanussi50450602018-01-15 20:52:01 -06003337 return -EINVAL;
Tom Zanussif404da62018-01-15 20:52:05 -06003338 }
Tom Zanussi50450602018-01-15 20:52:01 -06003339
3340 flags = HIST_FIELD_FL_VAR_REF;
3341 ref_field = create_hist_field(hist_data, NULL, flags, NULL);
3342 if (!ref_field)
3343 return -ENOMEM;
3344
3345 if (init_var_ref(ref_field, var_field, NULL, NULL)) {
3346 destroy_hist_field(ref_field, 0);
3347 ret = -ENOMEM;
3348 goto out;
3349 }
3350 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
3351 ref_field->var_ref_idx = hist_data->n_var_refs++;
3352 data->onmax.var = ref_field;
3353
3354 data->fn = onmax_save;
3355 data->onmax.max_var_ref_idx = var_ref_idx;
3356 max_var = create_var(hist_data, file, "max", sizeof(u64), "u64");
3357 if (IS_ERR(max_var)) {
Tom Zanussif404da62018-01-15 20:52:05 -06003358 hist_err("onmax: Couldn't create onmax variable: ", "max");
Tom Zanussi50450602018-01-15 20:52:01 -06003359 ret = PTR_ERR(max_var);
3360 goto out;
3361 }
3362 data->onmax.max_var = max_var;
3363
3364 for (i = 0; i < data->n_params; i++) {
3365 param = kstrdup(data->params[i], GFP_KERNEL);
3366 if (!param) {
3367 ret = -ENOMEM;
3368 goto out;
3369 }
3370
3371 field_var = create_target_field_var(hist_data, NULL, NULL, param);
3372 if (IS_ERR(field_var)) {
Tom Zanussif404da62018-01-15 20:52:05 -06003373 hist_err("onmax: Couldn't create field variable: ", param);
Tom Zanussi50450602018-01-15 20:52:01 -06003374 ret = PTR_ERR(field_var);
3375 kfree(param);
3376 goto out;
3377 }
3378
3379 hist_data->max_vars[hist_data->n_max_vars++] = field_var;
3380 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3381 hist_data->n_max_var_str++;
3382
3383 kfree(param);
3384 }
3385 out:
3386 return ret;
3387}
3388
3389static int parse_action_params(char *params, struct action_data *data)
3390{
3391 char *param, *saved_param;
3392 int ret = 0;
3393
3394 while (params) {
3395 if (data->n_params >= SYNTH_FIELDS_MAX)
3396 goto out;
3397
3398 param = strsep(&params, ",");
3399 if (!param) {
3400 ret = -EINVAL;
3401 goto out;
3402 }
3403
3404 param = strstrip(param);
3405 if (strlen(param) < 2) {
Tom Zanussif404da62018-01-15 20:52:05 -06003406 hist_err("Invalid action param: ", param);
Tom Zanussi50450602018-01-15 20:52:01 -06003407 ret = -EINVAL;
3408 goto out;
3409 }
3410
3411 saved_param = kstrdup(param, GFP_KERNEL);
3412 if (!saved_param) {
3413 ret = -ENOMEM;
3414 goto out;
3415 }
3416
3417 data->params[data->n_params++] = saved_param;
3418 }
3419 out:
3420 return ret;
3421}
3422
3423static struct action_data *onmax_parse(char *str)
3424{
3425 char *onmax_fn_name, *onmax_var_str;
3426 struct action_data *data;
3427 int ret = -EINVAL;
3428
3429 data = kzalloc(sizeof(*data), GFP_KERNEL);
3430 if (!data)
3431 return ERR_PTR(-ENOMEM);
3432
3433 onmax_var_str = strsep(&str, ")");
3434 if (!onmax_var_str || !str) {
3435 ret = -EINVAL;
3436 goto free;
3437 }
3438
3439 data->onmax.var_str = kstrdup(onmax_var_str, GFP_KERNEL);
3440 if (!data->onmax.var_str) {
3441 ret = -ENOMEM;
3442 goto free;
3443 }
3444
3445 strsep(&str, ".");
3446 if (!str)
3447 goto free;
3448
3449 onmax_fn_name = strsep(&str, "(");
3450 if (!onmax_fn_name || !str)
3451 goto free;
3452
3453 if (strncmp(onmax_fn_name, "save", strlen("save")) == 0) {
3454 char *params = strsep(&str, ")");
3455
3456 if (!params) {
3457 ret = -EINVAL;
3458 goto free;
3459 }
3460
3461 ret = parse_action_params(params, data);
3462 if (ret)
3463 goto free;
3464 } else
3465 goto free;
3466
3467 data->onmax.fn_name = kstrdup(onmax_fn_name, GFP_KERNEL);
3468 if (!data->onmax.fn_name) {
3469 ret = -ENOMEM;
3470 goto free;
3471 }
3472 out:
3473 return data;
3474 free:
3475 onmax_destroy(data);
3476 data = ERR_PTR(ret);
3477 goto out;
3478}
3479
Tom Zanussic282a382018-01-15 20:52:00 -06003480static void onmatch_destroy(struct action_data *data)
3481{
3482 unsigned int i;
3483
3484 mutex_lock(&synth_event_mutex);
3485
3486 kfree(data->onmatch.match_event);
3487 kfree(data->onmatch.match_event_system);
3488 kfree(data->onmatch.synth_event_name);
3489
3490 for (i = 0; i < data->n_params; i++)
3491 kfree(data->params[i]);
3492
3493 if (data->onmatch.synth_event)
3494 data->onmatch.synth_event->ref--;
3495
3496 kfree(data);
3497
3498 mutex_unlock(&synth_event_mutex);
3499}
3500
Tom Zanussi02205a62018-01-15 20:51:59 -06003501static void destroy_field_var(struct field_var *field_var)
3502{
3503 if (!field_var)
3504 return;
3505
3506 destroy_hist_field(field_var->var, 0);
3507 destroy_hist_field(field_var->val, 0);
3508
3509 kfree(field_var);
3510}
3511
3512static void destroy_field_vars(struct hist_trigger_data *hist_data)
3513{
3514 unsigned int i;
3515
3516 for (i = 0; i < hist_data->n_field_vars; i++)
3517 destroy_field_var(hist_data->field_vars[i]);
3518}
3519
Tom Zanussic282a382018-01-15 20:52:00 -06003520static void save_field_var(struct hist_trigger_data *hist_data,
3521 struct field_var *field_var)
Tom Zanussi02205a62018-01-15 20:51:59 -06003522{
3523 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
3524
3525 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3526 hist_data->n_field_var_str++;
3527}
3528
Tom Zanussic282a382018-01-15 20:52:00 -06003529
3530static void destroy_synth_var_refs(struct hist_trigger_data *hist_data)
3531{
3532 unsigned int i;
3533
3534 for (i = 0; i < hist_data->n_synth_var_refs; i++)
3535 destroy_hist_field(hist_data->synth_var_refs[i], 0);
3536}
3537
3538static void save_synth_var_ref(struct hist_trigger_data *hist_data,
3539 struct hist_field *var_ref)
3540{
3541 hist_data->synth_var_refs[hist_data->n_synth_var_refs++] = var_ref;
3542
3543 hist_data->var_refs[hist_data->n_var_refs] = var_ref;
3544 var_ref->var_ref_idx = hist_data->n_var_refs++;
3545}
3546
3547static int check_synth_field(struct synth_event *event,
3548 struct hist_field *hist_field,
3549 unsigned int field_pos)
3550{
3551 struct synth_field *field;
3552
3553 if (field_pos >= event->n_fields)
3554 return -EINVAL;
3555
3556 field = event->fields[field_pos];
3557
3558 if (strcmp(field->type, hist_field->type) != 0)
3559 return -EINVAL;
3560
3561 return 0;
3562}
3563
Tom Zanussic282a382018-01-15 20:52:00 -06003564static struct hist_field *
3565onmatch_find_var(struct hist_trigger_data *hist_data, struct action_data *data,
3566 char *system, char *event, char *var)
3567{
3568 struct hist_field *hist_field;
3569
3570 var++; /* skip '$' */
3571
3572 hist_field = find_target_event_var(hist_data, system, event, var);
3573 if (!hist_field) {
3574 if (!system) {
3575 system = data->onmatch.match_event_system;
3576 event = data->onmatch.match_event;
3577 }
3578
3579 hist_field = find_event_var(hist_data, system, event, var);
3580 }
3581
Tom Zanussif404da62018-01-15 20:52:05 -06003582 if (!hist_field)
3583 hist_err_event("onmatch: Couldn't find onmatch param: $", system, event, var);
3584
Tom Zanussic282a382018-01-15 20:52:00 -06003585 return hist_field;
3586}
3587
3588static struct hist_field *
3589onmatch_create_field_var(struct hist_trigger_data *hist_data,
3590 struct action_data *data, char *system,
3591 char *event, char *var)
3592{
3593 struct hist_field *hist_field = NULL;
3594 struct field_var *field_var;
3595
3596 /*
3597 * First try to create a field var on the target event (the
3598 * currently being defined). This will create a variable for
3599 * unqualified fields on the target event, or if qualified,
3600 * target fields that have qualified names matching the target.
3601 */
3602 field_var = create_target_field_var(hist_data, system, event, var);
3603
3604 if (field_var && !IS_ERR(field_var)) {
3605 save_field_var(hist_data, field_var);
3606 hist_field = field_var->var;
3607 } else {
3608 field_var = NULL;
3609 /*
3610 * If no explicit system.event is specfied, default to
3611 * looking for fields on the onmatch(system.event.xxx)
3612 * event.
3613 */
3614 if (!system) {
3615 system = data->onmatch.match_event_system;
3616 event = data->onmatch.match_event;
3617 }
3618
3619 /*
3620 * At this point, we're looking at a field on another
3621 * event. Because we can't modify a hist trigger on
3622 * another event to add a variable for a field, we need
3623 * to create a new trigger on that event and create the
3624 * variable at the same time.
3625 */
3626 hist_field = create_field_var_hist(hist_data, system, event, var);
3627 if (IS_ERR(hist_field))
3628 goto free;
3629 }
3630 out:
3631 return hist_field;
3632 free:
3633 destroy_field_var(field_var);
3634 hist_field = NULL;
3635 goto out;
3636}
3637
3638static int onmatch_create(struct hist_trigger_data *hist_data,
3639 struct trace_event_file *file,
3640 struct action_data *data)
3641{
3642 char *event_name, *param, *system = NULL;
3643 struct hist_field *hist_field, *var_ref;
3644 unsigned int i, var_ref_idx;
3645 unsigned int field_pos = 0;
3646 struct synth_event *event;
3647 int ret = 0;
3648
3649 mutex_lock(&synth_event_mutex);
3650 event = find_synth_event(data->onmatch.synth_event_name);
3651 if (!event) {
Tom Zanussif404da62018-01-15 20:52:05 -06003652 hist_err("onmatch: Couldn't find synthetic event: ", data->onmatch.synth_event_name);
Tom Zanussic282a382018-01-15 20:52:00 -06003653 mutex_unlock(&synth_event_mutex);
3654 return -EINVAL;
3655 }
3656 event->ref++;
3657 mutex_unlock(&synth_event_mutex);
3658
3659 var_ref_idx = hist_data->n_var_refs;
3660
3661 for (i = 0; i < data->n_params; i++) {
3662 char *p;
3663
3664 p = param = kstrdup(data->params[i], GFP_KERNEL);
3665 if (!param) {
3666 ret = -ENOMEM;
3667 goto err;
3668 }
3669
3670 system = strsep(&param, ".");
3671 if (!param) {
3672 param = (char *)system;
3673 system = event_name = NULL;
3674 } else {
3675 event_name = strsep(&param, ".");
3676 if (!param) {
3677 kfree(p);
3678 ret = -EINVAL;
3679 goto err;
3680 }
3681 }
3682
3683 if (param[0] == '$')
3684 hist_field = onmatch_find_var(hist_data, data, system,
3685 event_name, param);
3686 else
3687 hist_field = onmatch_create_field_var(hist_data, data,
3688 system,
3689 event_name,
3690 param);
3691
3692 if (!hist_field) {
3693 kfree(p);
3694 ret = -EINVAL;
3695 goto err;
3696 }
3697
3698 if (check_synth_field(event, hist_field, field_pos) == 0) {
3699 var_ref = create_var_ref(hist_field, system, event_name);
3700 if (!var_ref) {
3701 kfree(p);
3702 ret = -ENOMEM;
3703 goto err;
3704 }
3705
3706 save_synth_var_ref(hist_data, var_ref);
3707 field_pos++;
3708 kfree(p);
3709 continue;
3710 }
3711
Tom Zanussif404da62018-01-15 20:52:05 -06003712 hist_err_event("onmatch: Param type doesn't match synthetic event field type: ",
3713 system, event_name, param);
Tom Zanussic282a382018-01-15 20:52:00 -06003714 kfree(p);
3715 ret = -EINVAL;
3716 goto err;
3717 }
3718
3719 if (field_pos != event->n_fields) {
Tom Zanussif404da62018-01-15 20:52:05 -06003720 hist_err("onmatch: Param count doesn't match synthetic event field count: ", event->name);
Tom Zanussic282a382018-01-15 20:52:00 -06003721 ret = -EINVAL;
3722 goto err;
3723 }
3724
3725 data->fn = action_trace;
3726 data->onmatch.synth_event = event;
3727 data->onmatch.var_ref_idx = var_ref_idx;
3728 out:
3729 return ret;
3730 err:
3731 mutex_lock(&synth_event_mutex);
3732 event->ref--;
3733 mutex_unlock(&synth_event_mutex);
3734
3735 goto out;
3736}
3737
3738static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
3739{
3740 char *match_event, *match_event_system;
3741 char *synth_event_name, *params;
3742 struct action_data *data;
3743 int ret = -EINVAL;
3744
3745 data = kzalloc(sizeof(*data), GFP_KERNEL);
3746 if (!data)
3747 return ERR_PTR(-ENOMEM);
3748
3749 match_event = strsep(&str, ")");
Tom Zanussif404da62018-01-15 20:52:05 -06003750 if (!match_event || !str) {
3751 hist_err("onmatch: Missing closing paren: ", match_event);
Tom Zanussic282a382018-01-15 20:52:00 -06003752 goto free;
Tom Zanussif404da62018-01-15 20:52:05 -06003753 }
Tom Zanussic282a382018-01-15 20:52:00 -06003754
3755 match_event_system = strsep(&match_event, ".");
Tom Zanussif404da62018-01-15 20:52:05 -06003756 if (!match_event) {
3757 hist_err("onmatch: Missing subsystem for match event: ", match_event_system);
Tom Zanussic282a382018-01-15 20:52:00 -06003758 goto free;
Tom Zanussif404da62018-01-15 20:52:05 -06003759 }
Tom Zanussic282a382018-01-15 20:52:00 -06003760
Tom Zanussif404da62018-01-15 20:52:05 -06003761 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
3762 hist_err_event("onmatch: Invalid subsystem or event name: ",
3763 match_event_system, match_event, NULL);
Tom Zanussic282a382018-01-15 20:52:00 -06003764 goto free;
Tom Zanussif404da62018-01-15 20:52:05 -06003765 }
Tom Zanussic282a382018-01-15 20:52:00 -06003766
3767 data->onmatch.match_event = kstrdup(match_event, GFP_KERNEL);
3768 if (!data->onmatch.match_event) {
3769 ret = -ENOMEM;
3770 goto free;
3771 }
3772
3773 data->onmatch.match_event_system = kstrdup(match_event_system, GFP_KERNEL);
3774 if (!data->onmatch.match_event_system) {
3775 ret = -ENOMEM;
3776 goto free;
3777 }
3778
3779 strsep(&str, ".");
Tom Zanussif404da62018-01-15 20:52:05 -06003780 if (!str) {
3781 hist_err("onmatch: Missing . after onmatch(): ", str);
Tom Zanussic282a382018-01-15 20:52:00 -06003782 goto free;
Tom Zanussif404da62018-01-15 20:52:05 -06003783 }
Tom Zanussic282a382018-01-15 20:52:00 -06003784
3785 synth_event_name = strsep(&str, "(");
Tom Zanussif404da62018-01-15 20:52:05 -06003786 if (!synth_event_name || !str) {
3787 hist_err("onmatch: Missing opening paramlist paren: ", synth_event_name);
Tom Zanussic282a382018-01-15 20:52:00 -06003788 goto free;
Tom Zanussif404da62018-01-15 20:52:05 -06003789 }
Tom Zanussic282a382018-01-15 20:52:00 -06003790
3791 data->onmatch.synth_event_name = kstrdup(synth_event_name, GFP_KERNEL);
3792 if (!data->onmatch.synth_event_name) {
3793 ret = -ENOMEM;
3794 goto free;
3795 }
3796
3797 params = strsep(&str, ")");
Tom Zanussif404da62018-01-15 20:52:05 -06003798 if (!params || !str || (str && strlen(str))) {
3799 hist_err("onmatch: Missing closing paramlist paren: ", params);
Tom Zanussic282a382018-01-15 20:52:00 -06003800 goto free;
Tom Zanussif404da62018-01-15 20:52:05 -06003801 }
Tom Zanussic282a382018-01-15 20:52:00 -06003802
3803 ret = parse_action_params(params, data);
3804 if (ret)
3805 goto free;
3806 out:
3807 return data;
3808 free:
3809 onmatch_destroy(data);
3810 data = ERR_PTR(ret);
3811 goto out;
3812}
3813
Tom Zanussi7ef224d2016-03-03 12:54:42 -06003814static int create_hitcount_val(struct hist_trigger_data *hist_data)
3815{
3816 hist_data->fields[HITCOUNT_IDX] =
Tom Zanussi30350d62018-01-15 20:51:49 -06003817 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06003818 if (!hist_data->fields[HITCOUNT_IDX])
3819 return -ENOMEM;
3820
3821 hist_data->n_vals++;
Tom Zanussi30350d62018-01-15 20:51:49 -06003822 hist_data->n_fields++;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06003823
3824 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
3825 return -EINVAL;
3826
3827 return 0;
3828}
3829
Tom Zanussi30350d62018-01-15 20:51:49 -06003830static int __create_val_field(struct hist_trigger_data *hist_data,
3831 unsigned int val_idx,
3832 struct trace_event_file *file,
3833 char *var_name, char *field_str,
3834 unsigned long flags)
Tom Zanussif2606832016-03-03 12:54:43 -06003835{
Tom Zanussi100719d2018-01-15 20:51:52 -06003836 struct hist_field *hist_field;
Tom Zanussif2606832016-03-03 12:54:43 -06003837 int ret = 0;
3838
Tom Zanussi100719d2018-01-15 20:51:52 -06003839 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
3840 if (IS_ERR(hist_field)) {
3841 ret = PTR_ERR(hist_field);
Tom Zanussif2606832016-03-03 12:54:43 -06003842 goto out;
3843 }
3844
Tom Zanussi100719d2018-01-15 20:51:52 -06003845 hist_data->fields[val_idx] = hist_field;
3846
Tom Zanussif2606832016-03-03 12:54:43 -06003847 ++hist_data->n_vals;
Tom Zanussi30350d62018-01-15 20:51:49 -06003848 ++hist_data->n_fields;
Tom Zanussif2606832016-03-03 12:54:43 -06003849
Tom Zanussi30350d62018-01-15 20:51:49 -06003850 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
Tom Zanussif2606832016-03-03 12:54:43 -06003851 ret = -EINVAL;
3852 out:
3853 return ret;
3854}
3855
Tom Zanussi30350d62018-01-15 20:51:49 -06003856static int create_val_field(struct hist_trigger_data *hist_data,
3857 unsigned int val_idx,
3858 struct trace_event_file *file,
3859 char *field_str)
3860{
3861 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
3862 return -EINVAL;
3863
3864 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
3865}
3866
3867static int create_var_field(struct hist_trigger_data *hist_data,
3868 unsigned int val_idx,
3869 struct trace_event_file *file,
3870 char *var_name, char *expr_str)
3871{
3872 unsigned long flags = 0;
3873
3874 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
3875 return -EINVAL;
Tom Zanussif404da62018-01-15 20:52:05 -06003876
Tom Zanussi30350d62018-01-15 20:51:49 -06003877 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
Tom Zanussif404da62018-01-15 20:52:05 -06003878 hist_err("Variable already defined: ", var_name);
Tom Zanussi30350d62018-01-15 20:51:49 -06003879 return -EINVAL;
3880 }
3881
3882 flags |= HIST_FIELD_FL_VAR;
3883 hist_data->n_vars++;
3884 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
3885 return -EINVAL;
3886
3887 return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
3888}
3889
Tom Zanussi7ef224d2016-03-03 12:54:42 -06003890static int create_val_fields(struct hist_trigger_data *hist_data,
3891 struct trace_event_file *file)
3892{
Tom Zanussif2606832016-03-03 12:54:43 -06003893 char *fields_str, *field_str;
Tom Zanussi30350d62018-01-15 20:51:49 -06003894 unsigned int i, j = 1;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06003895 int ret;
3896
3897 ret = create_hitcount_val(hist_data);
Tom Zanussif2606832016-03-03 12:54:43 -06003898 if (ret)
3899 goto out;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06003900
Tom Zanussif2606832016-03-03 12:54:43 -06003901 fields_str = hist_data->attrs->vals_str;
3902 if (!fields_str)
3903 goto out;
3904
3905 strsep(&fields_str, "=");
3906 if (!fields_str)
3907 goto out;
3908
3909 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
3910 j < TRACING_MAP_VALS_MAX; i++) {
3911 field_str = strsep(&fields_str, ",");
3912 if (!field_str)
3913 break;
Tom Zanussi30350d62018-01-15 20:51:49 -06003914
Tom Zanussif2606832016-03-03 12:54:43 -06003915 if (strcmp(field_str, "hitcount") == 0)
3916 continue;
Tom Zanussi30350d62018-01-15 20:51:49 -06003917
Tom Zanussif2606832016-03-03 12:54:43 -06003918 ret = create_val_field(hist_data, j++, file, field_str);
3919 if (ret)
3920 goto out;
3921 }
Tom Zanussi30350d62018-01-15 20:51:49 -06003922
Tom Zanussif2606832016-03-03 12:54:43 -06003923 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
3924 ret = -EINVAL;
3925 out:
Tom Zanussi7ef224d2016-03-03 12:54:42 -06003926 return ret;
3927}
3928
3929static int create_key_field(struct hist_trigger_data *hist_data,
3930 unsigned int key_idx,
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06003931 unsigned int key_offset,
Tom Zanussi7ef224d2016-03-03 12:54:42 -06003932 struct trace_event_file *file,
3933 char *field_str)
3934{
Tom Zanussi30350d62018-01-15 20:51:49 -06003935 struct hist_field *hist_field = NULL;
Tom Zanussi100719d2018-01-15 20:51:52 -06003936
Tom Zanussi7ef224d2016-03-03 12:54:42 -06003937 unsigned long flags = 0;
3938 unsigned int key_size;
3939 int ret = 0;
3940
Tom Zanussi30350d62018-01-15 20:51:49 -06003941 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
Tom Zanussi7ef224d2016-03-03 12:54:42 -06003942 return -EINVAL;
3943
3944 flags |= HIST_FIELD_FL_KEY;
3945
Tom Zanussi69a02002016-03-03 12:54:52 -06003946 if (strcmp(field_str, "stacktrace") == 0) {
3947 flags |= HIST_FIELD_FL_STACKTRACE;
3948 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
Tom Zanussi30350d62018-01-15 20:51:49 -06003949 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
Tom Zanussi69a02002016-03-03 12:54:52 -06003950 } else {
Tom Zanussi100719d2018-01-15 20:51:52 -06003951 hist_field = parse_expr(hist_data, file, field_str, flags,
3952 NULL, 0);
3953 if (IS_ERR(hist_field)) {
3954 ret = PTR_ERR(hist_field);
3955 goto out;
Tom Zanussi69a02002016-03-03 12:54:52 -06003956 }
3957
Tom Zanussi067fe032018-01-15 20:51:56 -06003958 if (hist_field->flags & HIST_FIELD_FL_VAR_REF) {
Tom Zanussif404da62018-01-15 20:52:05 -06003959 hist_err("Using variable references as keys not supported: ", field_str);
Tom Zanussi067fe032018-01-15 20:51:56 -06003960 destroy_hist_field(hist_field, 0);
3961 ret = -EINVAL;
3962 goto out;
3963 }
3964
Tom Zanussi100719d2018-01-15 20:51:52 -06003965 key_size = hist_field->size;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06003966 }
3967
Tom Zanussi100719d2018-01-15 20:51:52 -06003968 hist_data->fields[key_idx] = hist_field;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06003969
3970 key_size = ALIGN(key_size, sizeof(u64));
3971 hist_data->fields[key_idx]->size = key_size;
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06003972 hist_data->fields[key_idx]->offset = key_offset;
Tom Zanussi100719d2018-01-15 20:51:52 -06003973
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06003974 hist_data->key_size += key_size;
Tom Zanussi100719d2018-01-15 20:51:52 -06003975
Tom Zanussi7ef224d2016-03-03 12:54:42 -06003976 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
3977 ret = -EINVAL;
3978 goto out;
3979 }
3980
3981 hist_data->n_keys++;
Tom Zanussi30350d62018-01-15 20:51:49 -06003982 hist_data->n_fields++;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06003983
3984 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
3985 return -EINVAL;
3986
3987 ret = key_size;
3988 out:
3989 return ret;
3990}
3991
3992static int create_key_fields(struct hist_trigger_data *hist_data,
3993 struct trace_event_file *file)
3994{
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06003995 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06003996 char *fields_str, *field_str;
3997 int ret = -EINVAL;
3998
3999 fields_str = hist_data->attrs->keys_str;
4000 if (!fields_str)
4001 goto out;
4002
4003 strsep(&fields_str, "=");
4004 if (!fields_str)
4005 goto out;
4006
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004007 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004008 field_str = strsep(&fields_str, ",");
4009 if (!field_str)
4010 break;
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004011 ret = create_key_field(hist_data, i, key_offset,
4012 file, field_str);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004013 if (ret < 0)
4014 goto out;
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004015 key_offset += ret;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004016 }
4017 if (fields_str) {
4018 ret = -EINVAL;
4019 goto out;
4020 }
4021 ret = 0;
4022 out:
4023 return ret;
4024}
4025
Tom Zanussi30350d62018-01-15 20:51:49 -06004026static int create_var_fields(struct hist_trigger_data *hist_data,
4027 struct trace_event_file *file)
4028{
4029 unsigned int i, j = hist_data->n_vals;
4030 int ret = 0;
4031
4032 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
4033
4034 for (i = 0; i < n_vars; i++) {
4035 char *var_name = hist_data->attrs->var_defs.name[i];
4036 char *expr = hist_data->attrs->var_defs.expr[i];
4037
4038 ret = create_var_field(hist_data, j++, file, var_name, expr);
4039 if (ret)
4040 goto out;
4041 }
4042 out:
4043 return ret;
4044}
4045
4046static void free_var_defs(struct hist_trigger_data *hist_data)
4047{
4048 unsigned int i;
4049
4050 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
4051 kfree(hist_data->attrs->var_defs.name[i]);
4052 kfree(hist_data->attrs->var_defs.expr[i]);
4053 }
4054
4055 hist_data->attrs->var_defs.n_vars = 0;
4056}
4057
4058static int parse_var_defs(struct hist_trigger_data *hist_data)
4059{
4060 char *s, *str, *var_name, *field_str;
4061 unsigned int i, j, n_vars = 0;
4062 int ret = 0;
4063
4064 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
4065 str = hist_data->attrs->assignment_str[i];
4066 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
4067 field_str = strsep(&str, ",");
4068 if (!field_str)
4069 break;
4070
4071 var_name = strsep(&field_str, "=");
4072 if (!var_name || !field_str) {
Tom Zanussif404da62018-01-15 20:52:05 -06004073 hist_err("Malformed assignment: ", var_name);
Tom Zanussi30350d62018-01-15 20:51:49 -06004074 ret = -EINVAL;
4075 goto free;
4076 }
4077
4078 if (n_vars == TRACING_MAP_VARS_MAX) {
Tom Zanussif404da62018-01-15 20:52:05 -06004079 hist_err("Too many variables defined: ", var_name);
Tom Zanussi30350d62018-01-15 20:51:49 -06004080 ret = -EINVAL;
4081 goto free;
4082 }
4083
4084 s = kstrdup(var_name, GFP_KERNEL);
4085 if (!s) {
4086 ret = -ENOMEM;
4087 goto free;
4088 }
4089 hist_data->attrs->var_defs.name[n_vars] = s;
4090
4091 s = kstrdup(field_str, GFP_KERNEL);
4092 if (!s) {
4093 kfree(hist_data->attrs->var_defs.name[n_vars]);
4094 ret = -ENOMEM;
4095 goto free;
4096 }
4097 hist_data->attrs->var_defs.expr[n_vars++] = s;
4098
4099 hist_data->attrs->var_defs.n_vars = n_vars;
4100 }
4101 }
4102
4103 return ret;
4104 free:
4105 free_var_defs(hist_data);
4106
4107 return ret;
4108}
4109
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004110static int create_hist_fields(struct hist_trigger_data *hist_data,
4111 struct trace_event_file *file)
4112{
4113 int ret;
4114
Tom Zanussi30350d62018-01-15 20:51:49 -06004115 ret = parse_var_defs(hist_data);
4116 if (ret)
4117 goto out;
4118
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004119 ret = create_val_fields(hist_data, file);
4120 if (ret)
4121 goto out;
4122
Tom Zanussi30350d62018-01-15 20:51:49 -06004123 ret = create_var_fields(hist_data, file);
4124 if (ret)
4125 goto out;
4126
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004127 ret = create_key_fields(hist_data, file);
4128 if (ret)
4129 goto out;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004130 out:
Tom Zanussi30350d62018-01-15 20:51:49 -06004131 free_var_defs(hist_data);
4132
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004133 return ret;
4134}
4135
Tom Zanussie62347d2016-03-03 12:54:45 -06004136static int is_descending(const char *str)
4137{
4138 if (!str)
4139 return 0;
4140
4141 if (strcmp(str, "descending") == 0)
4142 return 1;
4143
4144 if (strcmp(str, "ascending") == 0)
4145 return 0;
4146
4147 return -EINVAL;
4148}
4149
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004150static int create_sort_keys(struct hist_trigger_data *hist_data)
4151{
Tom Zanussie62347d2016-03-03 12:54:45 -06004152 char *fields_str = hist_data->attrs->sort_key_str;
Tom Zanussie62347d2016-03-03 12:54:45 -06004153 struct tracing_map_sort_key *sort_key;
4154 int descending, ret = 0;
Tom Zanussi30350d62018-01-15 20:51:49 -06004155 unsigned int i, j, k;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004156
Tom Zanussie62347d2016-03-03 12:54:45 -06004157 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004158
Tom Zanussie62347d2016-03-03 12:54:45 -06004159 if (!fields_str)
4160 goto out;
4161
4162 strsep(&fields_str, "=");
4163 if (!fields_str) {
4164 ret = -EINVAL;
4165 goto out;
4166 }
4167
4168 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
Tom Zanussi85013252017-09-22 14:58:22 -05004169 struct hist_field *hist_field;
Tom Zanussie62347d2016-03-03 12:54:45 -06004170 char *field_str, *field_name;
Tom Zanussi85013252017-09-22 14:58:22 -05004171 const char *test_name;
Tom Zanussie62347d2016-03-03 12:54:45 -06004172
4173 sort_key = &hist_data->sort_keys[i];
4174
4175 field_str = strsep(&fields_str, ",");
4176 if (!field_str) {
4177 if (i == 0)
4178 ret = -EINVAL;
4179 break;
4180 }
4181
4182 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4183 ret = -EINVAL;
4184 break;
4185 }
4186
4187 field_name = strsep(&field_str, ".");
4188 if (!field_name) {
4189 ret = -EINVAL;
4190 break;
4191 }
4192
4193 if (strcmp(field_name, "hitcount") == 0) {
4194 descending = is_descending(field_str);
4195 if (descending < 0) {
4196 ret = descending;
4197 break;
4198 }
4199 sort_key->descending = descending;
4200 continue;
4201 }
4202
Tom Zanussi30350d62018-01-15 20:51:49 -06004203 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4204 unsigned int idx;
4205
Tom Zanussi85013252017-09-22 14:58:22 -05004206 hist_field = hist_data->fields[j];
Tom Zanussi30350d62018-01-15 20:51:49 -06004207 if (hist_field->flags & HIST_FIELD_FL_VAR)
4208 continue;
4209
4210 idx = k++;
4211
Tom Zanussi85013252017-09-22 14:58:22 -05004212 test_name = hist_field_name(hist_field, 0);
4213
4214 if (strcmp(field_name, test_name) == 0) {
Tom Zanussi30350d62018-01-15 20:51:49 -06004215 sort_key->field_idx = idx;
Tom Zanussie62347d2016-03-03 12:54:45 -06004216 descending = is_descending(field_str);
4217 if (descending < 0) {
4218 ret = descending;
4219 goto out;
4220 }
4221 sort_key->descending = descending;
4222 break;
4223 }
4224 }
4225 if (j == hist_data->n_fields) {
4226 ret = -EINVAL;
4227 break;
4228 }
4229 }
Tom Zanussi30350d62018-01-15 20:51:49 -06004230
Tom Zanussie62347d2016-03-03 12:54:45 -06004231 hist_data->n_sort_keys = i;
4232 out:
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004233 return ret;
4234}
4235
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004236static void destroy_actions(struct hist_trigger_data *hist_data)
4237{
4238 unsigned int i;
4239
4240 for (i = 0; i < hist_data->n_actions; i++) {
4241 struct action_data *data = hist_data->actions[i];
4242
Tom Zanussic282a382018-01-15 20:52:00 -06004243 if (data->fn == action_trace)
4244 onmatch_destroy(data);
Tom Zanussi50450602018-01-15 20:52:01 -06004245 else if (data->fn == onmax_save)
4246 onmax_destroy(data);
Tom Zanussic282a382018-01-15 20:52:00 -06004247 else
4248 kfree(data);
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004249 }
4250}
4251
4252static int parse_actions(struct hist_trigger_data *hist_data)
4253{
Tom Zanussic282a382018-01-15 20:52:00 -06004254 struct trace_array *tr = hist_data->event_file->tr;
4255 struct action_data *data;
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004256 unsigned int i;
4257 int ret = 0;
4258 char *str;
4259
4260 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4261 str = hist_data->attrs->action_str[i];
Tom Zanussic282a382018-01-15 20:52:00 -06004262
4263 if (strncmp(str, "onmatch(", strlen("onmatch(")) == 0) {
4264 char *action_str = str + strlen("onmatch(");
4265
4266 data = onmatch_parse(tr, action_str);
4267 if (IS_ERR(data)) {
4268 ret = PTR_ERR(data);
4269 break;
4270 }
4271 data->fn = action_trace;
Tom Zanussi50450602018-01-15 20:52:01 -06004272 } else if (strncmp(str, "onmax(", strlen("onmax(")) == 0) {
4273 char *action_str = str + strlen("onmax(");
4274
4275 data = onmax_parse(action_str);
4276 if (IS_ERR(data)) {
4277 ret = PTR_ERR(data);
4278 break;
4279 }
4280 data->fn = onmax_save;
Tom Zanussic282a382018-01-15 20:52:00 -06004281 } else {
4282 ret = -EINVAL;
4283 break;
4284 }
4285
4286 hist_data->actions[hist_data->n_actions++] = data;
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004287 }
4288
4289 return ret;
4290}
4291
4292static int create_actions(struct hist_trigger_data *hist_data,
4293 struct trace_event_file *file)
4294{
4295 struct action_data *data;
4296 unsigned int i;
4297 int ret = 0;
4298
4299 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4300 data = hist_data->actions[i];
Tom Zanussic282a382018-01-15 20:52:00 -06004301
4302 if (data->fn == action_trace) {
4303 ret = onmatch_create(hist_data, file, data);
4304 if (ret)
4305 return ret;
Tom Zanussi50450602018-01-15 20:52:01 -06004306 } else if (data->fn == onmax_save) {
4307 ret = onmax_create(hist_data, data);
4308 if (ret)
4309 return ret;
Tom Zanussic282a382018-01-15 20:52:00 -06004310 }
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004311 }
4312
4313 return ret;
4314}
4315
Tom Zanussi50450602018-01-15 20:52:01 -06004316static void print_actions(struct seq_file *m,
4317 struct hist_trigger_data *hist_data,
4318 struct tracing_map_elt *elt)
4319{
4320 unsigned int i;
4321
4322 for (i = 0; i < hist_data->n_actions; i++) {
4323 struct action_data *data = hist_data->actions[i];
4324
4325 if (data->fn == onmax_save)
4326 onmax_print(m, hist_data, elt, data);
4327 }
4328}
4329
4330static void print_onmax_spec(struct seq_file *m,
4331 struct hist_trigger_data *hist_data,
4332 struct action_data *data)
4333{
4334 unsigned int i;
4335
4336 seq_puts(m, ":onmax(");
4337 seq_printf(m, "%s", data->onmax.var_str);
4338 seq_printf(m, ").%s(", data->onmax.fn_name);
4339
4340 for (i = 0; i < hist_data->n_max_vars; i++) {
4341 seq_printf(m, "%s", hist_data->max_vars[i]->var->var.name);
4342 if (i < hist_data->n_max_vars - 1)
4343 seq_puts(m, ",");
4344 }
4345 seq_puts(m, ")");
4346}
4347
Tom Zanussic282a382018-01-15 20:52:00 -06004348static void print_onmatch_spec(struct seq_file *m,
4349 struct hist_trigger_data *hist_data,
4350 struct action_data *data)
4351{
4352 unsigned int i;
4353
4354 seq_printf(m, ":onmatch(%s.%s).", data->onmatch.match_event_system,
4355 data->onmatch.match_event);
4356
4357 seq_printf(m, "%s(", data->onmatch.synth_event->name);
4358
4359 for (i = 0; i < data->n_params; i++) {
4360 if (i)
4361 seq_puts(m, ",");
4362 seq_printf(m, "%s", data->params[i]);
4363 }
4364
4365 seq_puts(m, ")");
4366}
4367
Tom Zanussi48f79472018-03-28 15:10:55 -05004368static bool actions_match(struct hist_trigger_data *hist_data,
4369 struct hist_trigger_data *hist_data_test)
4370{
4371 unsigned int i, j;
4372
4373 if (hist_data->n_actions != hist_data_test->n_actions)
4374 return false;
4375
4376 for (i = 0; i < hist_data->n_actions; i++) {
4377 struct action_data *data = hist_data->actions[i];
4378 struct action_data *data_test = hist_data_test->actions[i];
4379
4380 if (data->fn != data_test->fn)
4381 return false;
4382
4383 if (data->n_params != data_test->n_params)
4384 return false;
4385
4386 for (j = 0; j < data->n_params; j++) {
4387 if (strcmp(data->params[j], data_test->params[j]) != 0)
4388 return false;
4389 }
4390
4391 if (data->fn == action_trace) {
4392 if (strcmp(data->onmatch.synth_event_name,
4393 data_test->onmatch.synth_event_name) != 0)
4394 return false;
4395 if (strcmp(data->onmatch.match_event_system,
4396 data_test->onmatch.match_event_system) != 0)
4397 return false;
4398 if (strcmp(data->onmatch.match_event,
4399 data_test->onmatch.match_event) != 0)
4400 return false;
4401 } else if (data->fn == onmax_save) {
4402 if (strcmp(data->onmax.var_str,
4403 data_test->onmax.var_str) != 0)
4404 return false;
4405 if (strcmp(data->onmax.fn_name,
4406 data_test->onmax.fn_name) != 0)
4407 return false;
4408 }
4409 }
4410
4411 return true;
4412}
4413
4414
Tom Zanussic282a382018-01-15 20:52:00 -06004415static void print_actions_spec(struct seq_file *m,
4416 struct hist_trigger_data *hist_data)
4417{
4418 unsigned int i;
4419
4420 for (i = 0; i < hist_data->n_actions; i++) {
4421 struct action_data *data = hist_data->actions[i];
4422
4423 if (data->fn == action_trace)
4424 print_onmatch_spec(m, hist_data, data);
Tom Zanussi50450602018-01-15 20:52:01 -06004425 else if (data->fn == onmax_save)
4426 print_onmax_spec(m, hist_data, data);
Tom Zanussic282a382018-01-15 20:52:00 -06004427 }
4428}
4429
Tom Zanussi02205a62018-01-15 20:51:59 -06004430static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
4431{
4432 unsigned int i;
4433
4434 for (i = 0; i < hist_data->n_field_var_hists; i++) {
4435 kfree(hist_data->field_var_hists[i]->cmd);
4436 kfree(hist_data->field_var_hists[i]);
4437 }
4438}
4439
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004440static void destroy_hist_data(struct hist_trigger_data *hist_data)
4441{
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004442 if (!hist_data)
4443 return;
4444
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004445 destroy_hist_trigger_attrs(hist_data->attrs);
4446 destroy_hist_fields(hist_data);
4447 tracing_map_destroy(hist_data->map);
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004448
4449 destroy_actions(hist_data);
Tom Zanussi02205a62018-01-15 20:51:59 -06004450 destroy_field_vars(hist_data);
4451 destroy_field_var_hists(hist_data);
Tom Zanussic282a382018-01-15 20:52:00 -06004452 destroy_synth_var_refs(hist_data);
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004453
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004454 kfree(hist_data);
4455}
4456
4457static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
4458{
4459 struct tracing_map *map = hist_data->map;
4460 struct ftrace_event_field *field;
4461 struct hist_field *hist_field;
Dan Carpenterb28d7b22018-03-28 14:48:15 +03004462 int i, idx = 0;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004463
4464 for_each_hist_field(i, hist_data) {
4465 hist_field = hist_data->fields[i];
4466 if (hist_field->flags & HIST_FIELD_FL_KEY) {
4467 tracing_map_cmp_fn_t cmp_fn;
4468
4469 field = hist_field->field;
4470
Tom Zanussi69a02002016-03-03 12:54:52 -06004471 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
4472 cmp_fn = tracing_map_cmp_none;
Tom Zanussiad42feb2018-01-15 20:51:45 -06004473 else if (!field)
4474 cmp_fn = tracing_map_cmp_num(hist_field->size,
4475 hist_field->is_signed);
Tom Zanussi69a02002016-03-03 12:54:52 -06004476 else if (is_string_field(field))
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004477 cmp_fn = tracing_map_cmp_string;
4478 else
4479 cmp_fn = tracing_map_cmp_num(field->size,
4480 field->is_signed);
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004481 idx = tracing_map_add_key_field(map,
4482 hist_field->offset,
4483 cmp_fn);
Tom Zanussi30350d62018-01-15 20:51:49 -06004484 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004485 idx = tracing_map_add_sum_field(map);
4486
4487 if (idx < 0)
4488 return idx;
Tom Zanussi30350d62018-01-15 20:51:49 -06004489
4490 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4491 idx = tracing_map_add_var(map);
4492 if (idx < 0)
4493 return idx;
4494 hist_field->var.idx = idx;
4495 hist_field->var.hist_data = hist_data;
4496 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004497 }
4498
4499 return 0;
4500}
4501
4502static struct hist_trigger_data *
4503create_hist_data(unsigned int map_bits,
4504 struct hist_trigger_attrs *attrs,
Tom Zanussi30350d62018-01-15 20:51:49 -06004505 struct trace_event_file *file,
4506 bool remove)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004507{
Tom Zanussi6b4827a2016-03-03 12:54:50 -06004508 const struct tracing_map_ops *map_ops = NULL;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004509 struct hist_trigger_data *hist_data;
4510 int ret = 0;
4511
4512 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
4513 if (!hist_data)
4514 return ERR_PTR(-ENOMEM);
4515
4516 hist_data->attrs = attrs;
Tom Zanussi30350d62018-01-15 20:51:49 -06004517 hist_data->remove = remove;
Tom Zanussi067fe032018-01-15 20:51:56 -06004518 hist_data->event_file = file;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004519
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004520 ret = parse_actions(hist_data);
4521 if (ret)
4522 goto free;
4523
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004524 ret = create_hist_fields(hist_data, file);
4525 if (ret)
4526 goto free;
4527
4528 ret = create_sort_keys(hist_data);
4529 if (ret)
4530 goto free;
4531
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06004532 map_ops = &hist_trigger_elt_data_ops;
Tom Zanussi6b4827a2016-03-03 12:54:50 -06004533
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004534 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
Tom Zanussi6b4827a2016-03-03 12:54:50 -06004535 map_ops, hist_data);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004536 if (IS_ERR(hist_data->map)) {
4537 ret = PTR_ERR(hist_data->map);
4538 hist_data->map = NULL;
4539 goto free;
4540 }
4541
4542 ret = create_tracing_map_fields(hist_data);
4543 if (ret)
4544 goto free;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004545 out:
4546 return hist_data;
4547 free:
4548 hist_data->attrs = NULL;
4549
4550 destroy_hist_data(hist_data);
4551
4552 hist_data = ERR_PTR(ret);
4553
4554 goto out;
4555}
4556
4557static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
Tom Zanussifbd302c2018-01-15 20:51:43 -06004558 struct tracing_map_elt *elt, void *rec,
Tom Zanussi067fe032018-01-15 20:51:56 -06004559 struct ring_buffer_event *rbe,
4560 u64 *var_ref_vals)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004561{
Tom Zanussi067fe032018-01-15 20:51:56 -06004562 struct hist_elt_data *elt_data;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004563 struct hist_field *hist_field;
Tom Zanussi30350d62018-01-15 20:51:49 -06004564 unsigned int i, var_idx;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004565 u64 hist_val;
4566
Tom Zanussi067fe032018-01-15 20:51:56 -06004567 elt_data = elt->private_data;
4568 elt_data->var_ref_vals = var_ref_vals;
4569
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004570 for_each_hist_val_field(i, hist_data) {
4571 hist_field = hist_data->fields[i];
Tom Zanussidf35d932018-01-15 20:51:54 -06004572 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
Tom Zanussi30350d62018-01-15 20:51:49 -06004573 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4574 var_idx = hist_field->var.idx;
4575 tracing_map_set_var(elt, var_idx, hist_val);
4576 continue;
4577 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004578 tracing_map_update_sum(elt, i, hist_val);
4579 }
Tom Zanussi30350d62018-01-15 20:51:49 -06004580
4581 for_each_hist_key_field(i, hist_data) {
4582 hist_field = hist_data->fields[i];
4583 if (hist_field->flags & HIST_FIELD_FL_VAR) {
Tom Zanussidf35d932018-01-15 20:51:54 -06004584 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
Tom Zanussi30350d62018-01-15 20:51:49 -06004585 var_idx = hist_field->var.idx;
4586 tracing_map_set_var(elt, var_idx, hist_val);
4587 }
4588 }
Tom Zanussi02205a62018-01-15 20:51:59 -06004589
4590 update_field_vars(hist_data, elt, rbe, rec);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004591}
4592
Tom Zanussi6a475cb2016-03-03 12:54:54 -06004593static inline void add_to_key(char *compound_key, void *key,
4594 struct hist_field *key_field, void *rec)
4595{
4596 size_t size = key_field->size;
4597
4598 if (key_field->flags & HIST_FIELD_FL_STRING) {
4599 struct ftrace_event_field *field;
4600
4601 field = key_field->field;
4602 if (field->filter_type == FILTER_DYN_STRING)
4603 size = *(u32 *)(rec + field->offset) >> 16;
4604 else if (field->filter_type == FILTER_PTR_STRING)
4605 size = strlen(key);
4606 else if (field->filter_type == FILTER_STATIC_STRING)
4607 size = field->size;
4608
4609 /* ensure NULL-termination */
4610 if (size > key_field->size - 1)
4611 size = key_field->size - 1;
4612 }
4613
4614 memcpy(compound_key + key_field->offset, key, size);
4615}
4616
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004617static void
4618hist_trigger_actions(struct hist_trigger_data *hist_data,
4619 struct tracing_map_elt *elt, void *rec,
4620 struct ring_buffer_event *rbe, u64 *var_ref_vals)
4621{
4622 struct action_data *data;
4623 unsigned int i;
4624
4625 for (i = 0; i < hist_data->n_actions; i++) {
4626 data = hist_data->actions[i];
4627 data->fn(hist_data, elt, rec, rbe, data, var_ref_vals);
4628 }
4629}
4630
Tom Zanussi1ac4f512018-01-15 20:51:42 -06004631static void event_hist_trigger(struct event_trigger_data *data, void *rec,
Tom Zanussifbd302c2018-01-15 20:51:43 -06004632 struct ring_buffer_event *rbe)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004633{
4634 struct hist_trigger_data *hist_data = data->private_data;
Tom Zanussi6a475cb2016-03-03 12:54:54 -06004635 bool use_compound_key = (hist_data->n_keys > 1);
Tom Zanussi69a02002016-03-03 12:54:52 -06004636 unsigned long entries[HIST_STACKTRACE_DEPTH];
Tom Zanussi067fe032018-01-15 20:51:56 -06004637 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004638 char compound_key[HIST_KEY_SIZE_MAX];
Tom Zanussidf35d932018-01-15 20:51:54 -06004639 struct tracing_map_elt *elt = NULL;
Tom Zanussi69a02002016-03-03 12:54:52 -06004640 struct stack_trace stacktrace;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004641 struct hist_field *key_field;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004642 u64 field_contents;
4643 void *key = NULL;
4644 unsigned int i;
4645
Tom Zanussi6a475cb2016-03-03 12:54:54 -06004646 memset(compound_key, 0, hist_data->key_size);
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004647
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004648 for_each_hist_key_field(i, hist_data) {
4649 key_field = hist_data->fields[i];
4650
Tom Zanussi69a02002016-03-03 12:54:52 -06004651 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4652 stacktrace.max_entries = HIST_STACKTRACE_DEPTH;
4653 stacktrace.entries = entries;
4654 stacktrace.nr_entries = 0;
4655 stacktrace.skip = HIST_STACKTRACE_SKIP;
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004656
Tom Zanussi69a02002016-03-03 12:54:52 -06004657 memset(stacktrace.entries, 0, HIST_STACKTRACE_SIZE);
4658 save_stack_trace(&stacktrace);
4659
4660 key = entries;
4661 } else {
Tom Zanussidf35d932018-01-15 20:51:54 -06004662 field_contents = key_field->fn(key_field, elt, rbe, rec);
Tom Zanussi6a475cb2016-03-03 12:54:54 -06004663 if (key_field->flags & HIST_FIELD_FL_STRING) {
Tom Zanussi69a02002016-03-03 12:54:52 -06004664 key = (void *)(unsigned long)field_contents;
Tom Zanussi6a475cb2016-03-03 12:54:54 -06004665 use_compound_key = true;
4666 } else
Tom Zanussi69a02002016-03-03 12:54:52 -06004667 key = (void *)&field_contents;
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004668 }
Tom Zanussi6a475cb2016-03-03 12:54:54 -06004669
4670 if (use_compound_key)
4671 add_to_key(compound_key, key, key_field, rec);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004672 }
4673
Tom Zanussi6a475cb2016-03-03 12:54:54 -06004674 if (use_compound_key)
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004675 key = compound_key;
4676
Tom Zanussi067fe032018-01-15 20:51:56 -06004677 if (hist_data->n_var_refs &&
4678 !resolve_var_refs(hist_data, key, var_ref_vals, false))
4679 return;
4680
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004681 elt = tracing_map_insert(hist_data->map, key);
Tom Zanussi067fe032018-01-15 20:51:56 -06004682 if (!elt)
4683 return;
4684
4685 hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals);
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004686
4687 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
4688 hist_trigger_actions(hist_data, elt, rec, rbe, var_ref_vals);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004689}
4690
Tom Zanussi69a02002016-03-03 12:54:52 -06004691static void hist_trigger_stacktrace_print(struct seq_file *m,
4692 unsigned long *stacktrace_entries,
4693 unsigned int max_entries)
4694{
4695 char str[KSYM_SYMBOL_LEN];
4696 unsigned int spaces = 8;
4697 unsigned int i;
4698
4699 for (i = 0; i < max_entries; i++) {
4700 if (stacktrace_entries[i] == ULONG_MAX)
4701 return;
4702
4703 seq_printf(m, "%*c", 1 + spaces, ' ');
4704 sprint_symbol(str, stacktrace_entries[i]);
4705 seq_printf(m, "%s\n", str);
4706 }
4707}
4708
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004709static void
4710hist_trigger_entry_print(struct seq_file *m,
4711 struct hist_trigger_data *hist_data, void *key,
4712 struct tracing_map_elt *elt)
4713{
4714 struct hist_field *key_field;
Tom Zanussic6afad42016-03-03 12:54:49 -06004715 char str[KSYM_SYMBOL_LEN];
Tom Zanussi69a02002016-03-03 12:54:52 -06004716 bool multiline = false;
Tom Zanussi85013252017-09-22 14:58:22 -05004717 const char *field_name;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004718 unsigned int i;
4719 u64 uval;
4720
4721 seq_puts(m, "{ ");
4722
4723 for_each_hist_key_field(i, hist_data) {
4724 key_field = hist_data->fields[i];
4725
4726 if (i > hist_data->n_vals)
4727 seq_puts(m, ", ");
4728
Tom Zanussi85013252017-09-22 14:58:22 -05004729 field_name = hist_field_name(key_field, 0);
4730
Tom Zanussi0c4a6b42016-03-03 12:54:48 -06004731 if (key_field->flags & HIST_FIELD_FL_HEX) {
4732 uval = *(u64 *)(key + key_field->offset);
Tom Zanussi85013252017-09-22 14:58:22 -05004733 seq_printf(m, "%s: %llx", field_name, uval);
Tom Zanussic6afad42016-03-03 12:54:49 -06004734 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
4735 uval = *(u64 *)(key + key_field->offset);
4736 sprint_symbol_no_offset(str, uval);
Tom Zanussi85013252017-09-22 14:58:22 -05004737 seq_printf(m, "%s: [%llx] %-45s", field_name,
4738 uval, str);
Tom Zanussic6afad42016-03-03 12:54:49 -06004739 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
4740 uval = *(u64 *)(key + key_field->offset);
4741 sprint_symbol(str, uval);
Tom Zanussi85013252017-09-22 14:58:22 -05004742 seq_printf(m, "%s: [%llx] %-55s", field_name,
4743 uval, str);
Tom Zanussi6b4827a2016-03-03 12:54:50 -06004744 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06004745 struct hist_elt_data *elt_data = elt->private_data;
4746 char *comm;
4747
4748 if (WARN_ON_ONCE(!elt_data))
4749 return;
4750
4751 comm = elt_data->comm;
Tom Zanussi6b4827a2016-03-03 12:54:50 -06004752
4753 uval = *(u64 *)(key + key_field->offset);
Tom Zanussi85013252017-09-22 14:58:22 -05004754 seq_printf(m, "%s: %-16s[%10llu]", field_name,
4755 comm, uval);
Tom Zanussi31696192016-03-03 12:54:51 -06004756 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
4757 const char *syscall_name;
4758
4759 uval = *(u64 *)(key + key_field->offset);
4760 syscall_name = get_syscall_name(uval);
4761 if (!syscall_name)
4762 syscall_name = "unknown_syscall";
4763
Tom Zanussi85013252017-09-22 14:58:22 -05004764 seq_printf(m, "%s: %-30s[%3llu]", field_name,
4765 syscall_name, uval);
Tom Zanussi69a02002016-03-03 12:54:52 -06004766 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
4767 seq_puts(m, "stacktrace:\n");
4768 hist_trigger_stacktrace_print(m,
4769 key + key_field->offset,
4770 HIST_STACKTRACE_DEPTH);
4771 multiline = true;
Namhyung Kim4b94f5b2016-03-03 12:55:02 -06004772 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
Tom Zanussi85013252017-09-22 14:58:22 -05004773 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
Namhyung Kim4b94f5b2016-03-03 12:55:02 -06004774 *(u64 *)(key + key_field->offset));
Tom Zanussi0c4a6b42016-03-03 12:54:48 -06004775 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
Tom Zanussi85013252017-09-22 14:58:22 -05004776 seq_printf(m, "%s: %-50s", field_name,
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004777 (char *)(key + key_field->offset));
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004778 } else {
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004779 uval = *(u64 *)(key + key_field->offset);
Tom Zanussi85013252017-09-22 14:58:22 -05004780 seq_printf(m, "%s: %10llu", field_name, uval);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004781 }
4782 }
4783
Tom Zanussi69a02002016-03-03 12:54:52 -06004784 if (!multiline)
4785 seq_puts(m, " ");
4786
4787 seq_puts(m, "}");
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004788
4789 seq_printf(m, " hitcount: %10llu",
4790 tracing_map_read_sum(elt, HITCOUNT_IDX));
4791
Tom Zanussif2606832016-03-03 12:54:43 -06004792 for (i = 1; i < hist_data->n_vals; i++) {
Tom Zanussi85013252017-09-22 14:58:22 -05004793 field_name = hist_field_name(hist_data->fields[i], 0);
4794
Tom Zanussi100719d2018-01-15 20:51:52 -06004795 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
4796 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
Tom Zanussi30350d62018-01-15 20:51:49 -06004797 continue;
4798
Tom Zanussi0c4a6b42016-03-03 12:54:48 -06004799 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
Tom Zanussi85013252017-09-22 14:58:22 -05004800 seq_printf(m, " %s: %10llx", field_name,
Tom Zanussi0c4a6b42016-03-03 12:54:48 -06004801 tracing_map_read_sum(elt, i));
4802 } else {
Tom Zanussi85013252017-09-22 14:58:22 -05004803 seq_printf(m, " %s: %10llu", field_name,
Tom Zanussi0c4a6b42016-03-03 12:54:48 -06004804 tracing_map_read_sum(elt, i));
4805 }
Tom Zanussif2606832016-03-03 12:54:43 -06004806 }
4807
Tom Zanussi50450602018-01-15 20:52:01 -06004808 print_actions(m, hist_data, elt);
4809
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004810 seq_puts(m, "\n");
4811}
4812
4813static int print_entries(struct seq_file *m,
4814 struct hist_trigger_data *hist_data)
4815{
4816 struct tracing_map_sort_entry **sort_entries = NULL;
4817 struct tracing_map *map = hist_data->map;
Steven Rostedt (Red Hat)d50c7442016-03-08 17:17:15 -05004818 int i, n_entries;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004819
4820 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
4821 hist_data->n_sort_keys,
4822 &sort_entries);
4823 if (n_entries < 0)
4824 return n_entries;
4825
4826 for (i = 0; i < n_entries; i++)
4827 hist_trigger_entry_print(m, hist_data,
4828 sort_entries[i]->key,
4829 sort_entries[i]->elt);
4830
4831 tracing_map_destroy_sort_entries(sort_entries, n_entries);
4832
4833 return n_entries;
4834}
4835
Tom Zanussi52a7f162016-03-03 12:54:57 -06004836static void hist_trigger_show(struct seq_file *m,
4837 struct event_trigger_data *data, int n)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004838{
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004839 struct hist_trigger_data *hist_data;
Colin Ian King6e7a2392017-08-23 12:23:09 +01004840 int n_entries;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004841
Tom Zanussi52a7f162016-03-03 12:54:57 -06004842 if (n > 0)
4843 seq_puts(m, "\n\n");
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004844
4845 seq_puts(m, "# event histogram\n#\n# trigger info: ");
4846 data->ops->print(m, data->ops, data);
Tom Zanussi52a7f162016-03-03 12:54:57 -06004847 seq_puts(m, "#\n\n");
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004848
4849 hist_data = data->private_data;
4850 n_entries = print_entries(m, hist_data);
Colin Ian King6e7a2392017-08-23 12:23:09 +01004851 if (n_entries < 0)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004852 n_entries = 0;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004853
4854 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
4855 (u64)atomic64_read(&hist_data->map->hits),
4856 n_entries, (u64)atomic64_read(&hist_data->map->drops));
Tom Zanussi52a7f162016-03-03 12:54:57 -06004857}
4858
4859static int hist_show(struct seq_file *m, void *v)
4860{
4861 struct event_trigger_data *data;
4862 struct trace_event_file *event_file;
4863 int n = 0, ret = 0;
4864
4865 mutex_lock(&event_mutex);
4866
4867 event_file = event_file_data(m->private);
4868 if (unlikely(!event_file)) {
4869 ret = -ENODEV;
4870 goto out_unlock;
4871 }
4872
4873 list_for_each_entry_rcu(data, &event_file->triggers, list) {
4874 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
4875 hist_trigger_show(m, data, n++);
4876 }
4877
Tom Zanussif404da62018-01-15 20:52:05 -06004878 if (have_hist_err()) {
4879 seq_printf(m, "\nERROR: %s\n", hist_err_str);
4880 seq_printf(m, " Last command: %s\n", last_hist_cmd);
4881 }
4882
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004883 out_unlock:
4884 mutex_unlock(&event_mutex);
4885
4886 return ret;
4887}
4888
4889static int event_hist_open(struct inode *inode, struct file *file)
4890{
4891 return single_open(file, hist_show, file);
4892}
4893
4894const struct file_operations event_hist_fops = {
4895 .open = event_hist_open,
4896 .read = seq_read,
4897 .llseek = seq_lseek,
4898 .release = single_release,
4899};
4900
4901static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
4902{
Tom Zanussi85013252017-09-22 14:58:22 -05004903 const char *field_name = hist_field_name(hist_field, 0);
4904
Tom Zanussi30350d62018-01-15 20:51:49 -06004905 if (hist_field->var.name)
4906 seq_printf(m, "%s=", hist_field->var.name);
4907
Tom Zanussi0ae79612018-03-28 15:10:53 -05004908 if (hist_field->flags & HIST_FIELD_FL_CPU)
Tom Zanussi8b7622b2018-01-15 20:52:03 -06004909 seq_puts(m, "cpu");
Tom Zanussi067fe032018-01-15 20:51:56 -06004910 else if (field_name) {
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06004911 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
4912 hist_field->flags & HIST_FIELD_FL_ALIAS)
Tom Zanussi067fe032018-01-15 20:51:56 -06004913 seq_putc(m, '$');
Tom Zanussiad42feb2018-01-15 20:51:45 -06004914 seq_printf(m, "%s", field_name);
Tom Zanussi0ae79612018-03-28 15:10:53 -05004915 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
4916 seq_puts(m, "common_timestamp");
Tom Zanussi608940d2018-04-26 20:04:47 -05004917
4918 if (hist_field->flags) {
4919 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
4920 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
4921 const char *flags = get_hist_field_flags(hist_field);
4922
4923 if (flags)
4924 seq_printf(m, ".%s", flags);
4925 }
4926 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004927}
4928
4929static int event_hist_trigger_print(struct seq_file *m,
4930 struct event_trigger_ops *ops,
4931 struct event_trigger_data *data)
4932{
4933 struct hist_trigger_data *hist_data = data->private_data;
Tom Zanussi30350d62018-01-15 20:51:49 -06004934 struct hist_field *field;
4935 bool have_var = false;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004936 unsigned int i;
4937
Tom Zanussi5463bfd2016-03-03 12:54:59 -06004938 seq_puts(m, "hist:");
4939
4940 if (data->name)
4941 seq_printf(m, "%s:", data->name);
4942
4943 seq_puts(m, "keys=");
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004944
4945 for_each_hist_key_field(i, hist_data) {
Tom Zanussi30350d62018-01-15 20:51:49 -06004946 field = hist_data->fields[i];
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004947
4948 if (i > hist_data->n_vals)
4949 seq_puts(m, ",");
4950
Tom Zanussi30350d62018-01-15 20:51:49 -06004951 if (field->flags & HIST_FIELD_FL_STACKTRACE)
Tom Zanussi69a02002016-03-03 12:54:52 -06004952 seq_puts(m, "stacktrace");
4953 else
Tom Zanussi30350d62018-01-15 20:51:49 -06004954 hist_field_print(m, field);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004955 }
4956
4957 seq_puts(m, ":vals=");
Tom Zanussif2606832016-03-03 12:54:43 -06004958
4959 for_each_hist_val_field(i, hist_data) {
Tom Zanussi30350d62018-01-15 20:51:49 -06004960 field = hist_data->fields[i];
4961 if (field->flags & HIST_FIELD_FL_VAR) {
4962 have_var = true;
4963 continue;
4964 }
4965
Tom Zanussif2606832016-03-03 12:54:43 -06004966 if (i == HITCOUNT_IDX)
4967 seq_puts(m, "hitcount");
4968 else {
4969 seq_puts(m, ",");
Tom Zanussi30350d62018-01-15 20:51:49 -06004970 hist_field_print(m, field);
4971 }
4972 }
4973
4974 if (have_var) {
4975 unsigned int n = 0;
4976
4977 seq_puts(m, ":");
4978
4979 for_each_hist_val_field(i, hist_data) {
4980 field = hist_data->fields[i];
4981
4982 if (field->flags & HIST_FIELD_FL_VAR) {
4983 if (n++)
4984 seq_puts(m, ",");
4985 hist_field_print(m, field);
4986 }
Tom Zanussif2606832016-03-03 12:54:43 -06004987 }
4988 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004989
4990 seq_puts(m, ":sort=");
Tom Zanussie62347d2016-03-03 12:54:45 -06004991
4992 for (i = 0; i < hist_data->n_sort_keys; i++) {
4993 struct tracing_map_sort_key *sort_key;
Tom Zanussi30350d62018-01-15 20:51:49 -06004994 unsigned int idx, first_key_idx;
4995
4996 /* skip VAR vals */
4997 first_key_idx = hist_data->n_vals - hist_data->n_vars;
Tom Zanussie62347d2016-03-03 12:54:45 -06004998
4999 sort_key = &hist_data->sort_keys[i];
Tom Zanussiad42feb2018-01-15 20:51:45 -06005000 idx = sort_key->field_idx;
5001
Tom Zanussi1a361df2018-01-15 20:51:50 -06005002 if (WARN_ON(idx >= HIST_FIELDS_MAX))
Tom Zanussiad42feb2018-01-15 20:51:45 -06005003 return -EINVAL;
Tom Zanussie62347d2016-03-03 12:54:45 -06005004
5005 if (i > 0)
5006 seq_puts(m, ",");
5007
Tom Zanussiad42feb2018-01-15 20:51:45 -06005008 if (idx == HITCOUNT_IDX)
Tom Zanussie62347d2016-03-03 12:54:45 -06005009 seq_puts(m, "hitcount");
Tom Zanussi30350d62018-01-15 20:51:49 -06005010 else {
5011 if (idx >= first_key_idx)
5012 idx += hist_data->n_vars;
Tom Zanussie62347d2016-03-03 12:54:45 -06005013 hist_field_print(m, hist_data->fields[idx]);
Tom Zanussi30350d62018-01-15 20:51:49 -06005014 }
Tom Zanussie62347d2016-03-03 12:54:45 -06005015
5016 if (sort_key->descending)
5017 seq_puts(m, ".descending");
5018 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005019 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
Tom Zanussia4072fe2018-01-15 20:52:08 -06005020 if (hist_data->enable_timestamps)
5021 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005022
Tom Zanussic282a382018-01-15 20:52:00 -06005023 print_actions_spec(m, hist_data);
5024
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005025 if (data->filter_str)
5026 seq_printf(m, " if %s", data->filter_str);
5027
Tom Zanussi83e99912016-03-03 12:54:46 -06005028 if (data->paused)
5029 seq_puts(m, " [paused]");
5030 else
5031 seq_puts(m, " [active]");
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005032
5033 seq_putc(m, '\n');
5034
5035 return 0;
5036}
5037
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005038static int event_hist_trigger_init(struct event_trigger_ops *ops,
5039 struct event_trigger_data *data)
5040{
5041 struct hist_trigger_data *hist_data = data->private_data;
5042
5043 if (!data->ref && hist_data->attrs->name)
5044 save_named_trigger(hist_data->attrs->name, data);
5045
5046 data->ref++;
5047
5048 return 0;
5049}
5050
Tom Zanussi02205a62018-01-15 20:51:59 -06005051static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
5052{
5053 struct trace_event_file *file;
5054 unsigned int i;
5055 char *cmd;
5056 int ret;
5057
5058 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5059 file = hist_data->field_var_hists[i]->hist_data->event_file;
5060 cmd = hist_data->field_var_hists[i]->cmd;
5061 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
5062 "!hist", "hist", cmd);
5063 }
5064}
5065
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005066static void event_hist_trigger_free(struct event_trigger_ops *ops,
5067 struct event_trigger_data *data)
5068{
5069 struct hist_trigger_data *hist_data = data->private_data;
5070
5071 if (WARN_ON_ONCE(data->ref <= 0))
5072 return;
5073
5074 data->ref--;
5075 if (!data->ref) {
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005076 if (data->name)
5077 del_named_trigger(data);
Tom Zanussi067fe032018-01-15 20:51:56 -06005078
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005079 trigger_data_free(data);
Tom Zanussi067fe032018-01-15 20:51:56 -06005080
5081 remove_hist_vars(hist_data);
5082
Tom Zanussi02205a62018-01-15 20:51:59 -06005083 unregister_field_var_hists(hist_data);
5084
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005085 destroy_hist_data(hist_data);
5086 }
5087}
5088
5089static struct event_trigger_ops event_hist_trigger_ops = {
5090 .func = event_hist_trigger,
5091 .print = event_hist_trigger_print,
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005092 .init = event_hist_trigger_init,
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005093 .free = event_hist_trigger_free,
5094};
5095
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005096static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
5097 struct event_trigger_data *data)
5098{
5099 data->ref++;
5100
5101 save_named_trigger(data->named_data->name, data);
5102
5103 event_hist_trigger_init(ops, data->named_data);
5104
5105 return 0;
5106}
5107
5108static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
5109 struct event_trigger_data *data)
5110{
5111 if (WARN_ON_ONCE(data->ref <= 0))
5112 return;
5113
5114 event_hist_trigger_free(ops, data->named_data);
5115
5116 data->ref--;
5117 if (!data->ref) {
5118 del_named_trigger(data);
5119 trigger_data_free(data);
5120 }
5121}
5122
5123static struct event_trigger_ops event_hist_trigger_named_ops = {
5124 .func = event_hist_trigger,
5125 .print = event_hist_trigger_print,
5126 .init = event_hist_trigger_named_init,
5127 .free = event_hist_trigger_named_free,
5128};
5129
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005130static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
5131 char *param)
5132{
5133 return &event_hist_trigger_ops;
5134}
5135
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005136static void hist_clear(struct event_trigger_data *data)
5137{
5138 struct hist_trigger_data *hist_data = data->private_data;
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005139
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005140 if (data->name)
5141 pause_named_trigger(data);
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005142
5143 synchronize_sched();
5144
5145 tracing_map_clear(hist_data->map);
5146
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005147 if (data->name)
5148 unpause_named_trigger(data);
5149}
5150
5151static bool compatible_field(struct ftrace_event_field *field,
5152 struct ftrace_event_field *test_field)
5153{
5154 if (field == test_field)
5155 return true;
5156 if (field == NULL || test_field == NULL)
5157 return false;
5158 if (strcmp(field->name, test_field->name) != 0)
5159 return false;
5160 if (strcmp(field->type, test_field->type) != 0)
5161 return false;
5162 if (field->size != test_field->size)
5163 return false;
5164 if (field->is_signed != test_field->is_signed)
5165 return false;
5166
5167 return true;
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005168}
5169
Tom Zanussi52a7f162016-03-03 12:54:57 -06005170static bool hist_trigger_match(struct event_trigger_data *data,
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005171 struct event_trigger_data *data_test,
5172 struct event_trigger_data *named_data,
5173 bool ignore_filter)
Tom Zanussi52a7f162016-03-03 12:54:57 -06005174{
5175 struct tracing_map_sort_key *sort_key, *sort_key_test;
5176 struct hist_trigger_data *hist_data, *hist_data_test;
5177 struct hist_field *key_field, *key_field_test;
5178 unsigned int i;
5179
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005180 if (named_data && (named_data != data_test) &&
5181 (named_data != data_test->named_data))
5182 return false;
5183
5184 if (!named_data && is_named_trigger(data_test))
5185 return false;
5186
Tom Zanussi52a7f162016-03-03 12:54:57 -06005187 hist_data = data->private_data;
5188 hist_data_test = data_test->private_data;
5189
5190 if (hist_data->n_vals != hist_data_test->n_vals ||
5191 hist_data->n_fields != hist_data_test->n_fields ||
5192 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
5193 return false;
5194
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005195 if (!ignore_filter) {
5196 if ((data->filter_str && !data_test->filter_str) ||
5197 (!data->filter_str && data_test->filter_str))
5198 return false;
5199 }
Tom Zanussi52a7f162016-03-03 12:54:57 -06005200
5201 for_each_hist_field(i, hist_data) {
5202 key_field = hist_data->fields[i];
5203 key_field_test = hist_data_test->fields[i];
5204
5205 if (key_field->flags != key_field_test->flags)
5206 return false;
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005207 if (!compatible_field(key_field->field, key_field_test->field))
Tom Zanussi52a7f162016-03-03 12:54:57 -06005208 return false;
5209 if (key_field->offset != key_field_test->offset)
5210 return false;
Tom Zanussiad42feb2018-01-15 20:51:45 -06005211 if (key_field->size != key_field_test->size)
5212 return false;
5213 if (key_field->is_signed != key_field_test->is_signed)
5214 return false;
Tom Zanussi1a361df2018-01-15 20:51:50 -06005215 if (!!key_field->var.name != !!key_field_test->var.name)
5216 return false;
5217 if (key_field->var.name &&
5218 strcmp(key_field->var.name, key_field_test->var.name) != 0)
5219 return false;
Tom Zanussi52a7f162016-03-03 12:54:57 -06005220 }
5221
5222 for (i = 0; i < hist_data->n_sort_keys; i++) {
5223 sort_key = &hist_data->sort_keys[i];
5224 sort_key_test = &hist_data_test->sort_keys[i];
5225
5226 if (sort_key->field_idx != sort_key_test->field_idx ||
5227 sort_key->descending != sort_key_test->descending)
5228 return false;
5229 }
5230
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005231 if (!ignore_filter && data->filter_str &&
Tom Zanussi52a7f162016-03-03 12:54:57 -06005232 (strcmp(data->filter_str, data_test->filter_str) != 0))
5233 return false;
5234
Tom Zanussi48f79472018-03-28 15:10:55 -05005235 if (!actions_match(hist_data, hist_data_test))
5236 return false;
5237
Tom Zanussi52a7f162016-03-03 12:54:57 -06005238 return true;
5239}
5240
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005241static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
5242 struct event_trigger_data *data,
5243 struct trace_event_file *file)
5244{
Tom Zanussi83e99912016-03-03 12:54:46 -06005245 struct hist_trigger_data *hist_data = data->private_data;
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005246 struct event_trigger_data *test, *named_data = NULL;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005247 int ret = 0;
5248
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005249 if (hist_data->attrs->name) {
5250 named_data = find_named_trigger(hist_data->attrs->name);
5251 if (named_data) {
5252 if (!hist_trigger_match(data, named_data, named_data,
5253 true)) {
Tom Zanussif404da62018-01-15 20:52:05 -06005254 hist_err("Named hist trigger doesn't match existing named trigger (includes variables): ", hist_data->attrs->name);
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005255 ret = -EINVAL;
5256 goto out;
5257 }
5258 }
5259 }
5260
5261 if (hist_data->attrs->name && !named_data)
5262 goto new;
5263
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005264 list_for_each_entry_rcu(test, &file->triggers, list) {
5265 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005266 if (!hist_trigger_match(data, test, named_data, false))
Tom Zanussi52a7f162016-03-03 12:54:57 -06005267 continue;
Tom Zanussi83e99912016-03-03 12:54:46 -06005268 if (hist_data->attrs->pause)
5269 test->paused = true;
5270 else if (hist_data->attrs->cont)
5271 test->paused = false;
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005272 else if (hist_data->attrs->clear)
5273 hist_clear(test);
Tom Zanussif404da62018-01-15 20:52:05 -06005274 else {
5275 hist_err("Hist trigger already exists", NULL);
Tom Zanussi83e99912016-03-03 12:54:46 -06005276 ret = -EEXIST;
Tom Zanussif404da62018-01-15 20:52:05 -06005277 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005278 goto out;
5279 }
5280 }
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005281 new:
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005282 if (hist_data->attrs->cont || hist_data->attrs->clear) {
Tom Zanussif404da62018-01-15 20:52:05 -06005283 hist_err("Can't clear or continue a nonexistent hist trigger", NULL);
Tom Zanussi83e99912016-03-03 12:54:46 -06005284 ret = -ENOENT;
5285 goto out;
5286 }
5287
Tom Zanussi7522c032016-06-29 19:56:00 -05005288 if (hist_data->attrs->pause)
5289 data->paused = true;
5290
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005291 if (named_data) {
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005292 data->private_data = named_data->private_data;
5293 set_named_trigger_data(data, named_data);
5294 data->ops = &event_hist_trigger_named_ops;
5295 }
5296
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005297 if (data->ops->init) {
5298 ret = data->ops->init(data->ops, data);
5299 if (ret < 0)
5300 goto out;
5301 }
5302
Tom Zanussia4072fe2018-01-15 20:52:08 -06005303 if (hist_data->enable_timestamps) {
5304 char *clock = hist_data->attrs->clock;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005305
Tom Zanussia4072fe2018-01-15 20:52:08 -06005306 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
5307 if (ret) {
5308 hist_err("Couldn't set trace_clock: ", clock);
5309 goto out;
5310 }
5311
Tom Zanussiad42feb2018-01-15 20:51:45 -06005312 tracing_set_time_stamp_abs(file->tr, true);
Tom Zanussia4072fe2018-01-15 20:52:08 -06005313 }
5314
5315 if (named_data)
5316 destroy_hist_data(hist_data);
5317
5318 ret++;
Tom Zanussi067fe032018-01-15 20:51:56 -06005319 out:
5320 return ret;
5321}
5322
5323static int hist_trigger_enable(struct event_trigger_data *data,
5324 struct trace_event_file *file)
5325{
5326 int ret = 0;
5327
5328 list_add_tail_rcu(&data->list, &file->triggers);
5329
5330 update_cond_flag(file);
Tom Zanussiad42feb2018-01-15 20:51:45 -06005331
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005332 if (trace_event_trigger_enable_disable(file, 1) < 0) {
5333 list_del_rcu(&data->list);
5334 update_cond_flag(file);
5335 ret--;
5336 }
Tom Zanussi067fe032018-01-15 20:51:56 -06005337
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005338 return ret;
5339}
5340
Tom Zanussi4b147932018-01-15 20:51:58 -06005341static bool have_hist_trigger_match(struct event_trigger_data *data,
5342 struct trace_event_file *file)
5343{
5344 struct hist_trigger_data *hist_data = data->private_data;
5345 struct event_trigger_data *test, *named_data = NULL;
5346 bool match = false;
5347
5348 if (hist_data->attrs->name)
5349 named_data = find_named_trigger(hist_data->attrs->name);
5350
5351 list_for_each_entry_rcu(test, &file->triggers, list) {
5352 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5353 if (hist_trigger_match(data, test, named_data, false)) {
5354 match = true;
5355 break;
5356 }
5357 }
5358 }
5359
5360 return match;
5361}
5362
Tom Zanussi067fe032018-01-15 20:51:56 -06005363static bool hist_trigger_check_refs(struct event_trigger_data *data,
5364 struct trace_event_file *file)
5365{
5366 struct hist_trigger_data *hist_data = data->private_data;
5367 struct event_trigger_data *test, *named_data = NULL;
5368
5369 if (hist_data->attrs->name)
5370 named_data = find_named_trigger(hist_data->attrs->name);
5371
5372 list_for_each_entry_rcu(test, &file->triggers, list) {
5373 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5374 if (!hist_trigger_match(data, test, named_data, false))
5375 continue;
5376 hist_data = test->private_data;
5377 if (check_var_refs(hist_data))
5378 return true;
5379 break;
5380 }
5381 }
5382
5383 return false;
5384}
5385
Tom Zanussi52a7f162016-03-03 12:54:57 -06005386static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
5387 struct event_trigger_data *data,
5388 struct trace_event_file *file)
5389{
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005390 struct hist_trigger_data *hist_data = data->private_data;
5391 struct event_trigger_data *test, *named_data = NULL;
Tom Zanussi52a7f162016-03-03 12:54:57 -06005392 bool unregistered = false;
5393
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005394 if (hist_data->attrs->name)
5395 named_data = find_named_trigger(hist_data->attrs->name);
5396
Tom Zanussi52a7f162016-03-03 12:54:57 -06005397 list_for_each_entry_rcu(test, &file->triggers, list) {
5398 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005399 if (!hist_trigger_match(data, test, named_data, false))
Tom Zanussi52a7f162016-03-03 12:54:57 -06005400 continue;
5401 unregistered = true;
5402 list_del_rcu(&test->list);
5403 trace_event_trigger_enable_disable(file, 0);
5404 update_cond_flag(file);
5405 break;
5406 }
5407 }
5408
5409 if (unregistered && test->ops->free)
5410 test->ops->free(test->ops, test);
Tom Zanussiad42feb2018-01-15 20:51:45 -06005411
5412 if (hist_data->enable_timestamps) {
Tom Zanussi30350d62018-01-15 20:51:49 -06005413 if (!hist_data->remove || unregistered)
Tom Zanussiad42feb2018-01-15 20:51:45 -06005414 tracing_set_time_stamp_abs(file->tr, false);
5415 }
Tom Zanussi52a7f162016-03-03 12:54:57 -06005416}
5417
Tom Zanussi067fe032018-01-15 20:51:56 -06005418static bool hist_file_check_refs(struct trace_event_file *file)
5419{
5420 struct hist_trigger_data *hist_data;
5421 struct event_trigger_data *test;
5422
5423 list_for_each_entry_rcu(test, &file->triggers, list) {
5424 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5425 hist_data = test->private_data;
5426 if (check_var_refs(hist_data))
5427 return true;
5428 }
5429 }
5430
5431 return false;
5432}
5433
Tom Zanussi52a7f162016-03-03 12:54:57 -06005434static void hist_unreg_all(struct trace_event_file *file)
5435{
Steven Rostedt47c18562016-06-29 19:55:59 -05005436 struct event_trigger_data *test, *n;
Tom Zanussiad42feb2018-01-15 20:51:45 -06005437 struct hist_trigger_data *hist_data;
Tom Zanussi4b147932018-01-15 20:51:58 -06005438 struct synth_event *se;
5439 const char *se_name;
Tom Zanussi52a7f162016-03-03 12:54:57 -06005440
Tom Zanussi067fe032018-01-15 20:51:56 -06005441 if (hist_file_check_refs(file))
5442 return;
5443
Steven Rostedt47c18562016-06-29 19:55:59 -05005444 list_for_each_entry_safe(test, n, &file->triggers, list) {
Tom Zanussi52a7f162016-03-03 12:54:57 -06005445 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
Tom Zanussiad42feb2018-01-15 20:51:45 -06005446 hist_data = test->private_data;
Tom Zanussi52a7f162016-03-03 12:54:57 -06005447 list_del_rcu(&test->list);
5448 trace_event_trigger_enable_disable(file, 0);
Tom Zanussi4b147932018-01-15 20:51:58 -06005449
5450 mutex_lock(&synth_event_mutex);
5451 se_name = trace_event_name(file->event_call);
5452 se = find_synth_event(se_name);
5453 if (se)
5454 se->ref--;
5455 mutex_unlock(&synth_event_mutex);
5456
Tom Zanussi52a7f162016-03-03 12:54:57 -06005457 update_cond_flag(file);
Tom Zanussiad42feb2018-01-15 20:51:45 -06005458 if (hist_data->enable_timestamps)
5459 tracing_set_time_stamp_abs(file->tr, false);
Tom Zanussi52a7f162016-03-03 12:54:57 -06005460 if (test->ops->free)
5461 test->ops->free(test->ops, test);
5462 }
5463 }
5464}
5465
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005466static int event_hist_trigger_func(struct event_command *cmd_ops,
5467 struct trace_event_file *file,
5468 char *glob, char *cmd, char *param)
5469{
5470 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
5471 struct event_trigger_data *trigger_data;
5472 struct hist_trigger_attrs *attrs;
5473 struct event_trigger_ops *trigger_ops;
5474 struct hist_trigger_data *hist_data;
Tom Zanussi4b147932018-01-15 20:51:58 -06005475 struct synth_event *se;
5476 const char *se_name;
Tom Zanussi30350d62018-01-15 20:51:49 -06005477 bool remove = false;
Tom Zanussiec5ce092018-01-15 20:52:02 -06005478 char *trigger, *p;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005479 int ret = 0;
5480
Tom Zanussif404da62018-01-15 20:52:05 -06005481 if (glob && strlen(glob)) {
5482 last_cmd_set(param);
5483 hist_err_clear();
5484 }
5485
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005486 if (!param)
5487 return -EINVAL;
5488
Tom Zanussi30350d62018-01-15 20:51:49 -06005489 if (glob[0] == '!')
5490 remove = true;
5491
Tom Zanussiec5ce092018-01-15 20:52:02 -06005492 /*
5493 * separate the trigger from the filter (k:v [if filter])
5494 * allowing for whitespace in the trigger
5495 */
5496 p = trigger = param;
5497 do {
5498 p = strstr(p, "if");
5499 if (!p)
5500 break;
5501 if (p == param)
5502 return -EINVAL;
5503 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
5504 p++;
5505 continue;
5506 }
5507 if (p >= param + strlen(param) - strlen("if") - 1)
5508 return -EINVAL;
5509 if (*(p + strlen("if")) != ' ' && *(p + strlen("if")) != '\t') {
5510 p++;
5511 continue;
5512 }
5513 break;
5514 } while (p);
5515
5516 if (!p)
5517 param = NULL;
5518 else {
5519 *(p - 1) = '\0';
5520 param = strstrip(p);
5521 trigger = strstrip(trigger);
5522 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005523
5524 attrs = parse_hist_trigger_attrs(trigger);
5525 if (IS_ERR(attrs))
5526 return PTR_ERR(attrs);
5527
5528 if (attrs->map_bits)
5529 hist_trigger_bits = attrs->map_bits;
5530
Tom Zanussi30350d62018-01-15 20:51:49 -06005531 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005532 if (IS_ERR(hist_data)) {
5533 destroy_hist_trigger_attrs(attrs);
5534 return PTR_ERR(hist_data);
5535 }
5536
5537 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
5538
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005539 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
Tom Zanussi4b147932018-01-15 20:51:58 -06005540 if (!trigger_data) {
5541 ret = -ENOMEM;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005542 goto out_free;
Tom Zanussi4b147932018-01-15 20:51:58 -06005543 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005544
5545 trigger_data->count = -1;
5546 trigger_data->ops = trigger_ops;
5547 trigger_data->cmd_ops = cmd_ops;
5548
5549 INIT_LIST_HEAD(&trigger_data->list);
5550 RCU_INIT_POINTER(trigger_data->filter, NULL);
5551
5552 trigger_data->private_data = hist_data;
5553
Tom Zanussi52a7f162016-03-03 12:54:57 -06005554 /* if param is non-empty, it's supposed to be a filter */
5555 if (param && cmd_ops->set_filter) {
5556 ret = cmd_ops->set_filter(param, trigger_data, file);
5557 if (ret < 0)
5558 goto out_free;
5559 }
5560
Tom Zanussi30350d62018-01-15 20:51:49 -06005561 if (remove) {
Tom Zanussi4b147932018-01-15 20:51:58 -06005562 if (!have_hist_trigger_match(trigger_data, file))
5563 goto out_free;
5564
Tom Zanussi067fe032018-01-15 20:51:56 -06005565 if (hist_trigger_check_refs(trigger_data, file)) {
5566 ret = -EBUSY;
5567 goto out_free;
5568 }
5569
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005570 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
Tom Zanussi4b147932018-01-15 20:51:58 -06005571
5572 mutex_lock(&synth_event_mutex);
5573 se_name = trace_event_name(file->event_call);
5574 se = find_synth_event(se_name);
5575 if (se)
5576 se->ref--;
5577 mutex_unlock(&synth_event_mutex);
5578
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005579 ret = 0;
5580 goto out_free;
5581 }
5582
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005583 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
5584 /*
5585 * The above returns on success the # of triggers registered,
5586 * but if it didn't register any it returns zero. Consider no
5587 * triggers registered a failure too.
5588 */
5589 if (!ret) {
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005590 if (!(attrs->pause || attrs->cont || attrs->clear))
Tom Zanussi83e99912016-03-03 12:54:46 -06005591 ret = -ENOENT;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005592 goto out_free;
5593 } else if (ret < 0)
5594 goto out_free;
Tom Zanussi067fe032018-01-15 20:51:56 -06005595
5596 if (get_named_trigger_data(trigger_data))
5597 goto enable;
5598
5599 if (has_hist_vars(hist_data))
5600 save_hist_vars(hist_data);
5601
Tom Zanussi0212e2a2018-01-15 20:51:57 -06005602 ret = create_actions(hist_data, file);
5603 if (ret)
5604 goto out_unreg;
5605
Tom Zanussi067fe032018-01-15 20:51:56 -06005606 ret = tracing_map_init(hist_data->map);
5607 if (ret)
5608 goto out_unreg;
5609enable:
5610 ret = hist_trigger_enable(trigger_data, file);
5611 if (ret)
5612 goto out_unreg;
5613
Tom Zanussi4b147932018-01-15 20:51:58 -06005614 mutex_lock(&synth_event_mutex);
5615 se_name = trace_event_name(file->event_call);
5616 se = find_synth_event(se_name);
5617 if (se)
5618 se->ref++;
5619 mutex_unlock(&synth_event_mutex);
5620
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005621 /* Just return zero, not the number of registered triggers */
5622 ret = 0;
5623 out:
Tom Zanussif404da62018-01-15 20:52:05 -06005624 if (ret == 0)
5625 hist_err_clear();
5626
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005627 return ret;
Tom Zanussi067fe032018-01-15 20:51:56 -06005628 out_unreg:
5629 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005630 out_free:
5631 if (cmd_ops->set_filter)
5632 cmd_ops->set_filter(NULL, trigger_data, NULL);
5633
Tom Zanussi067fe032018-01-15 20:51:56 -06005634 remove_hist_vars(hist_data);
5635
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005636 kfree(trigger_data);
5637
5638 destroy_hist_data(hist_data);
5639 goto out;
5640}
5641
5642static struct event_command trigger_hist_cmd = {
5643 .name = "hist",
5644 .trigger_type = ETT_EVENT_HIST,
5645 .flags = EVENT_CMD_FL_NEEDS_REC,
5646 .func = event_hist_trigger_func,
5647 .reg = hist_register_trigger,
Tom Zanussi52a7f162016-03-03 12:54:57 -06005648 .unreg = hist_unregister_trigger,
5649 .unreg_all = hist_unreg_all,
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005650 .get_trigger_ops = event_hist_get_trigger_ops,
5651 .set_filter = set_trigger_filter,
5652};
5653
5654__init int register_trigger_hist_cmd(void)
5655{
5656 int ret;
5657
5658 ret = register_event_command(&trigger_hist_cmd);
5659 WARN_ON(ret < 0);
5660
5661 return ret;
5662}
Tom Zanussid0bad492016-03-03 12:54:55 -06005663
5664static void
Tom Zanussi1ac4f512018-01-15 20:51:42 -06005665hist_enable_trigger(struct event_trigger_data *data, void *rec,
5666 struct ring_buffer_event *event)
Tom Zanussid0bad492016-03-03 12:54:55 -06005667{
5668 struct enable_trigger_data *enable_data = data->private_data;
5669 struct event_trigger_data *test;
5670
5671 list_for_each_entry_rcu(test, &enable_data->file->triggers, list) {
5672 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5673 if (enable_data->enable)
5674 test->paused = false;
5675 else
5676 test->paused = true;
Tom Zanussid0bad492016-03-03 12:54:55 -06005677 }
5678 }
5679}
5680
5681static void
Tom Zanussi1ac4f512018-01-15 20:51:42 -06005682hist_enable_count_trigger(struct event_trigger_data *data, void *rec,
5683 struct ring_buffer_event *event)
Tom Zanussid0bad492016-03-03 12:54:55 -06005684{
5685 if (!data->count)
5686 return;
5687
5688 if (data->count != -1)
5689 (data->count)--;
5690
Tom Zanussi1ac4f512018-01-15 20:51:42 -06005691 hist_enable_trigger(data, rec, event);
Tom Zanussid0bad492016-03-03 12:54:55 -06005692}
5693
5694static struct event_trigger_ops hist_enable_trigger_ops = {
5695 .func = hist_enable_trigger,
5696 .print = event_enable_trigger_print,
5697 .init = event_trigger_init,
5698 .free = event_enable_trigger_free,
5699};
5700
5701static struct event_trigger_ops hist_enable_count_trigger_ops = {
5702 .func = hist_enable_count_trigger,
5703 .print = event_enable_trigger_print,
5704 .init = event_trigger_init,
5705 .free = event_enable_trigger_free,
5706};
5707
5708static struct event_trigger_ops hist_disable_trigger_ops = {
5709 .func = hist_enable_trigger,
5710 .print = event_enable_trigger_print,
5711 .init = event_trigger_init,
5712 .free = event_enable_trigger_free,
5713};
5714
5715static struct event_trigger_ops hist_disable_count_trigger_ops = {
5716 .func = hist_enable_count_trigger,
5717 .print = event_enable_trigger_print,
5718 .init = event_trigger_init,
5719 .free = event_enable_trigger_free,
5720};
5721
5722static struct event_trigger_ops *
5723hist_enable_get_trigger_ops(char *cmd, char *param)
5724{
5725 struct event_trigger_ops *ops;
5726 bool enable;
5727
5728 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
5729
5730 if (enable)
5731 ops = param ? &hist_enable_count_trigger_ops :
5732 &hist_enable_trigger_ops;
5733 else
5734 ops = param ? &hist_disable_count_trigger_ops :
5735 &hist_disable_trigger_ops;
5736
5737 return ops;
5738}
5739
Tom Zanussi52a7f162016-03-03 12:54:57 -06005740static void hist_enable_unreg_all(struct trace_event_file *file)
5741{
Steven Rostedt47c18562016-06-29 19:55:59 -05005742 struct event_trigger_data *test, *n;
Tom Zanussi52a7f162016-03-03 12:54:57 -06005743
Steven Rostedt47c18562016-06-29 19:55:59 -05005744 list_for_each_entry_safe(test, n, &file->triggers, list) {
Tom Zanussi52a7f162016-03-03 12:54:57 -06005745 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
5746 list_del_rcu(&test->list);
5747 update_cond_flag(file);
5748 trace_event_trigger_enable_disable(file, 0);
5749 if (test->ops->free)
5750 test->ops->free(test->ops, test);
5751 }
5752 }
5753}
5754
Tom Zanussid0bad492016-03-03 12:54:55 -06005755static struct event_command trigger_hist_enable_cmd = {
5756 .name = ENABLE_HIST_STR,
5757 .trigger_type = ETT_HIST_ENABLE,
5758 .func = event_enable_trigger_func,
5759 .reg = event_enable_register_trigger,
5760 .unreg = event_enable_unregister_trigger,
Tom Zanussi52a7f162016-03-03 12:54:57 -06005761 .unreg_all = hist_enable_unreg_all,
Tom Zanussid0bad492016-03-03 12:54:55 -06005762 .get_trigger_ops = hist_enable_get_trigger_ops,
5763 .set_filter = set_trigger_filter,
5764};
5765
5766static struct event_command trigger_hist_disable_cmd = {
5767 .name = DISABLE_HIST_STR,
5768 .trigger_type = ETT_HIST_ENABLE,
5769 .func = event_enable_trigger_func,
5770 .reg = event_enable_register_trigger,
5771 .unreg = event_enable_unregister_trigger,
Tom Zanussi52a7f162016-03-03 12:54:57 -06005772 .unreg_all = hist_enable_unreg_all,
Tom Zanussid0bad492016-03-03 12:54:55 -06005773 .get_trigger_ops = hist_enable_get_trigger_ops,
5774 .set_filter = set_trigger_filter,
5775};
5776
5777static __init void unregister_trigger_hist_enable_disable_cmds(void)
5778{
5779 unregister_event_command(&trigger_hist_enable_cmd);
5780 unregister_event_command(&trigger_hist_disable_cmd);
5781}
5782
5783__init int register_trigger_hist_enable_disable_cmds(void)
5784{
5785 int ret;
5786
5787 ret = register_event_command(&trigger_hist_enable_cmd);
5788 if (WARN_ON(ret < 0))
5789 return ret;
5790 ret = register_event_command(&trigger_hist_disable_cmd);
5791 if (WARN_ON(ret < 0))
5792 unregister_trigger_hist_enable_disable_cmds();
5793
5794 return ret;
5795}
Tom Zanussi4b147932018-01-15 20:51:58 -06005796
5797static __init int trace_events_hist_init(void)
5798{
5799 struct dentry *entry = NULL;
5800 struct dentry *d_tracer;
5801 int err = 0;
5802
5803 d_tracer = tracing_init_dentry();
5804 if (IS_ERR(d_tracer)) {
5805 err = PTR_ERR(d_tracer);
5806 goto err;
5807 }
5808
5809 entry = tracefs_create_file("synthetic_events", 0644, d_tracer,
5810 NULL, &synth_events_fops);
5811 if (!entry) {
5812 err = -ENODEV;
5813 goto err;
5814 }
5815
5816 return err;
5817 err:
5818 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
5819
5820 return err;
5821}
5822
5823fs_initcall(trace_events_hist_init);