blob: ca6b0dff60c5be2af6315f5b135ac754983e92c6 [file] [log] [blame]
Steven Rostedt (VMware)bcea3f92018-08-16 11:23:53 -04001// SPDX-License-Identifier: GPL-2.0
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002/*
3 * trace_events_hist - trace event hist triggers
4 *
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005 * Copyright (C) 2015 Tom Zanussi <tom.zanussi@linux.intel.com>
6 */
7
8#include <linux/module.h>
9#include <linux/kallsyms.h>
10#include <linux/mutex.h>
11#include <linux/slab.h>
12#include <linux/stacktrace.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +010013#include <linux/rculist.h>
Tom Zanussi4b147932018-01-15 20:51:58 -060014#include <linux/tracefs.h>
Tom Zanussi7ef224d2016-03-03 12:54:42 -060015
16#include "tracing_map.h"
17#include "trace.h"
Masami Hiramatsu7bbab382018-11-05 18:03:33 +090018#include "trace_dynevent.h"
Tom Zanussi7ef224d2016-03-03 12:54:42 -060019
Tom Zanussi4b147932018-01-15 20:51:58 -060020#define SYNTH_SYSTEM "synthetic"
21#define SYNTH_FIELDS_MAX 16
22
23#define STR_VAR_LEN_MAX 32 /* must be multiple of sizeof(u64) */
24
Tom Zanussid566c5e2019-03-31 18:48:17 -050025#define ERRORS \
26 C(NONE, "No error"), \
27 C(DUPLICATE_VAR, "Variable already defined"), \
28 C(VAR_NOT_UNIQUE, "Variable name not unique, need to use fully qualified name (subsys.event.var) for variable"), \
29 C(TOO_MANY_VARS, "Too many variables defined"), \
30 C(MALFORMED_ASSIGNMENT, "Malformed assignment"), \
31 C(NAMED_MISMATCH, "Named hist trigger doesn't match existing named trigger (includes variables)"), \
32 C(TRIGGER_EEXIST, "Hist trigger already exists"), \
33 C(TRIGGER_ENOENT_CLEAR, "Can't clear or continue a nonexistent hist trigger"), \
34 C(SET_CLOCK_FAIL, "Couldn't set trace_clock"), \
35 C(BAD_FIELD_MODIFIER, "Invalid field modifier"), \
36 C(TOO_MANY_SUBEXPR, "Too many subexpressions (3 max)"), \
37 C(TIMESTAMP_MISMATCH, "Timestamp units in expression don't match"), \
38 C(TOO_MANY_FIELD_VARS, "Too many field variables defined"), \
39 C(EVENT_FILE_NOT_FOUND, "Event file not found"), \
40 C(HIST_NOT_FOUND, "Matching event histogram not found"), \
41 C(HIST_CREATE_FAIL, "Couldn't create histogram for field"), \
42 C(SYNTH_VAR_NOT_FOUND, "Couldn't find synthetic variable"), \
43 C(SYNTH_EVENT_NOT_FOUND,"Couldn't find synthetic event"), \
44 C(SYNTH_TYPE_MISMATCH, "Param type doesn't match synthetic event field type"), \
45 C(SYNTH_COUNT_MISMATCH, "Param count doesn't match synthetic event field count"), \
46 C(FIELD_VAR_PARSE_FAIL, "Couldn't parse field variable"), \
47 C(VAR_CREATE_FIND_FAIL, "Couldn't create or find variable"), \
48 C(ONX_NOT_VAR, "For onmax(x) or onchange(x), x must be a variable"), \
49 C(ONX_VAR_NOT_FOUND, "Couldn't find onmax or onchange variable"), \
50 C(ONX_VAR_CREATE_FAIL, "Couldn't create onmax or onchange variable"), \
51 C(FIELD_VAR_CREATE_FAIL,"Couldn't create field variable"), \
52 C(TOO_MANY_PARAMS, "Too many action params"), \
53 C(PARAM_NOT_FOUND, "Couldn't find param"), \
54 C(INVALID_PARAM, "Invalid action param"), \
55 C(ACTION_NOT_FOUND, "No action found"), \
56 C(NO_SAVE_PARAMS, "No params found for save()"), \
57 C(TOO_MANY_SAVE_ACTIONS,"Can't have more than one save() action per hist"), \
58 C(ACTION_MISMATCH, "Handler doesn't support action"), \
59 C(NO_CLOSING_PAREN, "No closing paren found"), \
60 C(SUBSYS_NOT_FOUND, "Missing subsystem"), \
61 C(INVALID_SUBSYS_EVENT, "Invalid subsystem or event name"), \
Tom Zanussic8d94a12019-04-18 10:18:51 -050062 C(INVALID_REF_KEY, "Using variable references in keys not supported"), \
Tom Zanussid566c5e2019-03-31 18:48:17 -050063 C(VAR_NOT_FOUND, "Couldn't find variable"), \
64 C(FIELD_NOT_FOUND, "Couldn't find field"),
65
66#undef C
67#define C(a, b) HIST_ERR_##a
68
69enum { ERRORS };
70
71#undef C
72#define C(a, b) b
73
74static const char *err_text[] = { ERRORS };
75
Tom Zanussi7ef224d2016-03-03 12:54:42 -060076struct hist_field;
77
Tom Zanussidf35d932018-01-15 20:51:54 -060078typedef u64 (*hist_field_fn_t) (struct hist_field *field,
79 struct tracing_map_elt *elt,
80 struct ring_buffer_event *rbe,
81 void *event);
Tom Zanussi7ef224d2016-03-03 12:54:42 -060082
Tom Zanussi5819ead2017-09-22 14:58:23 -050083#define HIST_FIELD_OPERANDS_MAX 2
Tom Zanussi30350d62018-01-15 20:51:49 -060084#define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
Tom Zanussi0212e2a2018-01-15 20:51:57 -060085#define HIST_ACTIONS_MAX 8
Tom Zanussi30350d62018-01-15 20:51:49 -060086
Tom Zanussi100719d2018-01-15 20:51:52 -060087enum field_op_id {
88 FIELD_OP_NONE,
89 FIELD_OP_PLUS,
90 FIELD_OP_MINUS,
91 FIELD_OP_UNARY_MINUS,
92};
93
Tom Zanussi05ddb252018-12-18 14:33:26 -060094/*
95 * A hist_var (histogram variable) contains variable information for
96 * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF
97 * flag set. A hist_var has a variable name e.g. ts0, and is
98 * associated with a given histogram trigger, as specified by
99 * hist_data. The hist_var idx is the unique index assigned to the
100 * variable by the hist trigger's tracing_map. The idx is what is
101 * used to set a variable's value and, by a variable reference, to
102 * retrieve it.
103 */
Tom Zanussi30350d62018-01-15 20:51:49 -0600104struct hist_var {
105 char *name;
106 struct hist_trigger_data *hist_data;
107 unsigned int idx;
108};
Tom Zanussi5819ead2017-09-22 14:58:23 -0500109
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600110struct hist_field {
111 struct ftrace_event_field *field;
112 unsigned long flags;
113 hist_field_fn_t fn;
114 unsigned int size;
Tom Zanussi76a3b0c2016-03-03 12:54:44 -0600115 unsigned int offset;
Tom Zanussi5819ead2017-09-22 14:58:23 -0500116 unsigned int is_signed;
Tom Zanussi19a9fac2018-01-15 20:51:55 -0600117 const char *type;
Tom Zanussi5819ead2017-09-22 14:58:23 -0500118 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
Tom Zanussib559d003a2018-01-15 20:51:47 -0600119 struct hist_trigger_data *hist_data;
Tom Zanussi05ddb252018-12-18 14:33:26 -0600120
121 /*
122 * Variable fields contain variable-specific info in var.
123 */
Tom Zanussi30350d62018-01-15 20:51:49 -0600124 struct hist_var var;
Tom Zanussi100719d2018-01-15 20:51:52 -0600125 enum field_op_id operator;
Tom Zanussi067fe032018-01-15 20:51:56 -0600126 char *system;
127 char *event_name;
Tom Zanussi05ddb252018-12-18 14:33:26 -0600128
129 /*
130 * The name field is used for EXPR and VAR_REF fields. VAR
131 * fields contain the variable name in var.name.
132 */
Tom Zanussi100719d2018-01-15 20:51:52 -0600133 char *name;
Tom Zanussi05ddb252018-12-18 14:33:26 -0600134
135 /*
136 * When a histogram trigger is hit, if it has any references
137 * to variables, the values of those variables are collected
138 * into a var_ref_vals array by resolve_var_refs(). The
139 * current value of each variable is read from the tracing_map
140 * using the hist field's hist_var.idx and entered into the
141 * var_ref_idx entry i.e. var_ref_vals[var_ref_idx].
142 */
Tom Zanussi067fe032018-01-15 20:51:56 -0600143 unsigned int var_ref_idx;
144 bool read_once;
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600145};
146
Tom Zanussidf35d932018-01-15 20:51:54 -0600147static u64 hist_field_none(struct hist_field *field,
148 struct tracing_map_elt *elt,
149 struct ring_buffer_event *rbe,
150 void *event)
Tom Zanussi69a02002016-03-03 12:54:52 -0600151{
152 return 0;
153}
154
Tom Zanussidf35d932018-01-15 20:51:54 -0600155static u64 hist_field_counter(struct hist_field *field,
156 struct tracing_map_elt *elt,
157 struct ring_buffer_event *rbe,
158 void *event)
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600159{
160 return 1;
161}
162
Tom Zanussidf35d932018-01-15 20:51:54 -0600163static u64 hist_field_string(struct hist_field *hist_field,
164 struct tracing_map_elt *elt,
165 struct ring_buffer_event *rbe,
166 void *event)
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600167{
168 char *addr = (char *)(event + hist_field->field->offset);
169
170 return (u64)(unsigned long)addr;
171}
172
Tom Zanussidf35d932018-01-15 20:51:54 -0600173static u64 hist_field_dynstring(struct hist_field *hist_field,
174 struct tracing_map_elt *elt,
175 struct ring_buffer_event *rbe,
176 void *event)
Namhyung Kim79e577c2016-03-03 12:54:53 -0600177{
178 u32 str_item = *(u32 *)(event + hist_field->field->offset);
179 int str_loc = str_item & 0xffff;
180 char *addr = (char *)(event + str_loc);
181
182 return (u64)(unsigned long)addr;
183}
184
Tom Zanussidf35d932018-01-15 20:51:54 -0600185static u64 hist_field_pstring(struct hist_field *hist_field,
186 struct tracing_map_elt *elt,
187 struct ring_buffer_event *rbe,
188 void *event)
Namhyung Kim79e577c2016-03-03 12:54:53 -0600189{
190 char **addr = (char **)(event + hist_field->field->offset);
191
192 return (u64)(unsigned long)*addr;
193}
194
Tom Zanussidf35d932018-01-15 20:51:54 -0600195static u64 hist_field_log2(struct hist_field *hist_field,
196 struct tracing_map_elt *elt,
197 struct ring_buffer_event *rbe,
198 void *event)
Namhyung Kim4b94f5b2016-03-03 12:55:02 -0600199{
Tom Zanussi5819ead2017-09-22 14:58:23 -0500200 struct hist_field *operand = hist_field->operands[0];
201
Tom Zanussidf35d932018-01-15 20:51:54 -0600202 u64 val = operand->fn(operand, elt, rbe, event);
Namhyung Kim4b94f5b2016-03-03 12:55:02 -0600203
204 return (u64) ilog2(roundup_pow_of_two(val));
205}
206
Tom Zanussidf35d932018-01-15 20:51:54 -0600207static u64 hist_field_plus(struct hist_field *hist_field,
208 struct tracing_map_elt *elt,
209 struct ring_buffer_event *rbe,
210 void *event)
Tom Zanussi100719d2018-01-15 20:51:52 -0600211{
212 struct hist_field *operand1 = hist_field->operands[0];
213 struct hist_field *operand2 = hist_field->operands[1];
214
Tom Zanussidf35d932018-01-15 20:51:54 -0600215 u64 val1 = operand1->fn(operand1, elt, rbe, event);
216 u64 val2 = operand2->fn(operand2, elt, rbe, event);
Tom Zanussi100719d2018-01-15 20:51:52 -0600217
218 return val1 + val2;
219}
220
Tom Zanussidf35d932018-01-15 20:51:54 -0600221static u64 hist_field_minus(struct hist_field *hist_field,
222 struct tracing_map_elt *elt,
223 struct ring_buffer_event *rbe,
224 void *event)
Tom Zanussi100719d2018-01-15 20:51:52 -0600225{
226 struct hist_field *operand1 = hist_field->operands[0];
227 struct hist_field *operand2 = hist_field->operands[1];
228
Tom Zanussidf35d932018-01-15 20:51:54 -0600229 u64 val1 = operand1->fn(operand1, elt, rbe, event);
230 u64 val2 = operand2->fn(operand2, elt, rbe, event);
Tom Zanussi100719d2018-01-15 20:51:52 -0600231
232 return val1 - val2;
233}
234
Tom Zanussidf35d932018-01-15 20:51:54 -0600235static u64 hist_field_unary_minus(struct hist_field *hist_field,
236 struct tracing_map_elt *elt,
237 struct ring_buffer_event *rbe,
238 void *event)
Tom Zanussi100719d2018-01-15 20:51:52 -0600239{
240 struct hist_field *operand = hist_field->operands[0];
241
Tom Zanussidf35d932018-01-15 20:51:54 -0600242 s64 sval = (s64)operand->fn(operand, elt, rbe, event);
Tom Zanussi100719d2018-01-15 20:51:52 -0600243 u64 val = (u64)-sval;
244
245 return val;
246}
247
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600248#define DEFINE_HIST_FIELD_FN(type) \
Tom Zanussifbd302c2018-01-15 20:51:43 -0600249 static u64 hist_field_##type(struct hist_field *hist_field, \
Tom Zanussidf35d932018-01-15 20:51:54 -0600250 struct tracing_map_elt *elt, \
251 struct ring_buffer_event *rbe, \
252 void *event) \
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600253{ \
254 type *addr = (type *)(event + hist_field->field->offset); \
255 \
Namhyung Kim79e577c2016-03-03 12:54:53 -0600256 return (u64)(unsigned long)*addr; \
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600257}
258
259DEFINE_HIST_FIELD_FN(s64);
260DEFINE_HIST_FIELD_FN(u64);
261DEFINE_HIST_FIELD_FN(s32);
262DEFINE_HIST_FIELD_FN(u32);
263DEFINE_HIST_FIELD_FN(s16);
264DEFINE_HIST_FIELD_FN(u16);
265DEFINE_HIST_FIELD_FN(s8);
266DEFINE_HIST_FIELD_FN(u8);
267
268#define for_each_hist_field(i, hist_data) \
269 for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
270
271#define for_each_hist_val_field(i, hist_data) \
272 for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
273
274#define for_each_hist_key_field(i, hist_data) \
275 for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
276
Tom Zanussi69a02002016-03-03 12:54:52 -0600277#define HIST_STACKTRACE_DEPTH 16
278#define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
279#define HIST_STACKTRACE_SKIP 5
280
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600281#define HITCOUNT_IDX 0
Tom Zanussi69a02002016-03-03 12:54:52 -0600282#define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600283
284enum hist_field_flags {
Tom Zanussi0d7a8322017-09-22 14:58:21 -0500285 HIST_FIELD_FL_HITCOUNT = 1 << 0,
286 HIST_FIELD_FL_KEY = 1 << 1,
287 HIST_FIELD_FL_STRING = 1 << 2,
288 HIST_FIELD_FL_HEX = 1 << 3,
289 HIST_FIELD_FL_SYM = 1 << 4,
290 HIST_FIELD_FL_SYM_OFFSET = 1 << 5,
291 HIST_FIELD_FL_EXECNAME = 1 << 6,
292 HIST_FIELD_FL_SYSCALL = 1 << 7,
293 HIST_FIELD_FL_STACKTRACE = 1 << 8,
294 HIST_FIELD_FL_LOG2 = 1 << 9,
Tom Zanussiad42feb2018-01-15 20:51:45 -0600295 HIST_FIELD_FL_TIMESTAMP = 1 << 10,
Tom Zanussi860f9f62018-01-15 20:51:48 -0600296 HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11,
Tom Zanussi30350d62018-01-15 20:51:49 -0600297 HIST_FIELD_FL_VAR = 1 << 12,
Tom Zanussi100719d2018-01-15 20:51:52 -0600298 HIST_FIELD_FL_EXPR = 1 << 13,
Tom Zanussi067fe032018-01-15 20:51:56 -0600299 HIST_FIELD_FL_VAR_REF = 1 << 14,
Tom Zanussi8b7622b2018-01-15 20:52:03 -0600300 HIST_FIELD_FL_CPU = 1 << 15,
Tom Zanussi7e8b88a2018-01-15 20:52:04 -0600301 HIST_FIELD_FL_ALIAS = 1 << 16,
Tom Zanussi30350d62018-01-15 20:51:49 -0600302};
303
304struct var_defs {
305 unsigned int n_vars;
306 char *name[TRACING_MAP_VARS_MAX];
307 char *expr[TRACING_MAP_VARS_MAX];
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600308};
309
310struct hist_trigger_attrs {
311 char *keys_str;
Tom Zanussif2606832016-03-03 12:54:43 -0600312 char *vals_str;
Tom Zanussie62347d2016-03-03 12:54:45 -0600313 char *sort_key_str;
Tom Zanussi5463bfd2016-03-03 12:54:59 -0600314 char *name;
Tom Zanussia4072fe2018-01-15 20:52:08 -0600315 char *clock;
Tom Zanussi83e99912016-03-03 12:54:46 -0600316 bool pause;
317 bool cont;
Tom Zanussie86ae9b2016-03-03 12:54:47 -0600318 bool clear;
Tom Zanussi860f9f62018-01-15 20:51:48 -0600319 bool ts_in_usecs;
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600320 unsigned int map_bits;
Tom Zanussi30350d62018-01-15 20:51:49 -0600321
322 char *assignment_str[TRACING_MAP_VARS_MAX];
323 unsigned int n_assignments;
324
Tom Zanussi0212e2a2018-01-15 20:51:57 -0600325 char *action_str[HIST_ACTIONS_MAX];
326 unsigned int n_actions;
327
Tom Zanussi30350d62018-01-15 20:51:49 -0600328 struct var_defs var_defs;
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600329};
330
Tom Zanussi02205a62018-01-15 20:51:59 -0600331struct field_var {
332 struct hist_field *var;
333 struct hist_field *val;
334};
335
336struct field_var_hist {
337 struct hist_trigger_data *hist_data;
338 char *cmd;
339};
340
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600341struct hist_trigger_data {
Tom Zanussi30350d62018-01-15 20:51:49 -0600342 struct hist_field *fields[HIST_FIELDS_MAX];
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600343 unsigned int n_vals;
344 unsigned int n_keys;
345 unsigned int n_fields;
Tom Zanussi30350d62018-01-15 20:51:49 -0600346 unsigned int n_vars;
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600347 unsigned int key_size;
348 struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX];
349 unsigned int n_sort_keys;
350 struct trace_event_file *event_file;
351 struct hist_trigger_attrs *attrs;
352 struct tracing_map *map;
Tom Zanussiad42feb2018-01-15 20:51:45 -0600353 bool enable_timestamps;
Tom Zanussi30350d62018-01-15 20:51:49 -0600354 bool remove;
Tom Zanussi067fe032018-01-15 20:51:56 -0600355 struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
356 unsigned int n_var_refs;
Tom Zanussi0212e2a2018-01-15 20:51:57 -0600357
358 struct action_data *actions[HIST_ACTIONS_MAX];
359 unsigned int n_actions;
Tom Zanussi02205a62018-01-15 20:51:59 -0600360
361 struct field_var *field_vars[SYNTH_FIELDS_MAX];
362 unsigned int n_field_vars;
363 unsigned int n_field_var_str;
364 struct field_var_hist *field_var_hists[SYNTH_FIELDS_MAX];
365 unsigned int n_field_var_hists;
Tom Zanussi50450602018-01-15 20:52:01 -0600366
Tom Zanussi7d18a102019-02-13 17:42:41 -0600367 struct field_var *save_vars[SYNTH_FIELDS_MAX];
368 unsigned int n_save_vars;
369 unsigned int n_save_var_str;
Tom Zanussi0212e2a2018-01-15 20:51:57 -0600370};
371
Masami Hiramatsu7bbab382018-11-05 18:03:33 +0900372static int synth_event_create(int argc, const char **argv);
373static int synth_event_show(struct seq_file *m, struct dyn_event *ev);
374static int synth_event_release(struct dyn_event *ev);
375static bool synth_event_is_busy(struct dyn_event *ev);
376static bool synth_event_match(const char *system, const char *event,
377 struct dyn_event *ev);
378
379static struct dyn_event_operations synth_event_ops = {
380 .create = synth_event_create,
381 .show = synth_event_show,
382 .is_busy = synth_event_is_busy,
383 .free = synth_event_release,
384 .match = synth_event_match,
385};
386
Tom Zanussi4b147932018-01-15 20:51:58 -0600387struct synth_field {
388 char *type;
389 char *name;
390 size_t size;
391 bool is_signed;
392 bool is_string;
393};
394
395struct synth_event {
Masami Hiramatsu7bbab382018-11-05 18:03:33 +0900396 struct dyn_event devent;
Tom Zanussi4b147932018-01-15 20:51:58 -0600397 int ref;
398 char *name;
399 struct synth_field **fields;
400 unsigned int n_fields;
401 unsigned int n_u64;
402 struct trace_event_class class;
403 struct trace_event_call call;
404 struct tracepoint *tp;
405};
406
Masami Hiramatsu7bbab382018-11-05 18:03:33 +0900407static bool is_synth_event(struct dyn_event *ev)
408{
409 return ev->ops == &synth_event_ops;
410}
411
412static struct synth_event *to_synth_event(struct dyn_event *ev)
413{
414 return container_of(ev, struct synth_event, devent);
415}
416
417static bool synth_event_is_busy(struct dyn_event *ev)
418{
419 struct synth_event *event = to_synth_event(ev);
420
421 return event->ref != 0;
422}
423
424static bool synth_event_match(const char *system, const char *event,
425 struct dyn_event *ev)
426{
427 struct synth_event *sev = to_synth_event(ev);
428
429 return strcmp(sev->name, event) == 0 &&
430 (!system || strcmp(system, SYNTH_SYSTEM) == 0);
431}
432
Tom Zanussi0212e2a2018-01-15 20:51:57 -0600433struct action_data;
434
435typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
436 struct tracing_map_elt *elt, void *rec,
Tom Zanussi7d18a102019-02-13 17:42:41 -0600437 struct ring_buffer_event *rbe, void *key,
Tom Zanussi0212e2a2018-01-15 20:51:57 -0600438 struct action_data *data, u64 *var_ref_vals);
439
Tom Zanussi466f4522019-02-13 17:42:44 -0600440typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val);
441
Tom Zanussi7d18a102019-02-13 17:42:41 -0600442enum handler_id {
443 HANDLER_ONMATCH = 1,
444 HANDLER_ONMAX,
Tom Zanussidff81f52019-02-13 17:42:48 -0600445 HANDLER_ONCHANGE,
Tom Zanussi7d18a102019-02-13 17:42:41 -0600446};
447
448enum action_id {
449 ACTION_SAVE = 1,
450 ACTION_TRACE,
Tom Zanussia3785b72019-02-13 17:42:46 -0600451 ACTION_SNAPSHOT,
Tom Zanussi7d18a102019-02-13 17:42:41 -0600452};
453
Tom Zanussi0212e2a2018-01-15 20:51:57 -0600454struct action_data {
Tom Zanussi7d18a102019-02-13 17:42:41 -0600455 enum handler_id handler;
456 enum action_id action;
457 char *action_name;
Tom Zanussi0212e2a2018-01-15 20:51:57 -0600458 action_fn_t fn;
Tom Zanussi7d18a102019-02-13 17:42:41 -0600459
Tom Zanussic282a382018-01-15 20:52:00 -0600460 unsigned int n_params;
461 char *params[SYNTH_FIELDS_MAX];
462
Tom Zanussic3e49502019-02-13 17:42:43 -0600463 /*
464 * When a histogram trigger is hit, the values of any
465 * references to variables, including variables being passed
466 * as parameters to synthetic events, are collected into a
467 * var_ref_vals array. This var_ref_idx is the index of the
468 * first param in the array to be passed to the synthetic
469 * event invocation.
470 */
471 unsigned int var_ref_idx;
472 struct synth_event *synth_event;
Tom Zanussie91eefd72019-02-13 17:42:50 -0600473 bool use_trace_keyword;
474 char *synth_event_name;
Tom Zanussic3e49502019-02-13 17:42:43 -0600475
Tom Zanussic282a382018-01-15 20:52:00 -0600476 union {
477 struct {
Tom Zanussic3e49502019-02-13 17:42:43 -0600478 char *event;
479 char *event_system;
480 } match_data;
Tom Zanussi50450602018-01-15 20:52:01 -0600481
482 struct {
Tom Zanussi466f4522019-02-13 17:42:44 -0600483 /*
484 * var_str contains the $-unstripped variable
485 * name referenced by var_ref, and used when
486 * printing the action. Because var_ref
487 * creation is deferred to create_actions(),
488 * we need a per-action way to save it until
489 * then, thus var_str.
490 */
Tom Zanussi50450602018-01-15 20:52:01 -0600491 char *var_str;
Tom Zanussi466f4522019-02-13 17:42:44 -0600492
493 /*
494 * var_ref refers to the variable being
495 * tracked e.g onmax($var).
496 */
497 struct hist_field *var_ref;
498
499 /*
500 * track_var contains the 'invisible' tracking
501 * variable created to keep the current
502 * e.g. max value.
503 */
504 struct hist_field *track_var;
505
506 check_track_val_fn_t check_val;
507 action_fn_t save_data;
508 } track_data;
Tom Zanussic282a382018-01-15 20:52:00 -0600509 };
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600510};
511
Tom Zanussia3785b72019-02-13 17:42:46 -0600512struct track_data {
513 u64 track_val;
514 bool updated;
515
516 unsigned int key_len;
517 void *key;
518 struct tracing_map_elt elt;
519
520 struct action_data *action_data;
521 struct hist_trigger_data *hist_data;
522};
523
524struct hist_elt_data {
525 char *comm;
526 u64 *var_ref_vals;
527 char *field_var_str[SYNTH_FIELDS_MAX];
528};
529
530struct snapshot_context {
531 struct tracing_map_elt *elt;
532 void *key;
533};
534
535static void track_data_free(struct track_data *track_data)
536{
537 struct hist_elt_data *elt_data;
538
539 if (!track_data)
540 return;
541
542 kfree(track_data->key);
543
544 elt_data = track_data->elt.private_data;
545 if (elt_data) {
546 kfree(elt_data->comm);
547 kfree(elt_data);
548 }
549
550 kfree(track_data);
551}
552
553static struct track_data *track_data_alloc(unsigned int key_len,
554 struct action_data *action_data,
555 struct hist_trigger_data *hist_data)
556{
557 struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
558 struct hist_elt_data *elt_data;
559
560 if (!data)
561 return ERR_PTR(-ENOMEM);
562
563 data->key = kzalloc(key_len, GFP_KERNEL);
564 if (!data->key) {
565 track_data_free(data);
566 return ERR_PTR(-ENOMEM);
567 }
568
569 data->key_len = key_len;
570 data->action_data = action_data;
571 data->hist_data = hist_data;
572
573 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
574 if (!elt_data) {
575 track_data_free(data);
576 return ERR_PTR(-ENOMEM);
577 }
578 data->elt.private_data = elt_data;
579
580 elt_data->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL);
581 if (!elt_data->comm) {
582 track_data_free(data);
583 return ERR_PTR(-ENOMEM);
584 }
585
586 return data;
587}
588
Tom Zanussia1a05bb2019-03-31 18:48:16 -0500589static char last_cmd[MAX_FILTER_STR_VAL];
590static char last_cmd_loc[MAX_FILTER_STR_VAL];
Tom Zanussif404da62018-01-15 20:52:05 -0600591
Tom Zanussid566c5e2019-03-31 18:48:17 -0500592static int errpos(char *str)
Tom Zanussif404da62018-01-15 20:52:05 -0600593{
Tom Zanussid566c5e2019-03-31 18:48:17 -0500594 return err_pos(last_cmd, str);
Tom Zanussif404da62018-01-15 20:52:05 -0600595}
596
Tom Zanussia1a05bb2019-03-31 18:48:16 -0500597static void last_cmd_set(struct trace_event_file *file, char *str)
Tom Zanussif404da62018-01-15 20:52:05 -0600598{
Tom Zanussia1a05bb2019-03-31 18:48:16 -0500599 const char *system = NULL, *name = NULL;
600 struct trace_event_call *call;
Tom Zanussif404da62018-01-15 20:52:05 -0600601
602 if (!str)
603 return;
604
Tom Zanussia1a05bb2019-03-31 18:48:16 -0500605 strncpy(last_cmd, str, MAX_FILTER_STR_VAL - 1);
Tom Zanussif404da62018-01-15 20:52:05 -0600606
Tom Zanussia1a05bb2019-03-31 18:48:16 -0500607 if (file) {
608 call = file->event_call;
Tom Zanussif404da62018-01-15 20:52:05 -0600609
Tom Zanussia1a05bb2019-03-31 18:48:16 -0500610 system = call->class->system;
611 if (system) {
612 name = trace_event_name(call);
613 if (!name)
614 system = NULL;
615 }
616 }
Tom Zanussif404da62018-01-15 20:52:05 -0600617
Tom Zanussia1a05bb2019-03-31 18:48:16 -0500618 if (system)
619 snprintf(last_cmd_loc, MAX_FILTER_STR_VAL, "hist:%s:%s", system, name);
Tom Zanussif404da62018-01-15 20:52:05 -0600620}
621
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -0400622static void hist_err(struct trace_array *tr, u8 err_type, u8 err_pos)
Tom Zanussif404da62018-01-15 20:52:05 -0600623{
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -0400624 tracing_log_err(tr, last_cmd_loc, last_cmd, err_text,
625 err_type, err_pos);
Tom Zanussif404da62018-01-15 20:52:05 -0600626}
627
628static void hist_err_clear(void)
629{
Tom Zanussia1a05bb2019-03-31 18:48:16 -0500630 last_cmd[0] = '\0';
631 last_cmd_loc[0] = '\0';
Tom Zanussif404da62018-01-15 20:52:05 -0600632}
633
Tom Zanussi4b147932018-01-15 20:51:58 -0600634struct synth_trace_event {
635 struct trace_entry ent;
636 u64 fields[];
637};
638
639static int synth_event_define_fields(struct trace_event_call *call)
640{
641 struct synth_trace_event trace;
642 int offset = offsetof(typeof(trace), fields);
643 struct synth_event *event = call->data;
644 unsigned int i, size, n_u64;
645 char *name, *type;
646 bool is_signed;
647 int ret = 0;
648
649 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
650 size = event->fields[i]->size;
651 is_signed = event->fields[i]->is_signed;
652 type = event->fields[i]->type;
653 name = event->fields[i]->name;
654 ret = trace_define_field(call, type, name, offset, size,
655 is_signed, FILTER_OTHER);
656 if (ret)
657 break;
658
659 if (event->fields[i]->is_string) {
660 offset += STR_VAR_LEN_MAX;
661 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
662 } else {
663 offset += sizeof(u64);
664 n_u64++;
665 }
666 }
667
668 event->n_u64 = n_u64;
669
670 return ret;
671}
672
673static bool synth_field_signed(char *type)
674{
Steven Rostedt (VMware)b6b27352018-12-20 13:20:07 -0500675 if (str_has_prefix(type, "u"))
Tom Zanussi4b147932018-01-15 20:51:58 -0600676 return false;
677
678 return true;
679}
680
681static int synth_field_is_string(char *type)
682{
683 if (strstr(type, "char[") != NULL)
684 return true;
685
686 return false;
687}
688
689static int synth_field_string_size(char *type)
690{
691 char buf[4], *end, *start;
692 unsigned int len;
693 int size, err;
694
695 start = strstr(type, "char[");
696 if (start == NULL)
697 return -EINVAL;
Tom Zanussi2f31ed92018-12-18 14:33:21 -0600698 start += sizeof("char[") - 1;
Tom Zanussi4b147932018-01-15 20:51:58 -0600699
700 end = strchr(type, ']');
701 if (!end || end < start)
702 return -EINVAL;
703
704 len = end - start;
705 if (len > 3)
706 return -EINVAL;
707
708 strncpy(buf, start, len);
709 buf[len] = '\0';
710
711 err = kstrtouint(buf, 0, &size);
712 if (err)
713 return err;
714
715 if (size > STR_VAR_LEN_MAX)
716 return -EINVAL;
717
718 return size;
719}
720
721static int synth_field_size(char *type)
722{
723 int size = 0;
724
725 if (strcmp(type, "s64") == 0)
726 size = sizeof(s64);
727 else if (strcmp(type, "u64") == 0)
728 size = sizeof(u64);
729 else if (strcmp(type, "s32") == 0)
730 size = sizeof(s32);
731 else if (strcmp(type, "u32") == 0)
732 size = sizeof(u32);
733 else if (strcmp(type, "s16") == 0)
734 size = sizeof(s16);
735 else if (strcmp(type, "u16") == 0)
736 size = sizeof(u16);
737 else if (strcmp(type, "s8") == 0)
738 size = sizeof(s8);
739 else if (strcmp(type, "u8") == 0)
740 size = sizeof(u8);
741 else if (strcmp(type, "char") == 0)
742 size = sizeof(char);
743 else if (strcmp(type, "unsigned char") == 0)
744 size = sizeof(unsigned char);
745 else if (strcmp(type, "int") == 0)
746 size = sizeof(int);
747 else if (strcmp(type, "unsigned int") == 0)
748 size = sizeof(unsigned int);
749 else if (strcmp(type, "long") == 0)
750 size = sizeof(long);
751 else if (strcmp(type, "unsigned long") == 0)
752 size = sizeof(unsigned long);
753 else if (strcmp(type, "pid_t") == 0)
754 size = sizeof(pid_t);
755 else if (synth_field_is_string(type))
756 size = synth_field_string_size(type);
757
758 return size;
759}
760
761static const char *synth_field_fmt(char *type)
762{
763 const char *fmt = "%llu";
764
765 if (strcmp(type, "s64") == 0)
766 fmt = "%lld";
767 else if (strcmp(type, "u64") == 0)
768 fmt = "%llu";
769 else if (strcmp(type, "s32") == 0)
770 fmt = "%d";
771 else if (strcmp(type, "u32") == 0)
772 fmt = "%u";
773 else if (strcmp(type, "s16") == 0)
774 fmt = "%d";
775 else if (strcmp(type, "u16") == 0)
776 fmt = "%u";
777 else if (strcmp(type, "s8") == 0)
778 fmt = "%d";
779 else if (strcmp(type, "u8") == 0)
780 fmt = "%u";
781 else if (strcmp(type, "char") == 0)
782 fmt = "%d";
783 else if (strcmp(type, "unsigned char") == 0)
784 fmt = "%u";
785 else if (strcmp(type, "int") == 0)
786 fmt = "%d";
787 else if (strcmp(type, "unsigned int") == 0)
788 fmt = "%u";
789 else if (strcmp(type, "long") == 0)
790 fmt = "%ld";
791 else if (strcmp(type, "unsigned long") == 0)
792 fmt = "%lu";
793 else if (strcmp(type, "pid_t") == 0)
794 fmt = "%d";
795 else if (synth_field_is_string(type))
796 fmt = "%s";
797
798 return fmt;
799}
800
801static enum print_line_t print_synth_event(struct trace_iterator *iter,
802 int flags,
803 struct trace_event *event)
804{
805 struct trace_array *tr = iter->tr;
806 struct trace_seq *s = &iter->seq;
807 struct synth_trace_event *entry;
808 struct synth_event *se;
809 unsigned int i, n_u64;
810 char print_fmt[32];
811 const char *fmt;
812
813 entry = (struct synth_trace_event *)iter->ent;
814 se = container_of(event, struct synth_event, call.event);
815
816 trace_seq_printf(s, "%s: ", se->name);
817
818 for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
819 if (trace_seq_has_overflowed(s))
820 goto end;
821
822 fmt = synth_field_fmt(se->fields[i]->type);
823
824 /* parameter types */
825 if (tr->trace_flags & TRACE_ITER_VERBOSE)
826 trace_seq_printf(s, "%s ", fmt);
827
828 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
829
830 /* parameter values */
831 if (se->fields[i]->is_string) {
832 trace_seq_printf(s, print_fmt, se->fields[i]->name,
833 (char *)&entry->fields[n_u64],
834 i == se->n_fields - 1 ? "" : " ");
835 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
836 } else {
837 trace_seq_printf(s, print_fmt, se->fields[i]->name,
838 entry->fields[n_u64],
839 i == se->n_fields - 1 ? "" : " ");
840 n_u64++;
841 }
842 }
843end:
844 trace_seq_putc(s, '\n');
845
846 return trace_handle_return(s);
847}
848
849static struct trace_event_functions synth_event_funcs = {
850 .trace = print_synth_event
851};
852
853static notrace void trace_event_raw_event_synth(void *__data,
854 u64 *var_ref_vals,
855 unsigned int var_ref_idx)
856{
857 struct trace_event_file *trace_file = __data;
858 struct synth_trace_event *entry;
859 struct trace_event_buffer fbuffer;
Steven Rostedt (VMware)4708abc2018-02-07 17:29:46 -0500860 struct ring_buffer *buffer;
Tom Zanussi4b147932018-01-15 20:51:58 -0600861 struct synth_event *event;
862 unsigned int i, n_u64;
863 int fields_size = 0;
864
865 event = trace_file->event_call->data;
866
867 if (trace_trigger_soft_disabled(trace_file))
868 return;
869
870 fields_size = event->n_u64 * sizeof(u64);
871
Steven Rostedt (VMware)4708abc2018-02-07 17:29:46 -0500872 /*
873 * Avoid ring buffer recursion detection, as this event
874 * is being performed within another event.
875 */
876 buffer = trace_file->tr->trace_buffer.buffer;
877 ring_buffer_nest_start(buffer);
878
Tom Zanussi4b147932018-01-15 20:51:58 -0600879 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
880 sizeof(*entry) + fields_size);
881 if (!entry)
Steven Rostedt (VMware)4708abc2018-02-07 17:29:46 -0500882 goto out;
Tom Zanussi4b147932018-01-15 20:51:58 -0600883
884 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
885 if (event->fields[i]->is_string) {
886 char *str_val = (char *)(long)var_ref_vals[var_ref_idx + i];
887 char *str_field = (char *)&entry->fields[n_u64];
888
Tom Zanussiad452872018-03-28 15:10:56 -0500889 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
Tom Zanussi4b147932018-01-15 20:51:58 -0600890 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
891 } else {
892 entry->fields[n_u64] = var_ref_vals[var_ref_idx + i];
893 n_u64++;
894 }
895 }
896
897 trace_event_buffer_commit(&fbuffer);
Steven Rostedt (VMware)4708abc2018-02-07 17:29:46 -0500898out:
899 ring_buffer_nest_end(buffer);
Tom Zanussi4b147932018-01-15 20:51:58 -0600900}
901
902static void free_synth_event_print_fmt(struct trace_event_call *call)
903{
904 if (call) {
905 kfree(call->print_fmt);
906 call->print_fmt = NULL;
907 }
908}
909
910static int __set_synth_event_print_fmt(struct synth_event *event,
911 char *buf, int len)
912{
913 const char *fmt;
914 int pos = 0;
915 int i;
916
917 /* When len=0, we just calculate the needed length */
918#define LEN_OR_ZERO (len ? len - pos : 0)
919
920 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
921 for (i = 0; i < event->n_fields; i++) {
922 fmt = synth_field_fmt(event->fields[i]->type);
923 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
924 event->fields[i]->name, fmt,
925 i == event->n_fields - 1 ? "" : ", ");
926 }
927 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
928
929 for (i = 0; i < event->n_fields; i++) {
930 pos += snprintf(buf + pos, LEN_OR_ZERO,
931 ", REC->%s", event->fields[i]->name);
932 }
933
934#undef LEN_OR_ZERO
935
936 /* return the length of print_fmt */
937 return pos;
938}
939
940static int set_synth_event_print_fmt(struct trace_event_call *call)
941{
942 struct synth_event *event = call->data;
943 char *print_fmt;
944 int len;
945
946 /* First: called with 0 length to calculate the needed length */
947 len = __set_synth_event_print_fmt(event, NULL, 0);
948
949 print_fmt = kmalloc(len + 1, GFP_KERNEL);
950 if (!print_fmt)
951 return -ENOMEM;
952
953 /* Second: actually write the @print_fmt */
954 __set_synth_event_print_fmt(event, print_fmt, len + 1);
955 call->print_fmt = print_fmt;
956
957 return 0;
958}
959
960static void free_synth_field(struct synth_field *field)
961{
962 kfree(field->type);
963 kfree(field->name);
964 kfree(field);
965}
966
Masami Hiramatsu7bbab382018-11-05 18:03:33 +0900967static struct synth_field *parse_synth_field(int argc, const char **argv,
Masami Hiramatsu282447b2018-10-18 22:12:05 +0900968 int *consumed)
Tom Zanussi4b147932018-01-15 20:51:58 -0600969{
970 struct synth_field *field;
Masami Hiramatsu7bbab382018-11-05 18:03:33 +0900971 const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
Tom Zanussi4b147932018-01-15 20:51:58 -0600972 int len, ret = 0;
Tom Zanussi4b147932018-01-15 20:51:58 -0600973
974 if (field_type[0] == ';')
975 field_type++;
976
Masami Hiramatsu282447b2018-10-18 22:12:05 +0900977 if (!strcmp(field_type, "unsigned")) {
978 if (argc < 3)
979 return ERR_PTR(-EINVAL);
980 prefix = "unsigned ";
981 field_type = argv[1];
982 field_name = argv[2];
983 *consumed = 3;
984 } else {
985 field_name = argv[1];
986 *consumed = 2;
987 }
988
Tom Zanussi4b147932018-01-15 20:51:58 -0600989 field = kzalloc(sizeof(*field), GFP_KERNEL);
990 if (!field)
991 return ERR_PTR(-ENOMEM);
992
Masami Hiramatsu7bbab382018-11-05 18:03:33 +0900993 len = strlen(field_name);
Tom Zanussi4b147932018-01-15 20:51:58 -0600994 array = strchr(field_name, '[');
995 if (array)
Masami Hiramatsu7bbab382018-11-05 18:03:33 +0900996 len -= strlen(array);
997 else if (field_name[len - 1] == ';')
998 len--;
999
1000 field->name = kmemdup_nul(field_name, len, GFP_KERNEL);
1001 if (!field->name) {
1002 ret = -ENOMEM;
1003 goto free;
1004 }
1005
1006 if (field_type[0] == ';')
1007 field_type++;
1008 len = strlen(field_type) + 1;
1009 if (array)
Tom Zanussi4b147932018-01-15 20:51:58 -06001010 len += strlen(array);
Masami Hiramatsu282447b2018-10-18 22:12:05 +09001011 if (prefix)
1012 len += strlen(prefix);
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001013
Tom Zanussi4b147932018-01-15 20:51:58 -06001014 field->type = kzalloc(len, GFP_KERNEL);
1015 if (!field->type) {
1016 ret = -ENOMEM;
1017 goto free;
1018 }
Masami Hiramatsu282447b2018-10-18 22:12:05 +09001019 if (prefix)
1020 strcat(field->type, prefix);
Tom Zanussi4b147932018-01-15 20:51:58 -06001021 strcat(field->type, field_type);
1022 if (array) {
1023 strcat(field->type, array);
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001024 if (field->type[len - 1] == ';')
1025 field->type[len - 1] = '\0';
Tom Zanussi4b147932018-01-15 20:51:58 -06001026 }
1027
1028 field->size = synth_field_size(field->type);
1029 if (!field->size) {
1030 ret = -EINVAL;
1031 goto free;
1032 }
1033
1034 if (synth_field_is_string(field->type))
1035 field->is_string = true;
1036
1037 field->is_signed = synth_field_signed(field->type);
1038
Tom Zanussi4b147932018-01-15 20:51:58 -06001039 out:
1040 return field;
1041 free:
1042 free_synth_field(field);
1043 field = ERR_PTR(ret);
1044 goto out;
1045}
1046
1047static void free_synth_tracepoint(struct tracepoint *tp)
1048{
1049 if (!tp)
1050 return;
1051
1052 kfree(tp->name);
1053 kfree(tp);
1054}
1055
1056static struct tracepoint *alloc_synth_tracepoint(char *name)
1057{
1058 struct tracepoint *tp;
1059
1060 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
1061 if (!tp)
1062 return ERR_PTR(-ENOMEM);
1063
1064 tp->name = kstrdup(name, GFP_KERNEL);
1065 if (!tp->name) {
1066 kfree(tp);
1067 return ERR_PTR(-ENOMEM);
1068 }
1069
1070 return tp;
1071}
1072
1073typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
1074 unsigned int var_ref_idx);
1075
1076static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
1077 unsigned int var_ref_idx)
1078{
1079 struct tracepoint *tp = event->tp;
1080
1081 if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
1082 struct tracepoint_func *probe_func_ptr;
1083 synth_probe_func_t probe_func;
1084 void *__data;
1085
1086 if (!(cpu_online(raw_smp_processor_id())))
1087 return;
1088
1089 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
1090 if (probe_func_ptr) {
1091 do {
1092 probe_func = probe_func_ptr->func;
1093 __data = probe_func_ptr->data;
1094 probe_func(__data, var_ref_vals, var_ref_idx);
1095 } while ((++probe_func_ptr)->func);
1096 }
1097 }
1098}
1099
1100static struct synth_event *find_synth_event(const char *name)
1101{
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001102 struct dyn_event *pos;
Tom Zanussi4b147932018-01-15 20:51:58 -06001103 struct synth_event *event;
1104
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001105 for_each_dyn_event(pos) {
1106 if (!is_synth_event(pos))
1107 continue;
1108 event = to_synth_event(pos);
Tom Zanussi4b147932018-01-15 20:51:58 -06001109 if (strcmp(event->name, name) == 0)
1110 return event;
1111 }
1112
1113 return NULL;
1114}
1115
1116static int register_synth_event(struct synth_event *event)
1117{
1118 struct trace_event_call *call = &event->call;
1119 int ret = 0;
1120
1121 event->call.class = &event->class;
1122 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
1123 if (!event->class.system) {
1124 ret = -ENOMEM;
1125 goto out;
1126 }
1127
1128 event->tp = alloc_synth_tracepoint(event->name);
1129 if (IS_ERR(event->tp)) {
1130 ret = PTR_ERR(event->tp);
1131 event->tp = NULL;
1132 goto out;
1133 }
1134
1135 INIT_LIST_HEAD(&call->class->fields);
1136 call->event.funcs = &synth_event_funcs;
1137 call->class->define_fields = synth_event_define_fields;
1138
1139 ret = register_trace_event(&call->event);
1140 if (!ret) {
1141 ret = -ENODEV;
1142 goto out;
1143 }
1144 call->flags = TRACE_EVENT_FL_TRACEPOINT;
1145 call->class->reg = trace_event_reg;
1146 call->class->probe = trace_event_raw_event_synth;
1147 call->data = event;
1148 call->tp = event->tp;
1149
Steven Rostedt (VMware)7e1413e2018-12-04 13:35:45 -05001150 ret = trace_add_event_call(call);
Tom Zanussi4b147932018-01-15 20:51:58 -06001151 if (ret) {
1152 pr_warn("Failed to register synthetic event: %s\n",
1153 trace_event_name(call));
1154 goto err;
1155 }
1156
1157 ret = set_synth_event_print_fmt(call);
1158 if (ret < 0) {
Steven Rostedt (VMware)7e1413e2018-12-04 13:35:45 -05001159 trace_remove_event_call(call);
Tom Zanussi4b147932018-01-15 20:51:58 -06001160 goto err;
1161 }
1162 out:
1163 return ret;
1164 err:
1165 unregister_trace_event(&call->event);
1166 goto out;
1167}
1168
1169static int unregister_synth_event(struct synth_event *event)
1170{
1171 struct trace_event_call *call = &event->call;
1172 int ret;
1173
Steven Rostedt (VMware)7e1413e2018-12-04 13:35:45 -05001174 ret = trace_remove_event_call(call);
Tom Zanussi4b147932018-01-15 20:51:58 -06001175
1176 return ret;
1177}
1178
1179static void free_synth_event(struct synth_event *event)
1180{
1181 unsigned int i;
1182
1183 if (!event)
1184 return;
1185
1186 for (i = 0; i < event->n_fields; i++)
1187 free_synth_field(event->fields[i]);
1188
1189 kfree(event->fields);
1190 kfree(event->name);
1191 kfree(event->class.system);
1192 free_synth_tracepoint(event->tp);
1193 free_synth_event_print_fmt(&event->call);
1194 kfree(event);
1195}
1196
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001197static struct synth_event *alloc_synth_event(const char *name, int n_fields,
Tom Zanussi4b147932018-01-15 20:51:58 -06001198 struct synth_field **fields)
1199{
1200 struct synth_event *event;
1201 unsigned int i;
1202
1203 event = kzalloc(sizeof(*event), GFP_KERNEL);
1204 if (!event) {
1205 event = ERR_PTR(-ENOMEM);
1206 goto out;
1207 }
1208
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001209 event->name = kstrdup(name, GFP_KERNEL);
Tom Zanussi4b147932018-01-15 20:51:58 -06001210 if (!event->name) {
1211 kfree(event);
1212 event = ERR_PTR(-ENOMEM);
1213 goto out;
1214 }
1215
1216 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
1217 if (!event->fields) {
1218 free_synth_event(event);
1219 event = ERR_PTR(-ENOMEM);
1220 goto out;
1221 }
1222
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001223 dyn_event_init(&event->devent, &synth_event_ops);
1224
Tom Zanussi4b147932018-01-15 20:51:58 -06001225 for (i = 0; i < n_fields; i++)
1226 event->fields[i] = fields[i];
1227
1228 event->n_fields = n_fields;
1229 out:
1230 return event;
1231}
1232
Tom Zanussic282a382018-01-15 20:52:00 -06001233static void action_trace(struct hist_trigger_data *hist_data,
1234 struct tracing_map_elt *elt, void *rec,
Tom Zanussi7d18a102019-02-13 17:42:41 -06001235 struct ring_buffer_event *rbe, void *key,
Tom Zanussic282a382018-01-15 20:52:00 -06001236 struct action_data *data, u64 *var_ref_vals)
1237{
Tom Zanussic3e49502019-02-13 17:42:43 -06001238 struct synth_event *event = data->synth_event;
Tom Zanussic282a382018-01-15 20:52:00 -06001239
Tom Zanussic3e49502019-02-13 17:42:43 -06001240 trace_synth(event, var_ref_vals, data->var_ref_idx);
Tom Zanussic282a382018-01-15 20:52:00 -06001241}
1242
1243struct hist_var_data {
1244 struct list_head list;
1245 struct hist_trigger_data *hist_data;
1246};
1247
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001248static int __create_synth_event(int argc, const char *name, const char **argv)
Tom Zanussi4b147932018-01-15 20:51:58 -06001249{
1250 struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1251 struct synth_event *event = NULL;
Masami Hiramatsu282447b2018-10-18 22:12:05 +09001252 int i, consumed = 0, n_fields = 0, ret = 0;
Tom Zanussi4b147932018-01-15 20:51:58 -06001253
1254 /*
1255 * Argument syntax:
1256 * - Add synthetic event: <event_name> field[;field] ...
1257 * - Remove synthetic event: !<event_name> field[;field] ...
1258 * where 'field' = type field_name
1259 */
Tom Zanussi4b147932018-01-15 20:51:58 -06001260
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001261 if (name[0] == '\0' || argc < 1)
1262 return -EINVAL;
1263
1264 mutex_lock(&event_mutex);
Tom Zanussi4b147932018-01-15 20:51:58 -06001265
1266 event = find_synth_event(name);
1267 if (event) {
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001268 ret = -EEXIST;
Tom Zanussi4b147932018-01-15 20:51:58 -06001269 goto out;
Masami Hiramatsu18858512018-10-22 00:08:20 +09001270 }
Tom Zanussi4b147932018-01-15 20:51:58 -06001271
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001272 for (i = 0; i < argc - 1; i++) {
Tom Zanussi4b147932018-01-15 20:51:58 -06001273 if (strcmp(argv[i], ";") == 0)
1274 continue;
1275 if (n_fields == SYNTH_FIELDS_MAX) {
1276 ret = -EINVAL;
1277 goto err;
1278 }
1279
Masami Hiramatsu282447b2018-10-18 22:12:05 +09001280 field = parse_synth_field(argc - i, &argv[i], &consumed);
Tom Zanussi4b147932018-01-15 20:51:58 -06001281 if (IS_ERR(field)) {
1282 ret = PTR_ERR(field);
1283 goto err;
1284 }
Masami Hiramatsu282447b2018-10-18 22:12:05 +09001285 fields[n_fields++] = field;
1286 i += consumed - 1;
Tom Zanussi4b147932018-01-15 20:51:58 -06001287 }
1288
Masami Hiramatsua360d9e2018-10-18 22:12:34 +09001289 if (i < argc && strcmp(argv[i], ";") != 0) {
Tom Zanussi4b147932018-01-15 20:51:58 -06001290 ret = -EINVAL;
1291 goto err;
1292 }
1293
1294 event = alloc_synth_event(name, n_fields, fields);
1295 if (IS_ERR(event)) {
1296 ret = PTR_ERR(event);
1297 event = NULL;
1298 goto err;
1299 }
Masami Hiramatsufaacb3612018-11-05 18:01:12 +09001300 ret = register_synth_event(event);
1301 if (!ret)
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001302 dyn_event_add(&event->devent);
Masami Hiramatsufaacb3612018-11-05 18:01:12 +09001303 else
1304 free_synth_event(event);
Tom Zanussi4b147932018-01-15 20:51:58 -06001305 out:
Masami Hiramatsufc800a12018-11-05 18:00:43 +09001306 mutex_unlock(&event_mutex);
Tom Zanussi4b147932018-01-15 20:51:58 -06001307
1308 return ret;
1309 err:
Tom Zanussi4b147932018-01-15 20:51:58 -06001310 for (i = 0; i < n_fields; i++)
1311 free_synth_field(fields[i]);
Tom Zanussi4b147932018-01-15 20:51:58 -06001312
Masami Hiramatsufaacb3612018-11-05 18:01:12 +09001313 goto out;
Tom Zanussi4b147932018-01-15 20:51:58 -06001314}
1315
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001316static int create_or_delete_synth_event(int argc, char **argv)
Tom Zanussi4b147932018-01-15 20:51:58 -06001317{
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001318 const char *name = argv[0];
1319 struct synth_event *event = NULL;
1320 int ret;
Tom Zanussi4b147932018-01-15 20:51:58 -06001321
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001322 /* trace_run_command() ensures argc != 0 */
1323 if (name[0] == '!') {
1324 mutex_lock(&event_mutex);
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001325 event = find_synth_event(name + 1);
1326 if (event) {
1327 if (event->ref)
1328 ret = -EBUSY;
1329 else {
1330 ret = unregister_synth_event(event);
1331 if (!ret) {
1332 dyn_event_remove(&event->devent);
1333 free_synth_event(event);
1334 }
1335 }
Masami Hiramatsufaacb3612018-11-05 18:01:12 +09001336 } else
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001337 ret = -ENOENT;
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001338 mutex_unlock(&event_mutex);
1339 return ret;
Tom Zanussi4b147932018-01-15 20:51:58 -06001340 }
1341
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001342 ret = __create_synth_event(argc - 1, name, (const char **)argv + 1);
1343 return ret == -ECANCELED ? -EINVAL : ret;
Tom Zanussi4b147932018-01-15 20:51:58 -06001344}
1345
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001346static int synth_event_create(int argc, const char **argv)
Tom Zanussi4b147932018-01-15 20:51:58 -06001347{
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001348 const char *name = argv[0];
1349 int len;
Tom Zanussi4b147932018-01-15 20:51:58 -06001350
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001351 if (name[0] != 's' || name[1] != ':')
1352 return -ECANCELED;
1353 name += 2;
1354
1355 /* This interface accepts group name prefix */
1356 if (strchr(name, '/')) {
Tom Zanussied581aa2019-02-04 15:07:23 -06001357 len = str_has_prefix(name, SYNTH_SYSTEM "/");
1358 if (len == 0)
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001359 return -EINVAL;
1360 name += len;
1361 }
1362 return __create_synth_event(argc - 1, name, argv + 1);
Tom Zanussi4b147932018-01-15 20:51:58 -06001363}
1364
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001365static int synth_event_release(struct dyn_event *ev)
Tom Zanussi4b147932018-01-15 20:51:58 -06001366{
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001367 struct synth_event *event = to_synth_event(ev);
1368 int ret;
1369
1370 if (event->ref)
1371 return -EBUSY;
1372
1373 ret = unregister_synth_event(event);
1374 if (ret)
1375 return ret;
1376
1377 dyn_event_remove(ev);
1378 free_synth_event(event);
1379 return 0;
Tom Zanussi4b147932018-01-15 20:51:58 -06001380}
1381
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001382static int __synth_event_show(struct seq_file *m, struct synth_event *event)
Tom Zanussi4b147932018-01-15 20:51:58 -06001383{
1384 struct synth_field *field;
Tom Zanussi4b147932018-01-15 20:51:58 -06001385 unsigned int i;
1386
1387 seq_printf(m, "%s\t", event->name);
1388
1389 for (i = 0; i < event->n_fields; i++) {
1390 field = event->fields[i];
1391
1392 /* parameter values */
1393 seq_printf(m, "%s %s%s", field->type, field->name,
1394 i == event->n_fields - 1 ? "" : "; ");
1395 }
1396
1397 seq_putc(m, '\n');
1398
1399 return 0;
1400}
1401
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001402static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
1403{
1404 struct synth_event *event = to_synth_event(ev);
1405
1406 seq_printf(m, "s:%s/", event->class.system);
1407
1408 return __synth_event_show(m, event);
1409}
1410
1411static int synth_events_seq_show(struct seq_file *m, void *v)
1412{
1413 struct dyn_event *ev = v;
1414
1415 if (!is_synth_event(ev))
1416 return 0;
1417
1418 return __synth_event_show(m, to_synth_event(ev));
1419}
1420
Tom Zanussi4b147932018-01-15 20:51:58 -06001421static const struct seq_operations synth_events_seq_op = {
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001422 .start = dyn_event_seq_start,
1423 .next = dyn_event_seq_next,
1424 .stop = dyn_event_seq_stop,
1425 .show = synth_events_seq_show,
Tom Zanussi4b147932018-01-15 20:51:58 -06001426};
1427
1428static int synth_events_open(struct inode *inode, struct file *file)
1429{
1430 int ret;
1431
1432 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001433 ret = dyn_events_release_all(&synth_event_ops);
Tom Zanussi4b147932018-01-15 20:51:58 -06001434 if (ret < 0)
1435 return ret;
1436 }
1437
1438 return seq_open(file, &synth_events_seq_op);
1439}
1440
1441static ssize_t synth_events_write(struct file *file,
1442 const char __user *buffer,
1443 size_t count, loff_t *ppos)
1444{
1445 return trace_parse_run_command(file, buffer, count, ppos,
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001446 create_or_delete_synth_event);
Tom Zanussi4b147932018-01-15 20:51:58 -06001447}
1448
1449static const struct file_operations synth_events_fops = {
1450 .open = synth_events_open,
1451 .write = synth_events_write,
1452 .read = seq_read,
1453 .llseek = seq_lseek,
1454 .release = seq_release,
1455};
1456
Tom Zanussidf35d932018-01-15 20:51:54 -06001457static u64 hist_field_timestamp(struct hist_field *hist_field,
1458 struct tracing_map_elt *elt,
1459 struct ring_buffer_event *rbe,
1460 void *event)
Tom Zanussi860f9f62018-01-15 20:51:48 -06001461{
1462 struct hist_trigger_data *hist_data = hist_field->hist_data;
1463 struct trace_array *tr = hist_data->event_file->tr;
1464
1465 u64 ts = ring_buffer_event_time_stamp(rbe);
1466
1467 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
1468 ts = ns2usecs(ts);
1469
1470 return ts;
1471}
1472
Tom Zanussi8b7622b2018-01-15 20:52:03 -06001473static u64 hist_field_cpu(struct hist_field *hist_field,
1474 struct tracing_map_elt *elt,
1475 struct ring_buffer_event *rbe,
1476 void *event)
1477{
1478 int cpu = smp_processor_id();
1479
1480 return cpu;
1481}
1482
Tom Zanusside40f032018-12-18 14:33:23 -06001483/**
1484 * check_field_for_var_ref - Check if a VAR_REF field references a variable
1485 * @hist_field: The VAR_REF field to check
1486 * @var_data: The hist trigger that owns the variable
1487 * @var_idx: The trigger variable identifier
1488 *
1489 * Check the given VAR_REF field to see whether or not it references
1490 * the given variable associated with the given trigger.
1491 *
1492 * Return: The VAR_REF field if it does reference the variable, NULL if not
1493 */
Tom Zanussi067fe032018-01-15 20:51:56 -06001494static struct hist_field *
1495check_field_for_var_ref(struct hist_field *hist_field,
1496 struct hist_trigger_data *var_data,
1497 unsigned int var_idx)
1498{
Tom Zanussie4f6d242018-12-19 13:09:16 -06001499 WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
Tom Zanussi067fe032018-01-15 20:51:56 -06001500
Tom Zanussie4f6d242018-12-19 13:09:16 -06001501 if (hist_field && hist_field->var.idx == var_idx &&
1502 hist_field->var.hist_data == var_data)
1503 return hist_field;
Tom Zanussi067fe032018-01-15 20:51:56 -06001504
Tom Zanussie4f6d242018-12-19 13:09:16 -06001505 return NULL;
Tom Zanussi067fe032018-01-15 20:51:56 -06001506}
1507
Tom Zanusside40f032018-12-18 14:33:23 -06001508/**
1509 * find_var_ref - Check if a trigger has a reference to a trigger variable
1510 * @hist_data: The hist trigger that might have a reference to the variable
1511 * @var_data: The hist trigger that owns the variable
1512 * @var_idx: The trigger variable identifier
1513 *
1514 * Check the list of var_refs[] on the first hist trigger to see
1515 * whether any of them are references to the variable on the second
1516 * trigger.
1517 *
1518 * Return: The VAR_REF field referencing the variable if so, NULL if not
1519 */
Tom Zanussi067fe032018-01-15 20:51:56 -06001520static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
1521 struct hist_trigger_data *var_data,
1522 unsigned int var_idx)
1523{
Tom Zanussie4f6d242018-12-19 13:09:16 -06001524 struct hist_field *hist_field;
Tom Zanussi067fe032018-01-15 20:51:56 -06001525 unsigned int i;
1526
Tom Zanussie4f6d242018-12-19 13:09:16 -06001527 for (i = 0; i < hist_data->n_var_refs; i++) {
1528 hist_field = hist_data->var_refs[i];
1529 if (check_field_for_var_ref(hist_field, var_data, var_idx))
1530 return hist_field;
Tom Zanussi067fe032018-01-15 20:51:56 -06001531 }
1532
Tom Zanussie4f6d242018-12-19 13:09:16 -06001533 return NULL;
Tom Zanussi067fe032018-01-15 20:51:56 -06001534}
1535
Tom Zanusside40f032018-12-18 14:33:23 -06001536/**
1537 * find_any_var_ref - Check if there is a reference to a given trigger variable
1538 * @hist_data: The hist trigger
1539 * @var_idx: The trigger variable identifier
1540 *
1541 * Check to see whether the given variable is currently referenced by
1542 * any other trigger.
1543 *
1544 * The trigger the variable is defined on is explicitly excluded - the
1545 * assumption being that a self-reference doesn't prevent a trigger
1546 * from being removed.
1547 *
1548 * Return: The VAR_REF field referencing the variable if so, NULL if not
1549 */
Tom Zanussi067fe032018-01-15 20:51:56 -06001550static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
1551 unsigned int var_idx)
1552{
1553 struct trace_array *tr = hist_data->event_file->tr;
1554 struct hist_field *found = NULL;
1555 struct hist_var_data *var_data;
1556
1557 list_for_each_entry(var_data, &tr->hist_vars, list) {
1558 if (var_data->hist_data == hist_data)
1559 continue;
1560 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
1561 if (found)
1562 break;
1563 }
1564
1565 return found;
1566}
1567
Tom Zanusside40f032018-12-18 14:33:23 -06001568/**
1569 * check_var_refs - Check if there is a reference to any of trigger's variables
1570 * @hist_data: The hist trigger
1571 *
1572 * A trigger can define one or more variables. If any one of them is
1573 * currently referenced by any other trigger, this function will
1574 * determine that.
1575
1576 * Typically used to determine whether or not a trigger can be removed
1577 * - if there are any references to a trigger's variables, it cannot.
1578 *
1579 * Return: True if there is a reference to any of trigger's variables
1580 */
Tom Zanussi067fe032018-01-15 20:51:56 -06001581static bool check_var_refs(struct hist_trigger_data *hist_data)
1582{
1583 struct hist_field *field;
1584 bool found = false;
1585 int i;
1586
1587 for_each_hist_field(i, hist_data) {
1588 field = hist_data->fields[i];
1589 if (field && field->flags & HIST_FIELD_FL_VAR) {
1590 if (find_any_var_ref(hist_data, field->var.idx)) {
1591 found = true;
1592 break;
1593 }
1594 }
1595 }
1596
1597 return found;
1598}
1599
1600static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
1601{
1602 struct trace_array *tr = hist_data->event_file->tr;
1603 struct hist_var_data *var_data, *found = NULL;
1604
1605 list_for_each_entry(var_data, &tr->hist_vars, list) {
1606 if (var_data->hist_data == hist_data) {
1607 found = var_data;
1608 break;
1609 }
1610 }
1611
1612 return found;
1613}
1614
1615static bool field_has_hist_vars(struct hist_field *hist_field,
1616 unsigned int level)
1617{
1618 int i;
1619
1620 if (level > 3)
1621 return false;
1622
1623 if (!hist_field)
1624 return false;
1625
1626 if (hist_field->flags & HIST_FIELD_FL_VAR ||
1627 hist_field->flags & HIST_FIELD_FL_VAR_REF)
1628 return true;
1629
1630 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
1631 struct hist_field *operand;
1632
1633 operand = hist_field->operands[i];
1634 if (field_has_hist_vars(operand, level + 1))
1635 return true;
1636 }
1637
1638 return false;
1639}
1640
1641static bool has_hist_vars(struct hist_trigger_data *hist_data)
1642{
1643 struct hist_field *hist_field;
1644 int i;
1645
1646 for_each_hist_field(i, hist_data) {
1647 hist_field = hist_data->fields[i];
1648 if (field_has_hist_vars(hist_field, 0))
1649 return true;
1650 }
1651
1652 return false;
1653}
1654
1655static int save_hist_vars(struct hist_trigger_data *hist_data)
1656{
1657 struct trace_array *tr = hist_data->event_file->tr;
1658 struct hist_var_data *var_data;
1659
1660 var_data = find_hist_vars(hist_data);
1661 if (var_data)
1662 return 0;
1663
1664 if (trace_array_get(tr) < 0)
1665 return -ENODEV;
1666
1667 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
1668 if (!var_data) {
1669 trace_array_put(tr);
1670 return -ENOMEM;
1671 }
1672
1673 var_data->hist_data = hist_data;
1674 list_add(&var_data->list, &tr->hist_vars);
1675
1676 return 0;
1677}
1678
1679static void remove_hist_vars(struct hist_trigger_data *hist_data)
1680{
1681 struct trace_array *tr = hist_data->event_file->tr;
1682 struct hist_var_data *var_data;
1683
1684 var_data = find_hist_vars(hist_data);
1685 if (!var_data)
1686 return;
1687
1688 if (WARN_ON(check_var_refs(hist_data)))
1689 return;
1690
1691 list_del(&var_data->list);
1692
1693 kfree(var_data);
1694
1695 trace_array_put(tr);
1696}
1697
Tom Zanussi30350d62018-01-15 20:51:49 -06001698static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
1699 const char *var_name)
1700{
1701 struct hist_field *hist_field, *found = NULL;
1702 int i;
1703
1704 for_each_hist_field(i, hist_data) {
1705 hist_field = hist_data->fields[i];
1706 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
1707 strcmp(hist_field->var.name, var_name) == 0) {
1708 found = hist_field;
1709 break;
1710 }
1711 }
1712
1713 return found;
1714}
1715
1716static struct hist_field *find_var(struct hist_trigger_data *hist_data,
1717 struct trace_event_file *file,
1718 const char *var_name)
1719{
1720 struct hist_trigger_data *test_data;
1721 struct event_trigger_data *test;
1722 struct hist_field *hist_field;
1723
1724 hist_field = find_var_field(hist_data, var_name);
1725 if (hist_field)
1726 return hist_field;
1727
1728 list_for_each_entry_rcu(test, &file->triggers, list) {
1729 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1730 test_data = test->private_data;
1731 hist_field = find_var_field(test_data, var_name);
1732 if (hist_field)
1733 return hist_field;
1734 }
1735 }
1736
1737 return NULL;
1738}
1739
Tom Zanussi067fe032018-01-15 20:51:56 -06001740static struct trace_event_file *find_var_file(struct trace_array *tr,
1741 char *system,
1742 char *event_name,
1743 char *var_name)
1744{
1745 struct hist_trigger_data *var_hist_data;
1746 struct hist_var_data *var_data;
1747 struct trace_event_file *file, *found = NULL;
1748
1749 if (system)
1750 return find_event_file(tr, system, event_name);
1751
1752 list_for_each_entry(var_data, &tr->hist_vars, list) {
1753 var_hist_data = var_data->hist_data;
1754 file = var_hist_data->event_file;
1755 if (file == found)
1756 continue;
1757
1758 if (find_var_field(var_hist_data, var_name)) {
Tom Zanussif404da62018-01-15 20:52:05 -06001759 if (found) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04001760 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name));
Tom Zanussi067fe032018-01-15 20:51:56 -06001761 return NULL;
Tom Zanussif404da62018-01-15 20:52:05 -06001762 }
Tom Zanussi067fe032018-01-15 20:51:56 -06001763
1764 found = file;
1765 }
1766 }
1767
1768 return found;
1769}
1770
1771static struct hist_field *find_file_var(struct trace_event_file *file,
1772 const char *var_name)
1773{
1774 struct hist_trigger_data *test_data;
1775 struct event_trigger_data *test;
1776 struct hist_field *hist_field;
1777
1778 list_for_each_entry_rcu(test, &file->triggers, list) {
1779 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1780 test_data = test->private_data;
1781 hist_field = find_var_field(test_data, var_name);
1782 if (hist_field)
1783 return hist_field;
1784 }
1785 }
1786
1787 return NULL;
1788}
1789
Tom Zanussic282a382018-01-15 20:52:00 -06001790static struct hist_field *
1791find_match_var(struct hist_trigger_data *hist_data, char *var_name)
1792{
1793 struct trace_array *tr = hist_data->event_file->tr;
1794 struct hist_field *hist_field, *found = NULL;
1795 struct trace_event_file *file;
1796 unsigned int i;
1797
1798 for (i = 0; i < hist_data->n_actions; i++) {
1799 struct action_data *data = hist_data->actions[i];
1800
Tom Zanussi7d18a102019-02-13 17:42:41 -06001801 if (data->handler == HANDLER_ONMATCH) {
Tom Zanussic3e49502019-02-13 17:42:43 -06001802 char *system = data->match_data.event_system;
1803 char *event_name = data->match_data.event;
Tom Zanussic282a382018-01-15 20:52:00 -06001804
1805 file = find_var_file(tr, system, event_name, var_name);
1806 if (!file)
1807 continue;
1808 hist_field = find_file_var(file, var_name);
1809 if (hist_field) {
1810 if (found) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04001811 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE,
1812 errpos(var_name));
Tom Zanussic282a382018-01-15 20:52:00 -06001813 return ERR_PTR(-EINVAL);
1814 }
1815
1816 found = hist_field;
1817 }
1818 }
1819 }
1820 return found;
1821}
1822
Tom Zanussi067fe032018-01-15 20:51:56 -06001823static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
1824 char *system,
1825 char *event_name,
1826 char *var_name)
1827{
1828 struct trace_array *tr = hist_data->event_file->tr;
1829 struct hist_field *hist_field = NULL;
1830 struct trace_event_file *file;
1831
Tom Zanussic282a382018-01-15 20:52:00 -06001832 if (!system || !event_name) {
1833 hist_field = find_match_var(hist_data, var_name);
1834 if (IS_ERR(hist_field))
1835 return NULL;
1836 if (hist_field)
1837 return hist_field;
1838 }
1839
Tom Zanussi067fe032018-01-15 20:51:56 -06001840 file = find_var_file(tr, system, event_name, var_name);
1841 if (!file)
1842 return NULL;
1843
1844 hist_field = find_file_var(file, var_name);
1845
1846 return hist_field;
1847}
1848
Tom Zanussi067fe032018-01-15 20:51:56 -06001849static u64 hist_field_var_ref(struct hist_field *hist_field,
1850 struct tracing_map_elt *elt,
1851 struct ring_buffer_event *rbe,
1852 void *event)
1853{
1854 struct hist_elt_data *elt_data;
1855 u64 var_val = 0;
1856
Tom Zanussi55267c82019-04-18 10:18:50 -05001857 if (WARN_ON_ONCE(!elt))
1858 return var_val;
1859
Tom Zanussi067fe032018-01-15 20:51:56 -06001860 elt_data = elt->private_data;
1861 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
1862
1863 return var_val;
1864}
1865
1866static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
1867 u64 *var_ref_vals, bool self)
1868{
1869 struct hist_trigger_data *var_data;
1870 struct tracing_map_elt *var_elt;
1871 struct hist_field *hist_field;
1872 unsigned int i, var_idx;
1873 bool resolved = true;
1874 u64 var_val = 0;
1875
1876 for (i = 0; i < hist_data->n_var_refs; i++) {
1877 hist_field = hist_data->var_refs[i];
1878 var_idx = hist_field->var.idx;
1879 var_data = hist_field->var.hist_data;
1880
1881 if (var_data == NULL) {
1882 resolved = false;
1883 break;
1884 }
1885
1886 if ((self && var_data != hist_data) ||
1887 (!self && var_data == hist_data))
1888 continue;
1889
1890 var_elt = tracing_map_lookup(var_data->map, key);
1891 if (!var_elt) {
1892 resolved = false;
1893 break;
1894 }
1895
1896 if (!tracing_map_var_set(var_elt, var_idx)) {
1897 resolved = false;
1898 break;
1899 }
1900
1901 if (self || !hist_field->read_once)
1902 var_val = tracing_map_read_var(var_elt, var_idx);
1903 else
1904 var_val = tracing_map_read_var_once(var_elt, var_idx);
1905
1906 var_ref_vals[i] = var_val;
1907 }
1908
1909 return resolved;
1910}
1911
Tom Zanussi85013252017-09-22 14:58:22 -05001912static const char *hist_field_name(struct hist_field *field,
1913 unsigned int level)
1914{
1915 const char *field_name = "";
1916
1917 if (level > 1)
1918 return field_name;
1919
1920 if (field->field)
1921 field_name = field->field->name;
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06001922 else if (field->flags & HIST_FIELD_FL_LOG2 ||
1923 field->flags & HIST_FIELD_FL_ALIAS)
Tom Zanussi5819ead2017-09-22 14:58:23 -05001924 field_name = hist_field_name(field->operands[0], ++level);
Tom Zanussi8b7622b2018-01-15 20:52:03 -06001925 else if (field->flags & HIST_FIELD_FL_CPU)
1926 field_name = "cpu";
Tom Zanussi067fe032018-01-15 20:51:56 -06001927 else if (field->flags & HIST_FIELD_FL_EXPR ||
1928 field->flags & HIST_FIELD_FL_VAR_REF) {
1929 if (field->system) {
1930 static char full_name[MAX_FILTER_STR_VAL];
1931
1932 strcat(full_name, field->system);
1933 strcat(full_name, ".");
1934 strcat(full_name, field->event_name);
1935 strcat(full_name, ".");
1936 strcat(full_name, field->name);
1937 field_name = full_name;
1938 } else
1939 field_name = field->name;
Tom Zanussi0ae79612018-03-28 15:10:53 -05001940 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1941 field_name = "common_timestamp";
Tom Zanussi85013252017-09-22 14:58:22 -05001942
1943 if (field_name == NULL)
1944 field_name = "";
1945
1946 return field_name;
1947}
1948
Tom Zanussi7ef224d2016-03-03 12:54:42 -06001949static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
1950{
1951 hist_field_fn_t fn = NULL;
1952
1953 switch (field_size) {
1954 case 8:
1955 if (field_is_signed)
1956 fn = hist_field_s64;
1957 else
1958 fn = hist_field_u64;
1959 break;
1960 case 4:
1961 if (field_is_signed)
1962 fn = hist_field_s32;
1963 else
1964 fn = hist_field_u32;
1965 break;
1966 case 2:
1967 if (field_is_signed)
1968 fn = hist_field_s16;
1969 else
1970 fn = hist_field_u16;
1971 break;
1972 case 1:
1973 if (field_is_signed)
1974 fn = hist_field_s8;
1975 else
1976 fn = hist_field_u8;
1977 break;
1978 }
1979
1980 return fn;
1981}
1982
1983static int parse_map_size(char *str)
1984{
1985 unsigned long size, map_bits;
1986 int ret;
1987
1988 strsep(&str, "=");
1989 if (!str) {
1990 ret = -EINVAL;
1991 goto out;
1992 }
1993
1994 ret = kstrtoul(str, 0, &size);
1995 if (ret)
1996 goto out;
1997
1998 map_bits = ilog2(roundup_pow_of_two(size));
1999 if (map_bits < TRACING_MAP_BITS_MIN ||
2000 map_bits > TRACING_MAP_BITS_MAX)
2001 ret = -EINVAL;
2002 else
2003 ret = map_bits;
2004 out:
2005 return ret;
2006}
2007
2008static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
2009{
Tom Zanussi30350d62018-01-15 20:51:49 -06002010 unsigned int i;
2011
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002012 if (!attrs)
2013 return;
2014
Tom Zanussi30350d62018-01-15 20:51:49 -06002015 for (i = 0; i < attrs->n_assignments; i++)
2016 kfree(attrs->assignment_str[i]);
2017
Tom Zanussi0212e2a2018-01-15 20:51:57 -06002018 for (i = 0; i < attrs->n_actions; i++)
2019 kfree(attrs->action_str[i]);
2020
Tom Zanussi5463bfd2016-03-03 12:54:59 -06002021 kfree(attrs->name);
Tom Zanussie62347d2016-03-03 12:54:45 -06002022 kfree(attrs->sort_key_str);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002023 kfree(attrs->keys_str);
Tom Zanussif2606832016-03-03 12:54:43 -06002024 kfree(attrs->vals_str);
Tom Zanussia4072fe2018-01-15 20:52:08 -06002025 kfree(attrs->clock);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002026 kfree(attrs);
2027}
2028
Tom Zanussi0212e2a2018-01-15 20:51:57 -06002029static int parse_action(char *str, struct hist_trigger_attrs *attrs)
2030{
Tom Zanussic282a382018-01-15 20:52:00 -06002031 int ret = -EINVAL;
Tom Zanussi0212e2a2018-01-15 20:51:57 -06002032
2033 if (attrs->n_actions >= HIST_ACTIONS_MAX)
2034 return ret;
2035
Steven Rostedt (VMware)754481e2018-12-19 22:38:21 -05002036 if ((str_has_prefix(str, "onmatch(")) ||
Tom Zanussidff81f52019-02-13 17:42:48 -06002037 (str_has_prefix(str, "onmax(")) ||
2038 (str_has_prefix(str, "onchange("))) {
Tom Zanussic282a382018-01-15 20:52:00 -06002039 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
2040 if (!attrs->action_str[attrs->n_actions]) {
2041 ret = -ENOMEM;
2042 return ret;
2043 }
2044 attrs->n_actions++;
2045 ret = 0;
2046 }
Tom Zanussi0212e2a2018-01-15 20:51:57 -06002047 return ret;
2048}
2049
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04002050static int parse_assignment(struct trace_array *tr,
2051 char *str, struct hist_trigger_attrs *attrs)
Tom Zanussi9b1ae032018-01-15 20:51:44 -06002052{
2053 int ret = 0;
2054
Steven Rostedt (VMware)754481e2018-12-19 22:38:21 -05002055 if ((str_has_prefix(str, "key=")) ||
2056 (str_has_prefix(str, "keys="))) {
Tom Zanussi9b1ae032018-01-15 20:51:44 -06002057 attrs->keys_str = kstrdup(str, GFP_KERNEL);
2058 if (!attrs->keys_str) {
2059 ret = -ENOMEM;
2060 goto out;
2061 }
Steven Rostedt (VMware)754481e2018-12-19 22:38:21 -05002062 } else if ((str_has_prefix(str, "val=")) ||
2063 (str_has_prefix(str, "vals=")) ||
2064 (str_has_prefix(str, "values="))) {
Tom Zanussi9b1ae032018-01-15 20:51:44 -06002065 attrs->vals_str = kstrdup(str, GFP_KERNEL);
2066 if (!attrs->vals_str) {
2067 ret = -ENOMEM;
2068 goto out;
2069 }
Steven Rostedt (VMware)754481e2018-12-19 22:38:21 -05002070 } else if (str_has_prefix(str, "sort=")) {
Tom Zanussi9b1ae032018-01-15 20:51:44 -06002071 attrs->sort_key_str = kstrdup(str, GFP_KERNEL);
2072 if (!attrs->sort_key_str) {
2073 ret = -ENOMEM;
2074 goto out;
2075 }
Steven Rostedt (VMware)754481e2018-12-19 22:38:21 -05002076 } else if (str_has_prefix(str, "name=")) {
Tom Zanussi9b1ae032018-01-15 20:51:44 -06002077 attrs->name = kstrdup(str, GFP_KERNEL);
2078 if (!attrs->name) {
2079 ret = -ENOMEM;
2080 goto out;
2081 }
Steven Rostedt (VMware)754481e2018-12-19 22:38:21 -05002082 } else if (str_has_prefix(str, "clock=")) {
Tom Zanussia4072fe2018-01-15 20:52:08 -06002083 strsep(&str, "=");
2084 if (!str) {
2085 ret = -EINVAL;
2086 goto out;
2087 }
2088
2089 str = strstrip(str);
2090 attrs->clock = kstrdup(str, GFP_KERNEL);
2091 if (!attrs->clock) {
2092 ret = -ENOMEM;
2093 goto out;
2094 }
Steven Rostedt (VMware)754481e2018-12-19 22:38:21 -05002095 } else if (str_has_prefix(str, "size=")) {
Tom Zanussi9b1ae032018-01-15 20:51:44 -06002096 int map_bits = parse_map_size(str);
2097
2098 if (map_bits < 0) {
2099 ret = map_bits;
2100 goto out;
2101 }
2102 attrs->map_bits = map_bits;
Tom Zanussi30350d62018-01-15 20:51:49 -06002103 } else {
2104 char *assignment;
2105
2106 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04002107 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str));
Tom Zanussi30350d62018-01-15 20:51:49 -06002108 ret = -EINVAL;
2109 goto out;
2110 }
2111
2112 assignment = kstrdup(str, GFP_KERNEL);
2113 if (!assignment) {
2114 ret = -ENOMEM;
2115 goto out;
2116 }
2117
2118 attrs->assignment_str[attrs->n_assignments++] = assignment;
2119 }
Tom Zanussi9b1ae032018-01-15 20:51:44 -06002120 out:
2121 return ret;
2122}
2123
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04002124static struct hist_trigger_attrs *
2125parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002126{
2127 struct hist_trigger_attrs *attrs;
2128 int ret = 0;
2129
2130 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
2131 if (!attrs)
2132 return ERR_PTR(-ENOMEM);
2133
2134 while (trigger_str) {
2135 char *str = strsep(&trigger_str, ":");
2136
Tom Zanussi9b1ae032018-01-15 20:51:44 -06002137 if (strchr(str, '=')) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04002138 ret = parse_assignment(tr, str, attrs);
Tom Zanussi9b1ae032018-01-15 20:51:44 -06002139 if (ret)
2140 goto free;
2141 } else if (strcmp(str, "pause") == 0)
Tom Zanussi83e99912016-03-03 12:54:46 -06002142 attrs->pause = true;
2143 else if ((strcmp(str, "cont") == 0) ||
2144 (strcmp(str, "continue") == 0))
2145 attrs->cont = true;
Tom Zanussie86ae9b2016-03-03 12:54:47 -06002146 else if (strcmp(str, "clear") == 0)
2147 attrs->clear = true;
Tom Zanussi9b1ae032018-01-15 20:51:44 -06002148 else {
Tom Zanussi0212e2a2018-01-15 20:51:57 -06002149 ret = parse_action(str, attrs);
2150 if (ret)
2151 goto free;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002152 }
2153 }
2154
2155 if (!attrs->keys_str) {
2156 ret = -EINVAL;
2157 goto free;
2158 }
2159
Tom Zanussia4072fe2018-01-15 20:52:08 -06002160 if (!attrs->clock) {
2161 attrs->clock = kstrdup("global", GFP_KERNEL);
2162 if (!attrs->clock) {
2163 ret = -ENOMEM;
2164 goto free;
2165 }
2166 }
2167
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002168 return attrs;
2169 free:
2170 destroy_hist_trigger_attrs(attrs);
2171
2172 return ERR_PTR(ret);
2173}
2174
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002175static inline void save_comm(char *comm, struct task_struct *task)
2176{
2177 if (!task->pid) {
2178 strcpy(comm, "<idle>");
2179 return;
2180 }
2181
2182 if (WARN_ON_ONCE(task->pid < 0)) {
2183 strcpy(comm, "<XXX>");
2184 return;
2185 }
2186
Tom Zanussi27242c62019-03-05 10:11:59 -06002187 strncpy(comm, task->comm, TASK_COMM_LEN);
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002188}
2189
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002190static void hist_elt_data_free(struct hist_elt_data *elt_data)
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002191{
Tom Zanussi02205a62018-01-15 20:51:59 -06002192 unsigned int i;
2193
2194 for (i = 0; i < SYNTH_FIELDS_MAX; i++)
2195 kfree(elt_data->field_var_str[i]);
2196
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002197 kfree(elt_data->comm);
2198 kfree(elt_data);
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002199}
2200
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002201static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
2202{
2203 struct hist_elt_data *elt_data = elt->private_data;
2204
2205 hist_elt_data_free(elt_data);
2206}
2207
2208static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002209{
2210 struct hist_trigger_data *hist_data = elt->map->private_data;
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002211 unsigned int size = TASK_COMM_LEN;
2212 struct hist_elt_data *elt_data;
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002213 struct hist_field *key_field;
Tom Zanussi02205a62018-01-15 20:51:59 -06002214 unsigned int i, n_str;
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002215
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002216 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
2217 if (!elt_data)
2218 return -ENOMEM;
2219
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002220 for_each_hist_key_field(i, hist_data) {
2221 key_field = hist_data->fields[i];
2222
2223 if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002224 elt_data->comm = kzalloc(size, GFP_KERNEL);
2225 if (!elt_data->comm) {
2226 kfree(elt_data);
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002227 return -ENOMEM;
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002228 }
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002229 break;
2230 }
2231 }
2232
Tom Zanussi7d18a102019-02-13 17:42:41 -06002233 n_str = hist_data->n_field_var_str + hist_data->n_save_var_str;
Tom Zanussi02205a62018-01-15 20:51:59 -06002234
2235 size = STR_VAR_LEN_MAX;
2236
2237 for (i = 0; i < n_str; i++) {
2238 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
2239 if (!elt_data->field_var_str[i]) {
2240 hist_elt_data_free(elt_data);
2241 return -ENOMEM;
2242 }
2243 }
2244
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002245 elt->private_data = elt_data;
2246
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002247 return 0;
2248}
2249
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002250static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002251{
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002252 struct hist_elt_data *elt_data = elt->private_data;
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002253
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002254 if (elt_data->comm)
2255 save_comm(elt_data->comm, current);
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002256}
2257
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002258static const struct tracing_map_ops hist_trigger_elt_data_ops = {
2259 .elt_alloc = hist_trigger_elt_data_alloc,
2260 .elt_free = hist_trigger_elt_data_free,
2261 .elt_init = hist_trigger_elt_data_init,
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002262};
2263
Tom Zanussi2ece94f2018-01-15 20:51:51 -06002264static const char *get_hist_field_flags(struct hist_field *hist_field)
2265{
2266 const char *flags_str = NULL;
2267
2268 if (hist_field->flags & HIST_FIELD_FL_HEX)
2269 flags_str = "hex";
2270 else if (hist_field->flags & HIST_FIELD_FL_SYM)
2271 flags_str = "sym";
2272 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
2273 flags_str = "sym-offset";
2274 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
2275 flags_str = "execname";
2276 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
2277 flags_str = "syscall";
2278 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
2279 flags_str = "log2";
2280 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2281 flags_str = "usecs";
2282
2283 return flags_str;
2284}
2285
Tom Zanussi100719d2018-01-15 20:51:52 -06002286static void expr_field_str(struct hist_field *field, char *expr)
2287{
Tom Zanussi067fe032018-01-15 20:51:56 -06002288 if (field->flags & HIST_FIELD_FL_VAR_REF)
2289 strcat(expr, "$");
2290
Tom Zanussi100719d2018-01-15 20:51:52 -06002291 strcat(expr, hist_field_name(field, 0));
2292
Tom Zanussi76690942018-03-28 15:10:54 -05002293 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
Tom Zanussi100719d2018-01-15 20:51:52 -06002294 const char *flags_str = get_hist_field_flags(field);
2295
2296 if (flags_str) {
2297 strcat(expr, ".");
2298 strcat(expr, flags_str);
2299 }
2300 }
2301}
2302
2303static char *expr_str(struct hist_field *field, unsigned int level)
2304{
2305 char *expr;
2306
2307 if (level > 1)
2308 return NULL;
2309
2310 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2311 if (!expr)
2312 return NULL;
2313
2314 if (!field->operands[0]) {
2315 expr_field_str(field, expr);
2316 return expr;
2317 }
2318
2319 if (field->operator == FIELD_OP_UNARY_MINUS) {
2320 char *subexpr;
2321
2322 strcat(expr, "-(");
2323 subexpr = expr_str(field->operands[0], ++level);
2324 if (!subexpr) {
2325 kfree(expr);
2326 return NULL;
2327 }
2328 strcat(expr, subexpr);
2329 strcat(expr, ")");
2330
2331 kfree(subexpr);
2332
2333 return expr;
2334 }
2335
2336 expr_field_str(field->operands[0], expr);
2337
2338 switch (field->operator) {
2339 case FIELD_OP_MINUS:
2340 strcat(expr, "-");
2341 break;
2342 case FIELD_OP_PLUS:
2343 strcat(expr, "+");
2344 break;
2345 default:
2346 kfree(expr);
2347 return NULL;
2348 }
2349
2350 expr_field_str(field->operands[1], expr);
2351
2352 return expr;
2353}
2354
2355static int contains_operator(char *str)
2356{
2357 enum field_op_id field_op = FIELD_OP_NONE;
2358 char *op;
2359
2360 op = strpbrk(str, "+-");
2361 if (!op)
2362 return FIELD_OP_NONE;
2363
2364 switch (*op) {
2365 case '-':
2366 if (*str == '-')
2367 field_op = FIELD_OP_UNARY_MINUS;
2368 else
2369 field_op = FIELD_OP_MINUS;
2370 break;
2371 case '+':
2372 field_op = FIELD_OP_PLUS;
2373 break;
2374 default:
2375 break;
2376 }
2377
2378 return field_op;
2379}
2380
Tom Zanussi656fe2b2018-12-18 14:33:24 -06002381static void __destroy_hist_field(struct hist_field *hist_field)
2382{
2383 kfree(hist_field->var.name);
2384 kfree(hist_field->name);
2385 kfree(hist_field->type);
2386
2387 kfree(hist_field);
2388}
2389
Tom Zanussi5819ead2017-09-22 14:58:23 -05002390static void destroy_hist_field(struct hist_field *hist_field,
2391 unsigned int level)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002392{
Tom Zanussi5819ead2017-09-22 14:58:23 -05002393 unsigned int i;
2394
Tom Zanussi100719d2018-01-15 20:51:52 -06002395 if (level > 3)
Tom Zanussi5819ead2017-09-22 14:58:23 -05002396 return;
2397
2398 if (!hist_field)
2399 return;
2400
Tom Zanussi656fe2b2018-12-18 14:33:24 -06002401 if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
2402 return; /* var refs will be destroyed separately */
2403
Tom Zanussi5819ead2017-09-22 14:58:23 -05002404 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
2405 destroy_hist_field(hist_field->operands[i], level + 1);
2406
Tom Zanussi656fe2b2018-12-18 14:33:24 -06002407 __destroy_hist_field(hist_field);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002408}
2409
Tom Zanussib559d003a2018-01-15 20:51:47 -06002410static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
2411 struct ftrace_event_field *field,
Tom Zanussi30350d62018-01-15 20:51:49 -06002412 unsigned long flags,
2413 char *var_name)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002414{
2415 struct hist_field *hist_field;
2416
2417 if (field && is_function_field(field))
2418 return NULL;
2419
2420 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
2421 if (!hist_field)
2422 return NULL;
2423
Tom Zanussib559d003a2018-01-15 20:51:47 -06002424 hist_field->hist_data = hist_data;
2425
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06002426 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
Tom Zanussi100719d2018-01-15 20:51:52 -06002427 goto out; /* caller will populate */
2428
Tom Zanussi067fe032018-01-15 20:51:56 -06002429 if (flags & HIST_FIELD_FL_VAR_REF) {
2430 hist_field->fn = hist_field_var_ref;
2431 goto out;
2432 }
2433
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002434 if (flags & HIST_FIELD_FL_HITCOUNT) {
2435 hist_field->fn = hist_field_counter;
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002436 hist_field->size = sizeof(u64);
2437 hist_field->type = kstrdup("u64", GFP_KERNEL);
2438 if (!hist_field->type)
2439 goto free;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002440 goto out;
2441 }
2442
Tom Zanussi69a02002016-03-03 12:54:52 -06002443 if (flags & HIST_FIELD_FL_STACKTRACE) {
2444 hist_field->fn = hist_field_none;
2445 goto out;
2446 }
2447
Namhyung Kim4b94f5b2016-03-03 12:55:02 -06002448 if (flags & HIST_FIELD_FL_LOG2) {
Tom Zanussi5819ead2017-09-22 14:58:23 -05002449 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2;
Namhyung Kim4b94f5b2016-03-03 12:55:02 -06002450 hist_field->fn = hist_field_log2;
Tom Zanussi30350d62018-01-15 20:51:49 -06002451 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
Tom Zanussi5819ead2017-09-22 14:58:23 -05002452 hist_field->size = hist_field->operands[0]->size;
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002453 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL);
2454 if (!hist_field->type)
2455 goto free;
Namhyung Kim4b94f5b2016-03-03 12:55:02 -06002456 goto out;
2457 }
2458
Tom Zanussiad42feb2018-01-15 20:51:45 -06002459 if (flags & HIST_FIELD_FL_TIMESTAMP) {
2460 hist_field->fn = hist_field_timestamp;
2461 hist_field->size = sizeof(u64);
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002462 hist_field->type = kstrdup("u64", GFP_KERNEL);
2463 if (!hist_field->type)
2464 goto free;
Tom Zanussiad42feb2018-01-15 20:51:45 -06002465 goto out;
2466 }
2467
Tom Zanussi8b7622b2018-01-15 20:52:03 -06002468 if (flags & HIST_FIELD_FL_CPU) {
2469 hist_field->fn = hist_field_cpu;
2470 hist_field->size = sizeof(int);
2471 hist_field->type = kstrdup("unsigned int", GFP_KERNEL);
2472 if (!hist_field->type)
2473 goto free;
2474 goto out;
2475 }
2476
Tom Zanussi432480c2016-04-25 14:01:27 -05002477 if (WARN_ON_ONCE(!field))
2478 goto out;
2479
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002480 if (is_string_field(field)) {
2481 flags |= HIST_FIELD_FL_STRING;
Namhyung Kim79e577c2016-03-03 12:54:53 -06002482
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002483 hist_field->size = MAX_FILTER_STR_VAL;
2484 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2485 if (!hist_field->type)
2486 goto free;
2487
Namhyung Kim79e577c2016-03-03 12:54:53 -06002488 if (field->filter_type == FILTER_STATIC_STRING)
2489 hist_field->fn = hist_field_string;
2490 else if (field->filter_type == FILTER_DYN_STRING)
2491 hist_field->fn = hist_field_dynstring;
2492 else
2493 hist_field->fn = hist_field_pstring;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002494 } else {
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002495 hist_field->size = field->size;
2496 hist_field->is_signed = field->is_signed;
2497 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2498 if (!hist_field->type)
2499 goto free;
2500
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002501 hist_field->fn = select_value_fn(field->size,
2502 field->is_signed);
2503 if (!hist_field->fn) {
Tom Zanussi5819ead2017-09-22 14:58:23 -05002504 destroy_hist_field(hist_field, 0);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002505 return NULL;
2506 }
2507 }
2508 out:
2509 hist_field->field = field;
2510 hist_field->flags = flags;
2511
Tom Zanussi30350d62018-01-15 20:51:49 -06002512 if (var_name) {
2513 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
2514 if (!hist_field->var.name)
2515 goto free;
2516 }
2517
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002518 return hist_field;
Tom Zanussi30350d62018-01-15 20:51:49 -06002519 free:
2520 destroy_hist_field(hist_field, 0);
2521 return NULL;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002522}
2523
2524static void destroy_hist_fields(struct hist_trigger_data *hist_data)
2525{
2526 unsigned int i;
2527
Tom Zanussi30350d62018-01-15 20:51:49 -06002528 for (i = 0; i < HIST_FIELDS_MAX; i++) {
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002529 if (hist_data->fields[i]) {
Tom Zanussi5819ead2017-09-22 14:58:23 -05002530 destroy_hist_field(hist_data->fields[i], 0);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002531 hist_data->fields[i] = NULL;
2532 }
2533 }
Tom Zanussi656fe2b2018-12-18 14:33:24 -06002534
2535 for (i = 0; i < hist_data->n_var_refs; i++) {
2536 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
2537 __destroy_hist_field(hist_data->var_refs[i]);
2538 hist_data->var_refs[i] = NULL;
2539 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002540}
2541
Tom Zanussi067fe032018-01-15 20:51:56 -06002542static int init_var_ref(struct hist_field *ref_field,
2543 struct hist_field *var_field,
2544 char *system, char *event_name)
2545{
2546 int err = 0;
2547
2548 ref_field->var.idx = var_field->var.idx;
2549 ref_field->var.hist_data = var_field->hist_data;
2550 ref_field->size = var_field->size;
2551 ref_field->is_signed = var_field->is_signed;
2552 ref_field->flags |= var_field->flags &
2553 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2554
2555 if (system) {
2556 ref_field->system = kstrdup(system, GFP_KERNEL);
2557 if (!ref_field->system)
2558 return -ENOMEM;
2559 }
2560
2561 if (event_name) {
2562 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
2563 if (!ref_field->event_name) {
2564 err = -ENOMEM;
2565 goto free;
2566 }
2567 }
2568
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06002569 if (var_field->var.name) {
2570 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
2571 if (!ref_field->name) {
2572 err = -ENOMEM;
2573 goto free;
2574 }
2575 } else if (var_field->name) {
2576 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
2577 if (!ref_field->name) {
2578 err = -ENOMEM;
2579 goto free;
2580 }
Tom Zanussi067fe032018-01-15 20:51:56 -06002581 }
2582
2583 ref_field->type = kstrdup(var_field->type, GFP_KERNEL);
2584 if (!ref_field->type) {
2585 err = -ENOMEM;
2586 goto free;
2587 }
2588 out:
2589 return err;
2590 free:
2591 kfree(ref_field->system);
2592 kfree(ref_field->event_name);
2593 kfree(ref_field->name);
2594
2595 goto out;
2596}
2597
Tom Zanusside40f032018-12-18 14:33:23 -06002598/**
2599 * create_var_ref - Create a variable reference and attach it to trigger
2600 * @hist_data: The trigger that will be referencing the variable
2601 * @var_field: The VAR field to create a reference to
2602 * @system: The optional system string
2603 * @event_name: The optional event_name string
2604 *
2605 * Given a variable hist_field, create a VAR_REF hist_field that
2606 * represents a reference to it.
2607 *
2608 * This function also adds the reference to the trigger that
2609 * now references the variable.
2610 *
2611 * Return: The VAR_REF field if successful, NULL if not
2612 */
2613static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
2614 struct hist_field *var_field,
Tom Zanussi067fe032018-01-15 20:51:56 -06002615 char *system, char *event_name)
2616{
2617 unsigned long flags = HIST_FIELD_FL_VAR_REF;
2618 struct hist_field *ref_field;
2619
2620 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
2621 if (ref_field) {
2622 if (init_var_ref(ref_field, var_field, system, event_name)) {
2623 destroy_hist_field(ref_field, 0);
2624 return NULL;
2625 }
Tom Zanusside40f032018-12-18 14:33:23 -06002626
2627 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
2628 ref_field->var_ref_idx = hist_data->n_var_refs++;
Tom Zanussi067fe032018-01-15 20:51:56 -06002629 }
2630
2631 return ref_field;
2632}
2633
2634static bool is_var_ref(char *var_name)
2635{
2636 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
2637 return false;
2638
2639 return true;
2640}
2641
2642static char *field_name_from_var(struct hist_trigger_data *hist_data,
2643 char *var_name)
2644{
2645 char *name, *field;
2646 unsigned int i;
2647
2648 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
2649 name = hist_data->attrs->var_defs.name[i];
2650
2651 if (strcmp(var_name, name) == 0) {
2652 field = hist_data->attrs->var_defs.expr[i];
2653 if (contains_operator(field) || is_var_ref(field))
2654 continue;
2655 return field;
2656 }
2657 }
2658
2659 return NULL;
2660}
2661
2662static char *local_field_var_ref(struct hist_trigger_data *hist_data,
2663 char *system, char *event_name,
2664 char *var_name)
2665{
2666 struct trace_event_call *call;
2667
2668 if (system && event_name) {
2669 call = hist_data->event_file->event_call;
2670
2671 if (strcmp(system, call->class->system) != 0)
2672 return NULL;
2673
2674 if (strcmp(event_name, trace_event_name(call)) != 0)
2675 return NULL;
2676 }
2677
2678 if (!!system != !!event_name)
2679 return NULL;
2680
2681 if (!is_var_ref(var_name))
2682 return NULL;
2683
2684 var_name++;
2685
2686 return field_name_from_var(hist_data, var_name);
2687}
2688
2689static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
2690 char *system, char *event_name,
2691 char *var_name)
2692{
2693 struct hist_field *var_field = NULL, *ref_field = NULL;
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04002694 struct trace_array *tr = hist_data->event_file->tr;
Tom Zanussi067fe032018-01-15 20:51:56 -06002695
2696 if (!is_var_ref(var_name))
2697 return NULL;
2698
2699 var_name++;
2700
2701 var_field = find_event_var(hist_data, system, event_name, var_name);
2702 if (var_field)
Tom Zanusside40f032018-12-18 14:33:23 -06002703 ref_field = create_var_ref(hist_data, var_field,
2704 system, event_name);
Tom Zanussi067fe032018-01-15 20:51:56 -06002705
Tom Zanussif404da62018-01-15 20:52:05 -06002706 if (!ref_field)
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04002707 hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name));
Tom Zanussif404da62018-01-15 20:52:05 -06002708
Tom Zanussi067fe032018-01-15 20:51:56 -06002709 return ref_field;
2710}
2711
Tom Zanussi100719d2018-01-15 20:51:52 -06002712static struct ftrace_event_field *
2713parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
2714 char *field_str, unsigned long *flags)
2715{
2716 struct ftrace_event_field *field = NULL;
2717 char *field_name, *modifier, *str;
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04002718 struct trace_array *tr = file->tr;
Tom Zanussi100719d2018-01-15 20:51:52 -06002719
2720 modifier = str = kstrdup(field_str, GFP_KERNEL);
2721 if (!modifier)
2722 return ERR_PTR(-ENOMEM);
2723
2724 field_name = strsep(&modifier, ".");
2725 if (modifier) {
2726 if (strcmp(modifier, "hex") == 0)
2727 *flags |= HIST_FIELD_FL_HEX;
2728 else if (strcmp(modifier, "sym") == 0)
2729 *flags |= HIST_FIELD_FL_SYM;
2730 else if (strcmp(modifier, "sym-offset") == 0)
2731 *flags |= HIST_FIELD_FL_SYM_OFFSET;
2732 else if ((strcmp(modifier, "execname") == 0) &&
2733 (strcmp(field_name, "common_pid") == 0))
2734 *flags |= HIST_FIELD_FL_EXECNAME;
2735 else if (strcmp(modifier, "syscall") == 0)
2736 *flags |= HIST_FIELD_FL_SYSCALL;
2737 else if (strcmp(modifier, "log2") == 0)
2738 *flags |= HIST_FIELD_FL_LOG2;
2739 else if (strcmp(modifier, "usecs") == 0)
2740 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
2741 else {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04002742 hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier));
Tom Zanussi100719d2018-01-15 20:51:52 -06002743 field = ERR_PTR(-EINVAL);
2744 goto out;
2745 }
2746 }
2747
2748 if (strcmp(field_name, "common_timestamp") == 0) {
2749 *flags |= HIST_FIELD_FL_TIMESTAMP;
2750 hist_data->enable_timestamps = true;
2751 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2752 hist_data->attrs->ts_in_usecs = true;
Tom Zanussi8b7622b2018-01-15 20:52:03 -06002753 } else if (strcmp(field_name, "cpu") == 0)
2754 *flags |= HIST_FIELD_FL_CPU;
2755 else {
Tom Zanussi100719d2018-01-15 20:51:52 -06002756 field = trace_find_event_field(file->event_call, field_name);
2757 if (!field || !field->size) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04002758 hist_err(tr, HIST_ERR_FIELD_NOT_FOUND, errpos(field_name));
Tom Zanussi100719d2018-01-15 20:51:52 -06002759 field = ERR_PTR(-EINVAL);
2760 goto out;
2761 }
2762 }
2763 out:
2764 kfree(str);
2765
2766 return field;
2767}
2768
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06002769static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2770 struct hist_field *var_ref,
2771 char *var_name)
2772{
2773 struct hist_field *alias = NULL;
2774 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2775
2776 alias = create_hist_field(hist_data, NULL, flags, var_name);
2777 if (!alias)
2778 return NULL;
2779
2780 alias->fn = var_ref->fn;
2781 alias->operands[0] = var_ref;
2782
2783 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2784 destroy_hist_field(alias, 0);
2785 return NULL;
2786 }
2787
2788 return alias;
2789}
2790
Tom Zanussi100719d2018-01-15 20:51:52 -06002791static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2792 struct trace_event_file *file, char *str,
2793 unsigned long *flags, char *var_name)
2794{
Tom Zanussi067fe032018-01-15 20:51:56 -06002795 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
Tom Zanussi100719d2018-01-15 20:51:52 -06002796 struct ftrace_event_field *field = NULL;
2797 struct hist_field *hist_field = NULL;
2798 int ret = 0;
2799
Tom Zanussi067fe032018-01-15 20:51:56 -06002800 s = strchr(str, '.');
2801 if (s) {
2802 s = strchr(++s, '.');
2803 if (s) {
2804 ref_system = strsep(&str, ".");
2805 if (!str) {
2806 ret = -EINVAL;
2807 goto out;
2808 }
2809 ref_event = strsep(&str, ".");
2810 if (!str) {
2811 ret = -EINVAL;
2812 goto out;
2813 }
2814 ref_var = str;
2815 }
2816 }
2817
2818 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2819 if (!s) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04002820 hist_field = parse_var_ref(hist_data, ref_system,
2821 ref_event, ref_var);
Tom Zanussi067fe032018-01-15 20:51:56 -06002822 if (hist_field) {
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06002823 if (var_name) {
2824 hist_field = create_alias(hist_data, hist_field, var_name);
2825 if (!hist_field) {
2826 ret = -ENOMEM;
2827 goto out;
2828 }
2829 }
Tom Zanussi067fe032018-01-15 20:51:56 -06002830 return hist_field;
2831 }
2832 } else
2833 str = s;
2834
Tom Zanussi100719d2018-01-15 20:51:52 -06002835 field = parse_field(hist_data, file, str, flags);
2836 if (IS_ERR(field)) {
2837 ret = PTR_ERR(field);
2838 goto out;
2839 }
2840
2841 hist_field = create_hist_field(hist_data, field, *flags, var_name);
2842 if (!hist_field) {
2843 ret = -ENOMEM;
2844 goto out;
2845 }
2846
2847 return hist_field;
2848 out:
2849 return ERR_PTR(ret);
2850}
2851
2852static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2853 struct trace_event_file *file,
2854 char *str, unsigned long flags,
2855 char *var_name, unsigned int level);
2856
2857static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2858 struct trace_event_file *file,
2859 char *str, unsigned long flags,
2860 char *var_name, unsigned int level)
2861{
2862 struct hist_field *operand1, *expr = NULL;
2863 unsigned long operand_flags;
2864 int ret = 0;
2865 char *s;
2866
2867 /* we support only -(xxx) i.e. explicit parens required */
2868
2869 if (level > 3) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04002870 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
Tom Zanussi100719d2018-01-15 20:51:52 -06002871 ret = -EINVAL;
2872 goto free;
2873 }
2874
2875 str++; /* skip leading '-' */
2876
2877 s = strchr(str, '(');
2878 if (s)
2879 str++;
2880 else {
2881 ret = -EINVAL;
2882 goto free;
2883 }
2884
2885 s = strrchr(str, ')');
2886 if (s)
2887 *s = '\0';
2888 else {
2889 ret = -EINVAL; /* no closing ')' */
2890 goto free;
2891 }
2892
2893 flags |= HIST_FIELD_FL_EXPR;
2894 expr = create_hist_field(hist_data, NULL, flags, var_name);
2895 if (!expr) {
2896 ret = -ENOMEM;
2897 goto free;
2898 }
2899
2900 operand_flags = 0;
2901 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2902 if (IS_ERR(operand1)) {
2903 ret = PTR_ERR(operand1);
2904 goto free;
2905 }
2906
2907 expr->flags |= operand1->flags &
2908 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2909 expr->fn = hist_field_unary_minus;
2910 expr->operands[0] = operand1;
2911 expr->operator = FIELD_OP_UNARY_MINUS;
2912 expr->name = expr_str(expr, 0);
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002913 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2914 if (!expr->type) {
2915 ret = -ENOMEM;
2916 goto free;
2917 }
Tom Zanussi100719d2018-01-15 20:51:52 -06002918
2919 return expr;
2920 free:
2921 destroy_hist_field(expr, 0);
2922 return ERR_PTR(ret);
2923}
2924
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04002925static int check_expr_operands(struct trace_array *tr,
2926 struct hist_field *operand1,
Tom Zanussi100719d2018-01-15 20:51:52 -06002927 struct hist_field *operand2)
2928{
2929 unsigned long operand1_flags = operand1->flags;
2930 unsigned long operand2_flags = operand2->flags;
2931
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06002932 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2933 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2934 struct hist_field *var;
2935
2936 var = find_var_field(operand1->var.hist_data, operand1->name);
2937 if (!var)
2938 return -EINVAL;
2939 operand1_flags = var->flags;
2940 }
2941
2942 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2943 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2944 struct hist_field *var;
2945
2946 var = find_var_field(operand2->var.hist_data, operand2->name);
2947 if (!var)
2948 return -EINVAL;
2949 operand2_flags = var->flags;
2950 }
2951
Tom Zanussi100719d2018-01-15 20:51:52 -06002952 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
Tom Zanussif404da62018-01-15 20:52:05 -06002953 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04002954 hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0);
Tom Zanussi100719d2018-01-15 20:51:52 -06002955 return -EINVAL;
Tom Zanussif404da62018-01-15 20:52:05 -06002956 }
Tom Zanussi100719d2018-01-15 20:51:52 -06002957
2958 return 0;
2959}
2960
2961static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2962 struct trace_event_file *file,
2963 char *str, unsigned long flags,
2964 char *var_name, unsigned int level)
2965{
2966 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2967 unsigned long operand_flags;
2968 int field_op, ret = -EINVAL;
2969 char *sep, *operand1_str;
2970
Tom Zanussif404da62018-01-15 20:52:05 -06002971 if (level > 3) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04002972 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
Tom Zanussi100719d2018-01-15 20:51:52 -06002973 return ERR_PTR(-EINVAL);
Tom Zanussif404da62018-01-15 20:52:05 -06002974 }
Tom Zanussi100719d2018-01-15 20:51:52 -06002975
2976 field_op = contains_operator(str);
2977
2978 if (field_op == FIELD_OP_NONE)
2979 return parse_atom(hist_data, file, str, &flags, var_name);
2980
2981 if (field_op == FIELD_OP_UNARY_MINUS)
2982 return parse_unary(hist_data, file, str, flags, var_name, ++level);
2983
2984 switch (field_op) {
2985 case FIELD_OP_MINUS:
2986 sep = "-";
2987 break;
2988 case FIELD_OP_PLUS:
2989 sep = "+";
2990 break;
2991 default:
2992 goto free;
2993 }
2994
2995 operand1_str = strsep(&str, sep);
2996 if (!operand1_str || !str)
2997 goto free;
2998
2999 operand_flags = 0;
3000 operand1 = parse_atom(hist_data, file, operand1_str,
3001 &operand_flags, NULL);
3002 if (IS_ERR(operand1)) {
3003 ret = PTR_ERR(operand1);
3004 operand1 = NULL;
3005 goto free;
3006 }
3007
3008 /* rest of string could be another expression e.g. b+c in a+b+c */
3009 operand_flags = 0;
3010 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
3011 if (IS_ERR(operand2)) {
3012 ret = PTR_ERR(operand2);
3013 operand2 = NULL;
3014 goto free;
3015 }
3016
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003017 ret = check_expr_operands(file->tr, operand1, operand2);
Tom Zanussi100719d2018-01-15 20:51:52 -06003018 if (ret)
3019 goto free;
3020
3021 flags |= HIST_FIELD_FL_EXPR;
3022
3023 flags |= operand1->flags &
3024 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
3025
3026 expr = create_hist_field(hist_data, NULL, flags, var_name);
3027 if (!expr) {
3028 ret = -ENOMEM;
3029 goto free;
3030 }
3031
Tom Zanussi067fe032018-01-15 20:51:56 -06003032 operand1->read_once = true;
3033 operand2->read_once = true;
3034
Tom Zanussi100719d2018-01-15 20:51:52 -06003035 expr->operands[0] = operand1;
3036 expr->operands[1] = operand2;
3037 expr->operator = field_op;
3038 expr->name = expr_str(expr, 0);
Tom Zanussi19a9fac2018-01-15 20:51:55 -06003039 expr->type = kstrdup(operand1->type, GFP_KERNEL);
3040 if (!expr->type) {
3041 ret = -ENOMEM;
3042 goto free;
3043 }
Tom Zanussi100719d2018-01-15 20:51:52 -06003044
3045 switch (field_op) {
3046 case FIELD_OP_MINUS:
3047 expr->fn = hist_field_minus;
3048 break;
3049 case FIELD_OP_PLUS:
3050 expr->fn = hist_field_plus;
3051 break;
3052 default:
Dan Carpenter5e4cf2b2018-03-23 14:37:36 +03003053 ret = -EINVAL;
Tom Zanussi100719d2018-01-15 20:51:52 -06003054 goto free;
3055 }
3056
3057 return expr;
3058 free:
3059 destroy_hist_field(operand1, 0);
3060 destroy_hist_field(operand2, 0);
3061 destroy_hist_field(expr, 0);
3062
3063 return ERR_PTR(ret);
3064}
3065
Tom Zanussi02205a62018-01-15 20:51:59 -06003066static char *find_trigger_filter(struct hist_trigger_data *hist_data,
3067 struct trace_event_file *file)
3068{
3069 struct event_trigger_data *test;
3070
3071 list_for_each_entry_rcu(test, &file->triggers, list) {
3072 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
3073 if (test->private_data == hist_data)
3074 return test->filter_str;
3075 }
3076 }
3077
3078 return NULL;
3079}
3080
3081static struct event_command trigger_hist_cmd;
3082static int event_hist_trigger_func(struct event_command *cmd_ops,
3083 struct trace_event_file *file,
3084 char *glob, char *cmd, char *param);
3085
3086static bool compatible_keys(struct hist_trigger_data *target_hist_data,
3087 struct hist_trigger_data *hist_data,
3088 unsigned int n_keys)
3089{
3090 struct hist_field *target_hist_field, *hist_field;
3091 unsigned int n, i, j;
3092
3093 if (hist_data->n_fields - hist_data->n_vals != n_keys)
3094 return false;
3095
3096 i = hist_data->n_vals;
3097 j = target_hist_data->n_vals;
3098
3099 for (n = 0; n < n_keys; n++) {
3100 hist_field = hist_data->fields[i + n];
3101 target_hist_field = target_hist_data->fields[j + n];
3102
3103 if (strcmp(hist_field->type, target_hist_field->type) != 0)
3104 return false;
3105 if (hist_field->size != target_hist_field->size)
3106 return false;
3107 if (hist_field->is_signed != target_hist_field->is_signed)
3108 return false;
3109 }
3110
3111 return true;
3112}
3113
3114static struct hist_trigger_data *
3115find_compatible_hist(struct hist_trigger_data *target_hist_data,
3116 struct trace_event_file *file)
3117{
3118 struct hist_trigger_data *hist_data;
3119 struct event_trigger_data *test;
3120 unsigned int n_keys;
3121
3122 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
3123
3124 list_for_each_entry_rcu(test, &file->triggers, list) {
3125 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
3126 hist_data = test->private_data;
3127
3128 if (compatible_keys(target_hist_data, hist_data, n_keys))
3129 return hist_data;
3130 }
3131 }
3132
3133 return NULL;
3134}
3135
3136static struct trace_event_file *event_file(struct trace_array *tr,
3137 char *system, char *event_name)
3138{
3139 struct trace_event_file *file;
3140
Steven Rostedt (VMware)3be4c1e2018-05-10 12:42:10 -04003141 file = __find_event_file(tr, system, event_name);
Tom Zanussi02205a62018-01-15 20:51:59 -06003142 if (!file)
3143 return ERR_PTR(-EINVAL);
3144
3145 return file;
3146}
3147
3148static struct hist_field *
3149find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
3150 char *system, char *event_name, char *field_name)
3151{
3152 struct hist_field *event_var;
3153 char *synthetic_name;
3154
3155 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3156 if (!synthetic_name)
3157 return ERR_PTR(-ENOMEM);
3158
3159 strcpy(synthetic_name, "synthetic_");
3160 strcat(synthetic_name, field_name);
3161
3162 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
3163
3164 kfree(synthetic_name);
3165
3166 return event_var;
3167}
3168
3169/**
3170 * create_field_var_hist - Automatically create a histogram and var for a field
3171 * @target_hist_data: The target hist trigger
3172 * @subsys_name: Optional subsystem name
3173 * @event_name: Optional event name
3174 * @field_name: The name of the field (and the resulting variable)
3175 *
3176 * Hist trigger actions fetch data from variables, not directly from
3177 * events. However, for convenience, users are allowed to directly
3178 * specify an event field in an action, which will be automatically
3179 * converted into a variable on their behalf.
3180
3181 * If a user specifies a field on an event that isn't the event the
3182 * histogram currently being defined (the target event histogram), the
3183 * only way that can be accomplished is if a new hist trigger is
3184 * created and the field variable defined on that.
3185 *
3186 * This function creates a new histogram compatible with the target
3187 * event (meaning a histogram with the same key as the target
3188 * histogram), and creates a variable for the specified field, but
3189 * with 'synthetic_' prepended to the variable name in order to avoid
3190 * collision with normal field variables.
3191 *
3192 * Return: The variable created for the field.
3193 */
Tom Zanussic282a382018-01-15 20:52:00 -06003194static struct hist_field *
Tom Zanussi02205a62018-01-15 20:51:59 -06003195create_field_var_hist(struct hist_trigger_data *target_hist_data,
3196 char *subsys_name, char *event_name, char *field_name)
3197{
3198 struct trace_array *tr = target_hist_data->event_file->tr;
3199 struct hist_field *event_var = ERR_PTR(-EINVAL);
3200 struct hist_trigger_data *hist_data;
3201 unsigned int i, n, first = true;
3202 struct field_var_hist *var_hist;
3203 struct trace_event_file *file;
3204 struct hist_field *key_field;
3205 char *saved_filter;
3206 char *cmd;
3207 int ret;
3208
Tom Zanussif404da62018-01-15 20:52:05 -06003209 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003210 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
Tom Zanussi02205a62018-01-15 20:51:59 -06003211 return ERR_PTR(-EINVAL);
Tom Zanussif404da62018-01-15 20:52:05 -06003212 }
Tom Zanussi02205a62018-01-15 20:51:59 -06003213
3214 file = event_file(tr, subsys_name, event_name);
3215
3216 if (IS_ERR(file)) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003217 hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name));
Tom Zanussi02205a62018-01-15 20:51:59 -06003218 ret = PTR_ERR(file);
3219 return ERR_PTR(ret);
3220 }
3221
3222 /*
3223 * Look for a histogram compatible with target. We'll use the
3224 * found histogram specification to create a new matching
3225 * histogram with our variable on it. target_hist_data is not
3226 * yet a registered histogram so we can't use that.
3227 */
3228 hist_data = find_compatible_hist(target_hist_data, file);
Tom Zanussif404da62018-01-15 20:52:05 -06003229 if (!hist_data) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003230 hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name));
Tom Zanussi02205a62018-01-15 20:51:59 -06003231 return ERR_PTR(-EINVAL);
Tom Zanussif404da62018-01-15 20:52:05 -06003232 }
Tom Zanussi02205a62018-01-15 20:51:59 -06003233
3234 /* See if a synthetic field variable has already been created */
3235 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3236 event_name, field_name);
3237 if (!IS_ERR_OR_NULL(event_var))
3238 return event_var;
3239
3240 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
3241 if (!var_hist)
3242 return ERR_PTR(-ENOMEM);
3243
3244 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3245 if (!cmd) {
3246 kfree(var_hist);
3247 return ERR_PTR(-ENOMEM);
3248 }
3249
3250 /* Use the same keys as the compatible histogram */
3251 strcat(cmd, "keys=");
3252
3253 for_each_hist_key_field(i, hist_data) {
3254 key_field = hist_data->fields[i];
3255 if (!first)
3256 strcat(cmd, ",");
3257 strcat(cmd, key_field->field->name);
3258 first = false;
3259 }
3260
3261 /* Create the synthetic field variable specification */
3262 strcat(cmd, ":synthetic_");
3263 strcat(cmd, field_name);
3264 strcat(cmd, "=");
3265 strcat(cmd, field_name);
3266
3267 /* Use the same filter as the compatible histogram */
3268 saved_filter = find_trigger_filter(hist_data, file);
3269 if (saved_filter) {
3270 strcat(cmd, " if ");
3271 strcat(cmd, saved_filter);
3272 }
3273
3274 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
3275 if (!var_hist->cmd) {
3276 kfree(cmd);
3277 kfree(var_hist);
3278 return ERR_PTR(-ENOMEM);
3279 }
3280
3281 /* Save the compatible histogram information */
3282 var_hist->hist_data = hist_data;
3283
3284 /* Create the new histogram with our variable */
3285 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
3286 "", "hist", cmd);
3287 if (ret) {
3288 kfree(cmd);
3289 kfree(var_hist->cmd);
3290 kfree(var_hist);
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003291 hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name));
Tom Zanussi02205a62018-01-15 20:51:59 -06003292 return ERR_PTR(ret);
3293 }
3294
3295 kfree(cmd);
3296
3297 /* If we can't find the variable, something went wrong */
3298 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3299 event_name, field_name);
3300 if (IS_ERR_OR_NULL(event_var)) {
3301 kfree(var_hist->cmd);
3302 kfree(var_hist);
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003303 hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
Tom Zanussi02205a62018-01-15 20:51:59 -06003304 return ERR_PTR(-EINVAL);
3305 }
3306
3307 n = target_hist_data->n_field_var_hists;
3308 target_hist_data->field_var_hists[n] = var_hist;
3309 target_hist_data->n_field_var_hists++;
3310
3311 return event_var;
3312}
3313
Tom Zanussic282a382018-01-15 20:52:00 -06003314static struct hist_field *
Tom Zanussi02205a62018-01-15 20:51:59 -06003315find_target_event_var(struct hist_trigger_data *hist_data,
3316 char *subsys_name, char *event_name, char *var_name)
3317{
3318 struct trace_event_file *file = hist_data->event_file;
3319 struct hist_field *hist_field = NULL;
3320
3321 if (subsys_name) {
3322 struct trace_event_call *call;
3323
3324 if (!event_name)
3325 return NULL;
3326
3327 call = file->event_call;
3328
3329 if (strcmp(subsys_name, call->class->system) != 0)
3330 return NULL;
3331
3332 if (strcmp(event_name, trace_event_name(call)) != 0)
3333 return NULL;
3334 }
3335
3336 hist_field = find_var_field(hist_data, var_name);
3337
3338 return hist_field;
3339}
3340
3341static inline void __update_field_vars(struct tracing_map_elt *elt,
3342 struct ring_buffer_event *rbe,
3343 void *rec,
3344 struct field_var **field_vars,
3345 unsigned int n_field_vars,
3346 unsigned int field_var_str_start)
3347{
3348 struct hist_elt_data *elt_data = elt->private_data;
3349 unsigned int i, j, var_idx;
3350 u64 var_val;
3351
3352 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
3353 struct field_var *field_var = field_vars[i];
3354 struct hist_field *var = field_var->var;
3355 struct hist_field *val = field_var->val;
3356
3357 var_val = val->fn(val, elt, rbe, rec);
3358 var_idx = var->var.idx;
3359
3360 if (val->flags & HIST_FIELD_FL_STRING) {
3361 char *str = elt_data->field_var_str[j++];
3362 char *val_str = (char *)(uintptr_t)var_val;
3363
Tom Zanussiad452872018-03-28 15:10:56 -05003364 strscpy(str, val_str, STR_VAR_LEN_MAX);
Tom Zanussi02205a62018-01-15 20:51:59 -06003365 var_val = (u64)(uintptr_t)str;
3366 }
3367 tracing_map_set_var(elt, var_idx, var_val);
3368 }
3369}
3370
3371static void update_field_vars(struct hist_trigger_data *hist_data,
3372 struct tracing_map_elt *elt,
3373 struct ring_buffer_event *rbe,
3374 void *rec)
3375{
3376 __update_field_vars(elt, rbe, rec, hist_data->field_vars,
3377 hist_data->n_field_vars, 0);
3378}
3379
Tom Zanussi466f4522019-02-13 17:42:44 -06003380static void save_track_data_vars(struct hist_trigger_data *hist_data,
3381 struct tracing_map_elt *elt, void *rec,
3382 struct ring_buffer_event *rbe, void *key,
3383 struct action_data *data, u64 *var_ref_vals)
Tom Zanussi50450602018-01-15 20:52:01 -06003384{
Tom Zanussi7d18a102019-02-13 17:42:41 -06003385 __update_field_vars(elt, rbe, rec, hist_data->save_vars,
3386 hist_data->n_save_vars, hist_data->n_field_var_str);
Tom Zanussi50450602018-01-15 20:52:01 -06003387}
3388
Tom Zanussi02205a62018-01-15 20:51:59 -06003389static struct hist_field *create_var(struct hist_trigger_data *hist_data,
3390 struct trace_event_file *file,
3391 char *name, int size, const char *type)
3392{
3393 struct hist_field *var;
3394 int idx;
3395
3396 if (find_var(hist_data, file, name) && !hist_data->remove) {
3397 var = ERR_PTR(-EINVAL);
3398 goto out;
3399 }
3400
3401 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3402 if (!var) {
3403 var = ERR_PTR(-ENOMEM);
3404 goto out;
3405 }
3406
3407 idx = tracing_map_add_var(hist_data->map);
3408 if (idx < 0) {
3409 kfree(var);
3410 var = ERR_PTR(-EINVAL);
3411 goto out;
3412 }
3413
3414 var->flags = HIST_FIELD_FL_VAR;
3415 var->var.idx = idx;
3416 var->var.hist_data = var->hist_data = hist_data;
3417 var->size = size;
3418 var->var.name = kstrdup(name, GFP_KERNEL);
3419 var->type = kstrdup(type, GFP_KERNEL);
3420 if (!var->var.name || !var->type) {
3421 kfree(var->var.name);
3422 kfree(var->type);
3423 kfree(var);
3424 var = ERR_PTR(-ENOMEM);
3425 }
3426 out:
3427 return var;
3428}
3429
3430static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
3431 struct trace_event_file *file,
3432 char *field_name)
3433{
3434 struct hist_field *val = NULL, *var = NULL;
3435 unsigned long flags = HIST_FIELD_FL_VAR;
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003436 struct trace_array *tr = file->tr;
Tom Zanussi02205a62018-01-15 20:51:59 -06003437 struct field_var *field_var;
3438 int ret = 0;
3439
3440 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003441 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
Tom Zanussi02205a62018-01-15 20:51:59 -06003442 ret = -EINVAL;
3443 goto err;
3444 }
3445
3446 val = parse_atom(hist_data, file, field_name, &flags, NULL);
3447 if (IS_ERR(val)) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003448 hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name));
Tom Zanussi02205a62018-01-15 20:51:59 -06003449 ret = PTR_ERR(val);
3450 goto err;
3451 }
3452
3453 var = create_var(hist_data, file, field_name, val->size, val->type);
3454 if (IS_ERR(var)) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003455 hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name));
Tom Zanussi02205a62018-01-15 20:51:59 -06003456 kfree(val);
3457 ret = PTR_ERR(var);
3458 goto err;
3459 }
3460
3461 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
3462 if (!field_var) {
3463 kfree(val);
3464 kfree(var);
3465 ret = -ENOMEM;
3466 goto err;
3467 }
3468
3469 field_var->var = var;
3470 field_var->val = val;
3471 out:
3472 return field_var;
3473 err:
3474 field_var = ERR_PTR(ret);
3475 goto out;
3476}
3477
3478/**
3479 * create_target_field_var - Automatically create a variable for a field
3480 * @target_hist_data: The target hist trigger
3481 * @subsys_name: Optional subsystem name
3482 * @event_name: Optional event name
3483 * @var_name: The name of the field (and the resulting variable)
3484 *
3485 * Hist trigger actions fetch data from variables, not directly from
3486 * events. However, for convenience, users are allowed to directly
3487 * specify an event field in an action, which will be automatically
3488 * converted into a variable on their behalf.
3489
3490 * This function creates a field variable with the name var_name on
3491 * the hist trigger currently being defined on the target event. If
3492 * subsys_name and event_name are specified, this function simply
3493 * verifies that they do in fact match the target event subsystem and
3494 * event name.
3495 *
3496 * Return: The variable created for the field.
3497 */
Tom Zanussic282a382018-01-15 20:52:00 -06003498static struct field_var *
Tom Zanussi02205a62018-01-15 20:51:59 -06003499create_target_field_var(struct hist_trigger_data *target_hist_data,
3500 char *subsys_name, char *event_name, char *var_name)
3501{
3502 struct trace_event_file *file = target_hist_data->event_file;
3503
3504 if (subsys_name) {
3505 struct trace_event_call *call;
3506
3507 if (!event_name)
3508 return NULL;
3509
3510 call = file->event_call;
3511
3512 if (strcmp(subsys_name, call->class->system) != 0)
3513 return NULL;
3514
3515 if (strcmp(event_name, trace_event_name(call)) != 0)
3516 return NULL;
3517 }
3518
3519 return create_field_var(target_hist_data, file, var_name);
3520}
3521
Tom Zanussi466f4522019-02-13 17:42:44 -06003522static bool check_track_val_max(u64 track_val, u64 var_val)
Tom Zanussi50450602018-01-15 20:52:01 -06003523{
Tom Zanussi466f4522019-02-13 17:42:44 -06003524 if (var_val <= track_val)
3525 return false;
Tom Zanussi50450602018-01-15 20:52:01 -06003526
Tom Zanussi466f4522019-02-13 17:42:44 -06003527 return true;
3528}
3529
Tom Zanussidff81f52019-02-13 17:42:48 -06003530static bool check_track_val_changed(u64 track_val, u64 var_val)
3531{
3532 if (var_val == track_val)
3533 return false;
3534
3535 return true;
3536}
3537
Tom Zanussi466f4522019-02-13 17:42:44 -06003538static u64 get_track_val(struct hist_trigger_data *hist_data,
3539 struct tracing_map_elt *elt,
3540 struct action_data *data)
3541{
3542 unsigned int track_var_idx = data->track_data.track_var->var.idx;
3543 u64 track_val;
3544
3545 track_val = tracing_map_read_var(elt, track_var_idx);
3546
3547 return track_val;
3548}
3549
3550static void save_track_val(struct hist_trigger_data *hist_data,
3551 struct tracing_map_elt *elt,
3552 struct action_data *data, u64 var_val)
3553{
3554 unsigned int track_var_idx = data->track_data.track_var->var.idx;
3555
3556 tracing_map_set_var(elt, track_var_idx, var_val);
3557}
3558
3559static void save_track_data(struct hist_trigger_data *hist_data,
3560 struct tracing_map_elt *elt, void *rec,
3561 struct ring_buffer_event *rbe, void *key,
3562 struct action_data *data, u64 *var_ref_vals)
3563{
3564 if (data->track_data.save_data)
3565 data->track_data.save_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
3566}
3567
3568static bool check_track_val(struct tracing_map_elt *elt,
3569 struct action_data *data,
3570 u64 var_val)
3571{
3572 struct hist_trigger_data *hist_data;
3573 u64 track_val;
3574
3575 hist_data = data->track_data.track_var->hist_data;
3576 track_val = get_track_val(hist_data, elt, data);
3577
3578 return data->track_data.check_val(track_val, var_val);
3579}
3580
Tom Zanussia3785b72019-02-13 17:42:46 -06003581#ifdef CONFIG_TRACER_SNAPSHOT
3582static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3583{
3584 /* called with tr->max_lock held */
3585 struct track_data *track_data = tr->cond_snapshot->cond_data;
3586 struct hist_elt_data *elt_data, *track_elt_data;
3587 struct snapshot_context *context = cond_data;
Tom Zanussi9b2ca372019-04-18 10:18:52 -05003588 struct action_data *action;
Tom Zanussia3785b72019-02-13 17:42:46 -06003589 u64 track_val;
3590
3591 if (!track_data)
3592 return false;
3593
Tom Zanussi9b2ca372019-04-18 10:18:52 -05003594 action = track_data->action_data;
3595
Tom Zanussia3785b72019-02-13 17:42:46 -06003596 track_val = get_track_val(track_data->hist_data, context->elt,
3597 track_data->action_data);
3598
Tom Zanussi9b2ca372019-04-18 10:18:52 -05003599 if (!action->track_data.check_val(track_data->track_val, track_val))
3600 return false;
3601
Tom Zanussia3785b72019-02-13 17:42:46 -06003602 track_data->track_val = track_val;
3603 memcpy(track_data->key, context->key, track_data->key_len);
3604
3605 elt_data = context->elt->private_data;
3606 track_elt_data = track_data->elt.private_data;
3607 if (elt_data->comm)
Tom Zanussi27242c62019-03-05 10:11:59 -06003608 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
Tom Zanussia3785b72019-02-13 17:42:46 -06003609
3610 track_data->updated = true;
3611
3612 return true;
3613}
3614
3615static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3616 struct tracing_map_elt *elt, void *rec,
3617 struct ring_buffer_event *rbe, void *key,
3618 struct action_data *data,
3619 u64 *var_ref_vals)
3620{
3621 struct trace_event_file *file = hist_data->event_file;
3622 struct snapshot_context context;
3623
3624 context.elt = elt;
3625 context.key = key;
3626
3627 tracing_snapshot_cond(file->tr, &context);
3628}
3629
3630static void hist_trigger_print_key(struct seq_file *m,
3631 struct hist_trigger_data *hist_data,
3632 void *key,
3633 struct tracing_map_elt *elt);
3634
3635static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
3636{
3637 unsigned int i;
3638
3639 if (!hist_data->n_actions)
3640 return NULL;
3641
3642 for (i = 0; i < hist_data->n_actions; i++) {
3643 struct action_data *data = hist_data->actions[i];
3644
3645 if (data->action == ACTION_SNAPSHOT)
3646 return data;
3647 }
3648
3649 return NULL;
3650}
3651
3652static void track_data_snapshot_print(struct seq_file *m,
3653 struct hist_trigger_data *hist_data)
3654{
3655 struct trace_event_file *file = hist_data->event_file;
3656 struct track_data *track_data;
3657 struct action_data *action;
3658
3659 track_data = tracing_cond_snapshot_data(file->tr);
3660 if (!track_data)
3661 return;
3662
3663 if (!track_data->updated)
3664 return;
3665
3666 action = snapshot_action(hist_data);
3667 if (!action)
3668 return;
3669
3670 seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n");
3671 seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
3672 action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
3673 action->track_data.var_str, track_data->track_val);
3674
3675 seq_puts(m, "\ttriggered by event with key: ");
3676 hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
3677 seq_putc(m, '\n');
3678}
3679#else
3680static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3681{
3682 return false;
3683}
3684static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3685 struct tracing_map_elt *elt, void *rec,
3686 struct ring_buffer_event *rbe, void *key,
3687 struct action_data *data,
3688 u64 *var_ref_vals) {}
3689static void track_data_snapshot_print(struct seq_file *m,
3690 struct hist_trigger_data *hist_data) {}
3691#endif /* CONFIG_TRACER_SNAPSHOT */
3692
Tom Zanussi466f4522019-02-13 17:42:44 -06003693static void track_data_print(struct seq_file *m,
3694 struct hist_trigger_data *hist_data,
3695 struct tracing_map_elt *elt,
3696 struct action_data *data)
3697{
3698 u64 track_val = get_track_val(hist_data, elt, data);
3699 unsigned int i, save_var_idx;
3700
3701 if (data->handler == HANDLER_ONMAX)
3702 seq_printf(m, "\n\tmax: %10llu", track_val);
Tom Zanussidff81f52019-02-13 17:42:48 -06003703 else if (data->handler == HANDLER_ONCHANGE)
3704 seq_printf(m, "\n\tchanged: %10llu", track_val);
Tom Zanussi50450602018-01-15 20:52:01 -06003705
Tom Zanussia3785b72019-02-13 17:42:46 -06003706 if (data->action == ACTION_SNAPSHOT)
3707 return;
3708
Tom Zanussi7d18a102019-02-13 17:42:41 -06003709 for (i = 0; i < hist_data->n_save_vars; i++) {
3710 struct hist_field *save_val = hist_data->save_vars[i]->val;
3711 struct hist_field *save_var = hist_data->save_vars[i]->var;
Tom Zanussi50450602018-01-15 20:52:01 -06003712 u64 val;
3713
3714 save_var_idx = save_var->var.idx;
3715
3716 val = tracing_map_read_var(elt, save_var_idx);
3717
3718 if (save_val->flags & HIST_FIELD_FL_STRING) {
3719 seq_printf(m, " %s: %-32s", save_var->var.name,
3720 (char *)(uintptr_t)(val));
3721 } else
3722 seq_printf(m, " %s: %10llu", save_var->var.name, val);
3723 }
3724}
3725
Tom Zanussi466f4522019-02-13 17:42:44 -06003726static void ontrack_action(struct hist_trigger_data *hist_data,
3727 struct tracing_map_elt *elt, void *rec,
3728 struct ring_buffer_event *rbe, void *key,
3729 struct action_data *data, u64 *var_ref_vals)
Tom Zanussi50450602018-01-15 20:52:01 -06003730{
Tom Zanussi466f4522019-02-13 17:42:44 -06003731 u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
Tom Zanussi50450602018-01-15 20:52:01 -06003732
Tom Zanussi466f4522019-02-13 17:42:44 -06003733 if (check_track_val(elt, data, var_val)) {
3734 save_track_val(hist_data, elt, data, var_val);
3735 save_track_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
3736 }
Tom Zanussi50450602018-01-15 20:52:01 -06003737}
3738
Tom Zanussic3e49502019-02-13 17:42:43 -06003739static void action_data_destroy(struct action_data *data)
Tom Zanussi50450602018-01-15 20:52:01 -06003740{
3741 unsigned int i;
3742
Tom Zanussic3e49502019-02-13 17:42:43 -06003743 lockdep_assert_held(&event_mutex);
Tom Zanussi50450602018-01-15 20:52:01 -06003744
Tom Zanussi7d18a102019-02-13 17:42:41 -06003745 kfree(data->action_name);
Tom Zanussi50450602018-01-15 20:52:01 -06003746
3747 for (i = 0; i < data->n_params; i++)
3748 kfree(data->params[i]);
3749
Tom Zanussic3e49502019-02-13 17:42:43 -06003750 if (data->synth_event)
3751 data->synth_event->ref--;
3752
Tom Zanussie91eefd72019-02-13 17:42:50 -06003753 kfree(data->synth_event_name);
3754
Tom Zanussi50450602018-01-15 20:52:01 -06003755 kfree(data);
3756}
3757
Tom Zanussi466f4522019-02-13 17:42:44 -06003758static void track_data_destroy(struct hist_trigger_data *hist_data,
3759 struct action_data *data)
Tom Zanussic3e49502019-02-13 17:42:43 -06003760{
Tom Zanussia3785b72019-02-13 17:42:46 -06003761 struct trace_event_file *file = hist_data->event_file;
3762
Tom Zanussi466f4522019-02-13 17:42:44 -06003763 destroy_hist_field(data->track_data.track_var, 0);
Tom Zanussic3e49502019-02-13 17:42:43 -06003764
Tom Zanussia3785b72019-02-13 17:42:46 -06003765 if (data->action == ACTION_SNAPSHOT) {
3766 struct track_data *track_data;
3767
3768 track_data = tracing_cond_snapshot_data(file->tr);
3769 if (track_data && track_data->hist_data == hist_data) {
3770 tracing_snapshot_cond_disable(file->tr);
3771 track_data_free(track_data);
3772 }
3773 }
3774
Tom Zanussi466f4522019-02-13 17:42:44 -06003775 kfree(data->track_data.var_str);
Tom Zanussic3e49502019-02-13 17:42:43 -06003776
3777 action_data_destroy(data);
3778}
3779
Tom Zanussi7d18a102019-02-13 17:42:41 -06003780static int action_create(struct hist_trigger_data *hist_data,
3781 struct action_data *data);
3782
Tom Zanussi466f4522019-02-13 17:42:44 -06003783static int track_data_create(struct hist_trigger_data *hist_data,
3784 struct action_data *data)
Tom Zanussi50450602018-01-15 20:52:01 -06003785{
Tom Zanussi466f4522019-02-13 17:42:44 -06003786 struct hist_field *var_field, *ref_field, *track_var = NULL;
Tom Zanussi50450602018-01-15 20:52:01 -06003787 struct trace_event_file *file = hist_data->event_file;
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003788 struct trace_array *tr = file->tr;
Tom Zanussi466f4522019-02-13 17:42:44 -06003789 char *track_data_var_str;
Tom Zanussi50450602018-01-15 20:52:01 -06003790 int ret = 0;
3791
Tom Zanussi466f4522019-02-13 17:42:44 -06003792 track_data_var_str = data->track_data.var_str;
3793 if (track_data_var_str[0] != '$') {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003794 hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str));
Tom Zanussi50450602018-01-15 20:52:01 -06003795 return -EINVAL;
Tom Zanussif404da62018-01-15 20:52:05 -06003796 }
Tom Zanussi466f4522019-02-13 17:42:44 -06003797 track_data_var_str++;
Tom Zanussi50450602018-01-15 20:52:01 -06003798
Tom Zanussi466f4522019-02-13 17:42:44 -06003799 var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
Tom Zanussif404da62018-01-15 20:52:05 -06003800 if (!var_field) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003801 hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str));
Tom Zanussi50450602018-01-15 20:52:01 -06003802 return -EINVAL;
Tom Zanussif404da62018-01-15 20:52:05 -06003803 }
Tom Zanussi50450602018-01-15 20:52:01 -06003804
Tom Zanusside40f032018-12-18 14:33:23 -06003805 ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
Tom Zanussi50450602018-01-15 20:52:01 -06003806 if (!ref_field)
3807 return -ENOMEM;
3808
Tom Zanussi466f4522019-02-13 17:42:44 -06003809 data->track_data.var_ref = ref_field;
Tom Zanussi50450602018-01-15 20:52:01 -06003810
Tom Zanussi466f4522019-02-13 17:42:44 -06003811 if (data->handler == HANDLER_ONMAX)
3812 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
3813 if (IS_ERR(track_var)) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003814 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
Tom Zanussi466f4522019-02-13 17:42:44 -06003815 ret = PTR_ERR(track_var);
Tom Zanussi50450602018-01-15 20:52:01 -06003816 goto out;
3817 }
Tom Zanussidff81f52019-02-13 17:42:48 -06003818
3819 if (data->handler == HANDLER_ONCHANGE)
3820 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
3821 if (IS_ERR(track_var)) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003822 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
Tom Zanussidff81f52019-02-13 17:42:48 -06003823 ret = PTR_ERR(track_var);
3824 goto out;
3825 }
Tom Zanussi466f4522019-02-13 17:42:44 -06003826 data->track_data.track_var = track_var;
Tom Zanussi50450602018-01-15 20:52:01 -06003827
Tom Zanussi7d18a102019-02-13 17:42:41 -06003828 ret = action_create(hist_data, data);
Tom Zanussi50450602018-01-15 20:52:01 -06003829 out:
3830 return ret;
3831}
3832
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003833static int parse_action_params(struct trace_array *tr, char *params,
3834 struct action_data *data)
Tom Zanussi50450602018-01-15 20:52:01 -06003835{
3836 char *param, *saved_param;
Tom Zanussie91eefd72019-02-13 17:42:50 -06003837 bool first_param = true;
Tom Zanussi50450602018-01-15 20:52:01 -06003838 int ret = 0;
3839
3840 while (params) {
Tom Zanussi7d18a102019-02-13 17:42:41 -06003841 if (data->n_params >= SYNTH_FIELDS_MAX) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003842 hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0);
Tom Zanussi50450602018-01-15 20:52:01 -06003843 goto out;
Tom Zanussi7d18a102019-02-13 17:42:41 -06003844 }
Tom Zanussi50450602018-01-15 20:52:01 -06003845
3846 param = strsep(&params, ",");
3847 if (!param) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003848 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0);
Tom Zanussi50450602018-01-15 20:52:01 -06003849 ret = -EINVAL;
3850 goto out;
3851 }
3852
3853 param = strstrip(param);
3854 if (strlen(param) < 2) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003855 hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param));
Tom Zanussi50450602018-01-15 20:52:01 -06003856 ret = -EINVAL;
3857 goto out;
3858 }
3859
3860 saved_param = kstrdup(param, GFP_KERNEL);
3861 if (!saved_param) {
3862 ret = -ENOMEM;
3863 goto out;
3864 }
3865
Tom Zanussie91eefd72019-02-13 17:42:50 -06003866 if (first_param && data->use_trace_keyword) {
3867 data->synth_event_name = saved_param;
3868 first_param = false;
3869 continue;
3870 }
3871 first_param = false;
3872
Tom Zanussi50450602018-01-15 20:52:01 -06003873 data->params[data->n_params++] = saved_param;
3874 }
3875 out:
3876 return ret;
3877}
3878
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003879static int action_parse(struct trace_array *tr, char *str, struct action_data *data,
Tom Zanussi7d18a102019-02-13 17:42:41 -06003880 enum handler_id handler)
Tom Zanussi50450602018-01-15 20:52:01 -06003881{
Tom Zanussi7d18a102019-02-13 17:42:41 -06003882 char *action_name;
3883 int ret = 0;
3884
3885 strsep(&str, ".");
3886 if (!str) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003887 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
Tom Zanussi7d18a102019-02-13 17:42:41 -06003888 ret = -EINVAL;
3889 goto out;
3890 }
3891
3892 action_name = strsep(&str, "(");
3893 if (!action_name || !str) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003894 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
Tom Zanussi7d18a102019-02-13 17:42:41 -06003895 ret = -EINVAL;
3896 goto out;
3897 }
3898
3899 if (str_has_prefix(action_name, "save")) {
3900 char *params = strsep(&str, ")");
3901
3902 if (!params) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003903 hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0);
Tom Zanussi7d18a102019-02-13 17:42:41 -06003904 ret = -EINVAL;
3905 goto out;
3906 }
3907
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003908 ret = parse_action_params(tr, params, data);
Tom Zanussi7d18a102019-02-13 17:42:41 -06003909 if (ret)
3910 goto out;
3911
3912 if (handler == HANDLER_ONMAX)
Tom Zanussi466f4522019-02-13 17:42:44 -06003913 data->track_data.check_val = check_track_val_max;
Tom Zanussidff81f52019-02-13 17:42:48 -06003914 else if (handler == HANDLER_ONCHANGE)
3915 data->track_data.check_val = check_track_val_changed;
Tom Zanussi466f4522019-02-13 17:42:44 -06003916 else {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003917 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
Tom Zanussi466f4522019-02-13 17:42:44 -06003918 ret = -EINVAL;
3919 goto out;
3920 }
Tom Zanussi7d18a102019-02-13 17:42:41 -06003921
Tom Zanussi466f4522019-02-13 17:42:44 -06003922 data->track_data.save_data = save_track_data_vars;
3923 data->fn = ontrack_action;
Tom Zanussi7d18a102019-02-13 17:42:41 -06003924 data->action = ACTION_SAVE;
Tom Zanussia3785b72019-02-13 17:42:46 -06003925 } else if (str_has_prefix(action_name, "snapshot")) {
3926 char *params = strsep(&str, ")");
3927
3928 if (!str) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003929 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params));
Tom Zanussia3785b72019-02-13 17:42:46 -06003930 ret = -EINVAL;
3931 goto out;
3932 }
3933
3934 if (handler == HANDLER_ONMAX)
3935 data->track_data.check_val = check_track_val_max;
Tom Zanussidff81f52019-02-13 17:42:48 -06003936 else if (handler == HANDLER_ONCHANGE)
3937 data->track_data.check_val = check_track_val_changed;
Tom Zanussia3785b72019-02-13 17:42:46 -06003938 else {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003939 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
Tom Zanussia3785b72019-02-13 17:42:46 -06003940 ret = -EINVAL;
3941 goto out;
3942 }
3943
3944 data->track_data.save_data = save_track_data_snapshot;
3945 data->fn = ontrack_action;
3946 data->action = ACTION_SNAPSHOT;
Tom Zanussi7d18a102019-02-13 17:42:41 -06003947 } else {
3948 char *params = strsep(&str, ")");
3949
Tom Zanussie91eefd72019-02-13 17:42:50 -06003950 if (str_has_prefix(action_name, "trace"))
3951 data->use_trace_keyword = true;
3952
Tom Zanussi7d18a102019-02-13 17:42:41 -06003953 if (params) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04003954 ret = parse_action_params(tr, params, data);
Tom Zanussi7d18a102019-02-13 17:42:41 -06003955 if (ret)
3956 goto out;
3957 }
3958
Tom Zanussi466f4522019-02-13 17:42:44 -06003959 if (handler == HANDLER_ONMAX)
3960 data->track_data.check_val = check_track_val_max;
Tom Zanussidff81f52019-02-13 17:42:48 -06003961 else if (handler == HANDLER_ONCHANGE)
3962 data->track_data.check_val = check_track_val_changed;
Tom Zanussi466f4522019-02-13 17:42:44 -06003963
3964 if (handler != HANDLER_ONMATCH) {
3965 data->track_data.save_data = action_trace;
3966 data->fn = ontrack_action;
3967 } else
3968 data->fn = action_trace;
3969
Tom Zanussi7d18a102019-02-13 17:42:41 -06003970 data->action = ACTION_TRACE;
3971 }
3972
3973 data->action_name = kstrdup(action_name, GFP_KERNEL);
3974 if (!data->action_name) {
3975 ret = -ENOMEM;
3976 goto out;
3977 }
3978
3979 data->handler = handler;
3980 out:
3981 return ret;
3982}
3983
Tom Zanussi466f4522019-02-13 17:42:44 -06003984static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
3985 char *str, enum handler_id handler)
Tom Zanussi7d18a102019-02-13 17:42:41 -06003986{
Tom Zanussi50450602018-01-15 20:52:01 -06003987 struct action_data *data;
3988 int ret = -EINVAL;
Tom Zanussi466f4522019-02-13 17:42:44 -06003989 char *var_str;
Tom Zanussi50450602018-01-15 20:52:01 -06003990
3991 data = kzalloc(sizeof(*data), GFP_KERNEL);
3992 if (!data)
3993 return ERR_PTR(-ENOMEM);
3994
Tom Zanussi466f4522019-02-13 17:42:44 -06003995 var_str = strsep(&str, ")");
3996 if (!var_str || !str) {
Tom Zanussi50450602018-01-15 20:52:01 -06003997 ret = -EINVAL;
3998 goto free;
3999 }
4000
Tom Zanussi466f4522019-02-13 17:42:44 -06004001 data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
4002 if (!data->track_data.var_str) {
Tom Zanussi50450602018-01-15 20:52:01 -06004003 ret = -ENOMEM;
4004 goto free;
4005 }
4006
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004007 ret = action_parse(hist_data->event_file->tr, str, data, handler);
Tom Zanussi7d18a102019-02-13 17:42:41 -06004008 if (ret)
Tom Zanussi50450602018-01-15 20:52:01 -06004009 goto free;
Tom Zanussi50450602018-01-15 20:52:01 -06004010 out:
4011 return data;
4012 free:
Tom Zanussi466f4522019-02-13 17:42:44 -06004013 track_data_destroy(hist_data, data);
Tom Zanussi50450602018-01-15 20:52:01 -06004014 data = ERR_PTR(ret);
4015 goto out;
4016}
4017
Tom Zanussic282a382018-01-15 20:52:00 -06004018static void onmatch_destroy(struct action_data *data)
4019{
Tom Zanussic3e49502019-02-13 17:42:43 -06004020 kfree(data->match_data.event);
4021 kfree(data->match_data.event_system);
Tom Zanussic282a382018-01-15 20:52:00 -06004022
Tom Zanussic3e49502019-02-13 17:42:43 -06004023 action_data_destroy(data);
Tom Zanussic282a382018-01-15 20:52:00 -06004024}
4025
Tom Zanussi02205a62018-01-15 20:51:59 -06004026static void destroy_field_var(struct field_var *field_var)
4027{
4028 if (!field_var)
4029 return;
4030
4031 destroy_hist_field(field_var->var, 0);
4032 destroy_hist_field(field_var->val, 0);
4033
4034 kfree(field_var);
4035}
4036
4037static void destroy_field_vars(struct hist_trigger_data *hist_data)
4038{
4039 unsigned int i;
4040
4041 for (i = 0; i < hist_data->n_field_vars; i++)
4042 destroy_field_var(hist_data->field_vars[i]);
4043}
4044
Tom Zanussic282a382018-01-15 20:52:00 -06004045static void save_field_var(struct hist_trigger_data *hist_data,
4046 struct field_var *field_var)
Tom Zanussi02205a62018-01-15 20:51:59 -06004047{
4048 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
4049
4050 if (field_var->val->flags & HIST_FIELD_FL_STRING)
4051 hist_data->n_field_var_str++;
4052}
4053
Tom Zanussic282a382018-01-15 20:52:00 -06004054
Tom Zanussic282a382018-01-15 20:52:00 -06004055static int check_synth_field(struct synth_event *event,
4056 struct hist_field *hist_field,
4057 unsigned int field_pos)
4058{
4059 struct synth_field *field;
4060
4061 if (field_pos >= event->n_fields)
4062 return -EINVAL;
4063
4064 field = event->fields[field_pos];
4065
4066 if (strcmp(field->type, hist_field->type) != 0)
4067 return -EINVAL;
4068
4069 return 0;
4070}
4071
Tom Zanussic282a382018-01-15 20:52:00 -06004072static struct hist_field *
Tom Zanussi7d18a102019-02-13 17:42:41 -06004073trace_action_find_var(struct hist_trigger_data *hist_data,
4074 struct action_data *data,
4075 char *system, char *event, char *var)
Tom Zanussic282a382018-01-15 20:52:00 -06004076{
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004077 struct trace_array *tr = hist_data->event_file->tr;
Tom Zanussic282a382018-01-15 20:52:00 -06004078 struct hist_field *hist_field;
4079
4080 var++; /* skip '$' */
4081
4082 hist_field = find_target_event_var(hist_data, system, event, var);
4083 if (!hist_field) {
Tom Zanussi7d18a102019-02-13 17:42:41 -06004084 if (!system && data->handler == HANDLER_ONMATCH) {
Tom Zanussic3e49502019-02-13 17:42:43 -06004085 system = data->match_data.event_system;
4086 event = data->match_data.event;
Tom Zanussic282a382018-01-15 20:52:00 -06004087 }
4088
4089 hist_field = find_event_var(hist_data, system, event, var);
4090 }
4091
Tom Zanussif404da62018-01-15 20:52:05 -06004092 if (!hist_field)
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004093 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var));
Tom Zanussif404da62018-01-15 20:52:05 -06004094
Tom Zanussic282a382018-01-15 20:52:00 -06004095 return hist_field;
4096}
4097
4098static struct hist_field *
Tom Zanussi7d18a102019-02-13 17:42:41 -06004099trace_action_create_field_var(struct hist_trigger_data *hist_data,
4100 struct action_data *data, char *system,
4101 char *event, char *var)
Tom Zanussic282a382018-01-15 20:52:00 -06004102{
4103 struct hist_field *hist_field = NULL;
4104 struct field_var *field_var;
4105
4106 /*
4107 * First try to create a field var on the target event (the
4108 * currently being defined). This will create a variable for
4109 * unqualified fields on the target event, or if qualified,
4110 * target fields that have qualified names matching the target.
4111 */
4112 field_var = create_target_field_var(hist_data, system, event, var);
4113
4114 if (field_var && !IS_ERR(field_var)) {
4115 save_field_var(hist_data, field_var);
4116 hist_field = field_var->var;
4117 } else {
4118 field_var = NULL;
4119 /*
4120 * If no explicit system.event is specfied, default to
4121 * looking for fields on the onmatch(system.event.xxx)
4122 * event.
4123 */
Tom Zanussi7d18a102019-02-13 17:42:41 -06004124 if (!system && data->handler == HANDLER_ONMATCH) {
Tom Zanussic3e49502019-02-13 17:42:43 -06004125 system = data->match_data.event_system;
4126 event = data->match_data.event;
Tom Zanussic282a382018-01-15 20:52:00 -06004127 }
4128
4129 /*
4130 * At this point, we're looking at a field on another
4131 * event. Because we can't modify a hist trigger on
4132 * another event to add a variable for a field, we need
4133 * to create a new trigger on that event and create the
4134 * variable at the same time.
4135 */
4136 hist_field = create_field_var_hist(hist_data, system, event, var);
4137 if (IS_ERR(hist_field))
4138 goto free;
4139 }
4140 out:
4141 return hist_field;
4142 free:
4143 destroy_field_var(field_var);
4144 hist_field = NULL;
4145 goto out;
4146}
4147
Tom Zanussi7d18a102019-02-13 17:42:41 -06004148static int trace_action_create(struct hist_trigger_data *hist_data,
4149 struct action_data *data)
Tom Zanussic282a382018-01-15 20:52:00 -06004150{
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004151 struct trace_array *tr = hist_data->event_file->tr;
Tom Zanussic282a382018-01-15 20:52:00 -06004152 char *event_name, *param, *system = NULL;
4153 struct hist_field *hist_field, *var_ref;
4154 unsigned int i, var_ref_idx;
4155 unsigned int field_pos = 0;
4156 struct synth_event *event;
Tom Zanussie91eefd72019-02-13 17:42:50 -06004157 char *synth_event_name;
Tom Zanussic282a382018-01-15 20:52:00 -06004158 int ret = 0;
4159
Masami Hiramatsu0e2b81f72018-11-05 18:04:01 +09004160 lockdep_assert_held(&event_mutex);
4161
Tom Zanussie91eefd72019-02-13 17:42:50 -06004162 if (data->use_trace_keyword)
4163 synth_event_name = data->synth_event_name;
4164 else
4165 synth_event_name = data->action_name;
4166
4167 event = find_synth_event(synth_event_name);
Tom Zanussic282a382018-01-15 20:52:00 -06004168 if (!event) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004169 hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name));
Tom Zanussic282a382018-01-15 20:52:00 -06004170 return -EINVAL;
4171 }
Tom Zanussi7d18a102019-02-13 17:42:41 -06004172
Tom Zanussic282a382018-01-15 20:52:00 -06004173 event->ref++;
Tom Zanussic282a382018-01-15 20:52:00 -06004174
4175 var_ref_idx = hist_data->n_var_refs;
4176
4177 for (i = 0; i < data->n_params; i++) {
4178 char *p;
4179
4180 p = param = kstrdup(data->params[i], GFP_KERNEL);
4181 if (!param) {
4182 ret = -ENOMEM;
4183 goto err;
4184 }
4185
4186 system = strsep(&param, ".");
4187 if (!param) {
4188 param = (char *)system;
4189 system = event_name = NULL;
4190 } else {
4191 event_name = strsep(&param, ".");
4192 if (!param) {
4193 kfree(p);
4194 ret = -EINVAL;
4195 goto err;
4196 }
4197 }
4198
4199 if (param[0] == '$')
Tom Zanussi7d18a102019-02-13 17:42:41 -06004200 hist_field = trace_action_find_var(hist_data, data,
4201 system, event_name,
4202 param);
Tom Zanussic282a382018-01-15 20:52:00 -06004203 else
Tom Zanussi7d18a102019-02-13 17:42:41 -06004204 hist_field = trace_action_create_field_var(hist_data,
4205 data,
4206 system,
4207 event_name,
4208 param);
Tom Zanussic282a382018-01-15 20:52:00 -06004209
4210 if (!hist_field) {
4211 kfree(p);
4212 ret = -EINVAL;
4213 goto err;
4214 }
4215
4216 if (check_synth_field(event, hist_field, field_pos) == 0) {
Tom Zanusside40f032018-12-18 14:33:23 -06004217 var_ref = create_var_ref(hist_data, hist_field,
4218 system, event_name);
Tom Zanussic282a382018-01-15 20:52:00 -06004219 if (!var_ref) {
4220 kfree(p);
4221 ret = -ENOMEM;
4222 goto err;
4223 }
4224
Tom Zanussic282a382018-01-15 20:52:00 -06004225 field_pos++;
4226 kfree(p);
4227 continue;
4228 }
4229
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004230 hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param));
Tom Zanussic282a382018-01-15 20:52:00 -06004231 kfree(p);
4232 ret = -EINVAL;
4233 goto err;
4234 }
4235
4236 if (field_pos != event->n_fields) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004237 hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name));
Tom Zanussic282a382018-01-15 20:52:00 -06004238 ret = -EINVAL;
4239 goto err;
4240 }
4241
Tom Zanussic3e49502019-02-13 17:42:43 -06004242 data->synth_event = event;
4243 data->var_ref_idx = var_ref_idx;
Tom Zanussic282a382018-01-15 20:52:00 -06004244 out:
4245 return ret;
4246 err:
Tom Zanussic282a382018-01-15 20:52:00 -06004247 event->ref--;
Tom Zanussic282a382018-01-15 20:52:00 -06004248
4249 goto out;
4250}
4251
Tom Zanussi7d18a102019-02-13 17:42:41 -06004252static int action_create(struct hist_trigger_data *hist_data,
4253 struct action_data *data)
4254{
Tom Zanussia3785b72019-02-13 17:42:46 -06004255 struct trace_event_file *file = hist_data->event_file;
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004256 struct trace_array *tr = file->tr;
Tom Zanussia3785b72019-02-13 17:42:46 -06004257 struct track_data *track_data;
Tom Zanussi7d18a102019-02-13 17:42:41 -06004258 struct field_var *field_var;
4259 unsigned int i;
4260 char *param;
4261 int ret = 0;
4262
4263 if (data->action == ACTION_TRACE)
4264 return trace_action_create(hist_data, data);
4265
Tom Zanussia3785b72019-02-13 17:42:46 -06004266 if (data->action == ACTION_SNAPSHOT) {
4267 track_data = track_data_alloc(hist_data->key_size, data, hist_data);
4268 if (IS_ERR(track_data)) {
4269 ret = PTR_ERR(track_data);
4270 goto out;
4271 }
4272
4273 ret = tracing_snapshot_cond_enable(file->tr, track_data,
4274 cond_snapshot_update);
4275 if (ret)
4276 track_data_free(track_data);
4277
4278 goto out;
4279 }
4280
Tom Zanussi7d18a102019-02-13 17:42:41 -06004281 if (data->action == ACTION_SAVE) {
4282 if (hist_data->n_save_vars) {
4283 ret = -EEXIST;
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004284 hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0);
Tom Zanussi7d18a102019-02-13 17:42:41 -06004285 goto out;
4286 }
4287
4288 for (i = 0; i < data->n_params; i++) {
4289 param = kstrdup(data->params[i], GFP_KERNEL);
4290 if (!param) {
4291 ret = -ENOMEM;
4292 goto out;
4293 }
4294
4295 field_var = create_target_field_var(hist_data, NULL, NULL, param);
4296 if (IS_ERR(field_var)) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004297 hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL,
4298 errpos(param));
Tom Zanussi7d18a102019-02-13 17:42:41 -06004299 ret = PTR_ERR(field_var);
4300 kfree(param);
4301 goto out;
4302 }
4303
4304 hist_data->save_vars[hist_data->n_save_vars++] = field_var;
4305 if (field_var->val->flags & HIST_FIELD_FL_STRING)
4306 hist_data->n_save_var_str++;
4307 kfree(param);
4308 }
4309 }
4310 out:
4311 return ret;
4312}
4313
4314static int onmatch_create(struct hist_trigger_data *hist_data,
4315 struct action_data *data)
4316{
4317 return action_create(hist_data, data);
4318}
4319
Tom Zanussic282a382018-01-15 20:52:00 -06004320static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
4321{
4322 char *match_event, *match_event_system;
Tom Zanussic282a382018-01-15 20:52:00 -06004323 struct action_data *data;
4324 int ret = -EINVAL;
4325
4326 data = kzalloc(sizeof(*data), GFP_KERNEL);
4327 if (!data)
4328 return ERR_PTR(-ENOMEM);
4329
4330 match_event = strsep(&str, ")");
Tom Zanussif404da62018-01-15 20:52:05 -06004331 if (!match_event || !str) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004332 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event));
Tom Zanussic282a382018-01-15 20:52:00 -06004333 goto free;
Tom Zanussif404da62018-01-15 20:52:05 -06004334 }
Tom Zanussic282a382018-01-15 20:52:00 -06004335
4336 match_event_system = strsep(&match_event, ".");
Tom Zanussif404da62018-01-15 20:52:05 -06004337 if (!match_event) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004338 hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system));
Tom Zanussic282a382018-01-15 20:52:00 -06004339 goto free;
Tom Zanussif404da62018-01-15 20:52:05 -06004340 }
Tom Zanussic282a382018-01-15 20:52:00 -06004341
Tom Zanussif404da62018-01-15 20:52:05 -06004342 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004343 hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event));
Tom Zanussic282a382018-01-15 20:52:00 -06004344 goto free;
Tom Zanussif404da62018-01-15 20:52:05 -06004345 }
Tom Zanussic282a382018-01-15 20:52:00 -06004346
Tom Zanussic3e49502019-02-13 17:42:43 -06004347 data->match_data.event = kstrdup(match_event, GFP_KERNEL);
4348 if (!data->match_data.event) {
Tom Zanussic282a382018-01-15 20:52:00 -06004349 ret = -ENOMEM;
4350 goto free;
4351 }
4352
Tom Zanussic3e49502019-02-13 17:42:43 -06004353 data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
4354 if (!data->match_data.event_system) {
Tom Zanussic282a382018-01-15 20:52:00 -06004355 ret = -ENOMEM;
4356 goto free;
4357 }
4358
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004359 ret = action_parse(tr, str, data, HANDLER_ONMATCH);
Tom Zanussic282a382018-01-15 20:52:00 -06004360 if (ret)
4361 goto free;
4362 out:
4363 return data;
4364 free:
4365 onmatch_destroy(data);
4366 data = ERR_PTR(ret);
4367 goto out;
4368}
4369
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004370static int create_hitcount_val(struct hist_trigger_data *hist_data)
4371{
4372 hist_data->fields[HITCOUNT_IDX] =
Tom Zanussi30350d62018-01-15 20:51:49 -06004373 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004374 if (!hist_data->fields[HITCOUNT_IDX])
4375 return -ENOMEM;
4376
4377 hist_data->n_vals++;
Tom Zanussi30350d62018-01-15 20:51:49 -06004378 hist_data->n_fields++;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004379
4380 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
4381 return -EINVAL;
4382
4383 return 0;
4384}
4385
Tom Zanussi30350d62018-01-15 20:51:49 -06004386static int __create_val_field(struct hist_trigger_data *hist_data,
4387 unsigned int val_idx,
4388 struct trace_event_file *file,
4389 char *var_name, char *field_str,
4390 unsigned long flags)
Tom Zanussif2606832016-03-03 12:54:43 -06004391{
Tom Zanussi100719d2018-01-15 20:51:52 -06004392 struct hist_field *hist_field;
Tom Zanussif2606832016-03-03 12:54:43 -06004393 int ret = 0;
4394
Tom Zanussi100719d2018-01-15 20:51:52 -06004395 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
4396 if (IS_ERR(hist_field)) {
4397 ret = PTR_ERR(hist_field);
Tom Zanussif2606832016-03-03 12:54:43 -06004398 goto out;
4399 }
4400
Tom Zanussi100719d2018-01-15 20:51:52 -06004401 hist_data->fields[val_idx] = hist_field;
4402
Tom Zanussif2606832016-03-03 12:54:43 -06004403 ++hist_data->n_vals;
Tom Zanussi30350d62018-01-15 20:51:49 -06004404 ++hist_data->n_fields;
Tom Zanussif2606832016-03-03 12:54:43 -06004405
Tom Zanussi30350d62018-01-15 20:51:49 -06004406 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
Tom Zanussif2606832016-03-03 12:54:43 -06004407 ret = -EINVAL;
4408 out:
4409 return ret;
4410}
4411
Tom Zanussi30350d62018-01-15 20:51:49 -06004412static int create_val_field(struct hist_trigger_data *hist_data,
4413 unsigned int val_idx,
4414 struct trace_event_file *file,
4415 char *field_str)
4416{
4417 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
4418 return -EINVAL;
4419
4420 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
4421}
4422
4423static int create_var_field(struct hist_trigger_data *hist_data,
4424 unsigned int val_idx,
4425 struct trace_event_file *file,
4426 char *var_name, char *expr_str)
4427{
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004428 struct trace_array *tr = hist_data->event_file->tr;
Tom Zanussi30350d62018-01-15 20:51:49 -06004429 unsigned long flags = 0;
4430
4431 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4432 return -EINVAL;
Tom Zanussif404da62018-01-15 20:52:05 -06004433
Tom Zanussi30350d62018-01-15 20:51:49 -06004434 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004435 hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name));
Tom Zanussi30350d62018-01-15 20:51:49 -06004436 return -EINVAL;
4437 }
4438
4439 flags |= HIST_FIELD_FL_VAR;
4440 hist_data->n_vars++;
4441 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
4442 return -EINVAL;
4443
4444 return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
4445}
4446
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004447static int create_val_fields(struct hist_trigger_data *hist_data,
4448 struct trace_event_file *file)
4449{
Tom Zanussif2606832016-03-03 12:54:43 -06004450 char *fields_str, *field_str;
Tom Zanussi30350d62018-01-15 20:51:49 -06004451 unsigned int i, j = 1;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004452 int ret;
4453
4454 ret = create_hitcount_val(hist_data);
Tom Zanussif2606832016-03-03 12:54:43 -06004455 if (ret)
4456 goto out;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004457
Tom Zanussif2606832016-03-03 12:54:43 -06004458 fields_str = hist_data->attrs->vals_str;
4459 if (!fields_str)
4460 goto out;
4461
4462 strsep(&fields_str, "=");
4463 if (!fields_str)
4464 goto out;
4465
4466 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
4467 j < TRACING_MAP_VALS_MAX; i++) {
4468 field_str = strsep(&fields_str, ",");
4469 if (!field_str)
4470 break;
Tom Zanussi30350d62018-01-15 20:51:49 -06004471
Tom Zanussif2606832016-03-03 12:54:43 -06004472 if (strcmp(field_str, "hitcount") == 0)
4473 continue;
Tom Zanussi30350d62018-01-15 20:51:49 -06004474
Tom Zanussif2606832016-03-03 12:54:43 -06004475 ret = create_val_field(hist_data, j++, file, field_str);
4476 if (ret)
4477 goto out;
4478 }
Tom Zanussi30350d62018-01-15 20:51:49 -06004479
Tom Zanussif2606832016-03-03 12:54:43 -06004480 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
4481 ret = -EINVAL;
4482 out:
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004483 return ret;
4484}
4485
4486static int create_key_field(struct hist_trigger_data *hist_data,
4487 unsigned int key_idx,
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004488 unsigned int key_offset,
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004489 struct trace_event_file *file,
4490 char *field_str)
4491{
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004492 struct trace_array *tr = hist_data->event_file->tr;
Tom Zanussi30350d62018-01-15 20:51:49 -06004493 struct hist_field *hist_field = NULL;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004494 unsigned long flags = 0;
4495 unsigned int key_size;
4496 int ret = 0;
4497
Tom Zanussi30350d62018-01-15 20:51:49 -06004498 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004499 return -EINVAL;
4500
4501 flags |= HIST_FIELD_FL_KEY;
4502
Tom Zanussi69a02002016-03-03 12:54:52 -06004503 if (strcmp(field_str, "stacktrace") == 0) {
4504 flags |= HIST_FIELD_FL_STACKTRACE;
4505 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
Tom Zanussi30350d62018-01-15 20:51:49 -06004506 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
Tom Zanussi69a02002016-03-03 12:54:52 -06004507 } else {
Tom Zanussi100719d2018-01-15 20:51:52 -06004508 hist_field = parse_expr(hist_data, file, field_str, flags,
4509 NULL, 0);
4510 if (IS_ERR(hist_field)) {
4511 ret = PTR_ERR(hist_field);
4512 goto out;
Tom Zanussi69a02002016-03-03 12:54:52 -06004513 }
4514
Tom Zanussic8d94a12019-04-18 10:18:51 -05004515 if (field_has_hist_vars(hist_field, 0)) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004516 hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str));
Tom Zanussi067fe032018-01-15 20:51:56 -06004517 destroy_hist_field(hist_field, 0);
4518 ret = -EINVAL;
4519 goto out;
4520 }
4521
Tom Zanussi100719d2018-01-15 20:51:52 -06004522 key_size = hist_field->size;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004523 }
4524
Tom Zanussi100719d2018-01-15 20:51:52 -06004525 hist_data->fields[key_idx] = hist_field;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004526
4527 key_size = ALIGN(key_size, sizeof(u64));
4528 hist_data->fields[key_idx]->size = key_size;
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004529 hist_data->fields[key_idx]->offset = key_offset;
Tom Zanussi100719d2018-01-15 20:51:52 -06004530
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004531 hist_data->key_size += key_size;
Tom Zanussi100719d2018-01-15 20:51:52 -06004532
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004533 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
4534 ret = -EINVAL;
4535 goto out;
4536 }
4537
4538 hist_data->n_keys++;
Tom Zanussi30350d62018-01-15 20:51:49 -06004539 hist_data->n_fields++;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004540
4541 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
4542 return -EINVAL;
4543
4544 ret = key_size;
4545 out:
4546 return ret;
4547}
4548
4549static int create_key_fields(struct hist_trigger_data *hist_data,
4550 struct trace_event_file *file)
4551{
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004552 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004553 char *fields_str, *field_str;
4554 int ret = -EINVAL;
4555
4556 fields_str = hist_data->attrs->keys_str;
4557 if (!fields_str)
4558 goto out;
4559
4560 strsep(&fields_str, "=");
4561 if (!fields_str)
4562 goto out;
4563
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004564 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004565 field_str = strsep(&fields_str, ",");
4566 if (!field_str)
4567 break;
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004568 ret = create_key_field(hist_data, i, key_offset,
4569 file, field_str);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004570 if (ret < 0)
4571 goto out;
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004572 key_offset += ret;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004573 }
4574 if (fields_str) {
4575 ret = -EINVAL;
4576 goto out;
4577 }
4578 ret = 0;
4579 out:
4580 return ret;
4581}
4582
Tom Zanussi30350d62018-01-15 20:51:49 -06004583static int create_var_fields(struct hist_trigger_data *hist_data,
4584 struct trace_event_file *file)
4585{
4586 unsigned int i, j = hist_data->n_vals;
4587 int ret = 0;
4588
4589 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
4590
4591 for (i = 0; i < n_vars; i++) {
4592 char *var_name = hist_data->attrs->var_defs.name[i];
4593 char *expr = hist_data->attrs->var_defs.expr[i];
4594
4595 ret = create_var_field(hist_data, j++, file, var_name, expr);
4596 if (ret)
4597 goto out;
4598 }
4599 out:
4600 return ret;
4601}
4602
4603static void free_var_defs(struct hist_trigger_data *hist_data)
4604{
4605 unsigned int i;
4606
4607 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
4608 kfree(hist_data->attrs->var_defs.name[i]);
4609 kfree(hist_data->attrs->var_defs.expr[i]);
4610 }
4611
4612 hist_data->attrs->var_defs.n_vars = 0;
4613}
4614
4615static int parse_var_defs(struct hist_trigger_data *hist_data)
4616{
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004617 struct trace_array *tr = hist_data->event_file->tr;
Tom Zanussi30350d62018-01-15 20:51:49 -06004618 char *s, *str, *var_name, *field_str;
4619 unsigned int i, j, n_vars = 0;
4620 int ret = 0;
4621
4622 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
4623 str = hist_data->attrs->assignment_str[i];
4624 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
4625 field_str = strsep(&str, ",");
4626 if (!field_str)
4627 break;
4628
4629 var_name = strsep(&field_str, "=");
4630 if (!var_name || !field_str) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004631 hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT,
4632 errpos(var_name));
Tom Zanussi30350d62018-01-15 20:51:49 -06004633 ret = -EINVAL;
4634 goto free;
4635 }
4636
4637 if (n_vars == TRACING_MAP_VARS_MAX) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04004638 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name));
Tom Zanussi30350d62018-01-15 20:51:49 -06004639 ret = -EINVAL;
4640 goto free;
4641 }
4642
4643 s = kstrdup(var_name, GFP_KERNEL);
4644 if (!s) {
4645 ret = -ENOMEM;
4646 goto free;
4647 }
4648 hist_data->attrs->var_defs.name[n_vars] = s;
4649
4650 s = kstrdup(field_str, GFP_KERNEL);
4651 if (!s) {
4652 kfree(hist_data->attrs->var_defs.name[n_vars]);
4653 ret = -ENOMEM;
4654 goto free;
4655 }
4656 hist_data->attrs->var_defs.expr[n_vars++] = s;
4657
4658 hist_data->attrs->var_defs.n_vars = n_vars;
4659 }
4660 }
4661
4662 return ret;
4663 free:
4664 free_var_defs(hist_data);
4665
4666 return ret;
4667}
4668
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004669static int create_hist_fields(struct hist_trigger_data *hist_data,
4670 struct trace_event_file *file)
4671{
4672 int ret;
4673
Tom Zanussi30350d62018-01-15 20:51:49 -06004674 ret = parse_var_defs(hist_data);
4675 if (ret)
4676 goto out;
4677
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004678 ret = create_val_fields(hist_data, file);
4679 if (ret)
4680 goto out;
4681
Tom Zanussi30350d62018-01-15 20:51:49 -06004682 ret = create_var_fields(hist_data, file);
4683 if (ret)
4684 goto out;
4685
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004686 ret = create_key_fields(hist_data, file);
4687 if (ret)
4688 goto out;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004689 out:
Tom Zanussi30350d62018-01-15 20:51:49 -06004690 free_var_defs(hist_data);
4691
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004692 return ret;
4693}
4694
Tom Zanussie62347d2016-03-03 12:54:45 -06004695static int is_descending(const char *str)
4696{
4697 if (!str)
4698 return 0;
4699
4700 if (strcmp(str, "descending") == 0)
4701 return 1;
4702
4703 if (strcmp(str, "ascending") == 0)
4704 return 0;
4705
4706 return -EINVAL;
4707}
4708
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004709static int create_sort_keys(struct hist_trigger_data *hist_data)
4710{
Tom Zanussie62347d2016-03-03 12:54:45 -06004711 char *fields_str = hist_data->attrs->sort_key_str;
Tom Zanussie62347d2016-03-03 12:54:45 -06004712 struct tracing_map_sort_key *sort_key;
4713 int descending, ret = 0;
Tom Zanussi30350d62018-01-15 20:51:49 -06004714 unsigned int i, j, k;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004715
Tom Zanussie62347d2016-03-03 12:54:45 -06004716 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004717
Tom Zanussie62347d2016-03-03 12:54:45 -06004718 if (!fields_str)
4719 goto out;
4720
4721 strsep(&fields_str, "=");
4722 if (!fields_str) {
4723 ret = -EINVAL;
4724 goto out;
4725 }
4726
4727 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
Tom Zanussi85013252017-09-22 14:58:22 -05004728 struct hist_field *hist_field;
Tom Zanussie62347d2016-03-03 12:54:45 -06004729 char *field_str, *field_name;
Tom Zanussi85013252017-09-22 14:58:22 -05004730 const char *test_name;
Tom Zanussie62347d2016-03-03 12:54:45 -06004731
4732 sort_key = &hist_data->sort_keys[i];
4733
4734 field_str = strsep(&fields_str, ",");
4735 if (!field_str) {
4736 if (i == 0)
4737 ret = -EINVAL;
4738 break;
4739 }
4740
4741 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4742 ret = -EINVAL;
4743 break;
4744 }
4745
4746 field_name = strsep(&field_str, ".");
4747 if (!field_name) {
4748 ret = -EINVAL;
4749 break;
4750 }
4751
4752 if (strcmp(field_name, "hitcount") == 0) {
4753 descending = is_descending(field_str);
4754 if (descending < 0) {
4755 ret = descending;
4756 break;
4757 }
4758 sort_key->descending = descending;
4759 continue;
4760 }
4761
Tom Zanussi30350d62018-01-15 20:51:49 -06004762 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4763 unsigned int idx;
4764
Tom Zanussi85013252017-09-22 14:58:22 -05004765 hist_field = hist_data->fields[j];
Tom Zanussi30350d62018-01-15 20:51:49 -06004766 if (hist_field->flags & HIST_FIELD_FL_VAR)
4767 continue;
4768
4769 idx = k++;
4770
Tom Zanussi85013252017-09-22 14:58:22 -05004771 test_name = hist_field_name(hist_field, 0);
4772
4773 if (strcmp(field_name, test_name) == 0) {
Tom Zanussi30350d62018-01-15 20:51:49 -06004774 sort_key->field_idx = idx;
Tom Zanussie62347d2016-03-03 12:54:45 -06004775 descending = is_descending(field_str);
4776 if (descending < 0) {
4777 ret = descending;
4778 goto out;
4779 }
4780 sort_key->descending = descending;
4781 break;
4782 }
4783 }
4784 if (j == hist_data->n_fields) {
4785 ret = -EINVAL;
4786 break;
4787 }
4788 }
Tom Zanussi30350d62018-01-15 20:51:49 -06004789
Tom Zanussie62347d2016-03-03 12:54:45 -06004790 hist_data->n_sort_keys = i;
4791 out:
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004792 return ret;
4793}
4794
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004795static void destroy_actions(struct hist_trigger_data *hist_data)
4796{
4797 unsigned int i;
4798
4799 for (i = 0; i < hist_data->n_actions; i++) {
4800 struct action_data *data = hist_data->actions[i];
4801
Tom Zanussi7d18a102019-02-13 17:42:41 -06004802 if (data->handler == HANDLER_ONMATCH)
Tom Zanussic282a382018-01-15 20:52:00 -06004803 onmatch_destroy(data);
Tom Zanussidff81f52019-02-13 17:42:48 -06004804 else if (data->handler == HANDLER_ONMAX ||
4805 data->handler == HANDLER_ONCHANGE)
Tom Zanussi466f4522019-02-13 17:42:44 -06004806 track_data_destroy(hist_data, data);
Tom Zanussic282a382018-01-15 20:52:00 -06004807 else
4808 kfree(data);
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004809 }
4810}
4811
4812static int parse_actions(struct hist_trigger_data *hist_data)
4813{
Tom Zanussic282a382018-01-15 20:52:00 -06004814 struct trace_array *tr = hist_data->event_file->tr;
4815 struct action_data *data;
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004816 unsigned int i;
4817 int ret = 0;
4818 char *str;
Steven Rostedt (VMware)036876f2018-12-21 18:40:46 -05004819 int len;
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004820
4821 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4822 str = hist_data->attrs->action_str[i];
Tom Zanussic282a382018-01-15 20:52:00 -06004823
Steven Rostedt (VMware)036876f2018-12-21 18:40:46 -05004824 if ((len = str_has_prefix(str, "onmatch("))) {
4825 char *action_str = str + len;
Tom Zanussic282a382018-01-15 20:52:00 -06004826
4827 data = onmatch_parse(tr, action_str);
4828 if (IS_ERR(data)) {
4829 ret = PTR_ERR(data);
4830 break;
4831 }
Steven Rostedt (VMware)036876f2018-12-21 18:40:46 -05004832 } else if ((len = str_has_prefix(str, "onmax("))) {
4833 char *action_str = str + len;
Tom Zanussi50450602018-01-15 20:52:01 -06004834
Tom Zanussi466f4522019-02-13 17:42:44 -06004835 data = track_data_parse(hist_data, action_str,
4836 HANDLER_ONMAX);
Tom Zanussi50450602018-01-15 20:52:01 -06004837 if (IS_ERR(data)) {
4838 ret = PTR_ERR(data);
4839 break;
4840 }
Tom Zanussidff81f52019-02-13 17:42:48 -06004841 } else if ((len = str_has_prefix(str, "onchange("))) {
4842 char *action_str = str + len;
4843
4844 data = track_data_parse(hist_data, action_str,
4845 HANDLER_ONCHANGE);
4846 if (IS_ERR(data)) {
4847 ret = PTR_ERR(data);
4848 break;
4849 }
Tom Zanussic282a382018-01-15 20:52:00 -06004850 } else {
4851 ret = -EINVAL;
4852 break;
4853 }
4854
4855 hist_data->actions[hist_data->n_actions++] = data;
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004856 }
4857
4858 return ret;
4859}
4860
Tom Zanussi7d18a102019-02-13 17:42:41 -06004861static int create_actions(struct hist_trigger_data *hist_data)
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004862{
4863 struct action_data *data;
4864 unsigned int i;
4865 int ret = 0;
4866
4867 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4868 data = hist_data->actions[i];
Tom Zanussic282a382018-01-15 20:52:00 -06004869
Tom Zanussi7d18a102019-02-13 17:42:41 -06004870 if (data->handler == HANDLER_ONMATCH) {
4871 ret = onmatch_create(hist_data, data);
Tom Zanussic282a382018-01-15 20:52:00 -06004872 if (ret)
Tom Zanussi7d18a102019-02-13 17:42:41 -06004873 break;
Tom Zanussidff81f52019-02-13 17:42:48 -06004874 } else if (data->handler == HANDLER_ONMAX ||
4875 data->handler == HANDLER_ONCHANGE) {
Tom Zanussi466f4522019-02-13 17:42:44 -06004876 ret = track_data_create(hist_data, data);
Tom Zanussi50450602018-01-15 20:52:01 -06004877 if (ret)
Tom Zanussi7d18a102019-02-13 17:42:41 -06004878 break;
4879 } else {
4880 ret = -EINVAL;
4881 break;
Tom Zanussic282a382018-01-15 20:52:00 -06004882 }
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004883 }
4884
4885 return ret;
4886}
4887
Tom Zanussi50450602018-01-15 20:52:01 -06004888static void print_actions(struct seq_file *m,
4889 struct hist_trigger_data *hist_data,
4890 struct tracing_map_elt *elt)
4891{
4892 unsigned int i;
4893
4894 for (i = 0; i < hist_data->n_actions; i++) {
4895 struct action_data *data = hist_data->actions[i];
4896
Tom Zanussia3785b72019-02-13 17:42:46 -06004897 if (data->action == ACTION_SNAPSHOT)
4898 continue;
4899
Tom Zanussidff81f52019-02-13 17:42:48 -06004900 if (data->handler == HANDLER_ONMAX ||
4901 data->handler == HANDLER_ONCHANGE)
Tom Zanussi466f4522019-02-13 17:42:44 -06004902 track_data_print(m, hist_data, elt, data);
Tom Zanussi50450602018-01-15 20:52:01 -06004903 }
4904}
4905
Tom Zanussi7d18a102019-02-13 17:42:41 -06004906static void print_action_spec(struct seq_file *m,
4907 struct hist_trigger_data *hist_data,
4908 struct action_data *data)
4909{
4910 unsigned int i;
4911
4912 if (data->action == ACTION_SAVE) {
4913 for (i = 0; i < hist_data->n_save_vars; i++) {
4914 seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
4915 if (i < hist_data->n_save_vars - 1)
4916 seq_puts(m, ",");
4917 }
4918 } else if (data->action == ACTION_TRACE) {
Tom Zanussie91eefd72019-02-13 17:42:50 -06004919 if (data->use_trace_keyword)
4920 seq_printf(m, "%s", data->synth_event_name);
Tom Zanussi7d18a102019-02-13 17:42:41 -06004921 for (i = 0; i < data->n_params; i++) {
Tom Zanussie91eefd72019-02-13 17:42:50 -06004922 if (i || data->use_trace_keyword)
Tom Zanussi7d18a102019-02-13 17:42:41 -06004923 seq_puts(m, ",");
4924 seq_printf(m, "%s", data->params[i]);
4925 }
4926 }
4927}
4928
Tom Zanussi466f4522019-02-13 17:42:44 -06004929static void print_track_data_spec(struct seq_file *m,
4930 struct hist_trigger_data *hist_data,
4931 struct action_data *data)
Tom Zanussi50450602018-01-15 20:52:01 -06004932{
Tom Zanussi466f4522019-02-13 17:42:44 -06004933 if (data->handler == HANDLER_ONMAX)
4934 seq_puts(m, ":onmax(");
Tom Zanussidff81f52019-02-13 17:42:48 -06004935 else if (data->handler == HANDLER_ONCHANGE)
4936 seq_puts(m, ":onchange(");
Tom Zanussi466f4522019-02-13 17:42:44 -06004937 seq_printf(m, "%s", data->track_data.var_str);
Tom Zanussi7d18a102019-02-13 17:42:41 -06004938 seq_printf(m, ").%s(", data->action_name);
Tom Zanussi50450602018-01-15 20:52:01 -06004939
Tom Zanussi7d18a102019-02-13 17:42:41 -06004940 print_action_spec(m, hist_data, data);
4941
Tom Zanussi50450602018-01-15 20:52:01 -06004942 seq_puts(m, ")");
4943}
4944
Tom Zanussic282a382018-01-15 20:52:00 -06004945static void print_onmatch_spec(struct seq_file *m,
4946 struct hist_trigger_data *hist_data,
4947 struct action_data *data)
4948{
Tom Zanussic3e49502019-02-13 17:42:43 -06004949 seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
4950 data->match_data.event);
Tom Zanussic282a382018-01-15 20:52:00 -06004951
Tom Zanussi7d18a102019-02-13 17:42:41 -06004952 seq_printf(m, "%s(", data->action_name);
Tom Zanussic282a382018-01-15 20:52:00 -06004953
Tom Zanussi7d18a102019-02-13 17:42:41 -06004954 print_action_spec(m, hist_data, data);
Tom Zanussic282a382018-01-15 20:52:00 -06004955
4956 seq_puts(m, ")");
4957}
4958
Tom Zanussi48f79472018-03-28 15:10:55 -05004959static bool actions_match(struct hist_trigger_data *hist_data,
4960 struct hist_trigger_data *hist_data_test)
4961{
4962 unsigned int i, j;
4963
4964 if (hist_data->n_actions != hist_data_test->n_actions)
4965 return false;
4966
4967 for (i = 0; i < hist_data->n_actions; i++) {
4968 struct action_data *data = hist_data->actions[i];
4969 struct action_data *data_test = hist_data_test->actions[i];
Tom Zanussie91eefd72019-02-13 17:42:50 -06004970 char *action_name, *action_name_test;
Tom Zanussi48f79472018-03-28 15:10:55 -05004971
Tom Zanussi7d18a102019-02-13 17:42:41 -06004972 if (data->handler != data_test->handler)
4973 return false;
4974 if (data->action != data_test->action)
Tom Zanussi48f79472018-03-28 15:10:55 -05004975 return false;
4976
4977 if (data->n_params != data_test->n_params)
4978 return false;
4979
4980 for (j = 0; j < data->n_params; j++) {
4981 if (strcmp(data->params[j], data_test->params[j]) != 0)
4982 return false;
4983 }
4984
Tom Zanussie91eefd72019-02-13 17:42:50 -06004985 if (data->use_trace_keyword)
4986 action_name = data->synth_event_name;
4987 else
4988 action_name = data->action_name;
4989
4990 if (data_test->use_trace_keyword)
4991 action_name_test = data_test->synth_event_name;
4992 else
4993 action_name_test = data_test->action_name;
4994
4995 if (strcmp(action_name, action_name_test) != 0)
Tom Zanussi7d18a102019-02-13 17:42:41 -06004996 return false;
4997
4998 if (data->handler == HANDLER_ONMATCH) {
Tom Zanussic3e49502019-02-13 17:42:43 -06004999 if (strcmp(data->match_data.event_system,
5000 data_test->match_data.event_system) != 0)
Tom Zanussi48f79472018-03-28 15:10:55 -05005001 return false;
Tom Zanussic3e49502019-02-13 17:42:43 -06005002 if (strcmp(data->match_data.event,
5003 data_test->match_data.event) != 0)
Tom Zanussi48f79472018-03-28 15:10:55 -05005004 return false;
Tom Zanussidff81f52019-02-13 17:42:48 -06005005 } else if (data->handler == HANDLER_ONMAX ||
5006 data->handler == HANDLER_ONCHANGE) {
Tom Zanussi466f4522019-02-13 17:42:44 -06005007 if (strcmp(data->track_data.var_str,
5008 data_test->track_data.var_str) != 0)
Tom Zanussi48f79472018-03-28 15:10:55 -05005009 return false;
Tom Zanussi48f79472018-03-28 15:10:55 -05005010 }
5011 }
5012
5013 return true;
5014}
5015
5016
Tom Zanussic282a382018-01-15 20:52:00 -06005017static void print_actions_spec(struct seq_file *m,
5018 struct hist_trigger_data *hist_data)
5019{
5020 unsigned int i;
5021
5022 for (i = 0; i < hist_data->n_actions; i++) {
5023 struct action_data *data = hist_data->actions[i];
5024
Tom Zanussi7d18a102019-02-13 17:42:41 -06005025 if (data->handler == HANDLER_ONMATCH)
Tom Zanussic282a382018-01-15 20:52:00 -06005026 print_onmatch_spec(m, hist_data, data);
Tom Zanussidff81f52019-02-13 17:42:48 -06005027 else if (data->handler == HANDLER_ONMAX ||
5028 data->handler == HANDLER_ONCHANGE)
Tom Zanussi466f4522019-02-13 17:42:44 -06005029 print_track_data_spec(m, hist_data, data);
Tom Zanussic282a382018-01-15 20:52:00 -06005030 }
5031}
5032
Tom Zanussi02205a62018-01-15 20:51:59 -06005033static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
5034{
5035 unsigned int i;
5036
5037 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5038 kfree(hist_data->field_var_hists[i]->cmd);
5039 kfree(hist_data->field_var_hists[i]);
5040 }
5041}
5042
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005043static void destroy_hist_data(struct hist_trigger_data *hist_data)
5044{
Tom Zanussi0212e2a2018-01-15 20:51:57 -06005045 if (!hist_data)
5046 return;
5047
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005048 destroy_hist_trigger_attrs(hist_data->attrs);
5049 destroy_hist_fields(hist_data);
5050 tracing_map_destroy(hist_data->map);
Tom Zanussi0212e2a2018-01-15 20:51:57 -06005051
5052 destroy_actions(hist_data);
Tom Zanussi02205a62018-01-15 20:51:59 -06005053 destroy_field_vars(hist_data);
5054 destroy_field_var_hists(hist_data);
Tom Zanussi0212e2a2018-01-15 20:51:57 -06005055
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005056 kfree(hist_data);
5057}
5058
5059static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
5060{
5061 struct tracing_map *map = hist_data->map;
5062 struct ftrace_event_field *field;
5063 struct hist_field *hist_field;
Dan Carpenterb28d7b22018-03-28 14:48:15 +03005064 int i, idx = 0;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005065
5066 for_each_hist_field(i, hist_data) {
5067 hist_field = hist_data->fields[i];
5068 if (hist_field->flags & HIST_FIELD_FL_KEY) {
5069 tracing_map_cmp_fn_t cmp_fn;
5070
5071 field = hist_field->field;
5072
Tom Zanussi69a02002016-03-03 12:54:52 -06005073 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
5074 cmp_fn = tracing_map_cmp_none;
Tom Zanussiad42feb2018-01-15 20:51:45 -06005075 else if (!field)
5076 cmp_fn = tracing_map_cmp_num(hist_field->size,
5077 hist_field->is_signed);
Tom Zanussi69a02002016-03-03 12:54:52 -06005078 else if (is_string_field(field))
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005079 cmp_fn = tracing_map_cmp_string;
5080 else
5081 cmp_fn = tracing_map_cmp_num(field->size,
5082 field->is_signed);
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06005083 idx = tracing_map_add_key_field(map,
5084 hist_field->offset,
5085 cmp_fn);
Tom Zanussi30350d62018-01-15 20:51:49 -06005086 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005087 idx = tracing_map_add_sum_field(map);
5088
5089 if (idx < 0)
5090 return idx;
Tom Zanussi30350d62018-01-15 20:51:49 -06005091
5092 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5093 idx = tracing_map_add_var(map);
5094 if (idx < 0)
5095 return idx;
5096 hist_field->var.idx = idx;
5097 hist_field->var.hist_data = hist_data;
5098 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005099 }
5100
5101 return 0;
5102}
5103
5104static struct hist_trigger_data *
5105create_hist_data(unsigned int map_bits,
5106 struct hist_trigger_attrs *attrs,
Tom Zanussi30350d62018-01-15 20:51:49 -06005107 struct trace_event_file *file,
5108 bool remove)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005109{
Tom Zanussi6b4827a2016-03-03 12:54:50 -06005110 const struct tracing_map_ops *map_ops = NULL;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005111 struct hist_trigger_data *hist_data;
5112 int ret = 0;
5113
5114 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
5115 if (!hist_data)
5116 return ERR_PTR(-ENOMEM);
5117
5118 hist_data->attrs = attrs;
Tom Zanussi30350d62018-01-15 20:51:49 -06005119 hist_data->remove = remove;
Tom Zanussi067fe032018-01-15 20:51:56 -06005120 hist_data->event_file = file;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005121
Tom Zanussi0212e2a2018-01-15 20:51:57 -06005122 ret = parse_actions(hist_data);
5123 if (ret)
5124 goto free;
5125
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005126 ret = create_hist_fields(hist_data, file);
5127 if (ret)
5128 goto free;
5129
5130 ret = create_sort_keys(hist_data);
5131 if (ret)
5132 goto free;
5133
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06005134 map_ops = &hist_trigger_elt_data_ops;
Tom Zanussi6b4827a2016-03-03 12:54:50 -06005135
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005136 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
Tom Zanussi6b4827a2016-03-03 12:54:50 -06005137 map_ops, hist_data);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005138 if (IS_ERR(hist_data->map)) {
5139 ret = PTR_ERR(hist_data->map);
5140 hist_data->map = NULL;
5141 goto free;
5142 }
5143
5144 ret = create_tracing_map_fields(hist_data);
5145 if (ret)
5146 goto free;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005147 out:
5148 return hist_data;
5149 free:
5150 hist_data->attrs = NULL;
5151
5152 destroy_hist_data(hist_data);
5153
5154 hist_data = ERR_PTR(ret);
5155
5156 goto out;
5157}
5158
5159static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
Tom Zanussifbd302c2018-01-15 20:51:43 -06005160 struct tracing_map_elt *elt, void *rec,
Tom Zanussi067fe032018-01-15 20:51:56 -06005161 struct ring_buffer_event *rbe,
5162 u64 *var_ref_vals)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005163{
Tom Zanussi067fe032018-01-15 20:51:56 -06005164 struct hist_elt_data *elt_data;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005165 struct hist_field *hist_field;
Tom Zanussi30350d62018-01-15 20:51:49 -06005166 unsigned int i, var_idx;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005167 u64 hist_val;
5168
Tom Zanussi067fe032018-01-15 20:51:56 -06005169 elt_data = elt->private_data;
5170 elt_data->var_ref_vals = var_ref_vals;
5171
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005172 for_each_hist_val_field(i, hist_data) {
5173 hist_field = hist_data->fields[i];
Tom Zanussidf35d932018-01-15 20:51:54 -06005174 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
Tom Zanussi30350d62018-01-15 20:51:49 -06005175 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5176 var_idx = hist_field->var.idx;
5177 tracing_map_set_var(elt, var_idx, hist_val);
5178 continue;
5179 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005180 tracing_map_update_sum(elt, i, hist_val);
5181 }
Tom Zanussi30350d62018-01-15 20:51:49 -06005182
5183 for_each_hist_key_field(i, hist_data) {
5184 hist_field = hist_data->fields[i];
5185 if (hist_field->flags & HIST_FIELD_FL_VAR) {
Tom Zanussidf35d932018-01-15 20:51:54 -06005186 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
Tom Zanussi30350d62018-01-15 20:51:49 -06005187 var_idx = hist_field->var.idx;
5188 tracing_map_set_var(elt, var_idx, hist_val);
5189 }
5190 }
Tom Zanussi02205a62018-01-15 20:51:59 -06005191
5192 update_field_vars(hist_data, elt, rbe, rec);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005193}
5194
Tom Zanussi6a475cb2016-03-03 12:54:54 -06005195static inline void add_to_key(char *compound_key, void *key,
5196 struct hist_field *key_field, void *rec)
5197{
5198 size_t size = key_field->size;
5199
5200 if (key_field->flags & HIST_FIELD_FL_STRING) {
5201 struct ftrace_event_field *field;
5202
5203 field = key_field->field;
5204 if (field->filter_type == FILTER_DYN_STRING)
5205 size = *(u32 *)(rec + field->offset) >> 16;
5206 else if (field->filter_type == FILTER_PTR_STRING)
5207 size = strlen(key);
5208 else if (field->filter_type == FILTER_STATIC_STRING)
5209 size = field->size;
5210
5211 /* ensure NULL-termination */
5212 if (size > key_field->size - 1)
5213 size = key_field->size - 1;
Tom Zanussi6a475cb2016-03-03 12:54:54 -06005214
Tom Zanussi9f0bbf32019-02-04 15:07:24 -06005215 strncpy(compound_key + key_field->offset, (char *)key, size);
5216 } else
5217 memcpy(compound_key + key_field->offset, key, size);
Tom Zanussi6a475cb2016-03-03 12:54:54 -06005218}
5219
Tom Zanussi0212e2a2018-01-15 20:51:57 -06005220static void
5221hist_trigger_actions(struct hist_trigger_data *hist_data,
5222 struct tracing_map_elt *elt, void *rec,
Tom Zanussi7d18a102019-02-13 17:42:41 -06005223 struct ring_buffer_event *rbe, void *key,
5224 u64 *var_ref_vals)
Tom Zanussi0212e2a2018-01-15 20:51:57 -06005225{
5226 struct action_data *data;
5227 unsigned int i;
5228
5229 for (i = 0; i < hist_data->n_actions; i++) {
5230 data = hist_data->actions[i];
Tom Zanussi7d18a102019-02-13 17:42:41 -06005231 data->fn(hist_data, elt, rec, rbe, key, data, var_ref_vals);
Tom Zanussi0212e2a2018-01-15 20:51:57 -06005232 }
5233}
5234
Tom Zanussi1ac4f512018-01-15 20:51:42 -06005235static void event_hist_trigger(struct event_trigger_data *data, void *rec,
Tom Zanussifbd302c2018-01-15 20:51:43 -06005236 struct ring_buffer_event *rbe)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005237{
5238 struct hist_trigger_data *hist_data = data->private_data;
Tom Zanussi6a475cb2016-03-03 12:54:54 -06005239 bool use_compound_key = (hist_data->n_keys > 1);
Tom Zanussi69a02002016-03-03 12:54:52 -06005240 unsigned long entries[HIST_STACKTRACE_DEPTH];
Tom Zanussi067fe032018-01-15 20:51:56 -06005241 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06005242 char compound_key[HIST_KEY_SIZE_MAX];
Tom Zanussidf35d932018-01-15 20:51:54 -06005243 struct tracing_map_elt *elt = NULL;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005244 struct hist_field *key_field;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005245 u64 field_contents;
5246 void *key = NULL;
5247 unsigned int i;
5248
Tom Zanussi6a475cb2016-03-03 12:54:54 -06005249 memset(compound_key, 0, hist_data->key_size);
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06005250
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005251 for_each_hist_key_field(i, hist_data) {
5252 key_field = hist_data->fields[i];
5253
Tom Zanussi69a02002016-03-03 12:54:52 -06005254 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
Thomas Gleixnere7d91662019-04-25 11:45:13 +02005255 memset(entries, 0, HIST_STACKTRACE_SIZE);
5256 stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
5257 HIST_STACKTRACE_SKIP);
Tom Zanussi69a02002016-03-03 12:54:52 -06005258 key = entries;
5259 } else {
Tom Zanussidf35d932018-01-15 20:51:54 -06005260 field_contents = key_field->fn(key_field, elt, rbe, rec);
Tom Zanussi6a475cb2016-03-03 12:54:54 -06005261 if (key_field->flags & HIST_FIELD_FL_STRING) {
Tom Zanussi69a02002016-03-03 12:54:52 -06005262 key = (void *)(unsigned long)field_contents;
Tom Zanussi6a475cb2016-03-03 12:54:54 -06005263 use_compound_key = true;
5264 } else
Tom Zanussi69a02002016-03-03 12:54:52 -06005265 key = (void *)&field_contents;
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06005266 }
Tom Zanussi6a475cb2016-03-03 12:54:54 -06005267
5268 if (use_compound_key)
5269 add_to_key(compound_key, key, key_field, rec);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005270 }
5271
Tom Zanussi6a475cb2016-03-03 12:54:54 -06005272 if (use_compound_key)
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06005273 key = compound_key;
5274
Tom Zanussi067fe032018-01-15 20:51:56 -06005275 if (hist_data->n_var_refs &&
5276 !resolve_var_refs(hist_data, key, var_ref_vals, false))
5277 return;
5278
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005279 elt = tracing_map_insert(hist_data->map, key);
Tom Zanussi067fe032018-01-15 20:51:56 -06005280 if (!elt)
5281 return;
5282
5283 hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals);
Tom Zanussi0212e2a2018-01-15 20:51:57 -06005284
5285 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
Tom Zanussi7d18a102019-02-13 17:42:41 -06005286 hist_trigger_actions(hist_data, elt, rec, rbe, key, var_ref_vals);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005287}
5288
Tom Zanussi69a02002016-03-03 12:54:52 -06005289static void hist_trigger_stacktrace_print(struct seq_file *m,
5290 unsigned long *stacktrace_entries,
5291 unsigned int max_entries)
5292{
5293 char str[KSYM_SYMBOL_LEN];
5294 unsigned int spaces = 8;
5295 unsigned int i;
5296
5297 for (i = 0; i < max_entries; i++) {
Thomas Gleixner4285f2f2019-04-10 12:28:10 +02005298 if (!stacktrace_entries[i])
Tom Zanussi69a02002016-03-03 12:54:52 -06005299 return;
5300
5301 seq_printf(m, "%*c", 1 + spaces, ' ');
5302 sprint_symbol(str, stacktrace_entries[i]);
5303 seq_printf(m, "%s\n", str);
5304 }
5305}
5306
Tom Zanussia3785b72019-02-13 17:42:46 -06005307static void hist_trigger_print_key(struct seq_file *m,
5308 struct hist_trigger_data *hist_data,
5309 void *key,
5310 struct tracing_map_elt *elt)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005311{
5312 struct hist_field *key_field;
Tom Zanussic6afad42016-03-03 12:54:49 -06005313 char str[KSYM_SYMBOL_LEN];
Tom Zanussi69a02002016-03-03 12:54:52 -06005314 bool multiline = false;
Tom Zanussi85013252017-09-22 14:58:22 -05005315 const char *field_name;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005316 unsigned int i;
5317 u64 uval;
5318
5319 seq_puts(m, "{ ");
5320
5321 for_each_hist_key_field(i, hist_data) {
5322 key_field = hist_data->fields[i];
5323
5324 if (i > hist_data->n_vals)
5325 seq_puts(m, ", ");
5326
Tom Zanussi85013252017-09-22 14:58:22 -05005327 field_name = hist_field_name(key_field, 0);
5328
Tom Zanussi0c4a6b42016-03-03 12:54:48 -06005329 if (key_field->flags & HIST_FIELD_FL_HEX) {
5330 uval = *(u64 *)(key + key_field->offset);
Tom Zanussi85013252017-09-22 14:58:22 -05005331 seq_printf(m, "%s: %llx", field_name, uval);
Tom Zanussic6afad42016-03-03 12:54:49 -06005332 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
5333 uval = *(u64 *)(key + key_field->offset);
5334 sprint_symbol_no_offset(str, uval);
Tom Zanussi85013252017-09-22 14:58:22 -05005335 seq_printf(m, "%s: [%llx] %-45s", field_name,
5336 uval, str);
Tom Zanussic6afad42016-03-03 12:54:49 -06005337 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
5338 uval = *(u64 *)(key + key_field->offset);
5339 sprint_symbol(str, uval);
Tom Zanussi85013252017-09-22 14:58:22 -05005340 seq_printf(m, "%s: [%llx] %-55s", field_name,
5341 uval, str);
Tom Zanussi6b4827a2016-03-03 12:54:50 -06005342 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06005343 struct hist_elt_data *elt_data = elt->private_data;
5344 char *comm;
5345
5346 if (WARN_ON_ONCE(!elt_data))
5347 return;
5348
5349 comm = elt_data->comm;
Tom Zanussi6b4827a2016-03-03 12:54:50 -06005350
5351 uval = *(u64 *)(key + key_field->offset);
Tom Zanussi85013252017-09-22 14:58:22 -05005352 seq_printf(m, "%s: %-16s[%10llu]", field_name,
5353 comm, uval);
Tom Zanussi31696192016-03-03 12:54:51 -06005354 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
5355 const char *syscall_name;
5356
5357 uval = *(u64 *)(key + key_field->offset);
5358 syscall_name = get_syscall_name(uval);
5359 if (!syscall_name)
5360 syscall_name = "unknown_syscall";
5361
Tom Zanussi85013252017-09-22 14:58:22 -05005362 seq_printf(m, "%s: %-30s[%3llu]", field_name,
5363 syscall_name, uval);
Tom Zanussi69a02002016-03-03 12:54:52 -06005364 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5365 seq_puts(m, "stacktrace:\n");
5366 hist_trigger_stacktrace_print(m,
5367 key + key_field->offset,
5368 HIST_STACKTRACE_DEPTH);
5369 multiline = true;
Namhyung Kim4b94f5b2016-03-03 12:55:02 -06005370 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
Tom Zanussi85013252017-09-22 14:58:22 -05005371 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
Namhyung Kim4b94f5b2016-03-03 12:55:02 -06005372 *(u64 *)(key + key_field->offset));
Tom Zanussi0c4a6b42016-03-03 12:54:48 -06005373 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
Tom Zanussi85013252017-09-22 14:58:22 -05005374 seq_printf(m, "%s: %-50s", field_name,
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06005375 (char *)(key + key_field->offset));
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005376 } else {
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06005377 uval = *(u64 *)(key + key_field->offset);
Tom Zanussi85013252017-09-22 14:58:22 -05005378 seq_printf(m, "%s: %10llu", field_name, uval);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005379 }
5380 }
5381
Tom Zanussi69a02002016-03-03 12:54:52 -06005382 if (!multiline)
5383 seq_puts(m, " ");
5384
5385 seq_puts(m, "}");
Tom Zanussia3785b72019-02-13 17:42:46 -06005386}
5387
5388static void hist_trigger_entry_print(struct seq_file *m,
5389 struct hist_trigger_data *hist_data,
5390 void *key,
5391 struct tracing_map_elt *elt)
5392{
5393 const char *field_name;
5394 unsigned int i;
5395
5396 hist_trigger_print_key(m, hist_data, key, elt);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005397
5398 seq_printf(m, " hitcount: %10llu",
5399 tracing_map_read_sum(elt, HITCOUNT_IDX));
5400
Tom Zanussif2606832016-03-03 12:54:43 -06005401 for (i = 1; i < hist_data->n_vals; i++) {
Tom Zanussi85013252017-09-22 14:58:22 -05005402 field_name = hist_field_name(hist_data->fields[i], 0);
5403
Tom Zanussi100719d2018-01-15 20:51:52 -06005404 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
5405 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
Tom Zanussi30350d62018-01-15 20:51:49 -06005406 continue;
5407
Tom Zanussi0c4a6b42016-03-03 12:54:48 -06005408 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
Tom Zanussi85013252017-09-22 14:58:22 -05005409 seq_printf(m, " %s: %10llx", field_name,
Tom Zanussi0c4a6b42016-03-03 12:54:48 -06005410 tracing_map_read_sum(elt, i));
5411 } else {
Tom Zanussi85013252017-09-22 14:58:22 -05005412 seq_printf(m, " %s: %10llu", field_name,
Tom Zanussi0c4a6b42016-03-03 12:54:48 -06005413 tracing_map_read_sum(elt, i));
5414 }
Tom Zanussif2606832016-03-03 12:54:43 -06005415 }
5416
Tom Zanussi50450602018-01-15 20:52:01 -06005417 print_actions(m, hist_data, elt);
5418
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005419 seq_puts(m, "\n");
5420}
5421
5422static int print_entries(struct seq_file *m,
5423 struct hist_trigger_data *hist_data)
5424{
5425 struct tracing_map_sort_entry **sort_entries = NULL;
5426 struct tracing_map *map = hist_data->map;
Steven Rostedt (Red Hat)d50c7442016-03-08 17:17:15 -05005427 int i, n_entries;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005428
5429 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
5430 hist_data->n_sort_keys,
5431 &sort_entries);
5432 if (n_entries < 0)
5433 return n_entries;
5434
5435 for (i = 0; i < n_entries; i++)
5436 hist_trigger_entry_print(m, hist_data,
5437 sort_entries[i]->key,
5438 sort_entries[i]->elt);
5439
5440 tracing_map_destroy_sort_entries(sort_entries, n_entries);
5441
5442 return n_entries;
5443}
5444
Tom Zanussi52a7f162016-03-03 12:54:57 -06005445static void hist_trigger_show(struct seq_file *m,
5446 struct event_trigger_data *data, int n)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005447{
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005448 struct hist_trigger_data *hist_data;
Colin Ian King6e7a2392017-08-23 12:23:09 +01005449 int n_entries;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005450
Tom Zanussi52a7f162016-03-03 12:54:57 -06005451 if (n > 0)
5452 seq_puts(m, "\n\n");
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005453
5454 seq_puts(m, "# event histogram\n#\n# trigger info: ");
5455 data->ops->print(m, data->ops, data);
Tom Zanussi52a7f162016-03-03 12:54:57 -06005456 seq_puts(m, "#\n\n");
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005457
5458 hist_data = data->private_data;
5459 n_entries = print_entries(m, hist_data);
Colin Ian King6e7a2392017-08-23 12:23:09 +01005460 if (n_entries < 0)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005461 n_entries = 0;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005462
Tom Zanussia3785b72019-02-13 17:42:46 -06005463 track_data_snapshot_print(m, hist_data);
5464
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005465 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
5466 (u64)atomic64_read(&hist_data->map->hits),
5467 n_entries, (u64)atomic64_read(&hist_data->map->drops));
Tom Zanussi52a7f162016-03-03 12:54:57 -06005468}
5469
5470static int hist_show(struct seq_file *m, void *v)
5471{
5472 struct event_trigger_data *data;
5473 struct trace_event_file *event_file;
5474 int n = 0, ret = 0;
5475
5476 mutex_lock(&event_mutex);
5477
5478 event_file = event_file_data(m->private);
5479 if (unlikely(!event_file)) {
5480 ret = -ENODEV;
5481 goto out_unlock;
5482 }
5483
5484 list_for_each_entry_rcu(data, &event_file->triggers, list) {
5485 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5486 hist_trigger_show(m, data, n++);
5487 }
5488
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005489 out_unlock:
5490 mutex_unlock(&event_mutex);
5491
5492 return ret;
5493}
5494
5495static int event_hist_open(struct inode *inode, struct file *file)
5496{
5497 return single_open(file, hist_show, file);
5498}
5499
5500const struct file_operations event_hist_fops = {
5501 .open = event_hist_open,
5502 .read = seq_read,
5503 .llseek = seq_lseek,
5504 .release = single_release,
5505};
5506
5507static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
5508{
Tom Zanussi85013252017-09-22 14:58:22 -05005509 const char *field_name = hist_field_name(hist_field, 0);
5510
Tom Zanussi30350d62018-01-15 20:51:49 -06005511 if (hist_field->var.name)
5512 seq_printf(m, "%s=", hist_field->var.name);
5513
Tom Zanussi0ae79612018-03-28 15:10:53 -05005514 if (hist_field->flags & HIST_FIELD_FL_CPU)
Tom Zanussi8b7622b2018-01-15 20:52:03 -06005515 seq_puts(m, "cpu");
Tom Zanussi067fe032018-01-15 20:51:56 -06005516 else if (field_name) {
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06005517 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
5518 hist_field->flags & HIST_FIELD_FL_ALIAS)
Tom Zanussi067fe032018-01-15 20:51:56 -06005519 seq_putc(m, '$');
Tom Zanussiad42feb2018-01-15 20:51:45 -06005520 seq_printf(m, "%s", field_name);
Tom Zanussi0ae79612018-03-28 15:10:53 -05005521 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
5522 seq_puts(m, "common_timestamp");
Tom Zanussi608940d2018-04-26 20:04:47 -05005523
5524 if (hist_field->flags) {
5525 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
5526 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
5527 const char *flags = get_hist_field_flags(hist_field);
5528
5529 if (flags)
5530 seq_printf(m, ".%s", flags);
5531 }
5532 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005533}
5534
5535static int event_hist_trigger_print(struct seq_file *m,
5536 struct event_trigger_ops *ops,
5537 struct event_trigger_data *data)
5538{
5539 struct hist_trigger_data *hist_data = data->private_data;
Tom Zanussi30350d62018-01-15 20:51:49 -06005540 struct hist_field *field;
5541 bool have_var = false;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005542 unsigned int i;
5543
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005544 seq_puts(m, "hist:");
5545
5546 if (data->name)
5547 seq_printf(m, "%s:", data->name);
5548
5549 seq_puts(m, "keys=");
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005550
5551 for_each_hist_key_field(i, hist_data) {
Tom Zanussi30350d62018-01-15 20:51:49 -06005552 field = hist_data->fields[i];
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005553
5554 if (i > hist_data->n_vals)
5555 seq_puts(m, ",");
5556
Tom Zanussi30350d62018-01-15 20:51:49 -06005557 if (field->flags & HIST_FIELD_FL_STACKTRACE)
Tom Zanussi69a02002016-03-03 12:54:52 -06005558 seq_puts(m, "stacktrace");
5559 else
Tom Zanussi30350d62018-01-15 20:51:49 -06005560 hist_field_print(m, field);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005561 }
5562
5563 seq_puts(m, ":vals=");
Tom Zanussif2606832016-03-03 12:54:43 -06005564
5565 for_each_hist_val_field(i, hist_data) {
Tom Zanussi30350d62018-01-15 20:51:49 -06005566 field = hist_data->fields[i];
5567 if (field->flags & HIST_FIELD_FL_VAR) {
5568 have_var = true;
5569 continue;
5570 }
5571
Tom Zanussif2606832016-03-03 12:54:43 -06005572 if (i == HITCOUNT_IDX)
5573 seq_puts(m, "hitcount");
5574 else {
5575 seq_puts(m, ",");
Tom Zanussi30350d62018-01-15 20:51:49 -06005576 hist_field_print(m, field);
5577 }
5578 }
5579
5580 if (have_var) {
5581 unsigned int n = 0;
5582
5583 seq_puts(m, ":");
5584
5585 for_each_hist_val_field(i, hist_data) {
5586 field = hist_data->fields[i];
5587
5588 if (field->flags & HIST_FIELD_FL_VAR) {
5589 if (n++)
5590 seq_puts(m, ",");
5591 hist_field_print(m, field);
5592 }
Tom Zanussif2606832016-03-03 12:54:43 -06005593 }
5594 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005595
5596 seq_puts(m, ":sort=");
Tom Zanussie62347d2016-03-03 12:54:45 -06005597
5598 for (i = 0; i < hist_data->n_sort_keys; i++) {
5599 struct tracing_map_sort_key *sort_key;
Tom Zanussi30350d62018-01-15 20:51:49 -06005600 unsigned int idx, first_key_idx;
5601
5602 /* skip VAR vals */
5603 first_key_idx = hist_data->n_vals - hist_data->n_vars;
Tom Zanussie62347d2016-03-03 12:54:45 -06005604
5605 sort_key = &hist_data->sort_keys[i];
Tom Zanussiad42feb2018-01-15 20:51:45 -06005606 idx = sort_key->field_idx;
5607
Tom Zanussi1a361df2018-01-15 20:51:50 -06005608 if (WARN_ON(idx >= HIST_FIELDS_MAX))
Tom Zanussiad42feb2018-01-15 20:51:45 -06005609 return -EINVAL;
Tom Zanussie62347d2016-03-03 12:54:45 -06005610
5611 if (i > 0)
5612 seq_puts(m, ",");
5613
Tom Zanussiad42feb2018-01-15 20:51:45 -06005614 if (idx == HITCOUNT_IDX)
Tom Zanussie62347d2016-03-03 12:54:45 -06005615 seq_puts(m, "hitcount");
Tom Zanussi30350d62018-01-15 20:51:49 -06005616 else {
5617 if (idx >= first_key_idx)
5618 idx += hist_data->n_vars;
Tom Zanussie62347d2016-03-03 12:54:45 -06005619 hist_field_print(m, hist_data->fields[idx]);
Tom Zanussi30350d62018-01-15 20:51:49 -06005620 }
Tom Zanussie62347d2016-03-03 12:54:45 -06005621
5622 if (sort_key->descending)
5623 seq_puts(m, ".descending");
5624 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005625 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
Tom Zanussia4072fe2018-01-15 20:52:08 -06005626 if (hist_data->enable_timestamps)
5627 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005628
Tom Zanussic282a382018-01-15 20:52:00 -06005629 print_actions_spec(m, hist_data);
5630
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005631 if (data->filter_str)
5632 seq_printf(m, " if %s", data->filter_str);
5633
Tom Zanussi83e99912016-03-03 12:54:46 -06005634 if (data->paused)
5635 seq_puts(m, " [paused]");
5636 else
5637 seq_puts(m, " [active]");
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005638
5639 seq_putc(m, '\n');
5640
5641 return 0;
5642}
5643
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005644static int event_hist_trigger_init(struct event_trigger_ops *ops,
5645 struct event_trigger_data *data)
5646{
5647 struct hist_trigger_data *hist_data = data->private_data;
5648
5649 if (!data->ref && hist_data->attrs->name)
5650 save_named_trigger(hist_data->attrs->name, data);
5651
5652 data->ref++;
5653
5654 return 0;
5655}
5656
Tom Zanussi02205a62018-01-15 20:51:59 -06005657static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
5658{
5659 struct trace_event_file *file;
5660 unsigned int i;
5661 char *cmd;
5662 int ret;
5663
5664 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5665 file = hist_data->field_var_hists[i]->hist_data->event_file;
5666 cmd = hist_data->field_var_hists[i]->cmd;
5667 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
5668 "!hist", "hist", cmd);
5669 }
5670}
5671
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005672static void event_hist_trigger_free(struct event_trigger_ops *ops,
5673 struct event_trigger_data *data)
5674{
5675 struct hist_trigger_data *hist_data = data->private_data;
5676
5677 if (WARN_ON_ONCE(data->ref <= 0))
5678 return;
5679
5680 data->ref--;
5681 if (!data->ref) {
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005682 if (data->name)
5683 del_named_trigger(data);
Tom Zanussi067fe032018-01-15 20:51:56 -06005684
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005685 trigger_data_free(data);
Tom Zanussi067fe032018-01-15 20:51:56 -06005686
5687 remove_hist_vars(hist_data);
5688
Tom Zanussi02205a62018-01-15 20:51:59 -06005689 unregister_field_var_hists(hist_data);
5690
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005691 destroy_hist_data(hist_data);
5692 }
5693}
5694
5695static struct event_trigger_ops event_hist_trigger_ops = {
5696 .func = event_hist_trigger,
5697 .print = event_hist_trigger_print,
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005698 .init = event_hist_trigger_init,
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005699 .free = event_hist_trigger_free,
5700};
5701
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005702static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
5703 struct event_trigger_data *data)
5704{
5705 data->ref++;
5706
5707 save_named_trigger(data->named_data->name, data);
5708
5709 event_hist_trigger_init(ops, data->named_data);
5710
5711 return 0;
5712}
5713
5714static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
5715 struct event_trigger_data *data)
5716{
5717 if (WARN_ON_ONCE(data->ref <= 0))
5718 return;
5719
5720 event_hist_trigger_free(ops, data->named_data);
5721
5722 data->ref--;
5723 if (!data->ref) {
5724 del_named_trigger(data);
5725 trigger_data_free(data);
5726 }
5727}
5728
5729static struct event_trigger_ops event_hist_trigger_named_ops = {
5730 .func = event_hist_trigger,
5731 .print = event_hist_trigger_print,
5732 .init = event_hist_trigger_named_init,
5733 .free = event_hist_trigger_named_free,
5734};
5735
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005736static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
5737 char *param)
5738{
5739 return &event_hist_trigger_ops;
5740}
5741
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005742static void hist_clear(struct event_trigger_data *data)
5743{
5744 struct hist_trigger_data *hist_data = data->private_data;
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005745
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005746 if (data->name)
5747 pause_named_trigger(data);
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005748
Steven Rostedt (VMware)e0a568d2018-08-09 15:31:48 -04005749 tracepoint_synchronize_unregister();
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005750
5751 tracing_map_clear(hist_data->map);
5752
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005753 if (data->name)
5754 unpause_named_trigger(data);
5755}
5756
5757static bool compatible_field(struct ftrace_event_field *field,
5758 struct ftrace_event_field *test_field)
5759{
5760 if (field == test_field)
5761 return true;
5762 if (field == NULL || test_field == NULL)
5763 return false;
5764 if (strcmp(field->name, test_field->name) != 0)
5765 return false;
5766 if (strcmp(field->type, test_field->type) != 0)
5767 return false;
5768 if (field->size != test_field->size)
5769 return false;
5770 if (field->is_signed != test_field->is_signed)
5771 return false;
5772
5773 return true;
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005774}
5775
Tom Zanussi52a7f162016-03-03 12:54:57 -06005776static bool hist_trigger_match(struct event_trigger_data *data,
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005777 struct event_trigger_data *data_test,
5778 struct event_trigger_data *named_data,
5779 bool ignore_filter)
Tom Zanussi52a7f162016-03-03 12:54:57 -06005780{
5781 struct tracing_map_sort_key *sort_key, *sort_key_test;
5782 struct hist_trigger_data *hist_data, *hist_data_test;
5783 struct hist_field *key_field, *key_field_test;
5784 unsigned int i;
5785
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005786 if (named_data && (named_data != data_test) &&
5787 (named_data != data_test->named_data))
5788 return false;
5789
5790 if (!named_data && is_named_trigger(data_test))
5791 return false;
5792
Tom Zanussi52a7f162016-03-03 12:54:57 -06005793 hist_data = data->private_data;
5794 hist_data_test = data_test->private_data;
5795
5796 if (hist_data->n_vals != hist_data_test->n_vals ||
5797 hist_data->n_fields != hist_data_test->n_fields ||
5798 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
5799 return false;
5800
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005801 if (!ignore_filter) {
5802 if ((data->filter_str && !data_test->filter_str) ||
5803 (!data->filter_str && data_test->filter_str))
5804 return false;
5805 }
Tom Zanussi52a7f162016-03-03 12:54:57 -06005806
5807 for_each_hist_field(i, hist_data) {
5808 key_field = hist_data->fields[i];
5809 key_field_test = hist_data_test->fields[i];
5810
5811 if (key_field->flags != key_field_test->flags)
5812 return false;
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005813 if (!compatible_field(key_field->field, key_field_test->field))
Tom Zanussi52a7f162016-03-03 12:54:57 -06005814 return false;
5815 if (key_field->offset != key_field_test->offset)
5816 return false;
Tom Zanussiad42feb2018-01-15 20:51:45 -06005817 if (key_field->size != key_field_test->size)
5818 return false;
5819 if (key_field->is_signed != key_field_test->is_signed)
5820 return false;
Tom Zanussi1a361df2018-01-15 20:51:50 -06005821 if (!!key_field->var.name != !!key_field_test->var.name)
5822 return false;
5823 if (key_field->var.name &&
5824 strcmp(key_field->var.name, key_field_test->var.name) != 0)
5825 return false;
Tom Zanussi52a7f162016-03-03 12:54:57 -06005826 }
5827
5828 for (i = 0; i < hist_data->n_sort_keys; i++) {
5829 sort_key = &hist_data->sort_keys[i];
5830 sort_key_test = &hist_data_test->sort_keys[i];
5831
5832 if (sort_key->field_idx != sort_key_test->field_idx ||
5833 sort_key->descending != sort_key_test->descending)
5834 return false;
5835 }
5836
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005837 if (!ignore_filter && data->filter_str &&
Tom Zanussi52a7f162016-03-03 12:54:57 -06005838 (strcmp(data->filter_str, data_test->filter_str) != 0))
5839 return false;
5840
Tom Zanussi48f79472018-03-28 15:10:55 -05005841 if (!actions_match(hist_data, hist_data_test))
5842 return false;
5843
Tom Zanussi52a7f162016-03-03 12:54:57 -06005844 return true;
5845}
5846
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005847static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
5848 struct event_trigger_data *data,
5849 struct trace_event_file *file)
5850{
Tom Zanussi83e99912016-03-03 12:54:46 -06005851 struct hist_trigger_data *hist_data = data->private_data;
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005852 struct event_trigger_data *test, *named_data = NULL;
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04005853 struct trace_array *tr = file->tr;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005854 int ret = 0;
5855
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005856 if (hist_data->attrs->name) {
5857 named_data = find_named_trigger(hist_data->attrs->name);
5858 if (named_data) {
5859 if (!hist_trigger_match(data, named_data, named_data,
5860 true)) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04005861 hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name));
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005862 ret = -EINVAL;
5863 goto out;
5864 }
5865 }
5866 }
5867
5868 if (hist_data->attrs->name && !named_data)
5869 goto new;
5870
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005871 list_for_each_entry_rcu(test, &file->triggers, list) {
5872 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005873 if (!hist_trigger_match(data, test, named_data, false))
Tom Zanussi52a7f162016-03-03 12:54:57 -06005874 continue;
Tom Zanussi83e99912016-03-03 12:54:46 -06005875 if (hist_data->attrs->pause)
5876 test->paused = true;
5877 else if (hist_data->attrs->cont)
5878 test->paused = false;
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005879 else if (hist_data->attrs->clear)
5880 hist_clear(test);
Tom Zanussif404da62018-01-15 20:52:05 -06005881 else {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04005882 hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
Tom Zanussi83e99912016-03-03 12:54:46 -06005883 ret = -EEXIST;
Tom Zanussif404da62018-01-15 20:52:05 -06005884 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005885 goto out;
5886 }
5887 }
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005888 new:
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005889 if (hist_data->attrs->cont || hist_data->attrs->clear) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04005890 hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0);
Tom Zanussi83e99912016-03-03 12:54:46 -06005891 ret = -ENOENT;
5892 goto out;
5893 }
5894
Tom Zanussi7522c032016-06-29 19:56:00 -05005895 if (hist_data->attrs->pause)
5896 data->paused = true;
5897
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005898 if (named_data) {
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005899 data->private_data = named_data->private_data;
5900 set_named_trigger_data(data, named_data);
5901 data->ops = &event_hist_trigger_named_ops;
5902 }
5903
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005904 if (data->ops->init) {
5905 ret = data->ops->init(data->ops, data);
5906 if (ret < 0)
5907 goto out;
5908 }
5909
Tom Zanussia4072fe2018-01-15 20:52:08 -06005910 if (hist_data->enable_timestamps) {
5911 char *clock = hist_data->attrs->clock;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005912
Tom Zanussia4072fe2018-01-15 20:52:08 -06005913 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
5914 if (ret) {
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04005915 hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock));
Tom Zanussia4072fe2018-01-15 20:52:08 -06005916 goto out;
5917 }
5918
Tom Zanussiad42feb2018-01-15 20:51:45 -06005919 tracing_set_time_stamp_abs(file->tr, true);
Tom Zanussia4072fe2018-01-15 20:52:08 -06005920 }
5921
5922 if (named_data)
5923 destroy_hist_data(hist_data);
5924
5925 ret++;
Tom Zanussi067fe032018-01-15 20:51:56 -06005926 out:
5927 return ret;
5928}
5929
5930static int hist_trigger_enable(struct event_trigger_data *data,
5931 struct trace_event_file *file)
5932{
5933 int ret = 0;
5934
5935 list_add_tail_rcu(&data->list, &file->triggers);
5936
5937 update_cond_flag(file);
Tom Zanussiad42feb2018-01-15 20:51:45 -06005938
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005939 if (trace_event_trigger_enable_disable(file, 1) < 0) {
5940 list_del_rcu(&data->list);
5941 update_cond_flag(file);
5942 ret--;
5943 }
Tom Zanussi067fe032018-01-15 20:51:56 -06005944
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005945 return ret;
5946}
5947
Tom Zanussi4b147932018-01-15 20:51:58 -06005948static bool have_hist_trigger_match(struct event_trigger_data *data,
5949 struct trace_event_file *file)
5950{
5951 struct hist_trigger_data *hist_data = data->private_data;
5952 struct event_trigger_data *test, *named_data = NULL;
5953 bool match = false;
5954
5955 if (hist_data->attrs->name)
5956 named_data = find_named_trigger(hist_data->attrs->name);
5957
5958 list_for_each_entry_rcu(test, &file->triggers, list) {
5959 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5960 if (hist_trigger_match(data, test, named_data, false)) {
5961 match = true;
5962 break;
5963 }
5964 }
5965 }
5966
5967 return match;
5968}
5969
Tom Zanussi067fe032018-01-15 20:51:56 -06005970static bool hist_trigger_check_refs(struct event_trigger_data *data,
5971 struct trace_event_file *file)
5972{
5973 struct hist_trigger_data *hist_data = data->private_data;
5974 struct event_trigger_data *test, *named_data = NULL;
5975
5976 if (hist_data->attrs->name)
5977 named_data = find_named_trigger(hist_data->attrs->name);
5978
5979 list_for_each_entry_rcu(test, &file->triggers, list) {
5980 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5981 if (!hist_trigger_match(data, test, named_data, false))
5982 continue;
5983 hist_data = test->private_data;
5984 if (check_var_refs(hist_data))
5985 return true;
5986 break;
5987 }
5988 }
5989
5990 return false;
5991}
5992
Tom Zanussi52a7f162016-03-03 12:54:57 -06005993static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
5994 struct event_trigger_data *data,
5995 struct trace_event_file *file)
5996{
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005997 struct hist_trigger_data *hist_data = data->private_data;
5998 struct event_trigger_data *test, *named_data = NULL;
Tom Zanussi52a7f162016-03-03 12:54:57 -06005999 bool unregistered = false;
6000
Tom Zanussi5463bfd2016-03-03 12:54:59 -06006001 if (hist_data->attrs->name)
6002 named_data = find_named_trigger(hist_data->attrs->name);
6003
Tom Zanussi52a7f162016-03-03 12:54:57 -06006004 list_for_each_entry_rcu(test, &file->triggers, list) {
6005 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
Tom Zanussi5463bfd2016-03-03 12:54:59 -06006006 if (!hist_trigger_match(data, test, named_data, false))
Tom Zanussi52a7f162016-03-03 12:54:57 -06006007 continue;
6008 unregistered = true;
6009 list_del_rcu(&test->list);
6010 trace_event_trigger_enable_disable(file, 0);
6011 update_cond_flag(file);
6012 break;
6013 }
6014 }
6015
6016 if (unregistered && test->ops->free)
6017 test->ops->free(test->ops, test);
Tom Zanussiad42feb2018-01-15 20:51:45 -06006018
6019 if (hist_data->enable_timestamps) {
Tom Zanussi30350d62018-01-15 20:51:49 -06006020 if (!hist_data->remove || unregistered)
Tom Zanussiad42feb2018-01-15 20:51:45 -06006021 tracing_set_time_stamp_abs(file->tr, false);
6022 }
Tom Zanussi52a7f162016-03-03 12:54:57 -06006023}
6024
Tom Zanussi067fe032018-01-15 20:51:56 -06006025static bool hist_file_check_refs(struct trace_event_file *file)
6026{
6027 struct hist_trigger_data *hist_data;
6028 struct event_trigger_data *test;
6029
6030 list_for_each_entry_rcu(test, &file->triggers, list) {
6031 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6032 hist_data = test->private_data;
6033 if (check_var_refs(hist_data))
6034 return true;
6035 }
6036 }
6037
6038 return false;
6039}
6040
Tom Zanussi52a7f162016-03-03 12:54:57 -06006041static void hist_unreg_all(struct trace_event_file *file)
6042{
Steven Rostedt47c18562016-06-29 19:55:59 -05006043 struct event_trigger_data *test, *n;
Tom Zanussiad42feb2018-01-15 20:51:45 -06006044 struct hist_trigger_data *hist_data;
Tom Zanussi4b147932018-01-15 20:51:58 -06006045 struct synth_event *se;
6046 const char *se_name;
Tom Zanussi52a7f162016-03-03 12:54:57 -06006047
Masami Hiramatsu0e2b81f72018-11-05 18:04:01 +09006048 lockdep_assert_held(&event_mutex);
6049
Tom Zanussi067fe032018-01-15 20:51:56 -06006050 if (hist_file_check_refs(file))
6051 return;
6052
Steven Rostedt47c18562016-06-29 19:55:59 -05006053 list_for_each_entry_safe(test, n, &file->triggers, list) {
Tom Zanussi52a7f162016-03-03 12:54:57 -06006054 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
Tom Zanussiad42feb2018-01-15 20:51:45 -06006055 hist_data = test->private_data;
Tom Zanussi52a7f162016-03-03 12:54:57 -06006056 list_del_rcu(&test->list);
6057 trace_event_trigger_enable_disable(file, 0);
Tom Zanussi4b147932018-01-15 20:51:58 -06006058
Tom Zanussi4b147932018-01-15 20:51:58 -06006059 se_name = trace_event_name(file->event_call);
6060 se = find_synth_event(se_name);
6061 if (se)
6062 se->ref--;
Tom Zanussi4b147932018-01-15 20:51:58 -06006063
Tom Zanussi52a7f162016-03-03 12:54:57 -06006064 update_cond_flag(file);
Tom Zanussiad42feb2018-01-15 20:51:45 -06006065 if (hist_data->enable_timestamps)
6066 tracing_set_time_stamp_abs(file->tr, false);
Tom Zanussi52a7f162016-03-03 12:54:57 -06006067 if (test->ops->free)
6068 test->ops->free(test->ops, test);
6069 }
6070 }
6071}
6072
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006073static int event_hist_trigger_func(struct event_command *cmd_ops,
6074 struct trace_event_file *file,
6075 char *glob, char *cmd, char *param)
6076{
6077 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
6078 struct event_trigger_data *trigger_data;
6079 struct hist_trigger_attrs *attrs;
6080 struct event_trigger_ops *trigger_ops;
6081 struct hist_trigger_data *hist_data;
Tom Zanussi4b147932018-01-15 20:51:58 -06006082 struct synth_event *se;
6083 const char *se_name;
Tom Zanussi30350d62018-01-15 20:51:49 -06006084 bool remove = false;
Tom Zanussiec5ce092018-01-15 20:52:02 -06006085 char *trigger, *p;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006086 int ret = 0;
6087
Masami Hiramatsu0e2b81f72018-11-05 18:04:01 +09006088 lockdep_assert_held(&event_mutex);
6089
Tom Zanussif404da62018-01-15 20:52:05 -06006090 if (glob && strlen(glob)) {
Tom Zanussif404da62018-01-15 20:52:05 -06006091 hist_err_clear();
Tom Zanussia1a05bb2019-03-31 18:48:16 -05006092 last_cmd_set(file, param);
Tom Zanussif404da62018-01-15 20:52:05 -06006093 }
6094
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006095 if (!param)
6096 return -EINVAL;
6097
Tom Zanussi30350d62018-01-15 20:51:49 -06006098 if (glob[0] == '!')
6099 remove = true;
6100
Tom Zanussiec5ce092018-01-15 20:52:02 -06006101 /*
6102 * separate the trigger from the filter (k:v [if filter])
6103 * allowing for whitespace in the trigger
6104 */
6105 p = trigger = param;
6106 do {
6107 p = strstr(p, "if");
6108 if (!p)
6109 break;
6110 if (p == param)
6111 return -EINVAL;
6112 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
6113 p++;
6114 continue;
6115 }
Tom Zanussi2f31ed92018-12-18 14:33:21 -06006116 if (p >= param + strlen(param) - (sizeof("if") - 1) - 1)
Tom Zanussiec5ce092018-01-15 20:52:02 -06006117 return -EINVAL;
Tom Zanussi2f31ed92018-12-18 14:33:21 -06006118 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
Tom Zanussiec5ce092018-01-15 20:52:02 -06006119 p++;
6120 continue;
6121 }
6122 break;
6123 } while (p);
6124
6125 if (!p)
6126 param = NULL;
6127 else {
6128 *(p - 1) = '\0';
6129 param = strstrip(p);
6130 trigger = strstrip(trigger);
6131 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006132
Steven Rostedt (VMware)d0cd8712019-04-01 22:30:22 -04006133 attrs = parse_hist_trigger_attrs(file->tr, trigger);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006134 if (IS_ERR(attrs))
6135 return PTR_ERR(attrs);
6136
6137 if (attrs->map_bits)
6138 hist_trigger_bits = attrs->map_bits;
6139
Tom Zanussi30350d62018-01-15 20:51:49 -06006140 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006141 if (IS_ERR(hist_data)) {
6142 destroy_hist_trigger_attrs(attrs);
6143 return PTR_ERR(hist_data);
6144 }
6145
6146 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
6147
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006148 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
Tom Zanussi4b147932018-01-15 20:51:58 -06006149 if (!trigger_data) {
6150 ret = -ENOMEM;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006151 goto out_free;
Tom Zanussi4b147932018-01-15 20:51:58 -06006152 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006153
6154 trigger_data->count = -1;
6155 trigger_data->ops = trigger_ops;
6156 trigger_data->cmd_ops = cmd_ops;
6157
6158 INIT_LIST_HEAD(&trigger_data->list);
6159 RCU_INIT_POINTER(trigger_data->filter, NULL);
6160
6161 trigger_data->private_data = hist_data;
6162
Tom Zanussi52a7f162016-03-03 12:54:57 -06006163 /* if param is non-empty, it's supposed to be a filter */
6164 if (param && cmd_ops->set_filter) {
6165 ret = cmd_ops->set_filter(param, trigger_data, file);
6166 if (ret < 0)
6167 goto out_free;
6168 }
6169
Tom Zanussi30350d62018-01-15 20:51:49 -06006170 if (remove) {
Tom Zanussi4b147932018-01-15 20:51:58 -06006171 if (!have_hist_trigger_match(trigger_data, file))
6172 goto out_free;
6173
Tom Zanussi067fe032018-01-15 20:51:56 -06006174 if (hist_trigger_check_refs(trigger_data, file)) {
6175 ret = -EBUSY;
6176 goto out_free;
6177 }
6178
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006179 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
Tom Zanussi4b147932018-01-15 20:51:58 -06006180 se_name = trace_event_name(file->event_call);
6181 se = find_synth_event(se_name);
6182 if (se)
6183 se->ref--;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006184 ret = 0;
6185 goto out_free;
6186 }
6187
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006188 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
6189 /*
6190 * The above returns on success the # of triggers registered,
6191 * but if it didn't register any it returns zero. Consider no
6192 * triggers registered a failure too.
6193 */
6194 if (!ret) {
Tom Zanussie86ae9b2016-03-03 12:54:47 -06006195 if (!(attrs->pause || attrs->cont || attrs->clear))
Tom Zanussi83e99912016-03-03 12:54:46 -06006196 ret = -ENOENT;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006197 goto out_free;
6198 } else if (ret < 0)
6199 goto out_free;
Tom Zanussi067fe032018-01-15 20:51:56 -06006200
6201 if (get_named_trigger_data(trigger_data))
6202 goto enable;
6203
6204 if (has_hist_vars(hist_data))
6205 save_hist_vars(hist_data);
6206
Tom Zanussi7d18a102019-02-13 17:42:41 -06006207 ret = create_actions(hist_data);
Tom Zanussi0212e2a2018-01-15 20:51:57 -06006208 if (ret)
6209 goto out_unreg;
6210
Tom Zanussi067fe032018-01-15 20:51:56 -06006211 ret = tracing_map_init(hist_data->map);
6212 if (ret)
6213 goto out_unreg;
6214enable:
6215 ret = hist_trigger_enable(trigger_data, file);
6216 if (ret)
6217 goto out_unreg;
6218
Tom Zanussi4b147932018-01-15 20:51:58 -06006219 se_name = trace_event_name(file->event_call);
6220 se = find_synth_event(se_name);
6221 if (se)
6222 se->ref++;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006223 /* Just return zero, not the number of registered triggers */
6224 ret = 0;
6225 out:
Tom Zanussif404da62018-01-15 20:52:05 -06006226 if (ret == 0)
6227 hist_err_clear();
6228
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006229 return ret;
Tom Zanussi067fe032018-01-15 20:51:56 -06006230 out_unreg:
6231 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006232 out_free:
6233 if (cmd_ops->set_filter)
6234 cmd_ops->set_filter(NULL, trigger_data, NULL);
6235
Tom Zanussi067fe032018-01-15 20:51:56 -06006236 remove_hist_vars(hist_data);
6237
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006238 kfree(trigger_data);
6239
6240 destroy_hist_data(hist_data);
6241 goto out;
6242}
6243
6244static struct event_command trigger_hist_cmd = {
6245 .name = "hist",
6246 .trigger_type = ETT_EVENT_HIST,
6247 .flags = EVENT_CMD_FL_NEEDS_REC,
6248 .func = event_hist_trigger_func,
6249 .reg = hist_register_trigger,
Tom Zanussi52a7f162016-03-03 12:54:57 -06006250 .unreg = hist_unregister_trigger,
6251 .unreg_all = hist_unreg_all,
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006252 .get_trigger_ops = event_hist_get_trigger_ops,
6253 .set_filter = set_trigger_filter,
6254};
6255
6256__init int register_trigger_hist_cmd(void)
6257{
6258 int ret;
6259
6260 ret = register_event_command(&trigger_hist_cmd);
6261 WARN_ON(ret < 0);
6262
6263 return ret;
6264}
Tom Zanussid0bad492016-03-03 12:54:55 -06006265
6266static void
Tom Zanussi1ac4f512018-01-15 20:51:42 -06006267hist_enable_trigger(struct event_trigger_data *data, void *rec,
6268 struct ring_buffer_event *event)
Tom Zanussid0bad492016-03-03 12:54:55 -06006269{
6270 struct enable_trigger_data *enable_data = data->private_data;
6271 struct event_trigger_data *test;
6272
6273 list_for_each_entry_rcu(test, &enable_data->file->triggers, list) {
6274 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6275 if (enable_data->enable)
6276 test->paused = false;
6277 else
6278 test->paused = true;
Tom Zanussid0bad492016-03-03 12:54:55 -06006279 }
6280 }
6281}
6282
6283static void
Tom Zanussi1ac4f512018-01-15 20:51:42 -06006284hist_enable_count_trigger(struct event_trigger_data *data, void *rec,
6285 struct ring_buffer_event *event)
Tom Zanussid0bad492016-03-03 12:54:55 -06006286{
6287 if (!data->count)
6288 return;
6289
6290 if (data->count != -1)
6291 (data->count)--;
6292
Tom Zanussi1ac4f512018-01-15 20:51:42 -06006293 hist_enable_trigger(data, rec, event);
Tom Zanussid0bad492016-03-03 12:54:55 -06006294}
6295
6296static struct event_trigger_ops hist_enable_trigger_ops = {
6297 .func = hist_enable_trigger,
6298 .print = event_enable_trigger_print,
6299 .init = event_trigger_init,
6300 .free = event_enable_trigger_free,
6301};
6302
6303static struct event_trigger_ops hist_enable_count_trigger_ops = {
6304 .func = hist_enable_count_trigger,
6305 .print = event_enable_trigger_print,
6306 .init = event_trigger_init,
6307 .free = event_enable_trigger_free,
6308};
6309
6310static struct event_trigger_ops hist_disable_trigger_ops = {
6311 .func = hist_enable_trigger,
6312 .print = event_enable_trigger_print,
6313 .init = event_trigger_init,
6314 .free = event_enable_trigger_free,
6315};
6316
6317static struct event_trigger_ops hist_disable_count_trigger_ops = {
6318 .func = hist_enable_count_trigger,
6319 .print = event_enable_trigger_print,
6320 .init = event_trigger_init,
6321 .free = event_enable_trigger_free,
6322};
6323
6324static struct event_trigger_ops *
6325hist_enable_get_trigger_ops(char *cmd, char *param)
6326{
6327 struct event_trigger_ops *ops;
6328 bool enable;
6329
6330 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
6331
6332 if (enable)
6333 ops = param ? &hist_enable_count_trigger_ops :
6334 &hist_enable_trigger_ops;
6335 else
6336 ops = param ? &hist_disable_count_trigger_ops :
6337 &hist_disable_trigger_ops;
6338
6339 return ops;
6340}
6341
Tom Zanussi52a7f162016-03-03 12:54:57 -06006342static void hist_enable_unreg_all(struct trace_event_file *file)
6343{
Steven Rostedt47c18562016-06-29 19:55:59 -05006344 struct event_trigger_data *test, *n;
Tom Zanussi52a7f162016-03-03 12:54:57 -06006345
Steven Rostedt47c18562016-06-29 19:55:59 -05006346 list_for_each_entry_safe(test, n, &file->triggers, list) {
Tom Zanussi52a7f162016-03-03 12:54:57 -06006347 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
6348 list_del_rcu(&test->list);
6349 update_cond_flag(file);
6350 trace_event_trigger_enable_disable(file, 0);
6351 if (test->ops->free)
6352 test->ops->free(test->ops, test);
6353 }
6354 }
6355}
6356
Tom Zanussid0bad492016-03-03 12:54:55 -06006357static struct event_command trigger_hist_enable_cmd = {
6358 .name = ENABLE_HIST_STR,
6359 .trigger_type = ETT_HIST_ENABLE,
6360 .func = event_enable_trigger_func,
6361 .reg = event_enable_register_trigger,
6362 .unreg = event_enable_unregister_trigger,
Tom Zanussi52a7f162016-03-03 12:54:57 -06006363 .unreg_all = hist_enable_unreg_all,
Tom Zanussid0bad492016-03-03 12:54:55 -06006364 .get_trigger_ops = hist_enable_get_trigger_ops,
6365 .set_filter = set_trigger_filter,
6366};
6367
6368static struct event_command trigger_hist_disable_cmd = {
6369 .name = DISABLE_HIST_STR,
6370 .trigger_type = ETT_HIST_ENABLE,
6371 .func = event_enable_trigger_func,
6372 .reg = event_enable_register_trigger,
6373 .unreg = event_enable_unregister_trigger,
Tom Zanussi52a7f162016-03-03 12:54:57 -06006374 .unreg_all = hist_enable_unreg_all,
Tom Zanussid0bad492016-03-03 12:54:55 -06006375 .get_trigger_ops = hist_enable_get_trigger_ops,
6376 .set_filter = set_trigger_filter,
6377};
6378
6379static __init void unregister_trigger_hist_enable_disable_cmds(void)
6380{
6381 unregister_event_command(&trigger_hist_enable_cmd);
6382 unregister_event_command(&trigger_hist_disable_cmd);
6383}
6384
6385__init int register_trigger_hist_enable_disable_cmds(void)
6386{
6387 int ret;
6388
6389 ret = register_event_command(&trigger_hist_enable_cmd);
6390 if (WARN_ON(ret < 0))
6391 return ret;
6392 ret = register_event_command(&trigger_hist_disable_cmd);
6393 if (WARN_ON(ret < 0))
6394 unregister_trigger_hist_enable_disable_cmds();
6395
6396 return ret;
6397}
Tom Zanussi4b147932018-01-15 20:51:58 -06006398
6399static __init int trace_events_hist_init(void)
6400{
6401 struct dentry *entry = NULL;
6402 struct dentry *d_tracer;
6403 int err = 0;
6404
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09006405 err = dyn_event_register(&synth_event_ops);
6406 if (err) {
6407 pr_warn("Could not register synth_event_ops\n");
6408 return err;
6409 }
6410
Tom Zanussi4b147932018-01-15 20:51:58 -06006411 d_tracer = tracing_init_dentry();
6412 if (IS_ERR(d_tracer)) {
6413 err = PTR_ERR(d_tracer);
6414 goto err;
6415 }
6416
6417 entry = tracefs_create_file("synthetic_events", 0644, d_tracer,
6418 NULL, &synth_events_fops);
6419 if (!entry) {
6420 err = -ENODEV;
6421 goto err;
6422 }
6423
6424 return err;
6425 err:
6426 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
6427
6428 return err;
6429}
6430
6431fs_initcall(trace_events_hist_init);