blob: c7774fa119a7c42f916cf3ceca5f066924cde978 [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 Zanussi7ef224d2016-03-03 12:54:42 -060025struct hist_field;
26
Tom Zanussidf35d932018-01-15 20:51:54 -060027typedef u64 (*hist_field_fn_t) (struct hist_field *field,
28 struct tracing_map_elt *elt,
29 struct ring_buffer_event *rbe,
30 void *event);
Tom Zanussi7ef224d2016-03-03 12:54:42 -060031
Tom Zanussi5819ead2017-09-22 14:58:23 -050032#define HIST_FIELD_OPERANDS_MAX 2
Tom Zanussi30350d62018-01-15 20:51:49 -060033#define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
Tom Zanussi0212e2a2018-01-15 20:51:57 -060034#define HIST_ACTIONS_MAX 8
Tom Zanussi30350d62018-01-15 20:51:49 -060035
Tom Zanussi100719d2018-01-15 20:51:52 -060036enum field_op_id {
37 FIELD_OP_NONE,
38 FIELD_OP_PLUS,
39 FIELD_OP_MINUS,
40 FIELD_OP_UNARY_MINUS,
41};
42
Tom Zanussi05ddb252018-12-18 14:33:26 -060043/*
44 * A hist_var (histogram variable) contains variable information for
45 * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF
46 * flag set. A hist_var has a variable name e.g. ts0, and is
47 * associated with a given histogram trigger, as specified by
48 * hist_data. The hist_var idx is the unique index assigned to the
49 * variable by the hist trigger's tracing_map. The idx is what is
50 * used to set a variable's value and, by a variable reference, to
51 * retrieve it.
52 */
Tom Zanussi30350d62018-01-15 20:51:49 -060053struct hist_var {
54 char *name;
55 struct hist_trigger_data *hist_data;
56 unsigned int idx;
57};
Tom Zanussi5819ead2017-09-22 14:58:23 -050058
Tom Zanussi7ef224d2016-03-03 12:54:42 -060059struct hist_field {
60 struct ftrace_event_field *field;
61 unsigned long flags;
62 hist_field_fn_t fn;
63 unsigned int size;
Tom Zanussi76a3b0c2016-03-03 12:54:44 -060064 unsigned int offset;
Tom Zanussi5819ead2017-09-22 14:58:23 -050065 unsigned int is_signed;
Tom Zanussi19a9fac2018-01-15 20:51:55 -060066 const char *type;
Tom Zanussi5819ead2017-09-22 14:58:23 -050067 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
Tom Zanussib559d002018-01-15 20:51:47 -060068 struct hist_trigger_data *hist_data;
Tom Zanussi05ddb252018-12-18 14:33:26 -060069
70 /*
71 * Variable fields contain variable-specific info in var.
72 */
Tom Zanussi30350d62018-01-15 20:51:49 -060073 struct hist_var var;
Tom Zanussi100719d2018-01-15 20:51:52 -060074 enum field_op_id operator;
Tom Zanussi067fe032018-01-15 20:51:56 -060075 char *system;
76 char *event_name;
Tom Zanussi05ddb252018-12-18 14:33:26 -060077
78 /*
79 * The name field is used for EXPR and VAR_REF fields. VAR
80 * fields contain the variable name in var.name.
81 */
Tom Zanussi100719d2018-01-15 20:51:52 -060082 char *name;
Tom Zanussi05ddb252018-12-18 14:33:26 -060083
84 /*
85 * When a histogram trigger is hit, if it has any references
86 * to variables, the values of those variables are collected
87 * into a var_ref_vals array by resolve_var_refs(). The
88 * current value of each variable is read from the tracing_map
89 * using the hist field's hist_var.idx and entered into the
90 * var_ref_idx entry i.e. var_ref_vals[var_ref_idx].
91 */
Tom Zanussi067fe032018-01-15 20:51:56 -060092 unsigned int var_ref_idx;
93 bool read_once;
Tom Zanussi7ef224d2016-03-03 12:54:42 -060094};
95
Tom Zanussidf35d932018-01-15 20:51:54 -060096static u64 hist_field_none(struct hist_field *field,
97 struct tracing_map_elt *elt,
98 struct ring_buffer_event *rbe,
99 void *event)
Tom Zanussi69a02002016-03-03 12:54:52 -0600100{
101 return 0;
102}
103
Tom Zanussidf35d932018-01-15 20:51:54 -0600104static u64 hist_field_counter(struct hist_field *field,
105 struct tracing_map_elt *elt,
106 struct ring_buffer_event *rbe,
107 void *event)
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600108{
109 return 1;
110}
111
Tom Zanussidf35d932018-01-15 20:51:54 -0600112static u64 hist_field_string(struct hist_field *hist_field,
113 struct tracing_map_elt *elt,
114 struct ring_buffer_event *rbe,
115 void *event)
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600116{
117 char *addr = (char *)(event + hist_field->field->offset);
118
119 return (u64)(unsigned long)addr;
120}
121
Tom Zanussidf35d932018-01-15 20:51:54 -0600122static u64 hist_field_dynstring(struct hist_field *hist_field,
123 struct tracing_map_elt *elt,
124 struct ring_buffer_event *rbe,
125 void *event)
Namhyung Kim79e577c2016-03-03 12:54:53 -0600126{
127 u32 str_item = *(u32 *)(event + hist_field->field->offset);
128 int str_loc = str_item & 0xffff;
129 char *addr = (char *)(event + str_loc);
130
131 return (u64)(unsigned long)addr;
132}
133
Tom Zanussidf35d932018-01-15 20:51:54 -0600134static u64 hist_field_pstring(struct hist_field *hist_field,
135 struct tracing_map_elt *elt,
136 struct ring_buffer_event *rbe,
137 void *event)
Namhyung Kim79e577c2016-03-03 12:54:53 -0600138{
139 char **addr = (char **)(event + hist_field->field->offset);
140
141 return (u64)(unsigned long)*addr;
142}
143
Tom Zanussidf35d932018-01-15 20:51:54 -0600144static u64 hist_field_log2(struct hist_field *hist_field,
145 struct tracing_map_elt *elt,
146 struct ring_buffer_event *rbe,
147 void *event)
Namhyung Kim4b94f5b2016-03-03 12:55:02 -0600148{
Tom Zanussi5819ead2017-09-22 14:58:23 -0500149 struct hist_field *operand = hist_field->operands[0];
150
Tom Zanussidf35d932018-01-15 20:51:54 -0600151 u64 val = operand->fn(operand, elt, rbe, event);
Namhyung Kim4b94f5b2016-03-03 12:55:02 -0600152
153 return (u64) ilog2(roundup_pow_of_two(val));
154}
155
Tom Zanussidf35d932018-01-15 20:51:54 -0600156static u64 hist_field_plus(struct hist_field *hist_field,
157 struct tracing_map_elt *elt,
158 struct ring_buffer_event *rbe,
159 void *event)
Tom Zanussi100719d2018-01-15 20:51:52 -0600160{
161 struct hist_field *operand1 = hist_field->operands[0];
162 struct hist_field *operand2 = hist_field->operands[1];
163
Tom Zanussidf35d932018-01-15 20:51:54 -0600164 u64 val1 = operand1->fn(operand1, elt, rbe, event);
165 u64 val2 = operand2->fn(operand2, elt, rbe, event);
Tom Zanussi100719d2018-01-15 20:51:52 -0600166
167 return val1 + val2;
168}
169
Tom Zanussidf35d932018-01-15 20:51:54 -0600170static u64 hist_field_minus(struct hist_field *hist_field,
171 struct tracing_map_elt *elt,
172 struct ring_buffer_event *rbe,
173 void *event)
Tom Zanussi100719d2018-01-15 20:51:52 -0600174{
175 struct hist_field *operand1 = hist_field->operands[0];
176 struct hist_field *operand2 = hist_field->operands[1];
177
Tom Zanussidf35d932018-01-15 20:51:54 -0600178 u64 val1 = operand1->fn(operand1, elt, rbe, event);
179 u64 val2 = operand2->fn(operand2, elt, rbe, event);
Tom Zanussi100719d2018-01-15 20:51:52 -0600180
181 return val1 - val2;
182}
183
Tom Zanussidf35d932018-01-15 20:51:54 -0600184static u64 hist_field_unary_minus(struct hist_field *hist_field,
185 struct tracing_map_elt *elt,
186 struct ring_buffer_event *rbe,
187 void *event)
Tom Zanussi100719d2018-01-15 20:51:52 -0600188{
189 struct hist_field *operand = hist_field->operands[0];
190
Tom Zanussidf35d932018-01-15 20:51:54 -0600191 s64 sval = (s64)operand->fn(operand, elt, rbe, event);
Tom Zanussi100719d2018-01-15 20:51:52 -0600192 u64 val = (u64)-sval;
193
194 return val;
195}
196
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600197#define DEFINE_HIST_FIELD_FN(type) \
Tom Zanussifbd302c2018-01-15 20:51:43 -0600198 static u64 hist_field_##type(struct hist_field *hist_field, \
Tom Zanussidf35d932018-01-15 20:51:54 -0600199 struct tracing_map_elt *elt, \
200 struct ring_buffer_event *rbe, \
201 void *event) \
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600202{ \
203 type *addr = (type *)(event + hist_field->field->offset); \
204 \
Namhyung Kim79e577c2016-03-03 12:54:53 -0600205 return (u64)(unsigned long)*addr; \
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600206}
207
208DEFINE_HIST_FIELD_FN(s64);
209DEFINE_HIST_FIELD_FN(u64);
210DEFINE_HIST_FIELD_FN(s32);
211DEFINE_HIST_FIELD_FN(u32);
212DEFINE_HIST_FIELD_FN(s16);
213DEFINE_HIST_FIELD_FN(u16);
214DEFINE_HIST_FIELD_FN(s8);
215DEFINE_HIST_FIELD_FN(u8);
216
217#define for_each_hist_field(i, hist_data) \
218 for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
219
220#define for_each_hist_val_field(i, hist_data) \
221 for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
222
223#define for_each_hist_key_field(i, hist_data) \
224 for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
225
Tom Zanussi69a02002016-03-03 12:54:52 -0600226#define HIST_STACKTRACE_DEPTH 16
227#define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
228#define HIST_STACKTRACE_SKIP 5
229
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600230#define HITCOUNT_IDX 0
Tom Zanussi69a02002016-03-03 12:54:52 -0600231#define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600232
233enum hist_field_flags {
Tom Zanussi0d7a8322017-09-22 14:58:21 -0500234 HIST_FIELD_FL_HITCOUNT = 1 << 0,
235 HIST_FIELD_FL_KEY = 1 << 1,
236 HIST_FIELD_FL_STRING = 1 << 2,
237 HIST_FIELD_FL_HEX = 1 << 3,
238 HIST_FIELD_FL_SYM = 1 << 4,
239 HIST_FIELD_FL_SYM_OFFSET = 1 << 5,
240 HIST_FIELD_FL_EXECNAME = 1 << 6,
241 HIST_FIELD_FL_SYSCALL = 1 << 7,
242 HIST_FIELD_FL_STACKTRACE = 1 << 8,
243 HIST_FIELD_FL_LOG2 = 1 << 9,
Tom Zanussiad42feb2018-01-15 20:51:45 -0600244 HIST_FIELD_FL_TIMESTAMP = 1 << 10,
Tom Zanussi860f9f62018-01-15 20:51:48 -0600245 HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11,
Tom Zanussi30350d62018-01-15 20:51:49 -0600246 HIST_FIELD_FL_VAR = 1 << 12,
Tom Zanussi100719d2018-01-15 20:51:52 -0600247 HIST_FIELD_FL_EXPR = 1 << 13,
Tom Zanussi067fe032018-01-15 20:51:56 -0600248 HIST_FIELD_FL_VAR_REF = 1 << 14,
Tom Zanussi8b7622b2018-01-15 20:52:03 -0600249 HIST_FIELD_FL_CPU = 1 << 15,
Tom Zanussi7e8b88a2018-01-15 20:52:04 -0600250 HIST_FIELD_FL_ALIAS = 1 << 16,
Tom Zanussi30350d62018-01-15 20:51:49 -0600251};
252
253struct var_defs {
254 unsigned int n_vars;
255 char *name[TRACING_MAP_VARS_MAX];
256 char *expr[TRACING_MAP_VARS_MAX];
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600257};
258
259struct hist_trigger_attrs {
260 char *keys_str;
Tom Zanussif2606832016-03-03 12:54:43 -0600261 char *vals_str;
Tom Zanussie62347d2016-03-03 12:54:45 -0600262 char *sort_key_str;
Tom Zanussi5463bfd2016-03-03 12:54:59 -0600263 char *name;
Tom Zanussia4072fe2018-01-15 20:52:08 -0600264 char *clock;
Tom Zanussi83e99912016-03-03 12:54:46 -0600265 bool pause;
266 bool cont;
Tom Zanussie86ae9b2016-03-03 12:54:47 -0600267 bool clear;
Tom Zanussi860f9f62018-01-15 20:51:48 -0600268 bool ts_in_usecs;
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600269 unsigned int map_bits;
Tom Zanussi30350d62018-01-15 20:51:49 -0600270
271 char *assignment_str[TRACING_MAP_VARS_MAX];
272 unsigned int n_assignments;
273
Tom Zanussi0212e2a2018-01-15 20:51:57 -0600274 char *action_str[HIST_ACTIONS_MAX];
275 unsigned int n_actions;
276
Tom Zanussi30350d62018-01-15 20:51:49 -0600277 struct var_defs var_defs;
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600278};
279
Tom Zanussi02205a62018-01-15 20:51:59 -0600280struct field_var {
281 struct hist_field *var;
282 struct hist_field *val;
283};
284
285struct field_var_hist {
286 struct hist_trigger_data *hist_data;
287 char *cmd;
288};
289
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600290struct hist_trigger_data {
Tom Zanussi30350d62018-01-15 20:51:49 -0600291 struct hist_field *fields[HIST_FIELDS_MAX];
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600292 unsigned int n_vals;
293 unsigned int n_keys;
294 unsigned int n_fields;
Tom Zanussi30350d62018-01-15 20:51:49 -0600295 unsigned int n_vars;
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600296 unsigned int key_size;
297 struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX];
298 unsigned int n_sort_keys;
299 struct trace_event_file *event_file;
300 struct hist_trigger_attrs *attrs;
301 struct tracing_map *map;
Tom Zanussiad42feb2018-01-15 20:51:45 -0600302 bool enable_timestamps;
Tom Zanussi30350d62018-01-15 20:51:49 -0600303 bool remove;
Tom Zanussi067fe032018-01-15 20:51:56 -0600304 struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
305 unsigned int n_var_refs;
Tom Zanussi0212e2a2018-01-15 20:51:57 -0600306
307 struct action_data *actions[HIST_ACTIONS_MAX];
308 unsigned int n_actions;
Tom Zanussi02205a62018-01-15 20:51:59 -0600309
310 struct field_var *field_vars[SYNTH_FIELDS_MAX];
311 unsigned int n_field_vars;
312 unsigned int n_field_var_str;
313 struct field_var_hist *field_var_hists[SYNTH_FIELDS_MAX];
314 unsigned int n_field_var_hists;
Tom Zanussi50450602018-01-15 20:52:01 -0600315
Tom Zanussi7d18a102019-02-13 17:42:41 -0600316 struct field_var *save_vars[SYNTH_FIELDS_MAX];
317 unsigned int n_save_vars;
318 unsigned int n_save_var_str;
Tom Zanussi0212e2a2018-01-15 20:51:57 -0600319};
320
Masami Hiramatsu7bbab382018-11-05 18:03:33 +0900321static int synth_event_create(int argc, const char **argv);
322static int synth_event_show(struct seq_file *m, struct dyn_event *ev);
323static int synth_event_release(struct dyn_event *ev);
324static bool synth_event_is_busy(struct dyn_event *ev);
325static bool synth_event_match(const char *system, const char *event,
326 struct dyn_event *ev);
327
328static struct dyn_event_operations synth_event_ops = {
329 .create = synth_event_create,
330 .show = synth_event_show,
331 .is_busy = synth_event_is_busy,
332 .free = synth_event_release,
333 .match = synth_event_match,
334};
335
Tom Zanussi4b147932018-01-15 20:51:58 -0600336struct synth_field {
337 char *type;
338 char *name;
339 size_t size;
340 bool is_signed;
341 bool is_string;
342};
343
344struct synth_event {
Masami Hiramatsu7bbab382018-11-05 18:03:33 +0900345 struct dyn_event devent;
Tom Zanussi4b147932018-01-15 20:51:58 -0600346 int ref;
347 char *name;
348 struct synth_field **fields;
349 unsigned int n_fields;
350 unsigned int n_u64;
351 struct trace_event_class class;
352 struct trace_event_call call;
353 struct tracepoint *tp;
354};
355
Masami Hiramatsu7bbab382018-11-05 18:03:33 +0900356static bool is_synth_event(struct dyn_event *ev)
357{
358 return ev->ops == &synth_event_ops;
359}
360
361static struct synth_event *to_synth_event(struct dyn_event *ev)
362{
363 return container_of(ev, struct synth_event, devent);
364}
365
366static bool synth_event_is_busy(struct dyn_event *ev)
367{
368 struct synth_event *event = to_synth_event(ev);
369
370 return event->ref != 0;
371}
372
373static bool synth_event_match(const char *system, const char *event,
374 struct dyn_event *ev)
375{
376 struct synth_event *sev = to_synth_event(ev);
377
378 return strcmp(sev->name, event) == 0 &&
379 (!system || strcmp(system, SYNTH_SYSTEM) == 0);
380}
381
Tom Zanussi0212e2a2018-01-15 20:51:57 -0600382struct action_data;
383
384typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
385 struct tracing_map_elt *elt, void *rec,
Tom Zanussi7d18a102019-02-13 17:42:41 -0600386 struct ring_buffer_event *rbe, void *key,
Tom Zanussi0212e2a2018-01-15 20:51:57 -0600387 struct action_data *data, u64 *var_ref_vals);
388
Tom Zanussi466f4522019-02-13 17:42:44 -0600389typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val);
390
Tom Zanussi7d18a102019-02-13 17:42:41 -0600391enum handler_id {
392 HANDLER_ONMATCH = 1,
393 HANDLER_ONMAX,
Tom Zanussidff81f52019-02-13 17:42:48 -0600394 HANDLER_ONCHANGE,
Tom Zanussi7d18a102019-02-13 17:42:41 -0600395};
396
397enum action_id {
398 ACTION_SAVE = 1,
399 ACTION_TRACE,
Tom Zanussia3785b72019-02-13 17:42:46 -0600400 ACTION_SNAPSHOT,
Tom Zanussi7d18a102019-02-13 17:42:41 -0600401};
402
Tom Zanussi0212e2a2018-01-15 20:51:57 -0600403struct action_data {
Tom Zanussi7d18a102019-02-13 17:42:41 -0600404 enum handler_id handler;
405 enum action_id action;
406 char *action_name;
Tom Zanussi0212e2a2018-01-15 20:51:57 -0600407 action_fn_t fn;
Tom Zanussi7d18a102019-02-13 17:42:41 -0600408
Tom Zanussic282a382018-01-15 20:52:00 -0600409 unsigned int n_params;
410 char *params[SYNTH_FIELDS_MAX];
411
Tom Zanussic3e49502019-02-13 17:42:43 -0600412 /*
413 * When a histogram trigger is hit, the values of any
414 * references to variables, including variables being passed
415 * as parameters to synthetic events, are collected into a
416 * var_ref_vals array. This var_ref_idx is the index of the
417 * first param in the array to be passed to the synthetic
418 * event invocation.
419 */
420 unsigned int var_ref_idx;
421 struct synth_event *synth_event;
Tom Zanussie91eefd72019-02-13 17:42:50 -0600422 bool use_trace_keyword;
423 char *synth_event_name;
Tom Zanussic3e49502019-02-13 17:42:43 -0600424
Tom Zanussic282a382018-01-15 20:52:00 -0600425 union {
426 struct {
Tom Zanussic3e49502019-02-13 17:42:43 -0600427 char *event;
428 char *event_system;
429 } match_data;
Tom Zanussi50450602018-01-15 20:52:01 -0600430
431 struct {
Tom Zanussi466f4522019-02-13 17:42:44 -0600432 /*
433 * var_str contains the $-unstripped variable
434 * name referenced by var_ref, and used when
435 * printing the action. Because var_ref
436 * creation is deferred to create_actions(),
437 * we need a per-action way to save it until
438 * then, thus var_str.
439 */
Tom Zanussi50450602018-01-15 20:52:01 -0600440 char *var_str;
Tom Zanussi466f4522019-02-13 17:42:44 -0600441
442 /*
443 * var_ref refers to the variable being
444 * tracked e.g onmax($var).
445 */
446 struct hist_field *var_ref;
447
448 /*
449 * track_var contains the 'invisible' tracking
450 * variable created to keep the current
451 * e.g. max value.
452 */
453 struct hist_field *track_var;
454
455 check_track_val_fn_t check_val;
456 action_fn_t save_data;
457 } track_data;
Tom Zanussic282a382018-01-15 20:52:00 -0600458 };
Tom Zanussi7ef224d2016-03-03 12:54:42 -0600459};
460
Tom Zanussia3785b72019-02-13 17:42:46 -0600461struct track_data {
462 u64 track_val;
463 bool updated;
464
465 unsigned int key_len;
466 void *key;
467 struct tracing_map_elt elt;
468
469 struct action_data *action_data;
470 struct hist_trigger_data *hist_data;
471};
472
473struct hist_elt_data {
474 char *comm;
475 u64 *var_ref_vals;
476 char *field_var_str[SYNTH_FIELDS_MAX];
477};
478
479struct snapshot_context {
480 struct tracing_map_elt *elt;
481 void *key;
482};
483
484static void track_data_free(struct track_data *track_data)
485{
486 struct hist_elt_data *elt_data;
487
488 if (!track_data)
489 return;
490
491 kfree(track_data->key);
492
493 elt_data = track_data->elt.private_data;
494 if (elt_data) {
495 kfree(elt_data->comm);
496 kfree(elt_data);
497 }
498
499 kfree(track_data);
500}
501
502static struct track_data *track_data_alloc(unsigned int key_len,
503 struct action_data *action_data,
504 struct hist_trigger_data *hist_data)
505{
506 struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
507 struct hist_elt_data *elt_data;
508
509 if (!data)
510 return ERR_PTR(-ENOMEM);
511
512 data->key = kzalloc(key_len, GFP_KERNEL);
513 if (!data->key) {
514 track_data_free(data);
515 return ERR_PTR(-ENOMEM);
516 }
517
518 data->key_len = key_len;
519 data->action_data = action_data;
520 data->hist_data = hist_data;
521
522 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
523 if (!elt_data) {
524 track_data_free(data);
525 return ERR_PTR(-ENOMEM);
526 }
527 data->elt.private_data = elt_data;
528
529 elt_data->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL);
530 if (!elt_data->comm) {
531 track_data_free(data);
532 return ERR_PTR(-ENOMEM);
533 }
534
535 return data;
536}
537
Tom Zanussif404da62018-01-15 20:52:05 -0600538static char last_hist_cmd[MAX_FILTER_STR_VAL];
539static char hist_err_str[MAX_FILTER_STR_VAL];
540
541static void last_cmd_set(char *str)
542{
543 if (!str)
544 return;
545
546 strncpy(last_hist_cmd, str, MAX_FILTER_STR_VAL - 1);
547}
548
549static void hist_err(char *str, char *var)
550{
551 int maxlen = MAX_FILTER_STR_VAL - 1;
552
553 if (!str)
554 return;
555
556 if (strlen(hist_err_str))
557 return;
558
559 if (!var)
560 var = "";
561
562 if (strlen(hist_err_str) + strlen(str) + strlen(var) > maxlen)
563 return;
564
565 strcat(hist_err_str, str);
566 strcat(hist_err_str, var);
567}
568
569static void hist_err_event(char *str, char *system, char *event, char *var)
570{
571 char err[MAX_FILTER_STR_VAL];
572
573 if (system && var)
574 snprintf(err, MAX_FILTER_STR_VAL, "%s.%s.%s", system, event, var);
575 else if (system)
576 snprintf(err, MAX_FILTER_STR_VAL, "%s.%s", system, event);
577 else
Arnd Bergmanncf4d4182018-03-28 16:09:10 +0200578 strscpy(err, var, MAX_FILTER_STR_VAL);
Tom Zanussif404da62018-01-15 20:52:05 -0600579
580 hist_err(str, err);
581}
582
583static void hist_err_clear(void)
584{
585 hist_err_str[0] = '\0';
586}
587
588static bool have_hist_err(void)
589{
590 if (strlen(hist_err_str))
591 return true;
592
593 return false;
594}
595
Tom Zanussi4b147932018-01-15 20:51:58 -0600596struct synth_trace_event {
597 struct trace_entry ent;
598 u64 fields[];
599};
600
601static int synth_event_define_fields(struct trace_event_call *call)
602{
603 struct synth_trace_event trace;
604 int offset = offsetof(typeof(trace), fields);
605 struct synth_event *event = call->data;
606 unsigned int i, size, n_u64;
607 char *name, *type;
608 bool is_signed;
609 int ret = 0;
610
611 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
612 size = event->fields[i]->size;
613 is_signed = event->fields[i]->is_signed;
614 type = event->fields[i]->type;
615 name = event->fields[i]->name;
616 ret = trace_define_field(call, type, name, offset, size,
617 is_signed, FILTER_OTHER);
618 if (ret)
619 break;
620
621 if (event->fields[i]->is_string) {
622 offset += STR_VAR_LEN_MAX;
623 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
624 } else {
625 offset += sizeof(u64);
626 n_u64++;
627 }
628 }
629
630 event->n_u64 = n_u64;
631
632 return ret;
633}
634
635static bool synth_field_signed(char *type)
636{
Steven Rostedt (VMware)b6b27352018-12-20 13:20:07 -0500637 if (str_has_prefix(type, "u"))
Tom Zanussi4b147932018-01-15 20:51:58 -0600638 return false;
639
640 return true;
641}
642
643static int synth_field_is_string(char *type)
644{
645 if (strstr(type, "char[") != NULL)
646 return true;
647
648 return false;
649}
650
651static int synth_field_string_size(char *type)
652{
653 char buf[4], *end, *start;
654 unsigned int len;
655 int size, err;
656
657 start = strstr(type, "char[");
658 if (start == NULL)
659 return -EINVAL;
Tom Zanussi2f31ed92018-12-18 14:33:21 -0600660 start += sizeof("char[") - 1;
Tom Zanussi4b147932018-01-15 20:51:58 -0600661
662 end = strchr(type, ']');
663 if (!end || end < start)
664 return -EINVAL;
665
666 len = end - start;
667 if (len > 3)
668 return -EINVAL;
669
670 strncpy(buf, start, len);
671 buf[len] = '\0';
672
673 err = kstrtouint(buf, 0, &size);
674 if (err)
675 return err;
676
677 if (size > STR_VAR_LEN_MAX)
678 return -EINVAL;
679
680 return size;
681}
682
683static int synth_field_size(char *type)
684{
685 int size = 0;
686
687 if (strcmp(type, "s64") == 0)
688 size = sizeof(s64);
689 else if (strcmp(type, "u64") == 0)
690 size = sizeof(u64);
691 else if (strcmp(type, "s32") == 0)
692 size = sizeof(s32);
693 else if (strcmp(type, "u32") == 0)
694 size = sizeof(u32);
695 else if (strcmp(type, "s16") == 0)
696 size = sizeof(s16);
697 else if (strcmp(type, "u16") == 0)
698 size = sizeof(u16);
699 else if (strcmp(type, "s8") == 0)
700 size = sizeof(s8);
701 else if (strcmp(type, "u8") == 0)
702 size = sizeof(u8);
703 else if (strcmp(type, "char") == 0)
704 size = sizeof(char);
705 else if (strcmp(type, "unsigned char") == 0)
706 size = sizeof(unsigned char);
707 else if (strcmp(type, "int") == 0)
708 size = sizeof(int);
709 else if (strcmp(type, "unsigned int") == 0)
710 size = sizeof(unsigned int);
711 else if (strcmp(type, "long") == 0)
712 size = sizeof(long);
713 else if (strcmp(type, "unsigned long") == 0)
714 size = sizeof(unsigned long);
715 else if (strcmp(type, "pid_t") == 0)
716 size = sizeof(pid_t);
717 else if (synth_field_is_string(type))
718 size = synth_field_string_size(type);
719
720 return size;
721}
722
723static const char *synth_field_fmt(char *type)
724{
725 const char *fmt = "%llu";
726
727 if (strcmp(type, "s64") == 0)
728 fmt = "%lld";
729 else if (strcmp(type, "u64") == 0)
730 fmt = "%llu";
731 else if (strcmp(type, "s32") == 0)
732 fmt = "%d";
733 else if (strcmp(type, "u32") == 0)
734 fmt = "%u";
735 else if (strcmp(type, "s16") == 0)
736 fmt = "%d";
737 else if (strcmp(type, "u16") == 0)
738 fmt = "%u";
739 else if (strcmp(type, "s8") == 0)
740 fmt = "%d";
741 else if (strcmp(type, "u8") == 0)
742 fmt = "%u";
743 else if (strcmp(type, "char") == 0)
744 fmt = "%d";
745 else if (strcmp(type, "unsigned char") == 0)
746 fmt = "%u";
747 else if (strcmp(type, "int") == 0)
748 fmt = "%d";
749 else if (strcmp(type, "unsigned int") == 0)
750 fmt = "%u";
751 else if (strcmp(type, "long") == 0)
752 fmt = "%ld";
753 else if (strcmp(type, "unsigned long") == 0)
754 fmt = "%lu";
755 else if (strcmp(type, "pid_t") == 0)
756 fmt = "%d";
757 else if (synth_field_is_string(type))
758 fmt = "%s";
759
760 return fmt;
761}
762
763static enum print_line_t print_synth_event(struct trace_iterator *iter,
764 int flags,
765 struct trace_event *event)
766{
767 struct trace_array *tr = iter->tr;
768 struct trace_seq *s = &iter->seq;
769 struct synth_trace_event *entry;
770 struct synth_event *se;
771 unsigned int i, n_u64;
772 char print_fmt[32];
773 const char *fmt;
774
775 entry = (struct synth_trace_event *)iter->ent;
776 se = container_of(event, struct synth_event, call.event);
777
778 trace_seq_printf(s, "%s: ", se->name);
779
780 for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
781 if (trace_seq_has_overflowed(s))
782 goto end;
783
784 fmt = synth_field_fmt(se->fields[i]->type);
785
786 /* parameter types */
787 if (tr->trace_flags & TRACE_ITER_VERBOSE)
788 trace_seq_printf(s, "%s ", fmt);
789
790 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
791
792 /* parameter values */
793 if (se->fields[i]->is_string) {
794 trace_seq_printf(s, print_fmt, se->fields[i]->name,
795 (char *)&entry->fields[n_u64],
796 i == se->n_fields - 1 ? "" : " ");
797 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
798 } else {
799 trace_seq_printf(s, print_fmt, se->fields[i]->name,
800 entry->fields[n_u64],
801 i == se->n_fields - 1 ? "" : " ");
802 n_u64++;
803 }
804 }
805end:
806 trace_seq_putc(s, '\n');
807
808 return trace_handle_return(s);
809}
810
811static struct trace_event_functions synth_event_funcs = {
812 .trace = print_synth_event
813};
814
815static notrace void trace_event_raw_event_synth(void *__data,
816 u64 *var_ref_vals,
817 unsigned int var_ref_idx)
818{
819 struct trace_event_file *trace_file = __data;
820 struct synth_trace_event *entry;
821 struct trace_event_buffer fbuffer;
Steven Rostedt (VMware)4708abc2018-02-07 17:29:46 -0500822 struct ring_buffer *buffer;
Tom Zanussi4b147932018-01-15 20:51:58 -0600823 struct synth_event *event;
824 unsigned int i, n_u64;
825 int fields_size = 0;
826
827 event = trace_file->event_call->data;
828
829 if (trace_trigger_soft_disabled(trace_file))
830 return;
831
832 fields_size = event->n_u64 * sizeof(u64);
833
Steven Rostedt (VMware)4708abc2018-02-07 17:29:46 -0500834 /*
835 * Avoid ring buffer recursion detection, as this event
836 * is being performed within another event.
837 */
838 buffer = trace_file->tr->trace_buffer.buffer;
839 ring_buffer_nest_start(buffer);
840
Tom Zanussi4b147932018-01-15 20:51:58 -0600841 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
842 sizeof(*entry) + fields_size);
843 if (!entry)
Steven Rostedt (VMware)4708abc2018-02-07 17:29:46 -0500844 goto out;
Tom Zanussi4b147932018-01-15 20:51:58 -0600845
846 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
847 if (event->fields[i]->is_string) {
848 char *str_val = (char *)(long)var_ref_vals[var_ref_idx + i];
849 char *str_field = (char *)&entry->fields[n_u64];
850
Tom Zanussiad452872018-03-28 15:10:56 -0500851 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
Tom Zanussi4b147932018-01-15 20:51:58 -0600852 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
853 } else {
854 entry->fields[n_u64] = var_ref_vals[var_ref_idx + i];
855 n_u64++;
856 }
857 }
858
859 trace_event_buffer_commit(&fbuffer);
Steven Rostedt (VMware)4708abc2018-02-07 17:29:46 -0500860out:
861 ring_buffer_nest_end(buffer);
Tom Zanussi4b147932018-01-15 20:51:58 -0600862}
863
864static void free_synth_event_print_fmt(struct trace_event_call *call)
865{
866 if (call) {
867 kfree(call->print_fmt);
868 call->print_fmt = NULL;
869 }
870}
871
872static int __set_synth_event_print_fmt(struct synth_event *event,
873 char *buf, int len)
874{
875 const char *fmt;
876 int pos = 0;
877 int i;
878
879 /* When len=0, we just calculate the needed length */
880#define LEN_OR_ZERO (len ? len - pos : 0)
881
882 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
883 for (i = 0; i < event->n_fields; i++) {
884 fmt = synth_field_fmt(event->fields[i]->type);
885 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
886 event->fields[i]->name, fmt,
887 i == event->n_fields - 1 ? "" : ", ");
888 }
889 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
890
891 for (i = 0; i < event->n_fields; i++) {
892 pos += snprintf(buf + pos, LEN_OR_ZERO,
893 ", REC->%s", event->fields[i]->name);
894 }
895
896#undef LEN_OR_ZERO
897
898 /* return the length of print_fmt */
899 return pos;
900}
901
902static int set_synth_event_print_fmt(struct trace_event_call *call)
903{
904 struct synth_event *event = call->data;
905 char *print_fmt;
906 int len;
907
908 /* First: called with 0 length to calculate the needed length */
909 len = __set_synth_event_print_fmt(event, NULL, 0);
910
911 print_fmt = kmalloc(len + 1, GFP_KERNEL);
912 if (!print_fmt)
913 return -ENOMEM;
914
915 /* Second: actually write the @print_fmt */
916 __set_synth_event_print_fmt(event, print_fmt, len + 1);
917 call->print_fmt = print_fmt;
918
919 return 0;
920}
921
922static void free_synth_field(struct synth_field *field)
923{
924 kfree(field->type);
925 kfree(field->name);
926 kfree(field);
927}
928
Masami Hiramatsu7bbab382018-11-05 18:03:33 +0900929static struct synth_field *parse_synth_field(int argc, const char **argv,
Masami Hiramatsu282447b2018-10-18 22:12:05 +0900930 int *consumed)
Tom Zanussi4b147932018-01-15 20:51:58 -0600931{
932 struct synth_field *field;
Masami Hiramatsu7bbab382018-11-05 18:03:33 +0900933 const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
Tom Zanussi4b147932018-01-15 20:51:58 -0600934 int len, ret = 0;
Tom Zanussi4b147932018-01-15 20:51:58 -0600935
936 if (field_type[0] == ';')
937 field_type++;
938
Masami Hiramatsu282447b2018-10-18 22:12:05 +0900939 if (!strcmp(field_type, "unsigned")) {
940 if (argc < 3)
941 return ERR_PTR(-EINVAL);
942 prefix = "unsigned ";
943 field_type = argv[1];
944 field_name = argv[2];
945 *consumed = 3;
946 } else {
947 field_name = argv[1];
948 *consumed = 2;
949 }
950
Tom Zanussi4b147932018-01-15 20:51:58 -0600951 field = kzalloc(sizeof(*field), GFP_KERNEL);
952 if (!field)
953 return ERR_PTR(-ENOMEM);
954
Masami Hiramatsu7bbab382018-11-05 18:03:33 +0900955 len = strlen(field_name);
Tom Zanussi4b147932018-01-15 20:51:58 -0600956 array = strchr(field_name, '[');
957 if (array)
Masami Hiramatsu7bbab382018-11-05 18:03:33 +0900958 len -= strlen(array);
959 else if (field_name[len - 1] == ';')
960 len--;
961
962 field->name = kmemdup_nul(field_name, len, GFP_KERNEL);
963 if (!field->name) {
964 ret = -ENOMEM;
965 goto free;
966 }
967
968 if (field_type[0] == ';')
969 field_type++;
970 len = strlen(field_type) + 1;
971 if (array)
Tom Zanussi4b147932018-01-15 20:51:58 -0600972 len += strlen(array);
Masami Hiramatsu282447b2018-10-18 22:12:05 +0900973 if (prefix)
974 len += strlen(prefix);
Masami Hiramatsu7bbab382018-11-05 18:03:33 +0900975
Tom Zanussi4b147932018-01-15 20:51:58 -0600976 field->type = kzalloc(len, GFP_KERNEL);
977 if (!field->type) {
978 ret = -ENOMEM;
979 goto free;
980 }
Masami Hiramatsu282447b2018-10-18 22:12:05 +0900981 if (prefix)
982 strcat(field->type, prefix);
Tom Zanussi4b147932018-01-15 20:51:58 -0600983 strcat(field->type, field_type);
984 if (array) {
985 strcat(field->type, array);
Masami Hiramatsu7bbab382018-11-05 18:03:33 +0900986 if (field->type[len - 1] == ';')
987 field->type[len - 1] = '\0';
Tom Zanussi4b147932018-01-15 20:51:58 -0600988 }
989
990 field->size = synth_field_size(field->type);
991 if (!field->size) {
992 ret = -EINVAL;
993 goto free;
994 }
995
996 if (synth_field_is_string(field->type))
997 field->is_string = true;
998
999 field->is_signed = synth_field_signed(field->type);
1000
Tom Zanussi4b147932018-01-15 20:51:58 -06001001 out:
1002 return field;
1003 free:
1004 free_synth_field(field);
1005 field = ERR_PTR(ret);
1006 goto out;
1007}
1008
1009static void free_synth_tracepoint(struct tracepoint *tp)
1010{
1011 if (!tp)
1012 return;
1013
1014 kfree(tp->name);
1015 kfree(tp);
1016}
1017
1018static struct tracepoint *alloc_synth_tracepoint(char *name)
1019{
1020 struct tracepoint *tp;
1021
1022 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
1023 if (!tp)
1024 return ERR_PTR(-ENOMEM);
1025
1026 tp->name = kstrdup(name, GFP_KERNEL);
1027 if (!tp->name) {
1028 kfree(tp);
1029 return ERR_PTR(-ENOMEM);
1030 }
1031
1032 return tp;
1033}
1034
1035typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
1036 unsigned int var_ref_idx);
1037
1038static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
1039 unsigned int var_ref_idx)
1040{
1041 struct tracepoint *tp = event->tp;
1042
1043 if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
1044 struct tracepoint_func *probe_func_ptr;
1045 synth_probe_func_t probe_func;
1046 void *__data;
1047
1048 if (!(cpu_online(raw_smp_processor_id())))
1049 return;
1050
1051 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
1052 if (probe_func_ptr) {
1053 do {
1054 probe_func = probe_func_ptr->func;
1055 __data = probe_func_ptr->data;
1056 probe_func(__data, var_ref_vals, var_ref_idx);
1057 } while ((++probe_func_ptr)->func);
1058 }
1059 }
1060}
1061
1062static struct synth_event *find_synth_event(const char *name)
1063{
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001064 struct dyn_event *pos;
Tom Zanussi4b147932018-01-15 20:51:58 -06001065 struct synth_event *event;
1066
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001067 for_each_dyn_event(pos) {
1068 if (!is_synth_event(pos))
1069 continue;
1070 event = to_synth_event(pos);
Tom Zanussi4b147932018-01-15 20:51:58 -06001071 if (strcmp(event->name, name) == 0)
1072 return event;
1073 }
1074
1075 return NULL;
1076}
1077
1078static int register_synth_event(struct synth_event *event)
1079{
1080 struct trace_event_call *call = &event->call;
1081 int ret = 0;
1082
1083 event->call.class = &event->class;
1084 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
1085 if (!event->class.system) {
1086 ret = -ENOMEM;
1087 goto out;
1088 }
1089
1090 event->tp = alloc_synth_tracepoint(event->name);
1091 if (IS_ERR(event->tp)) {
1092 ret = PTR_ERR(event->tp);
1093 event->tp = NULL;
1094 goto out;
1095 }
1096
1097 INIT_LIST_HEAD(&call->class->fields);
1098 call->event.funcs = &synth_event_funcs;
1099 call->class->define_fields = synth_event_define_fields;
1100
1101 ret = register_trace_event(&call->event);
1102 if (!ret) {
1103 ret = -ENODEV;
1104 goto out;
1105 }
1106 call->flags = TRACE_EVENT_FL_TRACEPOINT;
1107 call->class->reg = trace_event_reg;
1108 call->class->probe = trace_event_raw_event_synth;
1109 call->data = event;
1110 call->tp = event->tp;
1111
Steven Rostedt (VMware)7e1413e2018-12-04 13:35:45 -05001112 ret = trace_add_event_call(call);
Tom Zanussi4b147932018-01-15 20:51:58 -06001113 if (ret) {
1114 pr_warn("Failed to register synthetic event: %s\n",
1115 trace_event_name(call));
1116 goto err;
1117 }
1118
1119 ret = set_synth_event_print_fmt(call);
1120 if (ret < 0) {
Steven Rostedt (VMware)7e1413e2018-12-04 13:35:45 -05001121 trace_remove_event_call(call);
Tom Zanussi4b147932018-01-15 20:51:58 -06001122 goto err;
1123 }
1124 out:
1125 return ret;
1126 err:
1127 unregister_trace_event(&call->event);
1128 goto out;
1129}
1130
1131static int unregister_synth_event(struct synth_event *event)
1132{
1133 struct trace_event_call *call = &event->call;
1134 int ret;
1135
Steven Rostedt (VMware)7e1413e2018-12-04 13:35:45 -05001136 ret = trace_remove_event_call(call);
Tom Zanussi4b147932018-01-15 20:51:58 -06001137
1138 return ret;
1139}
1140
1141static void free_synth_event(struct synth_event *event)
1142{
1143 unsigned int i;
1144
1145 if (!event)
1146 return;
1147
1148 for (i = 0; i < event->n_fields; i++)
1149 free_synth_field(event->fields[i]);
1150
1151 kfree(event->fields);
1152 kfree(event->name);
1153 kfree(event->class.system);
1154 free_synth_tracepoint(event->tp);
1155 free_synth_event_print_fmt(&event->call);
1156 kfree(event);
1157}
1158
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001159static struct synth_event *alloc_synth_event(const char *name, int n_fields,
Tom Zanussi4b147932018-01-15 20:51:58 -06001160 struct synth_field **fields)
1161{
1162 struct synth_event *event;
1163 unsigned int i;
1164
1165 event = kzalloc(sizeof(*event), GFP_KERNEL);
1166 if (!event) {
1167 event = ERR_PTR(-ENOMEM);
1168 goto out;
1169 }
1170
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001171 event->name = kstrdup(name, GFP_KERNEL);
Tom Zanussi4b147932018-01-15 20:51:58 -06001172 if (!event->name) {
1173 kfree(event);
1174 event = ERR_PTR(-ENOMEM);
1175 goto out;
1176 }
1177
1178 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
1179 if (!event->fields) {
1180 free_synth_event(event);
1181 event = ERR_PTR(-ENOMEM);
1182 goto out;
1183 }
1184
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001185 dyn_event_init(&event->devent, &synth_event_ops);
1186
Tom Zanussi4b147932018-01-15 20:51:58 -06001187 for (i = 0; i < n_fields; i++)
1188 event->fields[i] = fields[i];
1189
1190 event->n_fields = n_fields;
1191 out:
1192 return event;
1193}
1194
Tom Zanussic282a382018-01-15 20:52:00 -06001195static void action_trace(struct hist_trigger_data *hist_data,
1196 struct tracing_map_elt *elt, void *rec,
Tom Zanussi7d18a102019-02-13 17:42:41 -06001197 struct ring_buffer_event *rbe, void *key,
Tom Zanussic282a382018-01-15 20:52:00 -06001198 struct action_data *data, u64 *var_ref_vals)
1199{
Tom Zanussic3e49502019-02-13 17:42:43 -06001200 struct synth_event *event = data->synth_event;
Tom Zanussic282a382018-01-15 20:52:00 -06001201
Tom Zanussic3e49502019-02-13 17:42:43 -06001202 trace_synth(event, var_ref_vals, data->var_ref_idx);
Tom Zanussic282a382018-01-15 20:52:00 -06001203}
1204
1205struct hist_var_data {
1206 struct list_head list;
1207 struct hist_trigger_data *hist_data;
1208};
1209
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001210static int __create_synth_event(int argc, const char *name, const char **argv)
Tom Zanussi4b147932018-01-15 20:51:58 -06001211{
1212 struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1213 struct synth_event *event = NULL;
Masami Hiramatsu282447b2018-10-18 22:12:05 +09001214 int i, consumed = 0, n_fields = 0, ret = 0;
Tom Zanussi4b147932018-01-15 20:51:58 -06001215
1216 /*
1217 * Argument syntax:
1218 * - Add synthetic event: <event_name> field[;field] ...
1219 * - Remove synthetic event: !<event_name> field[;field] ...
1220 * where 'field' = type field_name
1221 */
Tom Zanussi4b147932018-01-15 20:51:58 -06001222
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001223 if (name[0] == '\0' || argc < 1)
1224 return -EINVAL;
1225
1226 mutex_lock(&event_mutex);
Tom Zanussi4b147932018-01-15 20:51:58 -06001227
1228 event = find_synth_event(name);
1229 if (event) {
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001230 ret = -EEXIST;
Tom Zanussi4b147932018-01-15 20:51:58 -06001231 goto out;
Masami Hiramatsu18858512018-10-22 00:08:20 +09001232 }
Tom Zanussi4b147932018-01-15 20:51:58 -06001233
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001234 for (i = 0; i < argc - 1; i++) {
Tom Zanussi4b147932018-01-15 20:51:58 -06001235 if (strcmp(argv[i], ";") == 0)
1236 continue;
1237 if (n_fields == SYNTH_FIELDS_MAX) {
1238 ret = -EINVAL;
1239 goto err;
1240 }
1241
Masami Hiramatsu282447b2018-10-18 22:12:05 +09001242 field = parse_synth_field(argc - i, &argv[i], &consumed);
Tom Zanussi4b147932018-01-15 20:51:58 -06001243 if (IS_ERR(field)) {
1244 ret = PTR_ERR(field);
1245 goto err;
1246 }
Masami Hiramatsu282447b2018-10-18 22:12:05 +09001247 fields[n_fields++] = field;
1248 i += consumed - 1;
Tom Zanussi4b147932018-01-15 20:51:58 -06001249 }
1250
Masami Hiramatsua360d9e2018-10-18 22:12:34 +09001251 if (i < argc && strcmp(argv[i], ";") != 0) {
Tom Zanussi4b147932018-01-15 20:51:58 -06001252 ret = -EINVAL;
1253 goto err;
1254 }
1255
1256 event = alloc_synth_event(name, n_fields, fields);
1257 if (IS_ERR(event)) {
1258 ret = PTR_ERR(event);
1259 event = NULL;
1260 goto err;
1261 }
Masami Hiramatsufaacb362018-11-05 18:01:12 +09001262 ret = register_synth_event(event);
1263 if (!ret)
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001264 dyn_event_add(&event->devent);
Masami Hiramatsufaacb362018-11-05 18:01:12 +09001265 else
1266 free_synth_event(event);
Tom Zanussi4b147932018-01-15 20:51:58 -06001267 out:
Masami Hiramatsufc800a12018-11-05 18:00:43 +09001268 mutex_unlock(&event_mutex);
Tom Zanussi4b147932018-01-15 20:51:58 -06001269
1270 return ret;
1271 err:
Tom Zanussi4b147932018-01-15 20:51:58 -06001272 for (i = 0; i < n_fields; i++)
1273 free_synth_field(fields[i]);
Tom Zanussi4b147932018-01-15 20:51:58 -06001274
Masami Hiramatsufaacb362018-11-05 18:01:12 +09001275 goto out;
Tom Zanussi4b147932018-01-15 20:51:58 -06001276}
1277
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001278static int create_or_delete_synth_event(int argc, char **argv)
Tom Zanussi4b147932018-01-15 20:51:58 -06001279{
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001280 const char *name = argv[0];
1281 struct synth_event *event = NULL;
1282 int ret;
Tom Zanussi4b147932018-01-15 20:51:58 -06001283
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001284 /* trace_run_command() ensures argc != 0 */
1285 if (name[0] == '!') {
1286 mutex_lock(&event_mutex);
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001287 event = find_synth_event(name + 1);
1288 if (event) {
1289 if (event->ref)
1290 ret = -EBUSY;
1291 else {
1292 ret = unregister_synth_event(event);
1293 if (!ret) {
1294 dyn_event_remove(&event->devent);
1295 free_synth_event(event);
1296 }
1297 }
Masami Hiramatsufaacb362018-11-05 18:01:12 +09001298 } else
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001299 ret = -ENOENT;
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001300 mutex_unlock(&event_mutex);
1301 return ret;
Tom Zanussi4b147932018-01-15 20:51:58 -06001302 }
1303
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001304 ret = __create_synth_event(argc - 1, name, (const char **)argv + 1);
1305 return ret == -ECANCELED ? -EINVAL : ret;
Tom Zanussi4b147932018-01-15 20:51:58 -06001306}
1307
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001308static int synth_event_create(int argc, const char **argv)
Tom Zanussi4b147932018-01-15 20:51:58 -06001309{
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001310 const char *name = argv[0];
1311 int len;
Tom Zanussi4b147932018-01-15 20:51:58 -06001312
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001313 if (name[0] != 's' || name[1] != ':')
1314 return -ECANCELED;
1315 name += 2;
1316
1317 /* This interface accepts group name prefix */
1318 if (strchr(name, '/')) {
Tom Zanussied581aa2019-02-04 15:07:23 -06001319 len = str_has_prefix(name, SYNTH_SYSTEM "/");
1320 if (len == 0)
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001321 return -EINVAL;
1322 name += len;
1323 }
1324 return __create_synth_event(argc - 1, name, argv + 1);
Tom Zanussi4b147932018-01-15 20:51:58 -06001325}
1326
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001327static int synth_event_release(struct dyn_event *ev)
Tom Zanussi4b147932018-01-15 20:51:58 -06001328{
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001329 struct synth_event *event = to_synth_event(ev);
1330 int ret;
1331
1332 if (event->ref)
1333 return -EBUSY;
1334
1335 ret = unregister_synth_event(event);
1336 if (ret)
1337 return ret;
1338
1339 dyn_event_remove(ev);
1340 free_synth_event(event);
1341 return 0;
Tom Zanussi4b147932018-01-15 20:51:58 -06001342}
1343
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001344static int __synth_event_show(struct seq_file *m, struct synth_event *event)
Tom Zanussi4b147932018-01-15 20:51:58 -06001345{
1346 struct synth_field *field;
Tom Zanussi4b147932018-01-15 20:51:58 -06001347 unsigned int i;
1348
1349 seq_printf(m, "%s\t", event->name);
1350
1351 for (i = 0; i < event->n_fields; i++) {
1352 field = event->fields[i];
1353
1354 /* parameter values */
1355 seq_printf(m, "%s %s%s", field->type, field->name,
1356 i == event->n_fields - 1 ? "" : "; ");
1357 }
1358
1359 seq_putc(m, '\n');
1360
1361 return 0;
1362}
1363
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001364static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
1365{
1366 struct synth_event *event = to_synth_event(ev);
1367
1368 seq_printf(m, "s:%s/", event->class.system);
1369
1370 return __synth_event_show(m, event);
1371}
1372
1373static int synth_events_seq_show(struct seq_file *m, void *v)
1374{
1375 struct dyn_event *ev = v;
1376
1377 if (!is_synth_event(ev))
1378 return 0;
1379
1380 return __synth_event_show(m, to_synth_event(ev));
1381}
1382
Tom Zanussi4b147932018-01-15 20:51:58 -06001383static const struct seq_operations synth_events_seq_op = {
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001384 .start = dyn_event_seq_start,
1385 .next = dyn_event_seq_next,
1386 .stop = dyn_event_seq_stop,
1387 .show = synth_events_seq_show,
Tom Zanussi4b147932018-01-15 20:51:58 -06001388};
1389
1390static int synth_events_open(struct inode *inode, struct file *file)
1391{
1392 int ret;
1393
1394 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001395 ret = dyn_events_release_all(&synth_event_ops);
Tom Zanussi4b147932018-01-15 20:51:58 -06001396 if (ret < 0)
1397 return ret;
1398 }
1399
1400 return seq_open(file, &synth_events_seq_op);
1401}
1402
1403static ssize_t synth_events_write(struct file *file,
1404 const char __user *buffer,
1405 size_t count, loff_t *ppos)
1406{
1407 return trace_parse_run_command(file, buffer, count, ppos,
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09001408 create_or_delete_synth_event);
Tom Zanussi4b147932018-01-15 20:51:58 -06001409}
1410
1411static const struct file_operations synth_events_fops = {
1412 .open = synth_events_open,
1413 .write = synth_events_write,
1414 .read = seq_read,
1415 .llseek = seq_lseek,
1416 .release = seq_release,
1417};
1418
Tom Zanussidf35d932018-01-15 20:51:54 -06001419static u64 hist_field_timestamp(struct hist_field *hist_field,
1420 struct tracing_map_elt *elt,
1421 struct ring_buffer_event *rbe,
1422 void *event)
Tom Zanussi860f9f62018-01-15 20:51:48 -06001423{
1424 struct hist_trigger_data *hist_data = hist_field->hist_data;
1425 struct trace_array *tr = hist_data->event_file->tr;
1426
1427 u64 ts = ring_buffer_event_time_stamp(rbe);
1428
1429 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
1430 ts = ns2usecs(ts);
1431
1432 return ts;
1433}
1434
Tom Zanussi8b7622b2018-01-15 20:52:03 -06001435static u64 hist_field_cpu(struct hist_field *hist_field,
1436 struct tracing_map_elt *elt,
1437 struct ring_buffer_event *rbe,
1438 void *event)
1439{
1440 int cpu = smp_processor_id();
1441
1442 return cpu;
1443}
1444
Tom Zanusside40f032018-12-18 14:33:23 -06001445/**
1446 * check_field_for_var_ref - Check if a VAR_REF field references a variable
1447 * @hist_field: The VAR_REF field to check
1448 * @var_data: The hist trigger that owns the variable
1449 * @var_idx: The trigger variable identifier
1450 *
1451 * Check the given VAR_REF field to see whether or not it references
1452 * the given variable associated with the given trigger.
1453 *
1454 * Return: The VAR_REF field if it does reference the variable, NULL if not
1455 */
Tom Zanussi067fe032018-01-15 20:51:56 -06001456static struct hist_field *
1457check_field_for_var_ref(struct hist_field *hist_field,
1458 struct hist_trigger_data *var_data,
1459 unsigned int var_idx)
1460{
Tom Zanussie4f6d242018-12-19 13:09:16 -06001461 WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
Tom Zanussi067fe032018-01-15 20:51:56 -06001462
Tom Zanussie4f6d242018-12-19 13:09:16 -06001463 if (hist_field && hist_field->var.idx == var_idx &&
1464 hist_field->var.hist_data == var_data)
1465 return hist_field;
Tom Zanussi067fe032018-01-15 20:51:56 -06001466
Tom Zanussie4f6d242018-12-19 13:09:16 -06001467 return NULL;
Tom Zanussi067fe032018-01-15 20:51:56 -06001468}
1469
Tom Zanusside40f032018-12-18 14:33:23 -06001470/**
1471 * find_var_ref - Check if a trigger has a reference to a trigger variable
1472 * @hist_data: The hist trigger that might have a reference to the variable
1473 * @var_data: The hist trigger that owns the variable
1474 * @var_idx: The trigger variable identifier
1475 *
1476 * Check the list of var_refs[] on the first hist trigger to see
1477 * whether any of them are references to the variable on the second
1478 * trigger.
1479 *
1480 * Return: The VAR_REF field referencing the variable if so, NULL if not
1481 */
Tom Zanussi067fe032018-01-15 20:51:56 -06001482static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
1483 struct hist_trigger_data *var_data,
1484 unsigned int var_idx)
1485{
Tom Zanussie4f6d242018-12-19 13:09:16 -06001486 struct hist_field *hist_field;
Tom Zanussi067fe032018-01-15 20:51:56 -06001487 unsigned int i;
1488
Tom Zanussie4f6d242018-12-19 13:09:16 -06001489 for (i = 0; i < hist_data->n_var_refs; i++) {
1490 hist_field = hist_data->var_refs[i];
1491 if (check_field_for_var_ref(hist_field, var_data, var_idx))
1492 return hist_field;
Tom Zanussi067fe032018-01-15 20:51:56 -06001493 }
1494
Tom Zanussie4f6d242018-12-19 13:09:16 -06001495 return NULL;
Tom Zanussi067fe032018-01-15 20:51:56 -06001496}
1497
Tom Zanusside40f032018-12-18 14:33:23 -06001498/**
1499 * find_any_var_ref - Check if there is a reference to a given trigger variable
1500 * @hist_data: The hist trigger
1501 * @var_idx: The trigger variable identifier
1502 *
1503 * Check to see whether the given variable is currently referenced by
1504 * any other trigger.
1505 *
1506 * The trigger the variable is defined on is explicitly excluded - the
1507 * assumption being that a self-reference doesn't prevent a trigger
1508 * from being removed.
1509 *
1510 * Return: The VAR_REF field referencing the variable if so, NULL if not
1511 */
Tom Zanussi067fe032018-01-15 20:51:56 -06001512static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
1513 unsigned int var_idx)
1514{
1515 struct trace_array *tr = hist_data->event_file->tr;
1516 struct hist_field *found = NULL;
1517 struct hist_var_data *var_data;
1518
1519 list_for_each_entry(var_data, &tr->hist_vars, list) {
1520 if (var_data->hist_data == hist_data)
1521 continue;
1522 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
1523 if (found)
1524 break;
1525 }
1526
1527 return found;
1528}
1529
Tom Zanusside40f032018-12-18 14:33:23 -06001530/**
1531 * check_var_refs - Check if there is a reference to any of trigger's variables
1532 * @hist_data: The hist trigger
1533 *
1534 * A trigger can define one or more variables. If any one of them is
1535 * currently referenced by any other trigger, this function will
1536 * determine that.
1537
1538 * Typically used to determine whether or not a trigger can be removed
1539 * - if there are any references to a trigger's variables, it cannot.
1540 *
1541 * Return: True if there is a reference to any of trigger's variables
1542 */
Tom Zanussi067fe032018-01-15 20:51:56 -06001543static bool check_var_refs(struct hist_trigger_data *hist_data)
1544{
1545 struct hist_field *field;
1546 bool found = false;
1547 int i;
1548
1549 for_each_hist_field(i, hist_data) {
1550 field = hist_data->fields[i];
1551 if (field && field->flags & HIST_FIELD_FL_VAR) {
1552 if (find_any_var_ref(hist_data, field->var.idx)) {
1553 found = true;
1554 break;
1555 }
1556 }
1557 }
1558
1559 return found;
1560}
1561
1562static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
1563{
1564 struct trace_array *tr = hist_data->event_file->tr;
1565 struct hist_var_data *var_data, *found = NULL;
1566
1567 list_for_each_entry(var_data, &tr->hist_vars, list) {
1568 if (var_data->hist_data == hist_data) {
1569 found = var_data;
1570 break;
1571 }
1572 }
1573
1574 return found;
1575}
1576
1577static bool field_has_hist_vars(struct hist_field *hist_field,
1578 unsigned int level)
1579{
1580 int i;
1581
1582 if (level > 3)
1583 return false;
1584
1585 if (!hist_field)
1586 return false;
1587
1588 if (hist_field->flags & HIST_FIELD_FL_VAR ||
1589 hist_field->flags & HIST_FIELD_FL_VAR_REF)
1590 return true;
1591
1592 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
1593 struct hist_field *operand;
1594
1595 operand = hist_field->operands[i];
1596 if (field_has_hist_vars(operand, level + 1))
1597 return true;
1598 }
1599
1600 return false;
1601}
1602
1603static bool has_hist_vars(struct hist_trigger_data *hist_data)
1604{
1605 struct hist_field *hist_field;
1606 int i;
1607
1608 for_each_hist_field(i, hist_data) {
1609 hist_field = hist_data->fields[i];
1610 if (field_has_hist_vars(hist_field, 0))
1611 return true;
1612 }
1613
1614 return false;
1615}
1616
1617static int save_hist_vars(struct hist_trigger_data *hist_data)
1618{
1619 struct trace_array *tr = hist_data->event_file->tr;
1620 struct hist_var_data *var_data;
1621
1622 var_data = find_hist_vars(hist_data);
1623 if (var_data)
1624 return 0;
1625
1626 if (trace_array_get(tr) < 0)
1627 return -ENODEV;
1628
1629 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
1630 if (!var_data) {
1631 trace_array_put(tr);
1632 return -ENOMEM;
1633 }
1634
1635 var_data->hist_data = hist_data;
1636 list_add(&var_data->list, &tr->hist_vars);
1637
1638 return 0;
1639}
1640
1641static void remove_hist_vars(struct hist_trigger_data *hist_data)
1642{
1643 struct trace_array *tr = hist_data->event_file->tr;
1644 struct hist_var_data *var_data;
1645
1646 var_data = find_hist_vars(hist_data);
1647 if (!var_data)
1648 return;
1649
1650 if (WARN_ON(check_var_refs(hist_data)))
1651 return;
1652
1653 list_del(&var_data->list);
1654
1655 kfree(var_data);
1656
1657 trace_array_put(tr);
1658}
1659
Tom Zanussi30350d62018-01-15 20:51:49 -06001660static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
1661 const char *var_name)
1662{
1663 struct hist_field *hist_field, *found = NULL;
1664 int i;
1665
1666 for_each_hist_field(i, hist_data) {
1667 hist_field = hist_data->fields[i];
1668 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
1669 strcmp(hist_field->var.name, var_name) == 0) {
1670 found = hist_field;
1671 break;
1672 }
1673 }
1674
1675 return found;
1676}
1677
1678static struct hist_field *find_var(struct hist_trigger_data *hist_data,
1679 struct trace_event_file *file,
1680 const char *var_name)
1681{
1682 struct hist_trigger_data *test_data;
1683 struct event_trigger_data *test;
1684 struct hist_field *hist_field;
1685
1686 hist_field = find_var_field(hist_data, var_name);
1687 if (hist_field)
1688 return hist_field;
1689
1690 list_for_each_entry_rcu(test, &file->triggers, list) {
1691 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1692 test_data = test->private_data;
1693 hist_field = find_var_field(test_data, var_name);
1694 if (hist_field)
1695 return hist_field;
1696 }
1697 }
1698
1699 return NULL;
1700}
1701
Tom Zanussi067fe032018-01-15 20:51:56 -06001702static struct trace_event_file *find_var_file(struct trace_array *tr,
1703 char *system,
1704 char *event_name,
1705 char *var_name)
1706{
1707 struct hist_trigger_data *var_hist_data;
1708 struct hist_var_data *var_data;
1709 struct trace_event_file *file, *found = NULL;
1710
1711 if (system)
1712 return find_event_file(tr, system, event_name);
1713
1714 list_for_each_entry(var_data, &tr->hist_vars, list) {
1715 var_hist_data = var_data->hist_data;
1716 file = var_hist_data->event_file;
1717 if (file == found)
1718 continue;
1719
1720 if (find_var_field(var_hist_data, var_name)) {
Tom Zanussif404da62018-01-15 20:52:05 -06001721 if (found) {
1722 hist_err_event("Variable name not unique, need to use fully qualified name (subsys.event.var) for variable: ", system, event_name, var_name);
Tom Zanussi067fe032018-01-15 20:51:56 -06001723 return NULL;
Tom Zanussif404da62018-01-15 20:52:05 -06001724 }
Tom Zanussi067fe032018-01-15 20:51:56 -06001725
1726 found = file;
1727 }
1728 }
1729
1730 return found;
1731}
1732
1733static struct hist_field *find_file_var(struct trace_event_file *file,
1734 const char *var_name)
1735{
1736 struct hist_trigger_data *test_data;
1737 struct event_trigger_data *test;
1738 struct hist_field *hist_field;
1739
1740 list_for_each_entry_rcu(test, &file->triggers, list) {
1741 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1742 test_data = test->private_data;
1743 hist_field = find_var_field(test_data, var_name);
1744 if (hist_field)
1745 return hist_field;
1746 }
1747 }
1748
1749 return NULL;
1750}
1751
Tom Zanussic282a382018-01-15 20:52:00 -06001752static struct hist_field *
1753find_match_var(struct hist_trigger_data *hist_data, char *var_name)
1754{
1755 struct trace_array *tr = hist_data->event_file->tr;
1756 struct hist_field *hist_field, *found = NULL;
1757 struct trace_event_file *file;
1758 unsigned int i;
1759
1760 for (i = 0; i < hist_data->n_actions; i++) {
1761 struct action_data *data = hist_data->actions[i];
1762
Tom Zanussi7d18a102019-02-13 17:42:41 -06001763 if (data->handler == HANDLER_ONMATCH) {
Tom Zanussic3e49502019-02-13 17:42:43 -06001764 char *system = data->match_data.event_system;
1765 char *event_name = data->match_data.event;
Tom Zanussic282a382018-01-15 20:52:00 -06001766
1767 file = find_var_file(tr, system, event_name, var_name);
1768 if (!file)
1769 continue;
1770 hist_field = find_file_var(file, var_name);
1771 if (hist_field) {
1772 if (found) {
Tom Zanussif404da62018-01-15 20:52:05 -06001773 hist_err_event("Variable name not unique, need to use fully qualified name (subsys.event.var) for variable: ", system, event_name, var_name);
Tom Zanussic282a382018-01-15 20:52:00 -06001774 return ERR_PTR(-EINVAL);
1775 }
1776
1777 found = hist_field;
1778 }
1779 }
1780 }
1781 return found;
1782}
1783
Tom Zanussi067fe032018-01-15 20:51:56 -06001784static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
1785 char *system,
1786 char *event_name,
1787 char *var_name)
1788{
1789 struct trace_array *tr = hist_data->event_file->tr;
1790 struct hist_field *hist_field = NULL;
1791 struct trace_event_file *file;
1792
Tom Zanussic282a382018-01-15 20:52:00 -06001793 if (!system || !event_name) {
1794 hist_field = find_match_var(hist_data, var_name);
1795 if (IS_ERR(hist_field))
1796 return NULL;
1797 if (hist_field)
1798 return hist_field;
1799 }
1800
Tom Zanussi067fe032018-01-15 20:51:56 -06001801 file = find_var_file(tr, system, event_name, var_name);
1802 if (!file)
1803 return NULL;
1804
1805 hist_field = find_file_var(file, var_name);
1806
1807 return hist_field;
1808}
1809
Tom Zanussi067fe032018-01-15 20:51:56 -06001810static u64 hist_field_var_ref(struct hist_field *hist_field,
1811 struct tracing_map_elt *elt,
1812 struct ring_buffer_event *rbe,
1813 void *event)
1814{
1815 struct hist_elt_data *elt_data;
1816 u64 var_val = 0;
1817
1818 elt_data = elt->private_data;
1819 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
1820
1821 return var_val;
1822}
1823
1824static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
1825 u64 *var_ref_vals, bool self)
1826{
1827 struct hist_trigger_data *var_data;
1828 struct tracing_map_elt *var_elt;
1829 struct hist_field *hist_field;
1830 unsigned int i, var_idx;
1831 bool resolved = true;
1832 u64 var_val = 0;
1833
1834 for (i = 0; i < hist_data->n_var_refs; i++) {
1835 hist_field = hist_data->var_refs[i];
1836 var_idx = hist_field->var.idx;
1837 var_data = hist_field->var.hist_data;
1838
1839 if (var_data == NULL) {
1840 resolved = false;
1841 break;
1842 }
1843
1844 if ((self && var_data != hist_data) ||
1845 (!self && var_data == hist_data))
1846 continue;
1847
1848 var_elt = tracing_map_lookup(var_data->map, key);
1849 if (!var_elt) {
1850 resolved = false;
1851 break;
1852 }
1853
1854 if (!tracing_map_var_set(var_elt, var_idx)) {
1855 resolved = false;
1856 break;
1857 }
1858
1859 if (self || !hist_field->read_once)
1860 var_val = tracing_map_read_var(var_elt, var_idx);
1861 else
1862 var_val = tracing_map_read_var_once(var_elt, var_idx);
1863
1864 var_ref_vals[i] = var_val;
1865 }
1866
1867 return resolved;
1868}
1869
Tom Zanussi85013252017-09-22 14:58:22 -05001870static const char *hist_field_name(struct hist_field *field,
1871 unsigned int level)
1872{
1873 const char *field_name = "";
1874
1875 if (level > 1)
1876 return field_name;
1877
1878 if (field->field)
1879 field_name = field->field->name;
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06001880 else if (field->flags & HIST_FIELD_FL_LOG2 ||
1881 field->flags & HIST_FIELD_FL_ALIAS)
Tom Zanussi5819ead2017-09-22 14:58:23 -05001882 field_name = hist_field_name(field->operands[0], ++level);
Tom Zanussi8b7622b2018-01-15 20:52:03 -06001883 else if (field->flags & HIST_FIELD_FL_CPU)
1884 field_name = "cpu";
Tom Zanussi067fe032018-01-15 20:51:56 -06001885 else if (field->flags & HIST_FIELD_FL_EXPR ||
1886 field->flags & HIST_FIELD_FL_VAR_REF) {
1887 if (field->system) {
1888 static char full_name[MAX_FILTER_STR_VAL];
1889
1890 strcat(full_name, field->system);
1891 strcat(full_name, ".");
1892 strcat(full_name, field->event_name);
1893 strcat(full_name, ".");
1894 strcat(full_name, field->name);
1895 field_name = full_name;
1896 } else
1897 field_name = field->name;
Tom Zanussi0ae79612018-03-28 15:10:53 -05001898 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1899 field_name = "common_timestamp";
Tom Zanussi85013252017-09-22 14:58:22 -05001900
1901 if (field_name == NULL)
1902 field_name = "";
1903
1904 return field_name;
1905}
1906
Tom Zanussi7ef224d2016-03-03 12:54:42 -06001907static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
1908{
1909 hist_field_fn_t fn = NULL;
1910
1911 switch (field_size) {
1912 case 8:
1913 if (field_is_signed)
1914 fn = hist_field_s64;
1915 else
1916 fn = hist_field_u64;
1917 break;
1918 case 4:
1919 if (field_is_signed)
1920 fn = hist_field_s32;
1921 else
1922 fn = hist_field_u32;
1923 break;
1924 case 2:
1925 if (field_is_signed)
1926 fn = hist_field_s16;
1927 else
1928 fn = hist_field_u16;
1929 break;
1930 case 1:
1931 if (field_is_signed)
1932 fn = hist_field_s8;
1933 else
1934 fn = hist_field_u8;
1935 break;
1936 }
1937
1938 return fn;
1939}
1940
1941static int parse_map_size(char *str)
1942{
1943 unsigned long size, map_bits;
1944 int ret;
1945
1946 strsep(&str, "=");
1947 if (!str) {
1948 ret = -EINVAL;
1949 goto out;
1950 }
1951
1952 ret = kstrtoul(str, 0, &size);
1953 if (ret)
1954 goto out;
1955
1956 map_bits = ilog2(roundup_pow_of_two(size));
1957 if (map_bits < TRACING_MAP_BITS_MIN ||
1958 map_bits > TRACING_MAP_BITS_MAX)
1959 ret = -EINVAL;
1960 else
1961 ret = map_bits;
1962 out:
1963 return ret;
1964}
1965
1966static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
1967{
Tom Zanussi30350d62018-01-15 20:51:49 -06001968 unsigned int i;
1969
Tom Zanussi7ef224d2016-03-03 12:54:42 -06001970 if (!attrs)
1971 return;
1972
Tom Zanussi30350d62018-01-15 20:51:49 -06001973 for (i = 0; i < attrs->n_assignments; i++)
1974 kfree(attrs->assignment_str[i]);
1975
Tom Zanussi0212e2a2018-01-15 20:51:57 -06001976 for (i = 0; i < attrs->n_actions; i++)
1977 kfree(attrs->action_str[i]);
1978
Tom Zanussi5463bfd2016-03-03 12:54:59 -06001979 kfree(attrs->name);
Tom Zanussie62347d2016-03-03 12:54:45 -06001980 kfree(attrs->sort_key_str);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06001981 kfree(attrs->keys_str);
Tom Zanussif2606832016-03-03 12:54:43 -06001982 kfree(attrs->vals_str);
Tom Zanussia4072fe2018-01-15 20:52:08 -06001983 kfree(attrs->clock);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06001984 kfree(attrs);
1985}
1986
Tom Zanussi0212e2a2018-01-15 20:51:57 -06001987static int parse_action(char *str, struct hist_trigger_attrs *attrs)
1988{
Tom Zanussic282a382018-01-15 20:52:00 -06001989 int ret = -EINVAL;
Tom Zanussi0212e2a2018-01-15 20:51:57 -06001990
1991 if (attrs->n_actions >= HIST_ACTIONS_MAX)
1992 return ret;
1993
Steven Rostedt (VMware)754481e2018-12-19 22:38:21 -05001994 if ((str_has_prefix(str, "onmatch(")) ||
Tom Zanussidff81f52019-02-13 17:42:48 -06001995 (str_has_prefix(str, "onmax(")) ||
1996 (str_has_prefix(str, "onchange("))) {
Tom Zanussic282a382018-01-15 20:52:00 -06001997 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
1998 if (!attrs->action_str[attrs->n_actions]) {
1999 ret = -ENOMEM;
2000 return ret;
2001 }
2002 attrs->n_actions++;
2003 ret = 0;
2004 }
2005
Tom Zanussi0212e2a2018-01-15 20:51:57 -06002006 return ret;
2007}
2008
Tom Zanussi9b1ae032018-01-15 20:51:44 -06002009static int parse_assignment(char *str, struct hist_trigger_attrs *attrs)
2010{
2011 int ret = 0;
2012
Steven Rostedt (VMware)754481e2018-12-19 22:38:21 -05002013 if ((str_has_prefix(str, "key=")) ||
2014 (str_has_prefix(str, "keys="))) {
Tom Zanussi9b1ae032018-01-15 20:51:44 -06002015 attrs->keys_str = kstrdup(str, GFP_KERNEL);
2016 if (!attrs->keys_str) {
2017 ret = -ENOMEM;
2018 goto out;
2019 }
Steven Rostedt (VMware)754481e2018-12-19 22:38:21 -05002020 } else if ((str_has_prefix(str, "val=")) ||
2021 (str_has_prefix(str, "vals=")) ||
2022 (str_has_prefix(str, "values="))) {
Tom Zanussi9b1ae032018-01-15 20:51:44 -06002023 attrs->vals_str = kstrdup(str, GFP_KERNEL);
2024 if (!attrs->vals_str) {
2025 ret = -ENOMEM;
2026 goto out;
2027 }
Steven Rostedt (VMware)754481e2018-12-19 22:38:21 -05002028 } else if (str_has_prefix(str, "sort=")) {
Tom Zanussi9b1ae032018-01-15 20:51:44 -06002029 attrs->sort_key_str = kstrdup(str, GFP_KERNEL);
2030 if (!attrs->sort_key_str) {
2031 ret = -ENOMEM;
2032 goto out;
2033 }
Steven Rostedt (VMware)754481e2018-12-19 22:38:21 -05002034 } else if (str_has_prefix(str, "name=")) {
Tom Zanussi9b1ae032018-01-15 20:51:44 -06002035 attrs->name = kstrdup(str, GFP_KERNEL);
2036 if (!attrs->name) {
2037 ret = -ENOMEM;
2038 goto out;
2039 }
Steven Rostedt (VMware)754481e2018-12-19 22:38:21 -05002040 } else if (str_has_prefix(str, "clock=")) {
Tom Zanussia4072fe2018-01-15 20:52:08 -06002041 strsep(&str, "=");
2042 if (!str) {
2043 ret = -EINVAL;
2044 goto out;
2045 }
2046
2047 str = strstrip(str);
2048 attrs->clock = kstrdup(str, GFP_KERNEL);
2049 if (!attrs->clock) {
2050 ret = -ENOMEM;
2051 goto out;
2052 }
Steven Rostedt (VMware)754481e2018-12-19 22:38:21 -05002053 } else if (str_has_prefix(str, "size=")) {
Tom Zanussi9b1ae032018-01-15 20:51:44 -06002054 int map_bits = parse_map_size(str);
2055
2056 if (map_bits < 0) {
2057 ret = map_bits;
2058 goto out;
2059 }
2060 attrs->map_bits = map_bits;
Tom Zanussi30350d62018-01-15 20:51:49 -06002061 } else {
2062 char *assignment;
2063
2064 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
Tom Zanussif404da62018-01-15 20:52:05 -06002065 hist_err("Too many variables defined: ", str);
Tom Zanussi30350d62018-01-15 20:51:49 -06002066 ret = -EINVAL;
2067 goto out;
2068 }
2069
2070 assignment = kstrdup(str, GFP_KERNEL);
2071 if (!assignment) {
2072 ret = -ENOMEM;
2073 goto out;
2074 }
2075
2076 attrs->assignment_str[attrs->n_assignments++] = assignment;
2077 }
Tom Zanussi9b1ae032018-01-15 20:51:44 -06002078 out:
2079 return ret;
2080}
2081
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002082static struct hist_trigger_attrs *parse_hist_trigger_attrs(char *trigger_str)
2083{
2084 struct hist_trigger_attrs *attrs;
2085 int ret = 0;
2086
2087 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
2088 if (!attrs)
2089 return ERR_PTR(-ENOMEM);
2090
2091 while (trigger_str) {
2092 char *str = strsep(&trigger_str, ":");
2093
Tom Zanussi9b1ae032018-01-15 20:51:44 -06002094 if (strchr(str, '=')) {
2095 ret = parse_assignment(str, attrs);
2096 if (ret)
2097 goto free;
2098 } else if (strcmp(str, "pause") == 0)
Tom Zanussi83e99912016-03-03 12:54:46 -06002099 attrs->pause = true;
2100 else if ((strcmp(str, "cont") == 0) ||
2101 (strcmp(str, "continue") == 0))
2102 attrs->cont = true;
Tom Zanussie86ae9b2016-03-03 12:54:47 -06002103 else if (strcmp(str, "clear") == 0)
2104 attrs->clear = true;
Tom Zanussi9b1ae032018-01-15 20:51:44 -06002105 else {
Tom Zanussi0212e2a2018-01-15 20:51:57 -06002106 ret = parse_action(str, attrs);
2107 if (ret)
2108 goto free;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002109 }
2110 }
2111
2112 if (!attrs->keys_str) {
2113 ret = -EINVAL;
2114 goto free;
2115 }
2116
Tom Zanussia4072fe2018-01-15 20:52:08 -06002117 if (!attrs->clock) {
2118 attrs->clock = kstrdup("global", GFP_KERNEL);
2119 if (!attrs->clock) {
2120 ret = -ENOMEM;
2121 goto free;
2122 }
2123 }
2124
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002125 return attrs;
2126 free:
2127 destroy_hist_trigger_attrs(attrs);
2128
2129 return ERR_PTR(ret);
2130}
2131
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002132static inline void save_comm(char *comm, struct task_struct *task)
2133{
2134 if (!task->pid) {
2135 strcpy(comm, "<idle>");
2136 return;
2137 }
2138
2139 if (WARN_ON_ONCE(task->pid < 0)) {
2140 strcpy(comm, "<XXX>");
2141 return;
2142 }
2143
2144 memcpy(comm, task->comm, TASK_COMM_LEN);
2145}
2146
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002147static void hist_elt_data_free(struct hist_elt_data *elt_data)
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002148{
Tom Zanussi02205a62018-01-15 20:51:59 -06002149 unsigned int i;
2150
2151 for (i = 0; i < SYNTH_FIELDS_MAX; i++)
2152 kfree(elt_data->field_var_str[i]);
2153
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002154 kfree(elt_data->comm);
2155 kfree(elt_data);
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002156}
2157
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002158static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
2159{
2160 struct hist_elt_data *elt_data = elt->private_data;
2161
2162 hist_elt_data_free(elt_data);
2163}
2164
2165static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002166{
2167 struct hist_trigger_data *hist_data = elt->map->private_data;
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002168 unsigned int size = TASK_COMM_LEN;
2169 struct hist_elt_data *elt_data;
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002170 struct hist_field *key_field;
Tom Zanussi02205a62018-01-15 20:51:59 -06002171 unsigned int i, n_str;
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002172
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002173 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
2174 if (!elt_data)
2175 return -ENOMEM;
2176
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002177 for_each_hist_key_field(i, hist_data) {
2178 key_field = hist_data->fields[i];
2179
2180 if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002181 elt_data->comm = kzalloc(size, GFP_KERNEL);
2182 if (!elt_data->comm) {
2183 kfree(elt_data);
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002184 return -ENOMEM;
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002185 }
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002186 break;
2187 }
2188 }
2189
Tom Zanussi7d18a102019-02-13 17:42:41 -06002190 n_str = hist_data->n_field_var_str + hist_data->n_save_var_str;
Tom Zanussi02205a62018-01-15 20:51:59 -06002191
2192 size = STR_VAR_LEN_MAX;
2193
2194 for (i = 0; i < n_str; i++) {
2195 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
2196 if (!elt_data->field_var_str[i]) {
2197 hist_elt_data_free(elt_data);
2198 return -ENOMEM;
2199 }
2200 }
2201
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002202 elt->private_data = elt_data;
2203
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002204 return 0;
2205}
2206
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002207static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002208{
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002209 struct hist_elt_data *elt_data = elt->private_data;
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002210
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002211 if (elt_data->comm)
2212 save_comm(elt_data->comm, current);
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002213}
2214
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06002215static const struct tracing_map_ops hist_trigger_elt_data_ops = {
2216 .elt_alloc = hist_trigger_elt_data_alloc,
2217 .elt_free = hist_trigger_elt_data_free,
2218 .elt_init = hist_trigger_elt_data_init,
Tom Zanussi6b4827a2016-03-03 12:54:50 -06002219};
2220
Tom Zanussi2ece94f2018-01-15 20:51:51 -06002221static const char *get_hist_field_flags(struct hist_field *hist_field)
2222{
2223 const char *flags_str = NULL;
2224
2225 if (hist_field->flags & HIST_FIELD_FL_HEX)
2226 flags_str = "hex";
2227 else if (hist_field->flags & HIST_FIELD_FL_SYM)
2228 flags_str = "sym";
2229 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
2230 flags_str = "sym-offset";
2231 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
2232 flags_str = "execname";
2233 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
2234 flags_str = "syscall";
2235 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
2236 flags_str = "log2";
2237 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2238 flags_str = "usecs";
2239
2240 return flags_str;
2241}
2242
Tom Zanussi100719d2018-01-15 20:51:52 -06002243static void expr_field_str(struct hist_field *field, char *expr)
2244{
Tom Zanussi067fe032018-01-15 20:51:56 -06002245 if (field->flags & HIST_FIELD_FL_VAR_REF)
2246 strcat(expr, "$");
2247
Tom Zanussi100719d2018-01-15 20:51:52 -06002248 strcat(expr, hist_field_name(field, 0));
2249
Tom Zanussi76690942018-03-28 15:10:54 -05002250 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
Tom Zanussi100719d2018-01-15 20:51:52 -06002251 const char *flags_str = get_hist_field_flags(field);
2252
2253 if (flags_str) {
2254 strcat(expr, ".");
2255 strcat(expr, flags_str);
2256 }
2257 }
2258}
2259
2260static char *expr_str(struct hist_field *field, unsigned int level)
2261{
2262 char *expr;
2263
2264 if (level > 1)
2265 return NULL;
2266
2267 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2268 if (!expr)
2269 return NULL;
2270
2271 if (!field->operands[0]) {
2272 expr_field_str(field, expr);
2273 return expr;
2274 }
2275
2276 if (field->operator == FIELD_OP_UNARY_MINUS) {
2277 char *subexpr;
2278
2279 strcat(expr, "-(");
2280 subexpr = expr_str(field->operands[0], ++level);
2281 if (!subexpr) {
2282 kfree(expr);
2283 return NULL;
2284 }
2285 strcat(expr, subexpr);
2286 strcat(expr, ")");
2287
2288 kfree(subexpr);
2289
2290 return expr;
2291 }
2292
2293 expr_field_str(field->operands[0], expr);
2294
2295 switch (field->operator) {
2296 case FIELD_OP_MINUS:
2297 strcat(expr, "-");
2298 break;
2299 case FIELD_OP_PLUS:
2300 strcat(expr, "+");
2301 break;
2302 default:
2303 kfree(expr);
2304 return NULL;
2305 }
2306
2307 expr_field_str(field->operands[1], expr);
2308
2309 return expr;
2310}
2311
2312static int contains_operator(char *str)
2313{
2314 enum field_op_id field_op = FIELD_OP_NONE;
2315 char *op;
2316
2317 op = strpbrk(str, "+-");
2318 if (!op)
2319 return FIELD_OP_NONE;
2320
2321 switch (*op) {
2322 case '-':
2323 if (*str == '-')
2324 field_op = FIELD_OP_UNARY_MINUS;
2325 else
2326 field_op = FIELD_OP_MINUS;
2327 break;
2328 case '+':
2329 field_op = FIELD_OP_PLUS;
2330 break;
2331 default:
2332 break;
2333 }
2334
2335 return field_op;
2336}
2337
Tom Zanussi656fe2b2018-12-18 14:33:24 -06002338static void __destroy_hist_field(struct hist_field *hist_field)
2339{
2340 kfree(hist_field->var.name);
2341 kfree(hist_field->name);
2342 kfree(hist_field->type);
2343
2344 kfree(hist_field);
2345}
2346
Tom Zanussi5819ead2017-09-22 14:58:23 -05002347static void destroy_hist_field(struct hist_field *hist_field,
2348 unsigned int level)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002349{
Tom Zanussi5819ead2017-09-22 14:58:23 -05002350 unsigned int i;
2351
Tom Zanussi100719d2018-01-15 20:51:52 -06002352 if (level > 3)
Tom Zanussi5819ead2017-09-22 14:58:23 -05002353 return;
2354
2355 if (!hist_field)
2356 return;
2357
Tom Zanussi656fe2b2018-12-18 14:33:24 -06002358 if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
2359 return; /* var refs will be destroyed separately */
2360
Tom Zanussi5819ead2017-09-22 14:58:23 -05002361 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
2362 destroy_hist_field(hist_field->operands[i], level + 1);
2363
Tom Zanussi656fe2b2018-12-18 14:33:24 -06002364 __destroy_hist_field(hist_field);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002365}
2366
Tom Zanussib559d002018-01-15 20:51:47 -06002367static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
2368 struct ftrace_event_field *field,
Tom Zanussi30350d62018-01-15 20:51:49 -06002369 unsigned long flags,
2370 char *var_name)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002371{
2372 struct hist_field *hist_field;
2373
2374 if (field && is_function_field(field))
2375 return NULL;
2376
2377 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
2378 if (!hist_field)
2379 return NULL;
2380
Tom Zanussib559d002018-01-15 20:51:47 -06002381 hist_field->hist_data = hist_data;
2382
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06002383 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
Tom Zanussi100719d2018-01-15 20:51:52 -06002384 goto out; /* caller will populate */
2385
Tom Zanussi067fe032018-01-15 20:51:56 -06002386 if (flags & HIST_FIELD_FL_VAR_REF) {
2387 hist_field->fn = hist_field_var_ref;
2388 goto out;
2389 }
2390
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002391 if (flags & HIST_FIELD_FL_HITCOUNT) {
2392 hist_field->fn = hist_field_counter;
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002393 hist_field->size = sizeof(u64);
2394 hist_field->type = kstrdup("u64", GFP_KERNEL);
2395 if (!hist_field->type)
2396 goto free;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002397 goto out;
2398 }
2399
Tom Zanussi69a02002016-03-03 12:54:52 -06002400 if (flags & HIST_FIELD_FL_STACKTRACE) {
2401 hist_field->fn = hist_field_none;
2402 goto out;
2403 }
2404
Namhyung Kim4b94f5b2016-03-03 12:55:02 -06002405 if (flags & HIST_FIELD_FL_LOG2) {
Tom Zanussi5819ead2017-09-22 14:58:23 -05002406 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2;
Namhyung Kim4b94f5b2016-03-03 12:55:02 -06002407 hist_field->fn = hist_field_log2;
Tom Zanussi30350d62018-01-15 20:51:49 -06002408 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
Tom Zanussi5819ead2017-09-22 14:58:23 -05002409 hist_field->size = hist_field->operands[0]->size;
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002410 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL);
2411 if (!hist_field->type)
2412 goto free;
Namhyung Kim4b94f5b2016-03-03 12:55:02 -06002413 goto out;
2414 }
2415
Tom Zanussiad42feb2018-01-15 20:51:45 -06002416 if (flags & HIST_FIELD_FL_TIMESTAMP) {
2417 hist_field->fn = hist_field_timestamp;
2418 hist_field->size = sizeof(u64);
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002419 hist_field->type = kstrdup("u64", GFP_KERNEL);
2420 if (!hist_field->type)
2421 goto free;
Tom Zanussiad42feb2018-01-15 20:51:45 -06002422 goto out;
2423 }
2424
Tom Zanussi8b7622b2018-01-15 20:52:03 -06002425 if (flags & HIST_FIELD_FL_CPU) {
2426 hist_field->fn = hist_field_cpu;
2427 hist_field->size = sizeof(int);
2428 hist_field->type = kstrdup("unsigned int", GFP_KERNEL);
2429 if (!hist_field->type)
2430 goto free;
2431 goto out;
2432 }
2433
Tom Zanussi432480c2016-04-25 14:01:27 -05002434 if (WARN_ON_ONCE(!field))
2435 goto out;
2436
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002437 if (is_string_field(field)) {
2438 flags |= HIST_FIELD_FL_STRING;
Namhyung Kim79e577c2016-03-03 12:54:53 -06002439
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002440 hist_field->size = MAX_FILTER_STR_VAL;
2441 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2442 if (!hist_field->type)
2443 goto free;
2444
Namhyung Kim79e577c2016-03-03 12:54:53 -06002445 if (field->filter_type == FILTER_STATIC_STRING)
2446 hist_field->fn = hist_field_string;
2447 else if (field->filter_type == FILTER_DYN_STRING)
2448 hist_field->fn = hist_field_dynstring;
2449 else
2450 hist_field->fn = hist_field_pstring;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002451 } else {
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002452 hist_field->size = field->size;
2453 hist_field->is_signed = field->is_signed;
2454 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2455 if (!hist_field->type)
2456 goto free;
2457
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002458 hist_field->fn = select_value_fn(field->size,
2459 field->is_signed);
2460 if (!hist_field->fn) {
Tom Zanussi5819ead2017-09-22 14:58:23 -05002461 destroy_hist_field(hist_field, 0);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002462 return NULL;
2463 }
2464 }
2465 out:
2466 hist_field->field = field;
2467 hist_field->flags = flags;
2468
Tom Zanussi30350d62018-01-15 20:51:49 -06002469 if (var_name) {
2470 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
2471 if (!hist_field->var.name)
2472 goto free;
2473 }
2474
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002475 return hist_field;
Tom Zanussi30350d62018-01-15 20:51:49 -06002476 free:
2477 destroy_hist_field(hist_field, 0);
2478 return NULL;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002479}
2480
2481static void destroy_hist_fields(struct hist_trigger_data *hist_data)
2482{
2483 unsigned int i;
2484
Tom Zanussi30350d62018-01-15 20:51:49 -06002485 for (i = 0; i < HIST_FIELDS_MAX; i++) {
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002486 if (hist_data->fields[i]) {
Tom Zanussi5819ead2017-09-22 14:58:23 -05002487 destroy_hist_field(hist_data->fields[i], 0);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002488 hist_data->fields[i] = NULL;
2489 }
2490 }
Tom Zanussi656fe2b2018-12-18 14:33:24 -06002491
2492 for (i = 0; i < hist_data->n_var_refs; i++) {
2493 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
2494 __destroy_hist_field(hist_data->var_refs[i]);
2495 hist_data->var_refs[i] = NULL;
2496 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002497}
2498
Tom Zanussi067fe032018-01-15 20:51:56 -06002499static int init_var_ref(struct hist_field *ref_field,
2500 struct hist_field *var_field,
2501 char *system, char *event_name)
2502{
2503 int err = 0;
2504
2505 ref_field->var.idx = var_field->var.idx;
2506 ref_field->var.hist_data = var_field->hist_data;
2507 ref_field->size = var_field->size;
2508 ref_field->is_signed = var_field->is_signed;
2509 ref_field->flags |= var_field->flags &
2510 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2511
2512 if (system) {
2513 ref_field->system = kstrdup(system, GFP_KERNEL);
2514 if (!ref_field->system)
2515 return -ENOMEM;
2516 }
2517
2518 if (event_name) {
2519 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
2520 if (!ref_field->event_name) {
2521 err = -ENOMEM;
2522 goto free;
2523 }
2524 }
2525
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06002526 if (var_field->var.name) {
2527 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
2528 if (!ref_field->name) {
2529 err = -ENOMEM;
2530 goto free;
2531 }
2532 } else if (var_field->name) {
2533 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
2534 if (!ref_field->name) {
2535 err = -ENOMEM;
2536 goto free;
2537 }
Tom Zanussi067fe032018-01-15 20:51:56 -06002538 }
2539
2540 ref_field->type = kstrdup(var_field->type, GFP_KERNEL);
2541 if (!ref_field->type) {
2542 err = -ENOMEM;
2543 goto free;
2544 }
2545 out:
2546 return err;
2547 free:
2548 kfree(ref_field->system);
2549 kfree(ref_field->event_name);
2550 kfree(ref_field->name);
2551
2552 goto out;
2553}
2554
Tom Zanusside40f032018-12-18 14:33:23 -06002555/**
2556 * create_var_ref - Create a variable reference and attach it to trigger
2557 * @hist_data: The trigger that will be referencing the variable
2558 * @var_field: The VAR field to create a reference to
2559 * @system: The optional system string
2560 * @event_name: The optional event_name string
2561 *
2562 * Given a variable hist_field, create a VAR_REF hist_field that
2563 * represents a reference to it.
2564 *
2565 * This function also adds the reference to the trigger that
2566 * now references the variable.
2567 *
2568 * Return: The VAR_REF field if successful, NULL if not
2569 */
2570static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
2571 struct hist_field *var_field,
Tom Zanussi067fe032018-01-15 20:51:56 -06002572 char *system, char *event_name)
2573{
2574 unsigned long flags = HIST_FIELD_FL_VAR_REF;
2575 struct hist_field *ref_field;
2576
2577 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
2578 if (ref_field) {
2579 if (init_var_ref(ref_field, var_field, system, event_name)) {
2580 destroy_hist_field(ref_field, 0);
2581 return NULL;
2582 }
Tom Zanusside40f032018-12-18 14:33:23 -06002583
2584 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
2585 ref_field->var_ref_idx = hist_data->n_var_refs++;
Tom Zanussi067fe032018-01-15 20:51:56 -06002586 }
2587
2588 return ref_field;
2589}
2590
2591static bool is_var_ref(char *var_name)
2592{
2593 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
2594 return false;
2595
2596 return true;
2597}
2598
2599static char *field_name_from_var(struct hist_trigger_data *hist_data,
2600 char *var_name)
2601{
2602 char *name, *field;
2603 unsigned int i;
2604
2605 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
2606 name = hist_data->attrs->var_defs.name[i];
2607
2608 if (strcmp(var_name, name) == 0) {
2609 field = hist_data->attrs->var_defs.expr[i];
2610 if (contains_operator(field) || is_var_ref(field))
2611 continue;
2612 return field;
2613 }
2614 }
2615
2616 return NULL;
2617}
2618
2619static char *local_field_var_ref(struct hist_trigger_data *hist_data,
2620 char *system, char *event_name,
2621 char *var_name)
2622{
2623 struct trace_event_call *call;
2624
2625 if (system && event_name) {
2626 call = hist_data->event_file->event_call;
2627
2628 if (strcmp(system, call->class->system) != 0)
2629 return NULL;
2630
2631 if (strcmp(event_name, trace_event_name(call)) != 0)
2632 return NULL;
2633 }
2634
2635 if (!!system != !!event_name)
2636 return NULL;
2637
2638 if (!is_var_ref(var_name))
2639 return NULL;
2640
2641 var_name++;
2642
2643 return field_name_from_var(hist_data, var_name);
2644}
2645
2646static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
2647 char *system, char *event_name,
2648 char *var_name)
2649{
2650 struct hist_field *var_field = NULL, *ref_field = NULL;
2651
2652 if (!is_var_ref(var_name))
2653 return NULL;
2654
2655 var_name++;
2656
2657 var_field = find_event_var(hist_data, system, event_name, var_name);
2658 if (var_field)
Tom Zanusside40f032018-12-18 14:33:23 -06002659 ref_field = create_var_ref(hist_data, var_field,
2660 system, event_name);
Tom Zanussi067fe032018-01-15 20:51:56 -06002661
Tom Zanussif404da62018-01-15 20:52:05 -06002662 if (!ref_field)
2663 hist_err_event("Couldn't find variable: $",
2664 system, event_name, var_name);
2665
Tom Zanussi067fe032018-01-15 20:51:56 -06002666 return ref_field;
2667}
2668
Tom Zanussi100719d2018-01-15 20:51:52 -06002669static struct ftrace_event_field *
2670parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
2671 char *field_str, unsigned long *flags)
2672{
2673 struct ftrace_event_field *field = NULL;
2674 char *field_name, *modifier, *str;
2675
2676 modifier = str = kstrdup(field_str, GFP_KERNEL);
2677 if (!modifier)
2678 return ERR_PTR(-ENOMEM);
2679
2680 field_name = strsep(&modifier, ".");
2681 if (modifier) {
2682 if (strcmp(modifier, "hex") == 0)
2683 *flags |= HIST_FIELD_FL_HEX;
2684 else if (strcmp(modifier, "sym") == 0)
2685 *flags |= HIST_FIELD_FL_SYM;
2686 else if (strcmp(modifier, "sym-offset") == 0)
2687 *flags |= HIST_FIELD_FL_SYM_OFFSET;
2688 else if ((strcmp(modifier, "execname") == 0) &&
2689 (strcmp(field_name, "common_pid") == 0))
2690 *flags |= HIST_FIELD_FL_EXECNAME;
2691 else if (strcmp(modifier, "syscall") == 0)
2692 *flags |= HIST_FIELD_FL_SYSCALL;
2693 else if (strcmp(modifier, "log2") == 0)
2694 *flags |= HIST_FIELD_FL_LOG2;
2695 else if (strcmp(modifier, "usecs") == 0)
2696 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
2697 else {
Tom Zanussidcf23452018-04-26 20:04:49 -05002698 hist_err("Invalid field modifier: ", modifier);
Tom Zanussi100719d2018-01-15 20:51:52 -06002699 field = ERR_PTR(-EINVAL);
2700 goto out;
2701 }
2702 }
2703
2704 if (strcmp(field_name, "common_timestamp") == 0) {
2705 *flags |= HIST_FIELD_FL_TIMESTAMP;
2706 hist_data->enable_timestamps = true;
2707 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2708 hist_data->attrs->ts_in_usecs = true;
Tom Zanussi8b7622b2018-01-15 20:52:03 -06002709 } else if (strcmp(field_name, "cpu") == 0)
2710 *flags |= HIST_FIELD_FL_CPU;
2711 else {
Tom Zanussi100719d2018-01-15 20:51:52 -06002712 field = trace_find_event_field(file->event_call, field_name);
2713 if (!field || !field->size) {
Tom Zanussi5ec432d2018-04-26 20:04:48 -05002714 hist_err("Couldn't find field: ", field_name);
Tom Zanussi100719d2018-01-15 20:51:52 -06002715 field = ERR_PTR(-EINVAL);
2716 goto out;
2717 }
2718 }
2719 out:
2720 kfree(str);
2721
2722 return field;
2723}
2724
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06002725static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2726 struct hist_field *var_ref,
2727 char *var_name)
2728{
2729 struct hist_field *alias = NULL;
2730 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2731
2732 alias = create_hist_field(hist_data, NULL, flags, var_name);
2733 if (!alias)
2734 return NULL;
2735
2736 alias->fn = var_ref->fn;
2737 alias->operands[0] = var_ref;
2738
2739 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2740 destroy_hist_field(alias, 0);
2741 return NULL;
2742 }
2743
2744 return alias;
2745}
2746
Tom Zanussi100719d2018-01-15 20:51:52 -06002747static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2748 struct trace_event_file *file, char *str,
2749 unsigned long *flags, char *var_name)
2750{
Tom Zanussi067fe032018-01-15 20:51:56 -06002751 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
Tom Zanussi100719d2018-01-15 20:51:52 -06002752 struct ftrace_event_field *field = NULL;
2753 struct hist_field *hist_field = NULL;
2754 int ret = 0;
2755
Tom Zanussi067fe032018-01-15 20:51:56 -06002756 s = strchr(str, '.');
2757 if (s) {
2758 s = strchr(++s, '.');
2759 if (s) {
2760 ref_system = strsep(&str, ".");
2761 if (!str) {
2762 ret = -EINVAL;
2763 goto out;
2764 }
2765 ref_event = strsep(&str, ".");
2766 if (!str) {
2767 ret = -EINVAL;
2768 goto out;
2769 }
2770 ref_var = str;
2771 }
2772 }
2773
2774 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2775 if (!s) {
2776 hist_field = parse_var_ref(hist_data, ref_system, ref_event, ref_var);
2777 if (hist_field) {
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06002778 if (var_name) {
2779 hist_field = create_alias(hist_data, hist_field, var_name);
2780 if (!hist_field) {
2781 ret = -ENOMEM;
2782 goto out;
2783 }
2784 }
Tom Zanussi067fe032018-01-15 20:51:56 -06002785 return hist_field;
2786 }
2787 } else
2788 str = s;
2789
Tom Zanussi100719d2018-01-15 20:51:52 -06002790 field = parse_field(hist_data, file, str, flags);
2791 if (IS_ERR(field)) {
2792 ret = PTR_ERR(field);
2793 goto out;
2794 }
2795
2796 hist_field = create_hist_field(hist_data, field, *flags, var_name);
2797 if (!hist_field) {
2798 ret = -ENOMEM;
2799 goto out;
2800 }
2801
2802 return hist_field;
2803 out:
2804 return ERR_PTR(ret);
2805}
2806
2807static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2808 struct trace_event_file *file,
2809 char *str, unsigned long flags,
2810 char *var_name, unsigned int level);
2811
2812static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2813 struct trace_event_file *file,
2814 char *str, unsigned long flags,
2815 char *var_name, unsigned int level)
2816{
2817 struct hist_field *operand1, *expr = NULL;
2818 unsigned long operand_flags;
2819 int ret = 0;
2820 char *s;
2821
2822 /* we support only -(xxx) i.e. explicit parens required */
2823
2824 if (level > 3) {
Tom Zanussif404da62018-01-15 20:52:05 -06002825 hist_err("Too many subexpressions (3 max): ", str);
Tom Zanussi100719d2018-01-15 20:51:52 -06002826 ret = -EINVAL;
2827 goto free;
2828 }
2829
2830 str++; /* skip leading '-' */
2831
2832 s = strchr(str, '(');
2833 if (s)
2834 str++;
2835 else {
2836 ret = -EINVAL;
2837 goto free;
2838 }
2839
2840 s = strrchr(str, ')');
2841 if (s)
2842 *s = '\0';
2843 else {
2844 ret = -EINVAL; /* no closing ')' */
2845 goto free;
2846 }
2847
2848 flags |= HIST_FIELD_FL_EXPR;
2849 expr = create_hist_field(hist_data, NULL, flags, var_name);
2850 if (!expr) {
2851 ret = -ENOMEM;
2852 goto free;
2853 }
2854
2855 operand_flags = 0;
2856 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2857 if (IS_ERR(operand1)) {
2858 ret = PTR_ERR(operand1);
2859 goto free;
2860 }
2861
2862 expr->flags |= operand1->flags &
2863 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2864 expr->fn = hist_field_unary_minus;
2865 expr->operands[0] = operand1;
2866 expr->operator = FIELD_OP_UNARY_MINUS;
2867 expr->name = expr_str(expr, 0);
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002868 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2869 if (!expr->type) {
2870 ret = -ENOMEM;
2871 goto free;
2872 }
Tom Zanussi100719d2018-01-15 20:51:52 -06002873
2874 return expr;
2875 free:
2876 destroy_hist_field(expr, 0);
2877 return ERR_PTR(ret);
2878}
2879
2880static int check_expr_operands(struct hist_field *operand1,
2881 struct hist_field *operand2)
2882{
2883 unsigned long operand1_flags = operand1->flags;
2884 unsigned long operand2_flags = operand2->flags;
2885
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06002886 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2887 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2888 struct hist_field *var;
2889
2890 var = find_var_field(operand1->var.hist_data, operand1->name);
2891 if (!var)
2892 return -EINVAL;
2893 operand1_flags = var->flags;
2894 }
2895
2896 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2897 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2898 struct hist_field *var;
2899
2900 var = find_var_field(operand2->var.hist_data, operand2->name);
2901 if (!var)
2902 return -EINVAL;
2903 operand2_flags = var->flags;
2904 }
2905
Tom Zanussi100719d2018-01-15 20:51:52 -06002906 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
Tom Zanussif404da62018-01-15 20:52:05 -06002907 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
2908 hist_err("Timestamp units in expression don't match", NULL);
Tom Zanussi100719d2018-01-15 20:51:52 -06002909 return -EINVAL;
Tom Zanussif404da62018-01-15 20:52:05 -06002910 }
Tom Zanussi100719d2018-01-15 20:51:52 -06002911
2912 return 0;
2913}
2914
2915static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2916 struct trace_event_file *file,
2917 char *str, unsigned long flags,
2918 char *var_name, unsigned int level)
2919{
2920 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2921 unsigned long operand_flags;
2922 int field_op, ret = -EINVAL;
2923 char *sep, *operand1_str;
2924
Tom Zanussif404da62018-01-15 20:52:05 -06002925 if (level > 3) {
2926 hist_err("Too many subexpressions (3 max): ", str);
Tom Zanussi100719d2018-01-15 20:51:52 -06002927 return ERR_PTR(-EINVAL);
Tom Zanussif404da62018-01-15 20:52:05 -06002928 }
Tom Zanussi100719d2018-01-15 20:51:52 -06002929
2930 field_op = contains_operator(str);
2931
2932 if (field_op == FIELD_OP_NONE)
2933 return parse_atom(hist_data, file, str, &flags, var_name);
2934
2935 if (field_op == FIELD_OP_UNARY_MINUS)
2936 return parse_unary(hist_data, file, str, flags, var_name, ++level);
2937
2938 switch (field_op) {
2939 case FIELD_OP_MINUS:
2940 sep = "-";
2941 break;
2942 case FIELD_OP_PLUS:
2943 sep = "+";
2944 break;
2945 default:
2946 goto free;
2947 }
2948
2949 operand1_str = strsep(&str, sep);
2950 if (!operand1_str || !str)
2951 goto free;
2952
2953 operand_flags = 0;
2954 operand1 = parse_atom(hist_data, file, operand1_str,
2955 &operand_flags, NULL);
2956 if (IS_ERR(operand1)) {
2957 ret = PTR_ERR(operand1);
2958 operand1 = NULL;
2959 goto free;
2960 }
2961
2962 /* rest of string could be another expression e.g. b+c in a+b+c */
2963 operand_flags = 0;
2964 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2965 if (IS_ERR(operand2)) {
2966 ret = PTR_ERR(operand2);
2967 operand2 = NULL;
2968 goto free;
2969 }
2970
2971 ret = check_expr_operands(operand1, operand2);
2972 if (ret)
2973 goto free;
2974
2975 flags |= HIST_FIELD_FL_EXPR;
2976
2977 flags |= operand1->flags &
2978 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2979
2980 expr = create_hist_field(hist_data, NULL, flags, var_name);
2981 if (!expr) {
2982 ret = -ENOMEM;
2983 goto free;
2984 }
2985
Tom Zanussi067fe032018-01-15 20:51:56 -06002986 operand1->read_once = true;
2987 operand2->read_once = true;
2988
Tom Zanussi100719d2018-01-15 20:51:52 -06002989 expr->operands[0] = operand1;
2990 expr->operands[1] = operand2;
2991 expr->operator = field_op;
2992 expr->name = expr_str(expr, 0);
Tom Zanussi19a9fac2018-01-15 20:51:55 -06002993 expr->type = kstrdup(operand1->type, GFP_KERNEL);
2994 if (!expr->type) {
2995 ret = -ENOMEM;
2996 goto free;
2997 }
Tom Zanussi100719d2018-01-15 20:51:52 -06002998
2999 switch (field_op) {
3000 case FIELD_OP_MINUS:
3001 expr->fn = hist_field_minus;
3002 break;
3003 case FIELD_OP_PLUS:
3004 expr->fn = hist_field_plus;
3005 break;
3006 default:
Dan Carpenter5e4cf2b2018-03-23 14:37:36 +03003007 ret = -EINVAL;
Tom Zanussi100719d2018-01-15 20:51:52 -06003008 goto free;
3009 }
3010
3011 return expr;
3012 free:
3013 destroy_hist_field(operand1, 0);
3014 destroy_hist_field(operand2, 0);
3015 destroy_hist_field(expr, 0);
3016
3017 return ERR_PTR(ret);
3018}
3019
Tom Zanussi02205a62018-01-15 20:51:59 -06003020static char *find_trigger_filter(struct hist_trigger_data *hist_data,
3021 struct trace_event_file *file)
3022{
3023 struct event_trigger_data *test;
3024
3025 list_for_each_entry_rcu(test, &file->triggers, list) {
3026 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
3027 if (test->private_data == hist_data)
3028 return test->filter_str;
3029 }
3030 }
3031
3032 return NULL;
3033}
3034
3035static struct event_command trigger_hist_cmd;
3036static int event_hist_trigger_func(struct event_command *cmd_ops,
3037 struct trace_event_file *file,
3038 char *glob, char *cmd, char *param);
3039
3040static bool compatible_keys(struct hist_trigger_data *target_hist_data,
3041 struct hist_trigger_data *hist_data,
3042 unsigned int n_keys)
3043{
3044 struct hist_field *target_hist_field, *hist_field;
3045 unsigned int n, i, j;
3046
3047 if (hist_data->n_fields - hist_data->n_vals != n_keys)
3048 return false;
3049
3050 i = hist_data->n_vals;
3051 j = target_hist_data->n_vals;
3052
3053 for (n = 0; n < n_keys; n++) {
3054 hist_field = hist_data->fields[i + n];
3055 target_hist_field = target_hist_data->fields[j + n];
3056
3057 if (strcmp(hist_field->type, target_hist_field->type) != 0)
3058 return false;
3059 if (hist_field->size != target_hist_field->size)
3060 return false;
3061 if (hist_field->is_signed != target_hist_field->is_signed)
3062 return false;
3063 }
3064
3065 return true;
3066}
3067
3068static struct hist_trigger_data *
3069find_compatible_hist(struct hist_trigger_data *target_hist_data,
3070 struct trace_event_file *file)
3071{
3072 struct hist_trigger_data *hist_data;
3073 struct event_trigger_data *test;
3074 unsigned int n_keys;
3075
3076 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
3077
3078 list_for_each_entry_rcu(test, &file->triggers, list) {
3079 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
3080 hist_data = test->private_data;
3081
3082 if (compatible_keys(target_hist_data, hist_data, n_keys))
3083 return hist_data;
3084 }
3085 }
3086
3087 return NULL;
3088}
3089
3090static struct trace_event_file *event_file(struct trace_array *tr,
3091 char *system, char *event_name)
3092{
3093 struct trace_event_file *file;
3094
Steven Rostedt (VMware)3be4c1e2018-05-10 12:42:10 -04003095 file = __find_event_file(tr, system, event_name);
Tom Zanussi02205a62018-01-15 20:51:59 -06003096 if (!file)
3097 return ERR_PTR(-EINVAL);
3098
3099 return file;
3100}
3101
3102static struct hist_field *
3103find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
3104 char *system, char *event_name, char *field_name)
3105{
3106 struct hist_field *event_var;
3107 char *synthetic_name;
3108
3109 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3110 if (!synthetic_name)
3111 return ERR_PTR(-ENOMEM);
3112
3113 strcpy(synthetic_name, "synthetic_");
3114 strcat(synthetic_name, field_name);
3115
3116 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
3117
3118 kfree(synthetic_name);
3119
3120 return event_var;
3121}
3122
3123/**
3124 * create_field_var_hist - Automatically create a histogram and var for a field
3125 * @target_hist_data: The target hist trigger
3126 * @subsys_name: Optional subsystem name
3127 * @event_name: Optional event name
3128 * @field_name: The name of the field (and the resulting variable)
3129 *
3130 * Hist trigger actions fetch data from variables, not directly from
3131 * events. However, for convenience, users are allowed to directly
3132 * specify an event field in an action, which will be automatically
3133 * converted into a variable on their behalf.
3134
3135 * If a user specifies a field on an event that isn't the event the
3136 * histogram currently being defined (the target event histogram), the
3137 * only way that can be accomplished is if a new hist trigger is
3138 * created and the field variable defined on that.
3139 *
3140 * This function creates a new histogram compatible with the target
3141 * event (meaning a histogram with the same key as the target
3142 * histogram), and creates a variable for the specified field, but
3143 * with 'synthetic_' prepended to the variable name in order to avoid
3144 * collision with normal field variables.
3145 *
3146 * Return: The variable created for the field.
3147 */
Tom Zanussic282a382018-01-15 20:52:00 -06003148static struct hist_field *
Tom Zanussi02205a62018-01-15 20:51:59 -06003149create_field_var_hist(struct hist_trigger_data *target_hist_data,
3150 char *subsys_name, char *event_name, char *field_name)
3151{
3152 struct trace_array *tr = target_hist_data->event_file->tr;
3153 struct hist_field *event_var = ERR_PTR(-EINVAL);
3154 struct hist_trigger_data *hist_data;
3155 unsigned int i, n, first = true;
3156 struct field_var_hist *var_hist;
3157 struct trace_event_file *file;
3158 struct hist_field *key_field;
3159 char *saved_filter;
3160 char *cmd;
3161 int ret;
3162
Tom Zanussif404da62018-01-15 20:52:05 -06003163 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
Tom Zanussi7d18a102019-02-13 17:42:41 -06003164 hist_err_event("trace action: Too many field variables defined: ",
Tom Zanussif404da62018-01-15 20:52:05 -06003165 subsys_name, event_name, field_name);
Tom Zanussi02205a62018-01-15 20:51:59 -06003166 return ERR_PTR(-EINVAL);
Tom Zanussif404da62018-01-15 20:52:05 -06003167 }
Tom Zanussi02205a62018-01-15 20:51:59 -06003168
3169 file = event_file(tr, subsys_name, event_name);
3170
3171 if (IS_ERR(file)) {
Tom Zanussi7d18a102019-02-13 17:42:41 -06003172 hist_err_event("trace action: Event file not found: ",
Tom Zanussif404da62018-01-15 20:52:05 -06003173 subsys_name, event_name, field_name);
Tom Zanussi02205a62018-01-15 20:51:59 -06003174 ret = PTR_ERR(file);
3175 return ERR_PTR(ret);
3176 }
3177
3178 /*
3179 * Look for a histogram compatible with target. We'll use the
3180 * found histogram specification to create a new matching
3181 * histogram with our variable on it. target_hist_data is not
3182 * yet a registered histogram so we can't use that.
3183 */
3184 hist_data = find_compatible_hist(target_hist_data, file);
Tom Zanussif404da62018-01-15 20:52:05 -06003185 if (!hist_data) {
Tom Zanussi7d18a102019-02-13 17:42:41 -06003186 hist_err_event("trace action: Matching event histogram not found: ",
Tom Zanussif404da62018-01-15 20:52:05 -06003187 subsys_name, event_name, field_name);
Tom Zanussi02205a62018-01-15 20:51:59 -06003188 return ERR_PTR(-EINVAL);
Tom Zanussif404da62018-01-15 20:52:05 -06003189 }
Tom Zanussi02205a62018-01-15 20:51:59 -06003190
3191 /* See if a synthetic field variable has already been created */
3192 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3193 event_name, field_name);
3194 if (!IS_ERR_OR_NULL(event_var))
3195 return event_var;
3196
3197 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
3198 if (!var_hist)
3199 return ERR_PTR(-ENOMEM);
3200
3201 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3202 if (!cmd) {
3203 kfree(var_hist);
3204 return ERR_PTR(-ENOMEM);
3205 }
3206
3207 /* Use the same keys as the compatible histogram */
3208 strcat(cmd, "keys=");
3209
3210 for_each_hist_key_field(i, hist_data) {
3211 key_field = hist_data->fields[i];
3212 if (!first)
3213 strcat(cmd, ",");
3214 strcat(cmd, key_field->field->name);
3215 first = false;
3216 }
3217
3218 /* Create the synthetic field variable specification */
3219 strcat(cmd, ":synthetic_");
3220 strcat(cmd, field_name);
3221 strcat(cmd, "=");
3222 strcat(cmd, field_name);
3223
3224 /* Use the same filter as the compatible histogram */
3225 saved_filter = find_trigger_filter(hist_data, file);
3226 if (saved_filter) {
3227 strcat(cmd, " if ");
3228 strcat(cmd, saved_filter);
3229 }
3230
3231 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
3232 if (!var_hist->cmd) {
3233 kfree(cmd);
3234 kfree(var_hist);
3235 return ERR_PTR(-ENOMEM);
3236 }
3237
3238 /* Save the compatible histogram information */
3239 var_hist->hist_data = hist_data;
3240
3241 /* Create the new histogram with our variable */
3242 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
3243 "", "hist", cmd);
3244 if (ret) {
3245 kfree(cmd);
3246 kfree(var_hist->cmd);
3247 kfree(var_hist);
Tom Zanussi7d18a102019-02-13 17:42:41 -06003248 hist_err_event("trace action: Couldn't create histogram for field: ",
Tom Zanussif404da62018-01-15 20:52:05 -06003249 subsys_name, event_name, field_name);
Tom Zanussi02205a62018-01-15 20:51:59 -06003250 return ERR_PTR(ret);
3251 }
3252
3253 kfree(cmd);
3254
3255 /* If we can't find the variable, something went wrong */
3256 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3257 event_name, field_name);
3258 if (IS_ERR_OR_NULL(event_var)) {
3259 kfree(var_hist->cmd);
3260 kfree(var_hist);
Tom Zanussi7d18a102019-02-13 17:42:41 -06003261 hist_err_event("trace action: Couldn't find synthetic variable: ",
Tom Zanussif404da62018-01-15 20:52:05 -06003262 subsys_name, event_name, field_name);
Tom Zanussi02205a62018-01-15 20:51:59 -06003263 return ERR_PTR(-EINVAL);
3264 }
3265
3266 n = target_hist_data->n_field_var_hists;
3267 target_hist_data->field_var_hists[n] = var_hist;
3268 target_hist_data->n_field_var_hists++;
3269
3270 return event_var;
3271}
3272
Tom Zanussic282a382018-01-15 20:52:00 -06003273static struct hist_field *
Tom Zanussi02205a62018-01-15 20:51:59 -06003274find_target_event_var(struct hist_trigger_data *hist_data,
3275 char *subsys_name, char *event_name, char *var_name)
3276{
3277 struct trace_event_file *file = hist_data->event_file;
3278 struct hist_field *hist_field = NULL;
3279
3280 if (subsys_name) {
3281 struct trace_event_call *call;
3282
3283 if (!event_name)
3284 return NULL;
3285
3286 call = file->event_call;
3287
3288 if (strcmp(subsys_name, call->class->system) != 0)
3289 return NULL;
3290
3291 if (strcmp(event_name, trace_event_name(call)) != 0)
3292 return NULL;
3293 }
3294
3295 hist_field = find_var_field(hist_data, var_name);
3296
3297 return hist_field;
3298}
3299
3300static inline void __update_field_vars(struct tracing_map_elt *elt,
3301 struct ring_buffer_event *rbe,
3302 void *rec,
3303 struct field_var **field_vars,
3304 unsigned int n_field_vars,
3305 unsigned int field_var_str_start)
3306{
3307 struct hist_elt_data *elt_data = elt->private_data;
3308 unsigned int i, j, var_idx;
3309 u64 var_val;
3310
3311 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
3312 struct field_var *field_var = field_vars[i];
3313 struct hist_field *var = field_var->var;
3314 struct hist_field *val = field_var->val;
3315
3316 var_val = val->fn(val, elt, rbe, rec);
3317 var_idx = var->var.idx;
3318
3319 if (val->flags & HIST_FIELD_FL_STRING) {
3320 char *str = elt_data->field_var_str[j++];
3321 char *val_str = (char *)(uintptr_t)var_val;
3322
Tom Zanussiad452872018-03-28 15:10:56 -05003323 strscpy(str, val_str, STR_VAR_LEN_MAX);
Tom Zanussi02205a62018-01-15 20:51:59 -06003324 var_val = (u64)(uintptr_t)str;
3325 }
3326 tracing_map_set_var(elt, var_idx, var_val);
3327 }
3328}
3329
3330static void update_field_vars(struct hist_trigger_data *hist_data,
3331 struct tracing_map_elt *elt,
3332 struct ring_buffer_event *rbe,
3333 void *rec)
3334{
3335 __update_field_vars(elt, rbe, rec, hist_data->field_vars,
3336 hist_data->n_field_vars, 0);
3337}
3338
Tom Zanussi466f4522019-02-13 17:42:44 -06003339static void save_track_data_vars(struct hist_trigger_data *hist_data,
3340 struct tracing_map_elt *elt, void *rec,
3341 struct ring_buffer_event *rbe, void *key,
3342 struct action_data *data, u64 *var_ref_vals)
Tom Zanussi50450602018-01-15 20:52:01 -06003343{
Tom Zanussi7d18a102019-02-13 17:42:41 -06003344 __update_field_vars(elt, rbe, rec, hist_data->save_vars,
3345 hist_data->n_save_vars, hist_data->n_field_var_str);
Tom Zanussi50450602018-01-15 20:52:01 -06003346}
3347
Tom Zanussi02205a62018-01-15 20:51:59 -06003348static struct hist_field *create_var(struct hist_trigger_data *hist_data,
3349 struct trace_event_file *file,
3350 char *name, int size, const char *type)
3351{
3352 struct hist_field *var;
3353 int idx;
3354
3355 if (find_var(hist_data, file, name) && !hist_data->remove) {
3356 var = ERR_PTR(-EINVAL);
3357 goto out;
3358 }
3359
3360 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3361 if (!var) {
3362 var = ERR_PTR(-ENOMEM);
3363 goto out;
3364 }
3365
3366 idx = tracing_map_add_var(hist_data->map);
3367 if (idx < 0) {
3368 kfree(var);
3369 var = ERR_PTR(-EINVAL);
3370 goto out;
3371 }
3372
3373 var->flags = HIST_FIELD_FL_VAR;
3374 var->var.idx = idx;
3375 var->var.hist_data = var->hist_data = hist_data;
3376 var->size = size;
3377 var->var.name = kstrdup(name, GFP_KERNEL);
3378 var->type = kstrdup(type, GFP_KERNEL);
3379 if (!var->var.name || !var->type) {
3380 kfree(var->var.name);
3381 kfree(var->type);
3382 kfree(var);
3383 var = ERR_PTR(-ENOMEM);
3384 }
3385 out:
3386 return var;
3387}
3388
3389static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
3390 struct trace_event_file *file,
3391 char *field_name)
3392{
3393 struct hist_field *val = NULL, *var = NULL;
3394 unsigned long flags = HIST_FIELD_FL_VAR;
3395 struct field_var *field_var;
3396 int ret = 0;
3397
3398 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
Tom Zanussif404da62018-01-15 20:52:05 -06003399 hist_err("Too many field variables defined: ", field_name);
Tom Zanussi02205a62018-01-15 20:51:59 -06003400 ret = -EINVAL;
3401 goto err;
3402 }
3403
3404 val = parse_atom(hist_data, file, field_name, &flags, NULL);
3405 if (IS_ERR(val)) {
Tom Zanussif404da62018-01-15 20:52:05 -06003406 hist_err("Couldn't parse field variable: ", field_name);
Tom Zanussi02205a62018-01-15 20:51:59 -06003407 ret = PTR_ERR(val);
3408 goto err;
3409 }
3410
3411 var = create_var(hist_data, file, field_name, val->size, val->type);
3412 if (IS_ERR(var)) {
Tom Zanussif404da62018-01-15 20:52:05 -06003413 hist_err("Couldn't create or find variable: ", field_name);
Tom Zanussi02205a62018-01-15 20:51:59 -06003414 kfree(val);
3415 ret = PTR_ERR(var);
3416 goto err;
3417 }
3418
3419 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
3420 if (!field_var) {
3421 kfree(val);
3422 kfree(var);
3423 ret = -ENOMEM;
3424 goto err;
3425 }
3426
3427 field_var->var = var;
3428 field_var->val = val;
3429 out:
3430 return field_var;
3431 err:
3432 field_var = ERR_PTR(ret);
3433 goto out;
3434}
3435
3436/**
3437 * create_target_field_var - Automatically create a variable for a field
3438 * @target_hist_data: The target hist trigger
3439 * @subsys_name: Optional subsystem name
3440 * @event_name: Optional event name
3441 * @var_name: The name of the field (and the resulting variable)
3442 *
3443 * Hist trigger actions fetch data from variables, not directly from
3444 * events. However, for convenience, users are allowed to directly
3445 * specify an event field in an action, which will be automatically
3446 * converted into a variable on their behalf.
3447
3448 * This function creates a field variable with the name var_name on
3449 * the hist trigger currently being defined on the target event. If
3450 * subsys_name and event_name are specified, this function simply
3451 * verifies that they do in fact match the target event subsystem and
3452 * event name.
3453 *
3454 * Return: The variable created for the field.
3455 */
Tom Zanussic282a382018-01-15 20:52:00 -06003456static struct field_var *
Tom Zanussi02205a62018-01-15 20:51:59 -06003457create_target_field_var(struct hist_trigger_data *target_hist_data,
3458 char *subsys_name, char *event_name, char *var_name)
3459{
3460 struct trace_event_file *file = target_hist_data->event_file;
3461
3462 if (subsys_name) {
3463 struct trace_event_call *call;
3464
3465 if (!event_name)
3466 return NULL;
3467
3468 call = file->event_call;
3469
3470 if (strcmp(subsys_name, call->class->system) != 0)
3471 return NULL;
3472
3473 if (strcmp(event_name, trace_event_name(call)) != 0)
3474 return NULL;
3475 }
3476
3477 return create_field_var(target_hist_data, file, var_name);
3478}
3479
Tom Zanussi466f4522019-02-13 17:42:44 -06003480static bool check_track_val_max(u64 track_val, u64 var_val)
Tom Zanussi50450602018-01-15 20:52:01 -06003481{
Tom Zanussi466f4522019-02-13 17:42:44 -06003482 if (var_val <= track_val)
3483 return false;
Tom Zanussi50450602018-01-15 20:52:01 -06003484
Tom Zanussi466f4522019-02-13 17:42:44 -06003485 return true;
3486}
3487
Tom Zanussidff81f52019-02-13 17:42:48 -06003488static bool check_track_val_changed(u64 track_val, u64 var_val)
3489{
3490 if (var_val == track_val)
3491 return false;
3492
3493 return true;
3494}
3495
Tom Zanussi466f4522019-02-13 17:42:44 -06003496static u64 get_track_val(struct hist_trigger_data *hist_data,
3497 struct tracing_map_elt *elt,
3498 struct action_data *data)
3499{
3500 unsigned int track_var_idx = data->track_data.track_var->var.idx;
3501 u64 track_val;
3502
3503 track_val = tracing_map_read_var(elt, track_var_idx);
3504
3505 return track_val;
3506}
3507
3508static void save_track_val(struct hist_trigger_data *hist_data,
3509 struct tracing_map_elt *elt,
3510 struct action_data *data, u64 var_val)
3511{
3512 unsigned int track_var_idx = data->track_data.track_var->var.idx;
3513
3514 tracing_map_set_var(elt, track_var_idx, var_val);
3515}
3516
3517static void save_track_data(struct hist_trigger_data *hist_data,
3518 struct tracing_map_elt *elt, void *rec,
3519 struct ring_buffer_event *rbe, void *key,
3520 struct action_data *data, u64 *var_ref_vals)
3521{
3522 if (data->track_data.save_data)
3523 data->track_data.save_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
3524}
3525
3526static bool check_track_val(struct tracing_map_elt *elt,
3527 struct action_data *data,
3528 u64 var_val)
3529{
3530 struct hist_trigger_data *hist_data;
3531 u64 track_val;
3532
3533 hist_data = data->track_data.track_var->hist_data;
3534 track_val = get_track_val(hist_data, elt, data);
3535
3536 return data->track_data.check_val(track_val, var_val);
3537}
3538
Tom Zanussia3785b72019-02-13 17:42:46 -06003539#ifdef CONFIG_TRACER_SNAPSHOT
3540static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3541{
3542 /* called with tr->max_lock held */
3543 struct track_data *track_data = tr->cond_snapshot->cond_data;
3544 struct hist_elt_data *elt_data, *track_elt_data;
3545 struct snapshot_context *context = cond_data;
3546 u64 track_val;
3547
3548 if (!track_data)
3549 return false;
3550
3551 track_val = get_track_val(track_data->hist_data, context->elt,
3552 track_data->action_data);
3553
3554 track_data->track_val = track_val;
3555 memcpy(track_data->key, context->key, track_data->key_len);
3556
3557 elt_data = context->elt->private_data;
3558 track_elt_data = track_data->elt.private_data;
3559 if (elt_data->comm)
3560 memcpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
3561
3562 track_data->updated = true;
3563
3564 return true;
3565}
3566
3567static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3568 struct tracing_map_elt *elt, void *rec,
3569 struct ring_buffer_event *rbe, void *key,
3570 struct action_data *data,
3571 u64 *var_ref_vals)
3572{
3573 struct trace_event_file *file = hist_data->event_file;
3574 struct snapshot_context context;
3575
3576 context.elt = elt;
3577 context.key = key;
3578
3579 tracing_snapshot_cond(file->tr, &context);
3580}
3581
3582static void hist_trigger_print_key(struct seq_file *m,
3583 struct hist_trigger_data *hist_data,
3584 void *key,
3585 struct tracing_map_elt *elt);
3586
3587static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
3588{
3589 unsigned int i;
3590
3591 if (!hist_data->n_actions)
3592 return NULL;
3593
3594 for (i = 0; i < hist_data->n_actions; i++) {
3595 struct action_data *data = hist_data->actions[i];
3596
3597 if (data->action == ACTION_SNAPSHOT)
3598 return data;
3599 }
3600
3601 return NULL;
3602}
3603
3604static void track_data_snapshot_print(struct seq_file *m,
3605 struct hist_trigger_data *hist_data)
3606{
3607 struct trace_event_file *file = hist_data->event_file;
3608 struct track_data *track_data;
3609 struct action_data *action;
3610
3611 track_data = tracing_cond_snapshot_data(file->tr);
3612 if (!track_data)
3613 return;
3614
3615 if (!track_data->updated)
3616 return;
3617
3618 action = snapshot_action(hist_data);
3619 if (!action)
3620 return;
3621
3622 seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n");
3623 seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
3624 action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
3625 action->track_data.var_str, track_data->track_val);
3626
3627 seq_puts(m, "\ttriggered by event with key: ");
3628 hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
3629 seq_putc(m, '\n');
3630}
3631#else
3632static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3633{
3634 return false;
3635}
3636static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3637 struct tracing_map_elt *elt, void *rec,
3638 struct ring_buffer_event *rbe, void *key,
3639 struct action_data *data,
3640 u64 *var_ref_vals) {}
3641static void track_data_snapshot_print(struct seq_file *m,
3642 struct hist_trigger_data *hist_data) {}
3643#endif /* CONFIG_TRACER_SNAPSHOT */
3644
Tom Zanussi466f4522019-02-13 17:42:44 -06003645static void track_data_print(struct seq_file *m,
3646 struct hist_trigger_data *hist_data,
3647 struct tracing_map_elt *elt,
3648 struct action_data *data)
3649{
3650 u64 track_val = get_track_val(hist_data, elt, data);
3651 unsigned int i, save_var_idx;
3652
3653 if (data->handler == HANDLER_ONMAX)
3654 seq_printf(m, "\n\tmax: %10llu", track_val);
Tom Zanussidff81f52019-02-13 17:42:48 -06003655 else if (data->handler == HANDLER_ONCHANGE)
3656 seq_printf(m, "\n\tchanged: %10llu", track_val);
Tom Zanussi50450602018-01-15 20:52:01 -06003657
Tom Zanussia3785b72019-02-13 17:42:46 -06003658 if (data->action == ACTION_SNAPSHOT)
3659 return;
3660
Tom Zanussi7d18a102019-02-13 17:42:41 -06003661 for (i = 0; i < hist_data->n_save_vars; i++) {
3662 struct hist_field *save_val = hist_data->save_vars[i]->val;
3663 struct hist_field *save_var = hist_data->save_vars[i]->var;
Tom Zanussi50450602018-01-15 20:52:01 -06003664 u64 val;
3665
3666 save_var_idx = save_var->var.idx;
3667
3668 val = tracing_map_read_var(elt, save_var_idx);
3669
3670 if (save_val->flags & HIST_FIELD_FL_STRING) {
3671 seq_printf(m, " %s: %-32s", save_var->var.name,
3672 (char *)(uintptr_t)(val));
3673 } else
3674 seq_printf(m, " %s: %10llu", save_var->var.name, val);
3675 }
3676}
3677
Tom Zanussi466f4522019-02-13 17:42:44 -06003678static void ontrack_action(struct hist_trigger_data *hist_data,
3679 struct tracing_map_elt *elt, void *rec,
3680 struct ring_buffer_event *rbe, void *key,
3681 struct action_data *data, u64 *var_ref_vals)
Tom Zanussi50450602018-01-15 20:52:01 -06003682{
Tom Zanussi466f4522019-02-13 17:42:44 -06003683 u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
Tom Zanussi50450602018-01-15 20:52:01 -06003684
Tom Zanussi466f4522019-02-13 17:42:44 -06003685 if (check_track_val(elt, data, var_val)) {
3686 save_track_val(hist_data, elt, data, var_val);
3687 save_track_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
3688 }
Tom Zanussi50450602018-01-15 20:52:01 -06003689}
3690
Tom Zanussic3e49502019-02-13 17:42:43 -06003691static void action_data_destroy(struct action_data *data)
Tom Zanussi50450602018-01-15 20:52:01 -06003692{
3693 unsigned int i;
3694
Tom Zanussic3e49502019-02-13 17:42:43 -06003695 lockdep_assert_held(&event_mutex);
Tom Zanussi50450602018-01-15 20:52:01 -06003696
Tom Zanussi7d18a102019-02-13 17:42:41 -06003697 kfree(data->action_name);
Tom Zanussi50450602018-01-15 20:52:01 -06003698
3699 for (i = 0; i < data->n_params; i++)
3700 kfree(data->params[i]);
3701
Tom Zanussic3e49502019-02-13 17:42:43 -06003702 if (data->synth_event)
3703 data->synth_event->ref--;
3704
Tom Zanussie91eefd72019-02-13 17:42:50 -06003705 kfree(data->synth_event_name);
3706
Tom Zanussi50450602018-01-15 20:52:01 -06003707 kfree(data);
3708}
3709
Tom Zanussi466f4522019-02-13 17:42:44 -06003710static void track_data_destroy(struct hist_trigger_data *hist_data,
3711 struct action_data *data)
Tom Zanussic3e49502019-02-13 17:42:43 -06003712{
Tom Zanussia3785b72019-02-13 17:42:46 -06003713 struct trace_event_file *file = hist_data->event_file;
3714
Tom Zanussi466f4522019-02-13 17:42:44 -06003715 destroy_hist_field(data->track_data.track_var, 0);
3716 destroy_hist_field(data->track_data.var_ref, 0);
Tom Zanussic3e49502019-02-13 17:42:43 -06003717
Tom Zanussia3785b72019-02-13 17:42:46 -06003718 if (data->action == ACTION_SNAPSHOT) {
3719 struct track_data *track_data;
3720
3721 track_data = tracing_cond_snapshot_data(file->tr);
3722 if (track_data && track_data->hist_data == hist_data) {
3723 tracing_snapshot_cond_disable(file->tr);
3724 track_data_free(track_data);
3725 }
3726 }
3727
Tom Zanussi466f4522019-02-13 17:42:44 -06003728 kfree(data->track_data.var_str);
Tom Zanussic3e49502019-02-13 17:42:43 -06003729
3730 action_data_destroy(data);
3731}
3732
Tom Zanussi7d18a102019-02-13 17:42:41 -06003733static int action_create(struct hist_trigger_data *hist_data,
3734 struct action_data *data);
3735
Tom Zanussi466f4522019-02-13 17:42:44 -06003736static int track_data_create(struct hist_trigger_data *hist_data,
3737 struct action_data *data)
Tom Zanussi50450602018-01-15 20:52:01 -06003738{
Tom Zanussi466f4522019-02-13 17:42:44 -06003739 struct hist_field *var_field, *ref_field, *track_var = NULL;
Tom Zanussi50450602018-01-15 20:52:01 -06003740 struct trace_event_file *file = hist_data->event_file;
Tom Zanussi466f4522019-02-13 17:42:44 -06003741 char *track_data_var_str;
Tom Zanussi50450602018-01-15 20:52:01 -06003742 int ret = 0;
3743
Tom Zanussi466f4522019-02-13 17:42:44 -06003744 track_data_var_str = data->track_data.var_str;
3745 if (track_data_var_str[0] != '$') {
Tom Zanussidff81f52019-02-13 17:42:48 -06003746 hist_err("For onmax(x) or onchange(x), x must be a variable: ", track_data_var_str);
Tom Zanussi50450602018-01-15 20:52:01 -06003747 return -EINVAL;
Tom Zanussif404da62018-01-15 20:52:05 -06003748 }
Tom Zanussi466f4522019-02-13 17:42:44 -06003749 track_data_var_str++;
Tom Zanussi50450602018-01-15 20:52:01 -06003750
Tom Zanussi466f4522019-02-13 17:42:44 -06003751 var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
Tom Zanussif404da62018-01-15 20:52:05 -06003752 if (!var_field) {
Tom Zanussidff81f52019-02-13 17:42:48 -06003753 hist_err("Couldn't find onmax or onchange variable: ", track_data_var_str);
Tom Zanussi50450602018-01-15 20:52:01 -06003754 return -EINVAL;
Tom Zanussif404da62018-01-15 20:52:05 -06003755 }
Tom Zanussi50450602018-01-15 20:52:01 -06003756
Tom Zanusside40f032018-12-18 14:33:23 -06003757 ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
Tom Zanussi50450602018-01-15 20:52:01 -06003758 if (!ref_field)
3759 return -ENOMEM;
3760
Tom Zanussi466f4522019-02-13 17:42:44 -06003761 data->track_data.var_ref = ref_field;
Tom Zanussi50450602018-01-15 20:52:01 -06003762
Tom Zanussi466f4522019-02-13 17:42:44 -06003763 if (data->handler == HANDLER_ONMAX)
3764 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
3765 if (IS_ERR(track_var)) {
3766 hist_err("Couldn't create onmax variable: ", "__max");
3767 ret = PTR_ERR(track_var);
Tom Zanussi50450602018-01-15 20:52:01 -06003768 goto out;
3769 }
Tom Zanussidff81f52019-02-13 17:42:48 -06003770
3771 if (data->handler == HANDLER_ONCHANGE)
3772 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
3773 if (IS_ERR(track_var)) {
3774 hist_err("Couldn't create onchange variable: ", "__change");
3775 ret = PTR_ERR(track_var);
3776 goto out;
3777 }
Tom Zanussi466f4522019-02-13 17:42:44 -06003778 data->track_data.track_var = track_var;
Tom Zanussi50450602018-01-15 20:52:01 -06003779
Tom Zanussi7d18a102019-02-13 17:42:41 -06003780 ret = action_create(hist_data, data);
Tom Zanussi50450602018-01-15 20:52:01 -06003781 out:
3782 return ret;
3783}
3784
3785static int parse_action_params(char *params, struct action_data *data)
3786{
3787 char *param, *saved_param;
Tom Zanussie91eefd72019-02-13 17:42:50 -06003788 bool first_param = true;
Tom Zanussi50450602018-01-15 20:52:01 -06003789 int ret = 0;
3790
3791 while (params) {
Tom Zanussi7d18a102019-02-13 17:42:41 -06003792 if (data->n_params >= SYNTH_FIELDS_MAX) {
3793 hist_err("Too many action params", "");
Tom Zanussi50450602018-01-15 20:52:01 -06003794 goto out;
Tom Zanussi7d18a102019-02-13 17:42:41 -06003795 }
Tom Zanussi50450602018-01-15 20:52:01 -06003796
3797 param = strsep(&params, ",");
3798 if (!param) {
Tom Zanussi7d18a102019-02-13 17:42:41 -06003799 hist_err("No action param found", "");
Tom Zanussi50450602018-01-15 20:52:01 -06003800 ret = -EINVAL;
3801 goto out;
3802 }
3803
3804 param = strstrip(param);
3805 if (strlen(param) < 2) {
Tom Zanussif404da62018-01-15 20:52:05 -06003806 hist_err("Invalid action param: ", param);
Tom Zanussi50450602018-01-15 20:52:01 -06003807 ret = -EINVAL;
3808 goto out;
3809 }
3810
3811 saved_param = kstrdup(param, GFP_KERNEL);
3812 if (!saved_param) {
3813 ret = -ENOMEM;
3814 goto out;
3815 }
3816
Tom Zanussie91eefd72019-02-13 17:42:50 -06003817 if (first_param && data->use_trace_keyword) {
3818 data->synth_event_name = saved_param;
3819 first_param = false;
3820 continue;
3821 }
3822 first_param = false;
3823
Tom Zanussi50450602018-01-15 20:52:01 -06003824 data->params[data->n_params++] = saved_param;
3825 }
3826 out:
3827 return ret;
3828}
3829
Tom Zanussi7d18a102019-02-13 17:42:41 -06003830static int action_parse(char *str, struct action_data *data,
3831 enum handler_id handler)
Tom Zanussi50450602018-01-15 20:52:01 -06003832{
Tom Zanussi7d18a102019-02-13 17:42:41 -06003833 char *action_name;
3834 int ret = 0;
3835
3836 strsep(&str, ".");
3837 if (!str) {
3838 hist_err("action parsing: No action found", "");
3839 ret = -EINVAL;
3840 goto out;
3841 }
3842
3843 action_name = strsep(&str, "(");
3844 if (!action_name || !str) {
3845 hist_err("action parsing: No action found", "");
3846 ret = -EINVAL;
3847 goto out;
3848 }
3849
3850 if (str_has_prefix(action_name, "save")) {
3851 char *params = strsep(&str, ")");
3852
3853 if (!params) {
3854 hist_err("action parsing: No params found for %s", "save");
3855 ret = -EINVAL;
3856 goto out;
3857 }
3858
3859 ret = parse_action_params(params, data);
3860 if (ret)
3861 goto out;
3862
3863 if (handler == HANDLER_ONMAX)
Tom Zanussi466f4522019-02-13 17:42:44 -06003864 data->track_data.check_val = check_track_val_max;
Tom Zanussidff81f52019-02-13 17:42:48 -06003865 else if (handler == HANDLER_ONCHANGE)
3866 data->track_data.check_val = check_track_val_changed;
Tom Zanussi466f4522019-02-13 17:42:44 -06003867 else {
3868 hist_err("action parsing: Handler doesn't support action: ", action_name);
3869 ret = -EINVAL;
3870 goto out;
3871 }
Tom Zanussi7d18a102019-02-13 17:42:41 -06003872
Tom Zanussi466f4522019-02-13 17:42:44 -06003873 data->track_data.save_data = save_track_data_vars;
3874 data->fn = ontrack_action;
Tom Zanussi7d18a102019-02-13 17:42:41 -06003875 data->action = ACTION_SAVE;
Tom Zanussia3785b72019-02-13 17:42:46 -06003876 } else if (str_has_prefix(action_name, "snapshot")) {
3877 char *params = strsep(&str, ")");
3878
3879 if (!str) {
3880 hist_err("action parsing: No closing paren found: %s", params);
3881 ret = -EINVAL;
3882 goto out;
3883 }
3884
3885 if (handler == HANDLER_ONMAX)
3886 data->track_data.check_val = check_track_val_max;
Tom Zanussidff81f52019-02-13 17:42:48 -06003887 else if (handler == HANDLER_ONCHANGE)
3888 data->track_data.check_val = check_track_val_changed;
Tom Zanussia3785b72019-02-13 17:42:46 -06003889 else {
3890 hist_err("action parsing: Handler doesn't support action: ", action_name);
3891 ret = -EINVAL;
3892 goto out;
3893 }
3894
3895 data->track_data.save_data = save_track_data_snapshot;
3896 data->fn = ontrack_action;
3897 data->action = ACTION_SNAPSHOT;
Tom Zanussi7d18a102019-02-13 17:42:41 -06003898 } else {
3899 char *params = strsep(&str, ")");
3900
Tom Zanussie91eefd72019-02-13 17:42:50 -06003901 if (str_has_prefix(action_name, "trace"))
3902 data->use_trace_keyword = true;
3903
Tom Zanussi7d18a102019-02-13 17:42:41 -06003904 if (params) {
3905 ret = parse_action_params(params, data);
3906 if (ret)
3907 goto out;
3908 }
3909
Tom Zanussi466f4522019-02-13 17:42:44 -06003910 if (handler == HANDLER_ONMAX)
3911 data->track_data.check_val = check_track_val_max;
Tom Zanussidff81f52019-02-13 17:42:48 -06003912 else if (handler == HANDLER_ONCHANGE)
3913 data->track_data.check_val = check_track_val_changed;
Tom Zanussi466f4522019-02-13 17:42:44 -06003914
3915 if (handler != HANDLER_ONMATCH) {
3916 data->track_data.save_data = action_trace;
3917 data->fn = ontrack_action;
3918 } else
3919 data->fn = action_trace;
3920
Tom Zanussi7d18a102019-02-13 17:42:41 -06003921 data->action = ACTION_TRACE;
3922 }
3923
3924 data->action_name = kstrdup(action_name, GFP_KERNEL);
3925 if (!data->action_name) {
3926 ret = -ENOMEM;
3927 goto out;
3928 }
3929
3930 data->handler = handler;
3931 out:
3932 return ret;
3933}
3934
Tom Zanussi466f4522019-02-13 17:42:44 -06003935static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
3936 char *str, enum handler_id handler)
Tom Zanussi7d18a102019-02-13 17:42:41 -06003937{
Tom Zanussi50450602018-01-15 20:52:01 -06003938 struct action_data *data;
3939 int ret = -EINVAL;
Tom Zanussi466f4522019-02-13 17:42:44 -06003940 char *var_str;
Tom Zanussi50450602018-01-15 20:52:01 -06003941
3942 data = kzalloc(sizeof(*data), GFP_KERNEL);
3943 if (!data)
3944 return ERR_PTR(-ENOMEM);
3945
Tom Zanussi466f4522019-02-13 17:42:44 -06003946 var_str = strsep(&str, ")");
3947 if (!var_str || !str) {
Tom Zanussi50450602018-01-15 20:52:01 -06003948 ret = -EINVAL;
3949 goto free;
3950 }
3951
Tom Zanussi466f4522019-02-13 17:42:44 -06003952 data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
3953 if (!data->track_data.var_str) {
Tom Zanussi50450602018-01-15 20:52:01 -06003954 ret = -ENOMEM;
3955 goto free;
3956 }
3957
Tom Zanussi7d18a102019-02-13 17:42:41 -06003958 ret = action_parse(str, data, handler);
3959 if (ret)
Tom Zanussi50450602018-01-15 20:52:01 -06003960 goto free;
Tom Zanussi50450602018-01-15 20:52:01 -06003961 out:
3962 return data;
3963 free:
Tom Zanussi466f4522019-02-13 17:42:44 -06003964 track_data_destroy(hist_data, data);
Tom Zanussi50450602018-01-15 20:52:01 -06003965 data = ERR_PTR(ret);
3966 goto out;
3967}
3968
Tom Zanussic282a382018-01-15 20:52:00 -06003969static void onmatch_destroy(struct action_data *data)
3970{
Tom Zanussic3e49502019-02-13 17:42:43 -06003971 kfree(data->match_data.event);
3972 kfree(data->match_data.event_system);
Tom Zanussic282a382018-01-15 20:52:00 -06003973
Tom Zanussic3e49502019-02-13 17:42:43 -06003974 action_data_destroy(data);
Tom Zanussic282a382018-01-15 20:52:00 -06003975}
3976
Tom Zanussi02205a62018-01-15 20:51:59 -06003977static void destroy_field_var(struct field_var *field_var)
3978{
3979 if (!field_var)
3980 return;
3981
3982 destroy_hist_field(field_var->var, 0);
3983 destroy_hist_field(field_var->val, 0);
3984
3985 kfree(field_var);
3986}
3987
3988static void destroy_field_vars(struct hist_trigger_data *hist_data)
3989{
3990 unsigned int i;
3991
3992 for (i = 0; i < hist_data->n_field_vars; i++)
3993 destroy_field_var(hist_data->field_vars[i]);
3994}
3995
Tom Zanussic282a382018-01-15 20:52:00 -06003996static void save_field_var(struct hist_trigger_data *hist_data,
3997 struct field_var *field_var)
Tom Zanussi02205a62018-01-15 20:51:59 -06003998{
3999 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
4000
4001 if (field_var->val->flags & HIST_FIELD_FL_STRING)
4002 hist_data->n_field_var_str++;
4003}
4004
Tom Zanussic282a382018-01-15 20:52:00 -06004005
Tom Zanussic282a382018-01-15 20:52:00 -06004006static int check_synth_field(struct synth_event *event,
4007 struct hist_field *hist_field,
4008 unsigned int field_pos)
4009{
4010 struct synth_field *field;
4011
4012 if (field_pos >= event->n_fields)
4013 return -EINVAL;
4014
4015 field = event->fields[field_pos];
4016
4017 if (strcmp(field->type, hist_field->type) != 0)
4018 return -EINVAL;
4019
4020 return 0;
4021}
4022
Tom Zanussic282a382018-01-15 20:52:00 -06004023static struct hist_field *
Tom Zanussi7d18a102019-02-13 17:42:41 -06004024trace_action_find_var(struct hist_trigger_data *hist_data,
4025 struct action_data *data,
4026 char *system, char *event, char *var)
Tom Zanussic282a382018-01-15 20:52:00 -06004027{
4028 struct hist_field *hist_field;
4029
4030 var++; /* skip '$' */
4031
4032 hist_field = find_target_event_var(hist_data, system, event, var);
4033 if (!hist_field) {
Tom Zanussi7d18a102019-02-13 17:42:41 -06004034 if (!system && data->handler == HANDLER_ONMATCH) {
Tom Zanussic3e49502019-02-13 17:42:43 -06004035 system = data->match_data.event_system;
4036 event = data->match_data.event;
Tom Zanussic282a382018-01-15 20:52:00 -06004037 }
4038
4039 hist_field = find_event_var(hist_data, system, event, var);
4040 }
4041
Tom Zanussif404da62018-01-15 20:52:05 -06004042 if (!hist_field)
Tom Zanussi7d18a102019-02-13 17:42:41 -06004043 hist_err_event("trace action: Couldn't find param: $", system, event, var);
Tom Zanussif404da62018-01-15 20:52:05 -06004044
Tom Zanussic282a382018-01-15 20:52:00 -06004045 return hist_field;
4046}
4047
4048static struct hist_field *
Tom Zanussi7d18a102019-02-13 17:42:41 -06004049trace_action_create_field_var(struct hist_trigger_data *hist_data,
4050 struct action_data *data, char *system,
4051 char *event, char *var)
Tom Zanussic282a382018-01-15 20:52:00 -06004052{
4053 struct hist_field *hist_field = NULL;
4054 struct field_var *field_var;
4055
4056 /*
4057 * First try to create a field var on the target event (the
4058 * currently being defined). This will create a variable for
4059 * unqualified fields on the target event, or if qualified,
4060 * target fields that have qualified names matching the target.
4061 */
4062 field_var = create_target_field_var(hist_data, system, event, var);
4063
4064 if (field_var && !IS_ERR(field_var)) {
4065 save_field_var(hist_data, field_var);
4066 hist_field = field_var->var;
4067 } else {
4068 field_var = NULL;
4069 /*
4070 * If no explicit system.event is specfied, default to
4071 * looking for fields on the onmatch(system.event.xxx)
4072 * event.
4073 */
Tom Zanussi7d18a102019-02-13 17:42:41 -06004074 if (!system && data->handler == HANDLER_ONMATCH) {
Tom Zanussic3e49502019-02-13 17:42:43 -06004075 system = data->match_data.event_system;
4076 event = data->match_data.event;
Tom Zanussic282a382018-01-15 20:52:00 -06004077 }
4078
4079 /*
4080 * At this point, we're looking at a field on another
4081 * event. Because we can't modify a hist trigger on
4082 * another event to add a variable for a field, we need
4083 * to create a new trigger on that event and create the
4084 * variable at the same time.
4085 */
4086 hist_field = create_field_var_hist(hist_data, system, event, var);
4087 if (IS_ERR(hist_field))
4088 goto free;
4089 }
4090 out:
4091 return hist_field;
4092 free:
4093 destroy_field_var(field_var);
4094 hist_field = NULL;
4095 goto out;
4096}
4097
Tom Zanussi7d18a102019-02-13 17:42:41 -06004098static int trace_action_create(struct hist_trigger_data *hist_data,
4099 struct action_data *data)
Tom Zanussic282a382018-01-15 20:52:00 -06004100{
4101 char *event_name, *param, *system = NULL;
4102 struct hist_field *hist_field, *var_ref;
4103 unsigned int i, var_ref_idx;
4104 unsigned int field_pos = 0;
4105 struct synth_event *event;
Tom Zanussie91eefd72019-02-13 17:42:50 -06004106 char *synth_event_name;
Tom Zanussic282a382018-01-15 20:52:00 -06004107 int ret = 0;
4108
Masami Hiramatsu0e2b81f72018-11-05 18:04:01 +09004109 lockdep_assert_held(&event_mutex);
4110
Tom Zanussie91eefd72019-02-13 17:42:50 -06004111 if (data->use_trace_keyword)
4112 synth_event_name = data->synth_event_name;
4113 else
4114 synth_event_name = data->action_name;
4115
4116 event = find_synth_event(synth_event_name);
Tom Zanussic282a382018-01-15 20:52:00 -06004117 if (!event) {
Tom Zanussie91eefd72019-02-13 17:42:50 -06004118 hist_err("trace action: Couldn't find synthetic event: ", synth_event_name);
Tom Zanussic282a382018-01-15 20:52:00 -06004119 return -EINVAL;
4120 }
Tom Zanussi7d18a102019-02-13 17:42:41 -06004121
Tom Zanussic282a382018-01-15 20:52:00 -06004122 event->ref++;
Tom Zanussic282a382018-01-15 20:52:00 -06004123
4124 var_ref_idx = hist_data->n_var_refs;
4125
4126 for (i = 0; i < data->n_params; i++) {
4127 char *p;
4128
4129 p = param = kstrdup(data->params[i], GFP_KERNEL);
4130 if (!param) {
4131 ret = -ENOMEM;
4132 goto err;
4133 }
4134
4135 system = strsep(&param, ".");
4136 if (!param) {
4137 param = (char *)system;
4138 system = event_name = NULL;
4139 } else {
4140 event_name = strsep(&param, ".");
4141 if (!param) {
4142 kfree(p);
4143 ret = -EINVAL;
4144 goto err;
4145 }
4146 }
4147
4148 if (param[0] == '$')
Tom Zanussi7d18a102019-02-13 17:42:41 -06004149 hist_field = trace_action_find_var(hist_data, data,
4150 system, event_name,
4151 param);
Tom Zanussic282a382018-01-15 20:52:00 -06004152 else
Tom Zanussi7d18a102019-02-13 17:42:41 -06004153 hist_field = trace_action_create_field_var(hist_data,
4154 data,
4155 system,
4156 event_name,
4157 param);
Tom Zanussic282a382018-01-15 20:52:00 -06004158
4159 if (!hist_field) {
4160 kfree(p);
4161 ret = -EINVAL;
4162 goto err;
4163 }
4164
4165 if (check_synth_field(event, hist_field, field_pos) == 0) {
Tom Zanusside40f032018-12-18 14:33:23 -06004166 var_ref = create_var_ref(hist_data, hist_field,
4167 system, event_name);
Tom Zanussic282a382018-01-15 20:52:00 -06004168 if (!var_ref) {
4169 kfree(p);
4170 ret = -ENOMEM;
4171 goto err;
4172 }
4173
Tom Zanussic282a382018-01-15 20:52:00 -06004174 field_pos++;
4175 kfree(p);
4176 continue;
4177 }
4178
Tom Zanussi7d18a102019-02-13 17:42:41 -06004179 hist_err_event("trace action: Param type doesn't match synthetic event field type: ",
Tom Zanussif404da62018-01-15 20:52:05 -06004180 system, event_name, param);
Tom Zanussic282a382018-01-15 20:52:00 -06004181 kfree(p);
4182 ret = -EINVAL;
4183 goto err;
4184 }
4185
4186 if (field_pos != event->n_fields) {
Tom Zanussi7d18a102019-02-13 17:42:41 -06004187 hist_err("trace action: Param count doesn't match synthetic event field count: ", event->name);
Tom Zanussic282a382018-01-15 20:52:00 -06004188 ret = -EINVAL;
4189 goto err;
4190 }
4191
Tom Zanussic3e49502019-02-13 17:42:43 -06004192 data->synth_event = event;
4193 data->var_ref_idx = var_ref_idx;
Tom Zanussic282a382018-01-15 20:52:00 -06004194 out:
4195 return ret;
4196 err:
Tom Zanussic282a382018-01-15 20:52:00 -06004197 event->ref--;
Tom Zanussic282a382018-01-15 20:52:00 -06004198
4199 goto out;
4200}
4201
Tom Zanussi7d18a102019-02-13 17:42:41 -06004202static int action_create(struct hist_trigger_data *hist_data,
4203 struct action_data *data)
4204{
Tom Zanussia3785b72019-02-13 17:42:46 -06004205 struct trace_event_file *file = hist_data->event_file;
4206 struct track_data *track_data;
Tom Zanussi7d18a102019-02-13 17:42:41 -06004207 struct field_var *field_var;
4208 unsigned int i;
4209 char *param;
4210 int ret = 0;
4211
4212 if (data->action == ACTION_TRACE)
4213 return trace_action_create(hist_data, data);
4214
Tom Zanussia3785b72019-02-13 17:42:46 -06004215 if (data->action == ACTION_SNAPSHOT) {
4216 track_data = track_data_alloc(hist_data->key_size, data, hist_data);
4217 if (IS_ERR(track_data)) {
4218 ret = PTR_ERR(track_data);
4219 goto out;
4220 }
4221
4222 ret = tracing_snapshot_cond_enable(file->tr, track_data,
4223 cond_snapshot_update);
4224 if (ret)
4225 track_data_free(track_data);
4226
4227 goto out;
4228 }
4229
Tom Zanussi7d18a102019-02-13 17:42:41 -06004230 if (data->action == ACTION_SAVE) {
4231 if (hist_data->n_save_vars) {
4232 ret = -EEXIST;
4233 hist_err("save action: Can't have more than one save() action per hist", "");
4234 goto out;
4235 }
4236
4237 for (i = 0; i < data->n_params; i++) {
4238 param = kstrdup(data->params[i], GFP_KERNEL);
4239 if (!param) {
4240 ret = -ENOMEM;
4241 goto out;
4242 }
4243
4244 field_var = create_target_field_var(hist_data, NULL, NULL, param);
4245 if (IS_ERR(field_var)) {
4246 hist_err("save action: Couldn't create field variable: ", param);
4247 ret = PTR_ERR(field_var);
4248 kfree(param);
4249 goto out;
4250 }
4251
4252 hist_data->save_vars[hist_data->n_save_vars++] = field_var;
4253 if (field_var->val->flags & HIST_FIELD_FL_STRING)
4254 hist_data->n_save_var_str++;
4255 kfree(param);
4256 }
4257 }
4258 out:
4259 return ret;
4260}
4261
4262static int onmatch_create(struct hist_trigger_data *hist_data,
4263 struct action_data *data)
4264{
4265 return action_create(hist_data, data);
4266}
4267
Tom Zanussic282a382018-01-15 20:52:00 -06004268static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
4269{
4270 char *match_event, *match_event_system;
Tom Zanussic282a382018-01-15 20:52:00 -06004271 struct action_data *data;
4272 int ret = -EINVAL;
4273
4274 data = kzalloc(sizeof(*data), GFP_KERNEL);
4275 if (!data)
4276 return ERR_PTR(-ENOMEM);
4277
4278 match_event = strsep(&str, ")");
Tom Zanussif404da62018-01-15 20:52:05 -06004279 if (!match_event || !str) {
4280 hist_err("onmatch: Missing closing paren: ", match_event);
Tom Zanussic282a382018-01-15 20:52:00 -06004281 goto free;
Tom Zanussif404da62018-01-15 20:52:05 -06004282 }
Tom Zanussic282a382018-01-15 20:52:00 -06004283
4284 match_event_system = strsep(&match_event, ".");
Tom Zanussif404da62018-01-15 20:52:05 -06004285 if (!match_event) {
4286 hist_err("onmatch: Missing subsystem for match event: ", match_event_system);
Tom Zanussic282a382018-01-15 20:52:00 -06004287 goto free;
Tom Zanussif404da62018-01-15 20:52:05 -06004288 }
Tom Zanussic282a382018-01-15 20:52:00 -06004289
Tom Zanussif404da62018-01-15 20:52:05 -06004290 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
4291 hist_err_event("onmatch: Invalid subsystem or event name: ",
4292 match_event_system, match_event, NULL);
Tom Zanussic282a382018-01-15 20:52:00 -06004293 goto free;
Tom Zanussif404da62018-01-15 20:52:05 -06004294 }
Tom Zanussic282a382018-01-15 20:52:00 -06004295
Tom Zanussic3e49502019-02-13 17:42:43 -06004296 data->match_data.event = kstrdup(match_event, GFP_KERNEL);
4297 if (!data->match_data.event) {
Tom Zanussic282a382018-01-15 20:52:00 -06004298 ret = -ENOMEM;
4299 goto free;
4300 }
4301
Tom Zanussic3e49502019-02-13 17:42:43 -06004302 data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
4303 if (!data->match_data.event_system) {
Tom Zanussic282a382018-01-15 20:52:00 -06004304 ret = -ENOMEM;
4305 goto free;
4306 }
4307
Tom Zanussi7d18a102019-02-13 17:42:41 -06004308 ret = action_parse(str, data, HANDLER_ONMATCH);
Tom Zanussic282a382018-01-15 20:52:00 -06004309 if (ret)
4310 goto free;
4311 out:
4312 return data;
4313 free:
4314 onmatch_destroy(data);
4315 data = ERR_PTR(ret);
4316 goto out;
4317}
4318
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004319static int create_hitcount_val(struct hist_trigger_data *hist_data)
4320{
4321 hist_data->fields[HITCOUNT_IDX] =
Tom Zanussi30350d62018-01-15 20:51:49 -06004322 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004323 if (!hist_data->fields[HITCOUNT_IDX])
4324 return -ENOMEM;
4325
4326 hist_data->n_vals++;
Tom Zanussi30350d62018-01-15 20:51:49 -06004327 hist_data->n_fields++;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004328
4329 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
4330 return -EINVAL;
4331
4332 return 0;
4333}
4334
Tom Zanussi30350d62018-01-15 20:51:49 -06004335static int __create_val_field(struct hist_trigger_data *hist_data,
4336 unsigned int val_idx,
4337 struct trace_event_file *file,
4338 char *var_name, char *field_str,
4339 unsigned long flags)
Tom Zanussif2606832016-03-03 12:54:43 -06004340{
Tom Zanussi100719d2018-01-15 20:51:52 -06004341 struct hist_field *hist_field;
Tom Zanussif2606832016-03-03 12:54:43 -06004342 int ret = 0;
4343
Tom Zanussi100719d2018-01-15 20:51:52 -06004344 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
4345 if (IS_ERR(hist_field)) {
4346 ret = PTR_ERR(hist_field);
Tom Zanussif2606832016-03-03 12:54:43 -06004347 goto out;
4348 }
4349
Tom Zanussi100719d2018-01-15 20:51:52 -06004350 hist_data->fields[val_idx] = hist_field;
4351
Tom Zanussif2606832016-03-03 12:54:43 -06004352 ++hist_data->n_vals;
Tom Zanussi30350d62018-01-15 20:51:49 -06004353 ++hist_data->n_fields;
Tom Zanussif2606832016-03-03 12:54:43 -06004354
Tom Zanussi30350d62018-01-15 20:51:49 -06004355 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
Tom Zanussif2606832016-03-03 12:54:43 -06004356 ret = -EINVAL;
4357 out:
4358 return ret;
4359}
4360
Tom Zanussi30350d62018-01-15 20:51:49 -06004361static int create_val_field(struct hist_trigger_data *hist_data,
4362 unsigned int val_idx,
4363 struct trace_event_file *file,
4364 char *field_str)
4365{
4366 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
4367 return -EINVAL;
4368
4369 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
4370}
4371
4372static int create_var_field(struct hist_trigger_data *hist_data,
4373 unsigned int val_idx,
4374 struct trace_event_file *file,
4375 char *var_name, char *expr_str)
4376{
4377 unsigned long flags = 0;
4378
4379 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4380 return -EINVAL;
Tom Zanussif404da62018-01-15 20:52:05 -06004381
Tom Zanussi30350d62018-01-15 20:51:49 -06004382 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
Tom Zanussif404da62018-01-15 20:52:05 -06004383 hist_err("Variable already defined: ", var_name);
Tom Zanussi30350d62018-01-15 20:51:49 -06004384 return -EINVAL;
4385 }
4386
4387 flags |= HIST_FIELD_FL_VAR;
4388 hist_data->n_vars++;
4389 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
4390 return -EINVAL;
4391
4392 return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
4393}
4394
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004395static int create_val_fields(struct hist_trigger_data *hist_data,
4396 struct trace_event_file *file)
4397{
Tom Zanussif2606832016-03-03 12:54:43 -06004398 char *fields_str, *field_str;
Tom Zanussi30350d62018-01-15 20:51:49 -06004399 unsigned int i, j = 1;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004400 int ret;
4401
4402 ret = create_hitcount_val(hist_data);
Tom Zanussif2606832016-03-03 12:54:43 -06004403 if (ret)
4404 goto out;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004405
Tom Zanussif2606832016-03-03 12:54:43 -06004406 fields_str = hist_data->attrs->vals_str;
4407 if (!fields_str)
4408 goto out;
4409
4410 strsep(&fields_str, "=");
4411 if (!fields_str)
4412 goto out;
4413
4414 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
4415 j < TRACING_MAP_VALS_MAX; i++) {
4416 field_str = strsep(&fields_str, ",");
4417 if (!field_str)
4418 break;
Tom Zanussi30350d62018-01-15 20:51:49 -06004419
Tom Zanussif2606832016-03-03 12:54:43 -06004420 if (strcmp(field_str, "hitcount") == 0)
4421 continue;
Tom Zanussi30350d62018-01-15 20:51:49 -06004422
Tom Zanussif2606832016-03-03 12:54:43 -06004423 ret = create_val_field(hist_data, j++, file, field_str);
4424 if (ret)
4425 goto out;
4426 }
Tom Zanussi30350d62018-01-15 20:51:49 -06004427
Tom Zanussif2606832016-03-03 12:54:43 -06004428 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
4429 ret = -EINVAL;
4430 out:
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004431 return ret;
4432}
4433
4434static int create_key_field(struct hist_trigger_data *hist_data,
4435 unsigned int key_idx,
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004436 unsigned int key_offset,
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004437 struct trace_event_file *file,
4438 char *field_str)
4439{
Tom Zanussi30350d62018-01-15 20:51:49 -06004440 struct hist_field *hist_field = NULL;
Tom Zanussi100719d2018-01-15 20:51:52 -06004441
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004442 unsigned long flags = 0;
4443 unsigned int key_size;
4444 int ret = 0;
4445
Tom Zanussi30350d62018-01-15 20:51:49 -06004446 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004447 return -EINVAL;
4448
4449 flags |= HIST_FIELD_FL_KEY;
4450
Tom Zanussi69a02002016-03-03 12:54:52 -06004451 if (strcmp(field_str, "stacktrace") == 0) {
4452 flags |= HIST_FIELD_FL_STACKTRACE;
4453 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
Tom Zanussi30350d62018-01-15 20:51:49 -06004454 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
Tom Zanussi69a02002016-03-03 12:54:52 -06004455 } else {
Tom Zanussi100719d2018-01-15 20:51:52 -06004456 hist_field = parse_expr(hist_data, file, field_str, flags,
4457 NULL, 0);
4458 if (IS_ERR(hist_field)) {
4459 ret = PTR_ERR(hist_field);
4460 goto out;
Tom Zanussi69a02002016-03-03 12:54:52 -06004461 }
4462
Tom Zanussi067fe032018-01-15 20:51:56 -06004463 if (hist_field->flags & HIST_FIELD_FL_VAR_REF) {
Tom Zanussif404da62018-01-15 20:52:05 -06004464 hist_err("Using variable references as keys not supported: ", field_str);
Tom Zanussi067fe032018-01-15 20:51:56 -06004465 destroy_hist_field(hist_field, 0);
4466 ret = -EINVAL;
4467 goto out;
4468 }
4469
Tom Zanussi100719d2018-01-15 20:51:52 -06004470 key_size = hist_field->size;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004471 }
4472
Tom Zanussi100719d2018-01-15 20:51:52 -06004473 hist_data->fields[key_idx] = hist_field;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004474
4475 key_size = ALIGN(key_size, sizeof(u64));
4476 hist_data->fields[key_idx]->size = key_size;
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004477 hist_data->fields[key_idx]->offset = key_offset;
Tom Zanussi100719d2018-01-15 20:51:52 -06004478
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004479 hist_data->key_size += key_size;
Tom Zanussi100719d2018-01-15 20:51:52 -06004480
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004481 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
4482 ret = -EINVAL;
4483 goto out;
4484 }
4485
4486 hist_data->n_keys++;
Tom Zanussi30350d62018-01-15 20:51:49 -06004487 hist_data->n_fields++;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004488
4489 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
4490 return -EINVAL;
4491
4492 ret = key_size;
4493 out:
4494 return ret;
4495}
4496
4497static int create_key_fields(struct hist_trigger_data *hist_data,
4498 struct trace_event_file *file)
4499{
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004500 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004501 char *fields_str, *field_str;
4502 int ret = -EINVAL;
4503
4504 fields_str = hist_data->attrs->keys_str;
4505 if (!fields_str)
4506 goto out;
4507
4508 strsep(&fields_str, "=");
4509 if (!fields_str)
4510 goto out;
4511
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004512 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004513 field_str = strsep(&fields_str, ",");
4514 if (!field_str)
4515 break;
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004516 ret = create_key_field(hist_data, i, key_offset,
4517 file, field_str);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004518 if (ret < 0)
4519 goto out;
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06004520 key_offset += ret;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004521 }
4522 if (fields_str) {
4523 ret = -EINVAL;
4524 goto out;
4525 }
4526 ret = 0;
4527 out:
4528 return ret;
4529}
4530
Tom Zanussi30350d62018-01-15 20:51:49 -06004531static int create_var_fields(struct hist_trigger_data *hist_data,
4532 struct trace_event_file *file)
4533{
4534 unsigned int i, j = hist_data->n_vals;
4535 int ret = 0;
4536
4537 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
4538
4539 for (i = 0; i < n_vars; i++) {
4540 char *var_name = hist_data->attrs->var_defs.name[i];
4541 char *expr = hist_data->attrs->var_defs.expr[i];
4542
4543 ret = create_var_field(hist_data, j++, file, var_name, expr);
4544 if (ret)
4545 goto out;
4546 }
4547 out:
4548 return ret;
4549}
4550
4551static void free_var_defs(struct hist_trigger_data *hist_data)
4552{
4553 unsigned int i;
4554
4555 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
4556 kfree(hist_data->attrs->var_defs.name[i]);
4557 kfree(hist_data->attrs->var_defs.expr[i]);
4558 }
4559
4560 hist_data->attrs->var_defs.n_vars = 0;
4561}
4562
4563static int parse_var_defs(struct hist_trigger_data *hist_data)
4564{
4565 char *s, *str, *var_name, *field_str;
4566 unsigned int i, j, n_vars = 0;
4567 int ret = 0;
4568
4569 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
4570 str = hist_data->attrs->assignment_str[i];
4571 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
4572 field_str = strsep(&str, ",");
4573 if (!field_str)
4574 break;
4575
4576 var_name = strsep(&field_str, "=");
4577 if (!var_name || !field_str) {
Tom Zanussif404da62018-01-15 20:52:05 -06004578 hist_err("Malformed assignment: ", var_name);
Tom Zanussi30350d62018-01-15 20:51:49 -06004579 ret = -EINVAL;
4580 goto free;
4581 }
4582
4583 if (n_vars == TRACING_MAP_VARS_MAX) {
Tom Zanussif404da62018-01-15 20:52:05 -06004584 hist_err("Too many variables defined: ", var_name);
Tom Zanussi30350d62018-01-15 20:51:49 -06004585 ret = -EINVAL;
4586 goto free;
4587 }
4588
4589 s = kstrdup(var_name, GFP_KERNEL);
4590 if (!s) {
4591 ret = -ENOMEM;
4592 goto free;
4593 }
4594 hist_data->attrs->var_defs.name[n_vars] = s;
4595
4596 s = kstrdup(field_str, GFP_KERNEL);
4597 if (!s) {
4598 kfree(hist_data->attrs->var_defs.name[n_vars]);
4599 ret = -ENOMEM;
4600 goto free;
4601 }
4602 hist_data->attrs->var_defs.expr[n_vars++] = s;
4603
4604 hist_data->attrs->var_defs.n_vars = n_vars;
4605 }
4606 }
4607
4608 return ret;
4609 free:
4610 free_var_defs(hist_data);
4611
4612 return ret;
4613}
4614
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004615static int create_hist_fields(struct hist_trigger_data *hist_data,
4616 struct trace_event_file *file)
4617{
4618 int ret;
4619
Tom Zanussi30350d62018-01-15 20:51:49 -06004620 ret = parse_var_defs(hist_data);
4621 if (ret)
4622 goto out;
4623
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004624 ret = create_val_fields(hist_data, file);
4625 if (ret)
4626 goto out;
4627
Tom Zanussi30350d62018-01-15 20:51:49 -06004628 ret = create_var_fields(hist_data, file);
4629 if (ret)
4630 goto out;
4631
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004632 ret = create_key_fields(hist_data, file);
4633 if (ret)
4634 goto out;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004635 out:
Tom Zanussi30350d62018-01-15 20:51:49 -06004636 free_var_defs(hist_data);
4637
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004638 return ret;
4639}
4640
Tom Zanussie62347d2016-03-03 12:54:45 -06004641static int is_descending(const char *str)
4642{
4643 if (!str)
4644 return 0;
4645
4646 if (strcmp(str, "descending") == 0)
4647 return 1;
4648
4649 if (strcmp(str, "ascending") == 0)
4650 return 0;
4651
4652 return -EINVAL;
4653}
4654
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004655static int create_sort_keys(struct hist_trigger_data *hist_data)
4656{
Tom Zanussie62347d2016-03-03 12:54:45 -06004657 char *fields_str = hist_data->attrs->sort_key_str;
Tom Zanussie62347d2016-03-03 12:54:45 -06004658 struct tracing_map_sort_key *sort_key;
4659 int descending, ret = 0;
Tom Zanussi30350d62018-01-15 20:51:49 -06004660 unsigned int i, j, k;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004661
Tom Zanussie62347d2016-03-03 12:54:45 -06004662 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004663
Tom Zanussie62347d2016-03-03 12:54:45 -06004664 if (!fields_str)
4665 goto out;
4666
4667 strsep(&fields_str, "=");
4668 if (!fields_str) {
4669 ret = -EINVAL;
4670 goto out;
4671 }
4672
4673 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
Tom Zanussi85013252017-09-22 14:58:22 -05004674 struct hist_field *hist_field;
Tom Zanussie62347d2016-03-03 12:54:45 -06004675 char *field_str, *field_name;
Tom Zanussi85013252017-09-22 14:58:22 -05004676 const char *test_name;
Tom Zanussie62347d2016-03-03 12:54:45 -06004677
4678 sort_key = &hist_data->sort_keys[i];
4679
4680 field_str = strsep(&fields_str, ",");
4681 if (!field_str) {
4682 if (i == 0)
4683 ret = -EINVAL;
4684 break;
4685 }
4686
4687 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4688 ret = -EINVAL;
4689 break;
4690 }
4691
4692 field_name = strsep(&field_str, ".");
4693 if (!field_name) {
4694 ret = -EINVAL;
4695 break;
4696 }
4697
4698 if (strcmp(field_name, "hitcount") == 0) {
4699 descending = is_descending(field_str);
4700 if (descending < 0) {
4701 ret = descending;
4702 break;
4703 }
4704 sort_key->descending = descending;
4705 continue;
4706 }
4707
Tom Zanussi30350d62018-01-15 20:51:49 -06004708 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4709 unsigned int idx;
4710
Tom Zanussi85013252017-09-22 14:58:22 -05004711 hist_field = hist_data->fields[j];
Tom Zanussi30350d62018-01-15 20:51:49 -06004712 if (hist_field->flags & HIST_FIELD_FL_VAR)
4713 continue;
4714
4715 idx = k++;
4716
Tom Zanussi85013252017-09-22 14:58:22 -05004717 test_name = hist_field_name(hist_field, 0);
4718
4719 if (strcmp(field_name, test_name) == 0) {
Tom Zanussi30350d62018-01-15 20:51:49 -06004720 sort_key->field_idx = idx;
Tom Zanussie62347d2016-03-03 12:54:45 -06004721 descending = is_descending(field_str);
4722 if (descending < 0) {
4723 ret = descending;
4724 goto out;
4725 }
4726 sort_key->descending = descending;
4727 break;
4728 }
4729 }
4730 if (j == hist_data->n_fields) {
4731 ret = -EINVAL;
4732 break;
4733 }
4734 }
Tom Zanussi30350d62018-01-15 20:51:49 -06004735
Tom Zanussie62347d2016-03-03 12:54:45 -06004736 hist_data->n_sort_keys = i;
4737 out:
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004738 return ret;
4739}
4740
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004741static void destroy_actions(struct hist_trigger_data *hist_data)
4742{
4743 unsigned int i;
4744
4745 for (i = 0; i < hist_data->n_actions; i++) {
4746 struct action_data *data = hist_data->actions[i];
4747
Tom Zanussi7d18a102019-02-13 17:42:41 -06004748 if (data->handler == HANDLER_ONMATCH)
Tom Zanussic282a382018-01-15 20:52:00 -06004749 onmatch_destroy(data);
Tom Zanussidff81f52019-02-13 17:42:48 -06004750 else if (data->handler == HANDLER_ONMAX ||
4751 data->handler == HANDLER_ONCHANGE)
Tom Zanussi466f4522019-02-13 17:42:44 -06004752 track_data_destroy(hist_data, data);
Tom Zanussic282a382018-01-15 20:52:00 -06004753 else
4754 kfree(data);
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004755 }
4756}
4757
4758static int parse_actions(struct hist_trigger_data *hist_data)
4759{
Tom Zanussic282a382018-01-15 20:52:00 -06004760 struct trace_array *tr = hist_data->event_file->tr;
4761 struct action_data *data;
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004762 unsigned int i;
4763 int ret = 0;
4764 char *str;
Steven Rostedt (VMware)036876f2018-12-21 18:40:46 -05004765 int len;
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004766
4767 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4768 str = hist_data->attrs->action_str[i];
Tom Zanussic282a382018-01-15 20:52:00 -06004769
Steven Rostedt (VMware)036876f2018-12-21 18:40:46 -05004770 if ((len = str_has_prefix(str, "onmatch("))) {
4771 char *action_str = str + len;
Tom Zanussic282a382018-01-15 20:52:00 -06004772
4773 data = onmatch_parse(tr, action_str);
4774 if (IS_ERR(data)) {
4775 ret = PTR_ERR(data);
4776 break;
4777 }
Steven Rostedt (VMware)036876f2018-12-21 18:40:46 -05004778 } else if ((len = str_has_prefix(str, "onmax("))) {
4779 char *action_str = str + len;
Tom Zanussi50450602018-01-15 20:52:01 -06004780
Tom Zanussi466f4522019-02-13 17:42:44 -06004781 data = track_data_parse(hist_data, action_str,
4782 HANDLER_ONMAX);
Tom Zanussi50450602018-01-15 20:52:01 -06004783 if (IS_ERR(data)) {
4784 ret = PTR_ERR(data);
4785 break;
4786 }
Tom Zanussidff81f52019-02-13 17:42:48 -06004787 } else if ((len = str_has_prefix(str, "onchange("))) {
4788 char *action_str = str + len;
4789
4790 data = track_data_parse(hist_data, action_str,
4791 HANDLER_ONCHANGE);
4792 if (IS_ERR(data)) {
4793 ret = PTR_ERR(data);
4794 break;
4795 }
Tom Zanussic282a382018-01-15 20:52:00 -06004796 } else {
4797 ret = -EINVAL;
4798 break;
4799 }
4800
4801 hist_data->actions[hist_data->n_actions++] = data;
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004802 }
4803
4804 return ret;
4805}
4806
Tom Zanussi7d18a102019-02-13 17:42:41 -06004807static int create_actions(struct hist_trigger_data *hist_data)
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004808{
4809 struct action_data *data;
4810 unsigned int i;
4811 int ret = 0;
4812
4813 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4814 data = hist_data->actions[i];
Tom Zanussic282a382018-01-15 20:52:00 -06004815
Tom Zanussi7d18a102019-02-13 17:42:41 -06004816 if (data->handler == HANDLER_ONMATCH) {
4817 ret = onmatch_create(hist_data, data);
Tom Zanussic282a382018-01-15 20:52:00 -06004818 if (ret)
Tom Zanussi7d18a102019-02-13 17:42:41 -06004819 break;
Tom Zanussidff81f52019-02-13 17:42:48 -06004820 } else if (data->handler == HANDLER_ONMAX ||
4821 data->handler == HANDLER_ONCHANGE) {
Tom Zanussi466f4522019-02-13 17:42:44 -06004822 ret = track_data_create(hist_data, data);
Tom Zanussi50450602018-01-15 20:52:01 -06004823 if (ret)
Tom Zanussi7d18a102019-02-13 17:42:41 -06004824 break;
4825 } else {
4826 ret = -EINVAL;
4827 break;
Tom Zanussic282a382018-01-15 20:52:00 -06004828 }
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004829 }
4830
4831 return ret;
4832}
4833
Tom Zanussi50450602018-01-15 20:52:01 -06004834static void print_actions(struct seq_file *m,
4835 struct hist_trigger_data *hist_data,
4836 struct tracing_map_elt *elt)
4837{
4838 unsigned int i;
4839
4840 for (i = 0; i < hist_data->n_actions; i++) {
4841 struct action_data *data = hist_data->actions[i];
4842
Tom Zanussia3785b72019-02-13 17:42:46 -06004843 if (data->action == ACTION_SNAPSHOT)
4844 continue;
4845
Tom Zanussidff81f52019-02-13 17:42:48 -06004846 if (data->handler == HANDLER_ONMAX ||
4847 data->handler == HANDLER_ONCHANGE)
Tom Zanussi466f4522019-02-13 17:42:44 -06004848 track_data_print(m, hist_data, elt, data);
Tom Zanussi50450602018-01-15 20:52:01 -06004849 }
4850}
4851
Tom Zanussi7d18a102019-02-13 17:42:41 -06004852static void print_action_spec(struct seq_file *m,
4853 struct hist_trigger_data *hist_data,
4854 struct action_data *data)
4855{
4856 unsigned int i;
4857
4858 if (data->action == ACTION_SAVE) {
4859 for (i = 0; i < hist_data->n_save_vars; i++) {
4860 seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
4861 if (i < hist_data->n_save_vars - 1)
4862 seq_puts(m, ",");
4863 }
4864 } else if (data->action == ACTION_TRACE) {
Tom Zanussie91eefd72019-02-13 17:42:50 -06004865 if (data->use_trace_keyword)
4866 seq_printf(m, "%s", data->synth_event_name);
Tom Zanussi7d18a102019-02-13 17:42:41 -06004867 for (i = 0; i < data->n_params; i++) {
Tom Zanussie91eefd72019-02-13 17:42:50 -06004868 if (i || data->use_trace_keyword)
Tom Zanussi7d18a102019-02-13 17:42:41 -06004869 seq_puts(m, ",");
4870 seq_printf(m, "%s", data->params[i]);
4871 }
4872 }
4873}
4874
Tom Zanussi466f4522019-02-13 17:42:44 -06004875static void print_track_data_spec(struct seq_file *m,
4876 struct hist_trigger_data *hist_data,
4877 struct action_data *data)
Tom Zanussi50450602018-01-15 20:52:01 -06004878{
Tom Zanussi466f4522019-02-13 17:42:44 -06004879 if (data->handler == HANDLER_ONMAX)
4880 seq_puts(m, ":onmax(");
Tom Zanussidff81f52019-02-13 17:42:48 -06004881 else if (data->handler == HANDLER_ONCHANGE)
4882 seq_puts(m, ":onchange(");
Tom Zanussi466f4522019-02-13 17:42:44 -06004883 seq_printf(m, "%s", data->track_data.var_str);
Tom Zanussi7d18a102019-02-13 17:42:41 -06004884 seq_printf(m, ").%s(", data->action_name);
Tom Zanussi50450602018-01-15 20:52:01 -06004885
Tom Zanussi7d18a102019-02-13 17:42:41 -06004886 print_action_spec(m, hist_data, data);
4887
Tom Zanussi50450602018-01-15 20:52:01 -06004888 seq_puts(m, ")");
4889}
4890
Tom Zanussic282a382018-01-15 20:52:00 -06004891static void print_onmatch_spec(struct seq_file *m,
4892 struct hist_trigger_data *hist_data,
4893 struct action_data *data)
4894{
Tom Zanussic3e49502019-02-13 17:42:43 -06004895 seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
4896 data->match_data.event);
Tom Zanussic282a382018-01-15 20:52:00 -06004897
Tom Zanussi7d18a102019-02-13 17:42:41 -06004898 seq_printf(m, "%s(", data->action_name);
Tom Zanussic282a382018-01-15 20:52:00 -06004899
Tom Zanussi7d18a102019-02-13 17:42:41 -06004900 print_action_spec(m, hist_data, data);
Tom Zanussic282a382018-01-15 20:52:00 -06004901
4902 seq_puts(m, ")");
4903}
4904
Tom Zanussi48f79472018-03-28 15:10:55 -05004905static bool actions_match(struct hist_trigger_data *hist_data,
4906 struct hist_trigger_data *hist_data_test)
4907{
4908 unsigned int i, j;
4909
4910 if (hist_data->n_actions != hist_data_test->n_actions)
4911 return false;
4912
4913 for (i = 0; i < hist_data->n_actions; i++) {
4914 struct action_data *data = hist_data->actions[i];
4915 struct action_data *data_test = hist_data_test->actions[i];
Tom Zanussie91eefd72019-02-13 17:42:50 -06004916 char *action_name, *action_name_test;
Tom Zanussi48f79472018-03-28 15:10:55 -05004917
Tom Zanussi7d18a102019-02-13 17:42:41 -06004918 if (data->handler != data_test->handler)
4919 return false;
4920 if (data->action != data_test->action)
Tom Zanussi48f79472018-03-28 15:10:55 -05004921 return false;
4922
4923 if (data->n_params != data_test->n_params)
4924 return false;
4925
4926 for (j = 0; j < data->n_params; j++) {
4927 if (strcmp(data->params[j], data_test->params[j]) != 0)
4928 return false;
4929 }
4930
Tom Zanussie91eefd72019-02-13 17:42:50 -06004931 if (data->use_trace_keyword)
4932 action_name = data->synth_event_name;
4933 else
4934 action_name = data->action_name;
4935
4936 if (data_test->use_trace_keyword)
4937 action_name_test = data_test->synth_event_name;
4938 else
4939 action_name_test = data_test->action_name;
4940
4941 if (strcmp(action_name, action_name_test) != 0)
Tom Zanussi7d18a102019-02-13 17:42:41 -06004942 return false;
4943
4944 if (data->handler == HANDLER_ONMATCH) {
Tom Zanussic3e49502019-02-13 17:42:43 -06004945 if (strcmp(data->match_data.event_system,
4946 data_test->match_data.event_system) != 0)
Tom Zanussi48f79472018-03-28 15:10:55 -05004947 return false;
Tom Zanussic3e49502019-02-13 17:42:43 -06004948 if (strcmp(data->match_data.event,
4949 data_test->match_data.event) != 0)
Tom Zanussi48f79472018-03-28 15:10:55 -05004950 return false;
Tom Zanussidff81f52019-02-13 17:42:48 -06004951 } else if (data->handler == HANDLER_ONMAX ||
4952 data->handler == HANDLER_ONCHANGE) {
Tom Zanussi466f4522019-02-13 17:42:44 -06004953 if (strcmp(data->track_data.var_str,
4954 data_test->track_data.var_str) != 0)
Tom Zanussi48f79472018-03-28 15:10:55 -05004955 return false;
Tom Zanussi48f79472018-03-28 15:10:55 -05004956 }
4957 }
4958
4959 return true;
4960}
4961
4962
Tom Zanussic282a382018-01-15 20:52:00 -06004963static void print_actions_spec(struct seq_file *m,
4964 struct hist_trigger_data *hist_data)
4965{
4966 unsigned int i;
4967
4968 for (i = 0; i < hist_data->n_actions; i++) {
4969 struct action_data *data = hist_data->actions[i];
4970
Tom Zanussi7d18a102019-02-13 17:42:41 -06004971 if (data->handler == HANDLER_ONMATCH)
Tom Zanussic282a382018-01-15 20:52:00 -06004972 print_onmatch_spec(m, hist_data, data);
Tom Zanussidff81f52019-02-13 17:42:48 -06004973 else if (data->handler == HANDLER_ONMAX ||
4974 data->handler == HANDLER_ONCHANGE)
Tom Zanussi466f4522019-02-13 17:42:44 -06004975 print_track_data_spec(m, hist_data, data);
Tom Zanussic282a382018-01-15 20:52:00 -06004976 }
4977}
4978
Tom Zanussi02205a62018-01-15 20:51:59 -06004979static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
4980{
4981 unsigned int i;
4982
4983 for (i = 0; i < hist_data->n_field_var_hists; i++) {
4984 kfree(hist_data->field_var_hists[i]->cmd);
4985 kfree(hist_data->field_var_hists[i]);
4986 }
4987}
4988
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004989static void destroy_hist_data(struct hist_trigger_data *hist_data)
4990{
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004991 if (!hist_data)
4992 return;
4993
Tom Zanussi7ef224d2016-03-03 12:54:42 -06004994 destroy_hist_trigger_attrs(hist_data->attrs);
4995 destroy_hist_fields(hist_data);
4996 tracing_map_destroy(hist_data->map);
Tom Zanussi0212e2a2018-01-15 20:51:57 -06004997
4998 destroy_actions(hist_data);
Tom Zanussi02205a62018-01-15 20:51:59 -06004999 destroy_field_vars(hist_data);
5000 destroy_field_var_hists(hist_data);
Tom Zanussi0212e2a2018-01-15 20:51:57 -06005001
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005002 kfree(hist_data);
5003}
5004
5005static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
5006{
5007 struct tracing_map *map = hist_data->map;
5008 struct ftrace_event_field *field;
5009 struct hist_field *hist_field;
Dan Carpenterb28d7b22018-03-28 14:48:15 +03005010 int i, idx = 0;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005011
5012 for_each_hist_field(i, hist_data) {
5013 hist_field = hist_data->fields[i];
5014 if (hist_field->flags & HIST_FIELD_FL_KEY) {
5015 tracing_map_cmp_fn_t cmp_fn;
5016
5017 field = hist_field->field;
5018
Tom Zanussi69a02002016-03-03 12:54:52 -06005019 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
5020 cmp_fn = tracing_map_cmp_none;
Tom Zanussiad42feb2018-01-15 20:51:45 -06005021 else if (!field)
5022 cmp_fn = tracing_map_cmp_num(hist_field->size,
5023 hist_field->is_signed);
Tom Zanussi69a02002016-03-03 12:54:52 -06005024 else if (is_string_field(field))
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005025 cmp_fn = tracing_map_cmp_string;
5026 else
5027 cmp_fn = tracing_map_cmp_num(field->size,
5028 field->is_signed);
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06005029 idx = tracing_map_add_key_field(map,
5030 hist_field->offset,
5031 cmp_fn);
Tom Zanussi30350d62018-01-15 20:51:49 -06005032 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005033 idx = tracing_map_add_sum_field(map);
5034
5035 if (idx < 0)
5036 return idx;
Tom Zanussi30350d62018-01-15 20:51:49 -06005037
5038 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5039 idx = tracing_map_add_var(map);
5040 if (idx < 0)
5041 return idx;
5042 hist_field->var.idx = idx;
5043 hist_field->var.hist_data = hist_data;
5044 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005045 }
5046
5047 return 0;
5048}
5049
5050static struct hist_trigger_data *
5051create_hist_data(unsigned int map_bits,
5052 struct hist_trigger_attrs *attrs,
Tom Zanussi30350d62018-01-15 20:51:49 -06005053 struct trace_event_file *file,
5054 bool remove)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005055{
Tom Zanussi6b4827a2016-03-03 12:54:50 -06005056 const struct tracing_map_ops *map_ops = NULL;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005057 struct hist_trigger_data *hist_data;
5058 int ret = 0;
5059
5060 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
5061 if (!hist_data)
5062 return ERR_PTR(-ENOMEM);
5063
5064 hist_data->attrs = attrs;
Tom Zanussi30350d62018-01-15 20:51:49 -06005065 hist_data->remove = remove;
Tom Zanussi067fe032018-01-15 20:51:56 -06005066 hist_data->event_file = file;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005067
Tom Zanussi0212e2a2018-01-15 20:51:57 -06005068 ret = parse_actions(hist_data);
5069 if (ret)
5070 goto free;
5071
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005072 ret = create_hist_fields(hist_data, file);
5073 if (ret)
5074 goto free;
5075
5076 ret = create_sort_keys(hist_data);
5077 if (ret)
5078 goto free;
5079
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06005080 map_ops = &hist_trigger_elt_data_ops;
Tom Zanussi6b4827a2016-03-03 12:54:50 -06005081
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005082 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
Tom Zanussi6b4827a2016-03-03 12:54:50 -06005083 map_ops, hist_data);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005084 if (IS_ERR(hist_data->map)) {
5085 ret = PTR_ERR(hist_data->map);
5086 hist_data->map = NULL;
5087 goto free;
5088 }
5089
5090 ret = create_tracing_map_fields(hist_data);
5091 if (ret)
5092 goto free;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005093 out:
5094 return hist_data;
5095 free:
5096 hist_data->attrs = NULL;
5097
5098 destroy_hist_data(hist_data);
5099
5100 hist_data = ERR_PTR(ret);
5101
5102 goto out;
5103}
5104
5105static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
Tom Zanussifbd302c2018-01-15 20:51:43 -06005106 struct tracing_map_elt *elt, void *rec,
Tom Zanussi067fe032018-01-15 20:51:56 -06005107 struct ring_buffer_event *rbe,
5108 u64 *var_ref_vals)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005109{
Tom Zanussi067fe032018-01-15 20:51:56 -06005110 struct hist_elt_data *elt_data;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005111 struct hist_field *hist_field;
Tom Zanussi30350d62018-01-15 20:51:49 -06005112 unsigned int i, var_idx;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005113 u64 hist_val;
5114
Tom Zanussi067fe032018-01-15 20:51:56 -06005115 elt_data = elt->private_data;
5116 elt_data->var_ref_vals = var_ref_vals;
5117
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005118 for_each_hist_val_field(i, hist_data) {
5119 hist_field = hist_data->fields[i];
Tom Zanussidf35d932018-01-15 20:51:54 -06005120 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
Tom Zanussi30350d62018-01-15 20:51:49 -06005121 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5122 var_idx = hist_field->var.idx;
5123 tracing_map_set_var(elt, var_idx, hist_val);
5124 continue;
5125 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005126 tracing_map_update_sum(elt, i, hist_val);
5127 }
Tom Zanussi30350d62018-01-15 20:51:49 -06005128
5129 for_each_hist_key_field(i, hist_data) {
5130 hist_field = hist_data->fields[i];
5131 if (hist_field->flags & HIST_FIELD_FL_VAR) {
Tom Zanussidf35d932018-01-15 20:51:54 -06005132 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
Tom Zanussi30350d62018-01-15 20:51:49 -06005133 var_idx = hist_field->var.idx;
5134 tracing_map_set_var(elt, var_idx, hist_val);
5135 }
5136 }
Tom Zanussi02205a62018-01-15 20:51:59 -06005137
5138 update_field_vars(hist_data, elt, rbe, rec);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005139}
5140
Tom Zanussi6a475cb2016-03-03 12:54:54 -06005141static inline void add_to_key(char *compound_key, void *key,
5142 struct hist_field *key_field, void *rec)
5143{
5144 size_t size = key_field->size;
5145
5146 if (key_field->flags & HIST_FIELD_FL_STRING) {
5147 struct ftrace_event_field *field;
5148
5149 field = key_field->field;
5150 if (field->filter_type == FILTER_DYN_STRING)
5151 size = *(u32 *)(rec + field->offset) >> 16;
5152 else if (field->filter_type == FILTER_PTR_STRING)
5153 size = strlen(key);
5154 else if (field->filter_type == FILTER_STATIC_STRING)
5155 size = field->size;
5156
5157 /* ensure NULL-termination */
5158 if (size > key_field->size - 1)
5159 size = key_field->size - 1;
Tom Zanussi6a475cb2016-03-03 12:54:54 -06005160
Tom Zanussi9f0bbf32019-02-04 15:07:24 -06005161 strncpy(compound_key + key_field->offset, (char *)key, size);
5162 } else
5163 memcpy(compound_key + key_field->offset, key, size);
Tom Zanussi6a475cb2016-03-03 12:54:54 -06005164}
5165
Tom Zanussi0212e2a2018-01-15 20:51:57 -06005166static void
5167hist_trigger_actions(struct hist_trigger_data *hist_data,
5168 struct tracing_map_elt *elt, void *rec,
Tom Zanussi7d18a102019-02-13 17:42:41 -06005169 struct ring_buffer_event *rbe, void *key,
5170 u64 *var_ref_vals)
Tom Zanussi0212e2a2018-01-15 20:51:57 -06005171{
5172 struct action_data *data;
5173 unsigned int i;
5174
5175 for (i = 0; i < hist_data->n_actions; i++) {
5176 data = hist_data->actions[i];
Tom Zanussi7d18a102019-02-13 17:42:41 -06005177 data->fn(hist_data, elt, rec, rbe, key, data, var_ref_vals);
Tom Zanussi0212e2a2018-01-15 20:51:57 -06005178 }
5179}
5180
Tom Zanussi1ac4f512018-01-15 20:51:42 -06005181static void event_hist_trigger(struct event_trigger_data *data, void *rec,
Tom Zanussifbd302c2018-01-15 20:51:43 -06005182 struct ring_buffer_event *rbe)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005183{
5184 struct hist_trigger_data *hist_data = data->private_data;
Tom Zanussi6a475cb2016-03-03 12:54:54 -06005185 bool use_compound_key = (hist_data->n_keys > 1);
Tom Zanussi69a02002016-03-03 12:54:52 -06005186 unsigned long entries[HIST_STACKTRACE_DEPTH];
Tom Zanussi067fe032018-01-15 20:51:56 -06005187 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06005188 char compound_key[HIST_KEY_SIZE_MAX];
Tom Zanussidf35d932018-01-15 20:51:54 -06005189 struct tracing_map_elt *elt = NULL;
Tom Zanussi69a02002016-03-03 12:54:52 -06005190 struct stack_trace stacktrace;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005191 struct hist_field *key_field;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005192 u64 field_contents;
5193 void *key = NULL;
5194 unsigned int i;
5195
Tom Zanussi6a475cb2016-03-03 12:54:54 -06005196 memset(compound_key, 0, hist_data->key_size);
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06005197
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005198 for_each_hist_key_field(i, hist_data) {
5199 key_field = hist_data->fields[i];
5200
Tom Zanussi69a02002016-03-03 12:54:52 -06005201 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5202 stacktrace.max_entries = HIST_STACKTRACE_DEPTH;
5203 stacktrace.entries = entries;
5204 stacktrace.nr_entries = 0;
5205 stacktrace.skip = HIST_STACKTRACE_SKIP;
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06005206
Tom Zanussi69a02002016-03-03 12:54:52 -06005207 memset(stacktrace.entries, 0, HIST_STACKTRACE_SIZE);
5208 save_stack_trace(&stacktrace);
5209
5210 key = entries;
5211 } else {
Tom Zanussidf35d932018-01-15 20:51:54 -06005212 field_contents = key_field->fn(key_field, elt, rbe, rec);
Tom Zanussi6a475cb2016-03-03 12:54:54 -06005213 if (key_field->flags & HIST_FIELD_FL_STRING) {
Tom Zanussi69a02002016-03-03 12:54:52 -06005214 key = (void *)(unsigned long)field_contents;
Tom Zanussi6a475cb2016-03-03 12:54:54 -06005215 use_compound_key = true;
5216 } else
Tom Zanussi69a02002016-03-03 12:54:52 -06005217 key = (void *)&field_contents;
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06005218 }
Tom Zanussi6a475cb2016-03-03 12:54:54 -06005219
5220 if (use_compound_key)
5221 add_to_key(compound_key, key, key_field, rec);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005222 }
5223
Tom Zanussi6a475cb2016-03-03 12:54:54 -06005224 if (use_compound_key)
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06005225 key = compound_key;
5226
Tom Zanussi067fe032018-01-15 20:51:56 -06005227 if (hist_data->n_var_refs &&
5228 !resolve_var_refs(hist_data, key, var_ref_vals, false))
5229 return;
5230
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005231 elt = tracing_map_insert(hist_data->map, key);
Tom Zanussi067fe032018-01-15 20:51:56 -06005232 if (!elt)
5233 return;
5234
5235 hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals);
Tom Zanussi0212e2a2018-01-15 20:51:57 -06005236
5237 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
Tom Zanussi7d18a102019-02-13 17:42:41 -06005238 hist_trigger_actions(hist_data, elt, rec, rbe, key, var_ref_vals);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005239}
5240
Tom Zanussi69a02002016-03-03 12:54:52 -06005241static void hist_trigger_stacktrace_print(struct seq_file *m,
5242 unsigned long *stacktrace_entries,
5243 unsigned int max_entries)
5244{
5245 char str[KSYM_SYMBOL_LEN];
5246 unsigned int spaces = 8;
5247 unsigned int i;
5248
5249 for (i = 0; i < max_entries; i++) {
5250 if (stacktrace_entries[i] == ULONG_MAX)
5251 return;
5252
5253 seq_printf(m, "%*c", 1 + spaces, ' ');
5254 sprint_symbol(str, stacktrace_entries[i]);
5255 seq_printf(m, "%s\n", str);
5256 }
5257}
5258
Tom Zanussia3785b72019-02-13 17:42:46 -06005259static void hist_trigger_print_key(struct seq_file *m,
5260 struct hist_trigger_data *hist_data,
5261 void *key,
5262 struct tracing_map_elt *elt)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005263{
5264 struct hist_field *key_field;
Tom Zanussic6afad42016-03-03 12:54:49 -06005265 char str[KSYM_SYMBOL_LEN];
Tom Zanussi69a02002016-03-03 12:54:52 -06005266 bool multiline = false;
Tom Zanussi85013252017-09-22 14:58:22 -05005267 const char *field_name;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005268 unsigned int i;
5269 u64 uval;
5270
5271 seq_puts(m, "{ ");
5272
5273 for_each_hist_key_field(i, hist_data) {
5274 key_field = hist_data->fields[i];
5275
5276 if (i > hist_data->n_vals)
5277 seq_puts(m, ", ");
5278
Tom Zanussi85013252017-09-22 14:58:22 -05005279 field_name = hist_field_name(key_field, 0);
5280
Tom Zanussi0c4a6b42016-03-03 12:54:48 -06005281 if (key_field->flags & HIST_FIELD_FL_HEX) {
5282 uval = *(u64 *)(key + key_field->offset);
Tom Zanussi85013252017-09-22 14:58:22 -05005283 seq_printf(m, "%s: %llx", field_name, uval);
Tom Zanussic6afad42016-03-03 12:54:49 -06005284 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
5285 uval = *(u64 *)(key + key_field->offset);
5286 sprint_symbol_no_offset(str, uval);
Tom Zanussi85013252017-09-22 14:58:22 -05005287 seq_printf(m, "%s: [%llx] %-45s", field_name,
5288 uval, str);
Tom Zanussic6afad42016-03-03 12:54:49 -06005289 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
5290 uval = *(u64 *)(key + key_field->offset);
5291 sprint_symbol(str, uval);
Tom Zanussi85013252017-09-22 14:58:22 -05005292 seq_printf(m, "%s: [%llx] %-55s", field_name,
5293 uval, str);
Tom Zanussi6b4827a2016-03-03 12:54:50 -06005294 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
Tom Zanussiaf6a29b2018-01-15 20:51:53 -06005295 struct hist_elt_data *elt_data = elt->private_data;
5296 char *comm;
5297
5298 if (WARN_ON_ONCE(!elt_data))
5299 return;
5300
5301 comm = elt_data->comm;
Tom Zanussi6b4827a2016-03-03 12:54:50 -06005302
5303 uval = *(u64 *)(key + key_field->offset);
Tom Zanussi85013252017-09-22 14:58:22 -05005304 seq_printf(m, "%s: %-16s[%10llu]", field_name,
5305 comm, uval);
Tom Zanussi31696192016-03-03 12:54:51 -06005306 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
5307 const char *syscall_name;
5308
5309 uval = *(u64 *)(key + key_field->offset);
5310 syscall_name = get_syscall_name(uval);
5311 if (!syscall_name)
5312 syscall_name = "unknown_syscall";
5313
Tom Zanussi85013252017-09-22 14:58:22 -05005314 seq_printf(m, "%s: %-30s[%3llu]", field_name,
5315 syscall_name, uval);
Tom Zanussi69a02002016-03-03 12:54:52 -06005316 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5317 seq_puts(m, "stacktrace:\n");
5318 hist_trigger_stacktrace_print(m,
5319 key + key_field->offset,
5320 HIST_STACKTRACE_DEPTH);
5321 multiline = true;
Namhyung Kim4b94f5b2016-03-03 12:55:02 -06005322 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
Tom Zanussi85013252017-09-22 14:58:22 -05005323 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
Namhyung Kim4b94f5b2016-03-03 12:55:02 -06005324 *(u64 *)(key + key_field->offset));
Tom Zanussi0c4a6b42016-03-03 12:54:48 -06005325 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
Tom Zanussi85013252017-09-22 14:58:22 -05005326 seq_printf(m, "%s: %-50s", field_name,
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06005327 (char *)(key + key_field->offset));
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005328 } else {
Tom Zanussi76a3b0c2016-03-03 12:54:44 -06005329 uval = *(u64 *)(key + key_field->offset);
Tom Zanussi85013252017-09-22 14:58:22 -05005330 seq_printf(m, "%s: %10llu", field_name, uval);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005331 }
5332 }
5333
Tom Zanussi69a02002016-03-03 12:54:52 -06005334 if (!multiline)
5335 seq_puts(m, " ");
5336
5337 seq_puts(m, "}");
Tom Zanussia3785b72019-02-13 17:42:46 -06005338}
5339
5340static void hist_trigger_entry_print(struct seq_file *m,
5341 struct hist_trigger_data *hist_data,
5342 void *key,
5343 struct tracing_map_elt *elt)
5344{
5345 const char *field_name;
5346 unsigned int i;
5347
5348 hist_trigger_print_key(m, hist_data, key, elt);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005349
5350 seq_printf(m, " hitcount: %10llu",
5351 tracing_map_read_sum(elt, HITCOUNT_IDX));
5352
Tom Zanussif2606832016-03-03 12:54:43 -06005353 for (i = 1; i < hist_data->n_vals; i++) {
Tom Zanussi85013252017-09-22 14:58:22 -05005354 field_name = hist_field_name(hist_data->fields[i], 0);
5355
Tom Zanussi100719d2018-01-15 20:51:52 -06005356 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
5357 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
Tom Zanussi30350d62018-01-15 20:51:49 -06005358 continue;
5359
Tom Zanussi0c4a6b42016-03-03 12:54:48 -06005360 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
Tom Zanussi85013252017-09-22 14:58:22 -05005361 seq_printf(m, " %s: %10llx", field_name,
Tom Zanussi0c4a6b42016-03-03 12:54:48 -06005362 tracing_map_read_sum(elt, i));
5363 } else {
Tom Zanussi85013252017-09-22 14:58:22 -05005364 seq_printf(m, " %s: %10llu", field_name,
Tom Zanussi0c4a6b42016-03-03 12:54:48 -06005365 tracing_map_read_sum(elt, i));
5366 }
Tom Zanussif2606832016-03-03 12:54:43 -06005367 }
5368
Tom Zanussi50450602018-01-15 20:52:01 -06005369 print_actions(m, hist_data, elt);
5370
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005371 seq_puts(m, "\n");
5372}
5373
5374static int print_entries(struct seq_file *m,
5375 struct hist_trigger_data *hist_data)
5376{
5377 struct tracing_map_sort_entry **sort_entries = NULL;
5378 struct tracing_map *map = hist_data->map;
Steven Rostedt (Red Hat)d50c7442016-03-08 17:17:15 -05005379 int i, n_entries;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005380
5381 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
5382 hist_data->n_sort_keys,
5383 &sort_entries);
5384 if (n_entries < 0)
5385 return n_entries;
5386
5387 for (i = 0; i < n_entries; i++)
5388 hist_trigger_entry_print(m, hist_data,
5389 sort_entries[i]->key,
5390 sort_entries[i]->elt);
5391
5392 tracing_map_destroy_sort_entries(sort_entries, n_entries);
5393
5394 return n_entries;
5395}
5396
Tom Zanussi52a7f162016-03-03 12:54:57 -06005397static void hist_trigger_show(struct seq_file *m,
5398 struct event_trigger_data *data, int n)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005399{
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005400 struct hist_trigger_data *hist_data;
Colin Ian King6e7a2392017-08-23 12:23:09 +01005401 int n_entries;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005402
Tom Zanussi52a7f162016-03-03 12:54:57 -06005403 if (n > 0)
5404 seq_puts(m, "\n\n");
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005405
5406 seq_puts(m, "# event histogram\n#\n# trigger info: ");
5407 data->ops->print(m, data->ops, data);
Tom Zanussi52a7f162016-03-03 12:54:57 -06005408 seq_puts(m, "#\n\n");
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005409
5410 hist_data = data->private_data;
5411 n_entries = print_entries(m, hist_data);
Colin Ian King6e7a2392017-08-23 12:23:09 +01005412 if (n_entries < 0)
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005413 n_entries = 0;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005414
Tom Zanussia3785b72019-02-13 17:42:46 -06005415 track_data_snapshot_print(m, hist_data);
5416
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005417 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
5418 (u64)atomic64_read(&hist_data->map->hits),
5419 n_entries, (u64)atomic64_read(&hist_data->map->drops));
Tom Zanussi52a7f162016-03-03 12:54:57 -06005420}
5421
5422static int hist_show(struct seq_file *m, void *v)
5423{
5424 struct event_trigger_data *data;
5425 struct trace_event_file *event_file;
5426 int n = 0, ret = 0;
5427
5428 mutex_lock(&event_mutex);
5429
5430 event_file = event_file_data(m->private);
5431 if (unlikely(!event_file)) {
5432 ret = -ENODEV;
5433 goto out_unlock;
5434 }
5435
5436 list_for_each_entry_rcu(data, &event_file->triggers, list) {
5437 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5438 hist_trigger_show(m, data, n++);
5439 }
5440
Tom Zanussif404da62018-01-15 20:52:05 -06005441 if (have_hist_err()) {
5442 seq_printf(m, "\nERROR: %s\n", hist_err_str);
5443 seq_printf(m, " Last command: %s\n", last_hist_cmd);
5444 }
5445
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005446 out_unlock:
5447 mutex_unlock(&event_mutex);
5448
5449 return ret;
5450}
5451
5452static int event_hist_open(struct inode *inode, struct file *file)
5453{
5454 return single_open(file, hist_show, file);
5455}
5456
5457const struct file_operations event_hist_fops = {
5458 .open = event_hist_open,
5459 .read = seq_read,
5460 .llseek = seq_lseek,
5461 .release = single_release,
5462};
5463
5464static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
5465{
Tom Zanussi85013252017-09-22 14:58:22 -05005466 const char *field_name = hist_field_name(hist_field, 0);
5467
Tom Zanussi30350d62018-01-15 20:51:49 -06005468 if (hist_field->var.name)
5469 seq_printf(m, "%s=", hist_field->var.name);
5470
Tom Zanussi0ae79612018-03-28 15:10:53 -05005471 if (hist_field->flags & HIST_FIELD_FL_CPU)
Tom Zanussi8b7622b2018-01-15 20:52:03 -06005472 seq_puts(m, "cpu");
Tom Zanussi067fe032018-01-15 20:51:56 -06005473 else if (field_name) {
Tom Zanussi7e8b88a2018-01-15 20:52:04 -06005474 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
5475 hist_field->flags & HIST_FIELD_FL_ALIAS)
Tom Zanussi067fe032018-01-15 20:51:56 -06005476 seq_putc(m, '$');
Tom Zanussiad42feb2018-01-15 20:51:45 -06005477 seq_printf(m, "%s", field_name);
Tom Zanussi0ae79612018-03-28 15:10:53 -05005478 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
5479 seq_puts(m, "common_timestamp");
Tom Zanussi608940d2018-04-26 20:04:47 -05005480
5481 if (hist_field->flags) {
5482 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
5483 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
5484 const char *flags = get_hist_field_flags(hist_field);
5485
5486 if (flags)
5487 seq_printf(m, ".%s", flags);
5488 }
5489 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005490}
5491
5492static int event_hist_trigger_print(struct seq_file *m,
5493 struct event_trigger_ops *ops,
5494 struct event_trigger_data *data)
5495{
5496 struct hist_trigger_data *hist_data = data->private_data;
Tom Zanussi30350d62018-01-15 20:51:49 -06005497 struct hist_field *field;
5498 bool have_var = false;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005499 unsigned int i;
5500
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005501 seq_puts(m, "hist:");
5502
5503 if (data->name)
5504 seq_printf(m, "%s:", data->name);
5505
5506 seq_puts(m, "keys=");
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005507
5508 for_each_hist_key_field(i, hist_data) {
Tom Zanussi30350d62018-01-15 20:51:49 -06005509 field = hist_data->fields[i];
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005510
5511 if (i > hist_data->n_vals)
5512 seq_puts(m, ",");
5513
Tom Zanussi30350d62018-01-15 20:51:49 -06005514 if (field->flags & HIST_FIELD_FL_STACKTRACE)
Tom Zanussi69a02002016-03-03 12:54:52 -06005515 seq_puts(m, "stacktrace");
5516 else
Tom Zanussi30350d62018-01-15 20:51:49 -06005517 hist_field_print(m, field);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005518 }
5519
5520 seq_puts(m, ":vals=");
Tom Zanussif2606832016-03-03 12:54:43 -06005521
5522 for_each_hist_val_field(i, hist_data) {
Tom Zanussi30350d62018-01-15 20:51:49 -06005523 field = hist_data->fields[i];
5524 if (field->flags & HIST_FIELD_FL_VAR) {
5525 have_var = true;
5526 continue;
5527 }
5528
Tom Zanussif2606832016-03-03 12:54:43 -06005529 if (i == HITCOUNT_IDX)
5530 seq_puts(m, "hitcount");
5531 else {
5532 seq_puts(m, ",");
Tom Zanussi30350d62018-01-15 20:51:49 -06005533 hist_field_print(m, field);
5534 }
5535 }
5536
5537 if (have_var) {
5538 unsigned int n = 0;
5539
5540 seq_puts(m, ":");
5541
5542 for_each_hist_val_field(i, hist_data) {
5543 field = hist_data->fields[i];
5544
5545 if (field->flags & HIST_FIELD_FL_VAR) {
5546 if (n++)
5547 seq_puts(m, ",");
5548 hist_field_print(m, field);
5549 }
Tom Zanussif2606832016-03-03 12:54:43 -06005550 }
5551 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005552
5553 seq_puts(m, ":sort=");
Tom Zanussie62347d2016-03-03 12:54:45 -06005554
5555 for (i = 0; i < hist_data->n_sort_keys; i++) {
5556 struct tracing_map_sort_key *sort_key;
Tom Zanussi30350d62018-01-15 20:51:49 -06005557 unsigned int idx, first_key_idx;
5558
5559 /* skip VAR vals */
5560 first_key_idx = hist_data->n_vals - hist_data->n_vars;
Tom Zanussie62347d2016-03-03 12:54:45 -06005561
5562 sort_key = &hist_data->sort_keys[i];
Tom Zanussiad42feb2018-01-15 20:51:45 -06005563 idx = sort_key->field_idx;
5564
Tom Zanussi1a361df2018-01-15 20:51:50 -06005565 if (WARN_ON(idx >= HIST_FIELDS_MAX))
Tom Zanussiad42feb2018-01-15 20:51:45 -06005566 return -EINVAL;
Tom Zanussie62347d2016-03-03 12:54:45 -06005567
5568 if (i > 0)
5569 seq_puts(m, ",");
5570
Tom Zanussiad42feb2018-01-15 20:51:45 -06005571 if (idx == HITCOUNT_IDX)
Tom Zanussie62347d2016-03-03 12:54:45 -06005572 seq_puts(m, "hitcount");
Tom Zanussi30350d62018-01-15 20:51:49 -06005573 else {
5574 if (idx >= first_key_idx)
5575 idx += hist_data->n_vars;
Tom Zanussie62347d2016-03-03 12:54:45 -06005576 hist_field_print(m, hist_data->fields[idx]);
Tom Zanussi30350d62018-01-15 20:51:49 -06005577 }
Tom Zanussie62347d2016-03-03 12:54:45 -06005578
5579 if (sort_key->descending)
5580 seq_puts(m, ".descending");
5581 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005582 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
Tom Zanussia4072fe2018-01-15 20:52:08 -06005583 if (hist_data->enable_timestamps)
5584 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005585
Tom Zanussic282a382018-01-15 20:52:00 -06005586 print_actions_spec(m, hist_data);
5587
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005588 if (data->filter_str)
5589 seq_printf(m, " if %s", data->filter_str);
5590
Tom Zanussi83e99912016-03-03 12:54:46 -06005591 if (data->paused)
5592 seq_puts(m, " [paused]");
5593 else
5594 seq_puts(m, " [active]");
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005595
5596 seq_putc(m, '\n');
5597
5598 return 0;
5599}
5600
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005601static int event_hist_trigger_init(struct event_trigger_ops *ops,
5602 struct event_trigger_data *data)
5603{
5604 struct hist_trigger_data *hist_data = data->private_data;
5605
5606 if (!data->ref && hist_data->attrs->name)
5607 save_named_trigger(hist_data->attrs->name, data);
5608
5609 data->ref++;
5610
5611 return 0;
5612}
5613
Tom Zanussi02205a62018-01-15 20:51:59 -06005614static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
5615{
5616 struct trace_event_file *file;
5617 unsigned int i;
5618 char *cmd;
5619 int ret;
5620
5621 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5622 file = hist_data->field_var_hists[i]->hist_data->event_file;
5623 cmd = hist_data->field_var_hists[i]->cmd;
5624 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
5625 "!hist", "hist", cmd);
5626 }
5627}
5628
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005629static void event_hist_trigger_free(struct event_trigger_ops *ops,
5630 struct event_trigger_data *data)
5631{
5632 struct hist_trigger_data *hist_data = data->private_data;
5633
5634 if (WARN_ON_ONCE(data->ref <= 0))
5635 return;
5636
5637 data->ref--;
5638 if (!data->ref) {
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005639 if (data->name)
5640 del_named_trigger(data);
Tom Zanussi067fe032018-01-15 20:51:56 -06005641
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005642 trigger_data_free(data);
Tom Zanussi067fe032018-01-15 20:51:56 -06005643
5644 remove_hist_vars(hist_data);
5645
Tom Zanussi02205a62018-01-15 20:51:59 -06005646 unregister_field_var_hists(hist_data);
5647
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005648 destroy_hist_data(hist_data);
5649 }
5650}
5651
5652static struct event_trigger_ops event_hist_trigger_ops = {
5653 .func = event_hist_trigger,
5654 .print = event_hist_trigger_print,
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005655 .init = event_hist_trigger_init,
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005656 .free = event_hist_trigger_free,
5657};
5658
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005659static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
5660 struct event_trigger_data *data)
5661{
5662 data->ref++;
5663
5664 save_named_trigger(data->named_data->name, data);
5665
5666 event_hist_trigger_init(ops, data->named_data);
5667
5668 return 0;
5669}
5670
5671static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
5672 struct event_trigger_data *data)
5673{
5674 if (WARN_ON_ONCE(data->ref <= 0))
5675 return;
5676
5677 event_hist_trigger_free(ops, data->named_data);
5678
5679 data->ref--;
5680 if (!data->ref) {
5681 del_named_trigger(data);
5682 trigger_data_free(data);
5683 }
5684}
5685
5686static struct event_trigger_ops event_hist_trigger_named_ops = {
5687 .func = event_hist_trigger,
5688 .print = event_hist_trigger_print,
5689 .init = event_hist_trigger_named_init,
5690 .free = event_hist_trigger_named_free,
5691};
5692
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005693static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
5694 char *param)
5695{
5696 return &event_hist_trigger_ops;
5697}
5698
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005699static void hist_clear(struct event_trigger_data *data)
5700{
5701 struct hist_trigger_data *hist_data = data->private_data;
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005702
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005703 if (data->name)
5704 pause_named_trigger(data);
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005705
Steven Rostedt (VMware)e0a568d2018-08-09 15:31:48 -04005706 tracepoint_synchronize_unregister();
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005707
5708 tracing_map_clear(hist_data->map);
5709
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005710 if (data->name)
5711 unpause_named_trigger(data);
5712}
5713
5714static bool compatible_field(struct ftrace_event_field *field,
5715 struct ftrace_event_field *test_field)
5716{
5717 if (field == test_field)
5718 return true;
5719 if (field == NULL || test_field == NULL)
5720 return false;
5721 if (strcmp(field->name, test_field->name) != 0)
5722 return false;
5723 if (strcmp(field->type, test_field->type) != 0)
5724 return false;
5725 if (field->size != test_field->size)
5726 return false;
5727 if (field->is_signed != test_field->is_signed)
5728 return false;
5729
5730 return true;
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005731}
5732
Tom Zanussi52a7f162016-03-03 12:54:57 -06005733static bool hist_trigger_match(struct event_trigger_data *data,
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005734 struct event_trigger_data *data_test,
5735 struct event_trigger_data *named_data,
5736 bool ignore_filter)
Tom Zanussi52a7f162016-03-03 12:54:57 -06005737{
5738 struct tracing_map_sort_key *sort_key, *sort_key_test;
5739 struct hist_trigger_data *hist_data, *hist_data_test;
5740 struct hist_field *key_field, *key_field_test;
5741 unsigned int i;
5742
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005743 if (named_data && (named_data != data_test) &&
5744 (named_data != data_test->named_data))
5745 return false;
5746
5747 if (!named_data && is_named_trigger(data_test))
5748 return false;
5749
Tom Zanussi52a7f162016-03-03 12:54:57 -06005750 hist_data = data->private_data;
5751 hist_data_test = data_test->private_data;
5752
5753 if (hist_data->n_vals != hist_data_test->n_vals ||
5754 hist_data->n_fields != hist_data_test->n_fields ||
5755 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
5756 return false;
5757
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005758 if (!ignore_filter) {
5759 if ((data->filter_str && !data_test->filter_str) ||
5760 (!data->filter_str && data_test->filter_str))
5761 return false;
5762 }
Tom Zanussi52a7f162016-03-03 12:54:57 -06005763
5764 for_each_hist_field(i, hist_data) {
5765 key_field = hist_data->fields[i];
5766 key_field_test = hist_data_test->fields[i];
5767
5768 if (key_field->flags != key_field_test->flags)
5769 return false;
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005770 if (!compatible_field(key_field->field, key_field_test->field))
Tom Zanussi52a7f162016-03-03 12:54:57 -06005771 return false;
5772 if (key_field->offset != key_field_test->offset)
5773 return false;
Tom Zanussiad42feb2018-01-15 20:51:45 -06005774 if (key_field->size != key_field_test->size)
5775 return false;
5776 if (key_field->is_signed != key_field_test->is_signed)
5777 return false;
Tom Zanussi1a361df2018-01-15 20:51:50 -06005778 if (!!key_field->var.name != !!key_field_test->var.name)
5779 return false;
5780 if (key_field->var.name &&
5781 strcmp(key_field->var.name, key_field_test->var.name) != 0)
5782 return false;
Tom Zanussi52a7f162016-03-03 12:54:57 -06005783 }
5784
5785 for (i = 0; i < hist_data->n_sort_keys; i++) {
5786 sort_key = &hist_data->sort_keys[i];
5787 sort_key_test = &hist_data_test->sort_keys[i];
5788
5789 if (sort_key->field_idx != sort_key_test->field_idx ||
5790 sort_key->descending != sort_key_test->descending)
5791 return false;
5792 }
5793
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005794 if (!ignore_filter && data->filter_str &&
Tom Zanussi52a7f162016-03-03 12:54:57 -06005795 (strcmp(data->filter_str, data_test->filter_str) != 0))
5796 return false;
5797
Tom Zanussi48f79472018-03-28 15:10:55 -05005798 if (!actions_match(hist_data, hist_data_test))
5799 return false;
5800
Tom Zanussi52a7f162016-03-03 12:54:57 -06005801 return true;
5802}
5803
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005804static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
5805 struct event_trigger_data *data,
5806 struct trace_event_file *file)
5807{
Tom Zanussi83e99912016-03-03 12:54:46 -06005808 struct hist_trigger_data *hist_data = data->private_data;
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005809 struct event_trigger_data *test, *named_data = NULL;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005810 int ret = 0;
5811
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005812 if (hist_data->attrs->name) {
5813 named_data = find_named_trigger(hist_data->attrs->name);
5814 if (named_data) {
5815 if (!hist_trigger_match(data, named_data, named_data,
5816 true)) {
Tom Zanussif404da62018-01-15 20:52:05 -06005817 hist_err("Named hist trigger doesn't match existing named trigger (includes variables): ", hist_data->attrs->name);
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005818 ret = -EINVAL;
5819 goto out;
5820 }
5821 }
5822 }
5823
5824 if (hist_data->attrs->name && !named_data)
5825 goto new;
5826
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005827 list_for_each_entry_rcu(test, &file->triggers, list) {
5828 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005829 if (!hist_trigger_match(data, test, named_data, false))
Tom Zanussi52a7f162016-03-03 12:54:57 -06005830 continue;
Tom Zanussi83e99912016-03-03 12:54:46 -06005831 if (hist_data->attrs->pause)
5832 test->paused = true;
5833 else if (hist_data->attrs->cont)
5834 test->paused = false;
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005835 else if (hist_data->attrs->clear)
5836 hist_clear(test);
Tom Zanussif404da62018-01-15 20:52:05 -06005837 else {
5838 hist_err("Hist trigger already exists", NULL);
Tom Zanussi83e99912016-03-03 12:54:46 -06005839 ret = -EEXIST;
Tom Zanussif404da62018-01-15 20:52:05 -06005840 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005841 goto out;
5842 }
5843 }
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005844 new:
Tom Zanussie86ae9b2016-03-03 12:54:47 -06005845 if (hist_data->attrs->cont || hist_data->attrs->clear) {
Tom Zanussif404da62018-01-15 20:52:05 -06005846 hist_err("Can't clear or continue a nonexistent hist trigger", NULL);
Tom Zanussi83e99912016-03-03 12:54:46 -06005847 ret = -ENOENT;
5848 goto out;
5849 }
5850
Tom Zanussi7522c032016-06-29 19:56:00 -05005851 if (hist_data->attrs->pause)
5852 data->paused = true;
5853
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005854 if (named_data) {
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005855 data->private_data = named_data->private_data;
5856 set_named_trigger_data(data, named_data);
5857 data->ops = &event_hist_trigger_named_ops;
5858 }
5859
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005860 if (data->ops->init) {
5861 ret = data->ops->init(data->ops, data);
5862 if (ret < 0)
5863 goto out;
5864 }
5865
Tom Zanussia4072fe2018-01-15 20:52:08 -06005866 if (hist_data->enable_timestamps) {
5867 char *clock = hist_data->attrs->clock;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005868
Tom Zanussia4072fe2018-01-15 20:52:08 -06005869 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
5870 if (ret) {
5871 hist_err("Couldn't set trace_clock: ", clock);
5872 goto out;
5873 }
5874
Tom Zanussiad42feb2018-01-15 20:51:45 -06005875 tracing_set_time_stamp_abs(file->tr, true);
Tom Zanussia4072fe2018-01-15 20:52:08 -06005876 }
5877
5878 if (named_data)
5879 destroy_hist_data(hist_data);
5880
5881 ret++;
Tom Zanussi067fe032018-01-15 20:51:56 -06005882 out:
5883 return ret;
5884}
5885
5886static int hist_trigger_enable(struct event_trigger_data *data,
5887 struct trace_event_file *file)
5888{
5889 int ret = 0;
5890
5891 list_add_tail_rcu(&data->list, &file->triggers);
5892
5893 update_cond_flag(file);
Tom Zanussiad42feb2018-01-15 20:51:45 -06005894
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005895 if (trace_event_trigger_enable_disable(file, 1) < 0) {
5896 list_del_rcu(&data->list);
5897 update_cond_flag(file);
5898 ret--;
5899 }
Tom Zanussi067fe032018-01-15 20:51:56 -06005900
Tom Zanussi7ef224d2016-03-03 12:54:42 -06005901 return ret;
5902}
5903
Tom Zanussi4b147932018-01-15 20:51:58 -06005904static bool have_hist_trigger_match(struct event_trigger_data *data,
5905 struct trace_event_file *file)
5906{
5907 struct hist_trigger_data *hist_data = data->private_data;
5908 struct event_trigger_data *test, *named_data = NULL;
5909 bool match = false;
5910
5911 if (hist_data->attrs->name)
5912 named_data = find_named_trigger(hist_data->attrs->name);
5913
5914 list_for_each_entry_rcu(test, &file->triggers, list) {
5915 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5916 if (hist_trigger_match(data, test, named_data, false)) {
5917 match = true;
5918 break;
5919 }
5920 }
5921 }
5922
5923 return match;
5924}
5925
Tom Zanussi067fe032018-01-15 20:51:56 -06005926static bool hist_trigger_check_refs(struct event_trigger_data *data,
5927 struct trace_event_file *file)
5928{
5929 struct hist_trigger_data *hist_data = data->private_data;
5930 struct event_trigger_data *test, *named_data = NULL;
5931
5932 if (hist_data->attrs->name)
5933 named_data = find_named_trigger(hist_data->attrs->name);
5934
5935 list_for_each_entry_rcu(test, &file->triggers, list) {
5936 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5937 if (!hist_trigger_match(data, test, named_data, false))
5938 continue;
5939 hist_data = test->private_data;
5940 if (check_var_refs(hist_data))
5941 return true;
5942 break;
5943 }
5944 }
5945
5946 return false;
5947}
5948
Tom Zanussi52a7f162016-03-03 12:54:57 -06005949static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
5950 struct event_trigger_data *data,
5951 struct trace_event_file *file)
5952{
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005953 struct hist_trigger_data *hist_data = data->private_data;
5954 struct event_trigger_data *test, *named_data = NULL;
Tom Zanussi52a7f162016-03-03 12:54:57 -06005955 bool unregistered = false;
5956
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005957 if (hist_data->attrs->name)
5958 named_data = find_named_trigger(hist_data->attrs->name);
5959
Tom Zanussi52a7f162016-03-03 12:54:57 -06005960 list_for_each_entry_rcu(test, &file->triggers, list) {
5961 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
Tom Zanussi5463bfd2016-03-03 12:54:59 -06005962 if (!hist_trigger_match(data, test, named_data, false))
Tom Zanussi52a7f162016-03-03 12:54:57 -06005963 continue;
5964 unregistered = true;
5965 list_del_rcu(&test->list);
5966 trace_event_trigger_enable_disable(file, 0);
5967 update_cond_flag(file);
5968 break;
5969 }
5970 }
5971
5972 if (unregistered && test->ops->free)
5973 test->ops->free(test->ops, test);
Tom Zanussiad42feb2018-01-15 20:51:45 -06005974
5975 if (hist_data->enable_timestamps) {
Tom Zanussi30350d62018-01-15 20:51:49 -06005976 if (!hist_data->remove || unregistered)
Tom Zanussiad42feb2018-01-15 20:51:45 -06005977 tracing_set_time_stamp_abs(file->tr, false);
5978 }
Tom Zanussi52a7f162016-03-03 12:54:57 -06005979}
5980
Tom Zanussi067fe032018-01-15 20:51:56 -06005981static bool hist_file_check_refs(struct trace_event_file *file)
5982{
5983 struct hist_trigger_data *hist_data;
5984 struct event_trigger_data *test;
5985
5986 list_for_each_entry_rcu(test, &file->triggers, list) {
5987 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5988 hist_data = test->private_data;
5989 if (check_var_refs(hist_data))
5990 return true;
5991 }
5992 }
5993
5994 return false;
5995}
5996
Tom Zanussi52a7f162016-03-03 12:54:57 -06005997static void hist_unreg_all(struct trace_event_file *file)
5998{
Steven Rostedt47c18562016-06-29 19:55:59 -05005999 struct event_trigger_data *test, *n;
Tom Zanussiad42feb2018-01-15 20:51:45 -06006000 struct hist_trigger_data *hist_data;
Tom Zanussi4b147932018-01-15 20:51:58 -06006001 struct synth_event *se;
6002 const char *se_name;
Tom Zanussi52a7f162016-03-03 12:54:57 -06006003
Masami Hiramatsu0e2b81f72018-11-05 18:04:01 +09006004 lockdep_assert_held(&event_mutex);
6005
Tom Zanussi067fe032018-01-15 20:51:56 -06006006 if (hist_file_check_refs(file))
6007 return;
6008
Steven Rostedt47c18562016-06-29 19:55:59 -05006009 list_for_each_entry_safe(test, n, &file->triggers, list) {
Tom Zanussi52a7f162016-03-03 12:54:57 -06006010 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
Tom Zanussiad42feb2018-01-15 20:51:45 -06006011 hist_data = test->private_data;
Tom Zanussi52a7f162016-03-03 12:54:57 -06006012 list_del_rcu(&test->list);
6013 trace_event_trigger_enable_disable(file, 0);
Tom Zanussi4b147932018-01-15 20:51:58 -06006014
Tom Zanussi4b147932018-01-15 20:51:58 -06006015 se_name = trace_event_name(file->event_call);
6016 se = find_synth_event(se_name);
6017 if (se)
6018 se->ref--;
Tom Zanussi4b147932018-01-15 20:51:58 -06006019
Tom Zanussi52a7f162016-03-03 12:54:57 -06006020 update_cond_flag(file);
Tom Zanussiad42feb2018-01-15 20:51:45 -06006021 if (hist_data->enable_timestamps)
6022 tracing_set_time_stamp_abs(file->tr, false);
Tom Zanussi52a7f162016-03-03 12:54:57 -06006023 if (test->ops->free)
6024 test->ops->free(test->ops, test);
6025 }
6026 }
6027}
6028
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006029static int event_hist_trigger_func(struct event_command *cmd_ops,
6030 struct trace_event_file *file,
6031 char *glob, char *cmd, char *param)
6032{
6033 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
6034 struct event_trigger_data *trigger_data;
6035 struct hist_trigger_attrs *attrs;
6036 struct event_trigger_ops *trigger_ops;
6037 struct hist_trigger_data *hist_data;
Tom Zanussi4b147932018-01-15 20:51:58 -06006038 struct synth_event *se;
6039 const char *se_name;
Tom Zanussi30350d62018-01-15 20:51:49 -06006040 bool remove = false;
Tom Zanussiec5ce092018-01-15 20:52:02 -06006041 char *trigger, *p;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006042 int ret = 0;
6043
Masami Hiramatsu0e2b81f72018-11-05 18:04:01 +09006044 lockdep_assert_held(&event_mutex);
6045
Tom Zanussif404da62018-01-15 20:52:05 -06006046 if (glob && strlen(glob)) {
6047 last_cmd_set(param);
6048 hist_err_clear();
6049 }
6050
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006051 if (!param)
6052 return -EINVAL;
6053
Tom Zanussi30350d62018-01-15 20:51:49 -06006054 if (glob[0] == '!')
6055 remove = true;
6056
Tom Zanussiec5ce092018-01-15 20:52:02 -06006057 /*
6058 * separate the trigger from the filter (k:v [if filter])
6059 * allowing for whitespace in the trigger
6060 */
6061 p = trigger = param;
6062 do {
6063 p = strstr(p, "if");
6064 if (!p)
6065 break;
6066 if (p == param)
6067 return -EINVAL;
6068 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
6069 p++;
6070 continue;
6071 }
Tom Zanussi2f31ed92018-12-18 14:33:21 -06006072 if (p >= param + strlen(param) - (sizeof("if") - 1) - 1)
Tom Zanussiec5ce092018-01-15 20:52:02 -06006073 return -EINVAL;
Tom Zanussi2f31ed92018-12-18 14:33:21 -06006074 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
Tom Zanussiec5ce092018-01-15 20:52:02 -06006075 p++;
6076 continue;
6077 }
6078 break;
6079 } while (p);
6080
6081 if (!p)
6082 param = NULL;
6083 else {
6084 *(p - 1) = '\0';
6085 param = strstrip(p);
6086 trigger = strstrip(trigger);
6087 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006088
6089 attrs = parse_hist_trigger_attrs(trigger);
6090 if (IS_ERR(attrs))
6091 return PTR_ERR(attrs);
6092
6093 if (attrs->map_bits)
6094 hist_trigger_bits = attrs->map_bits;
6095
Tom Zanussi30350d62018-01-15 20:51:49 -06006096 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006097 if (IS_ERR(hist_data)) {
6098 destroy_hist_trigger_attrs(attrs);
6099 return PTR_ERR(hist_data);
6100 }
6101
6102 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
6103
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006104 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
Tom Zanussi4b147932018-01-15 20:51:58 -06006105 if (!trigger_data) {
6106 ret = -ENOMEM;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006107 goto out_free;
Tom Zanussi4b147932018-01-15 20:51:58 -06006108 }
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006109
6110 trigger_data->count = -1;
6111 trigger_data->ops = trigger_ops;
6112 trigger_data->cmd_ops = cmd_ops;
6113
6114 INIT_LIST_HEAD(&trigger_data->list);
6115 RCU_INIT_POINTER(trigger_data->filter, NULL);
6116
6117 trigger_data->private_data = hist_data;
6118
Tom Zanussi52a7f162016-03-03 12:54:57 -06006119 /* if param is non-empty, it's supposed to be a filter */
6120 if (param && cmd_ops->set_filter) {
6121 ret = cmd_ops->set_filter(param, trigger_data, file);
6122 if (ret < 0)
6123 goto out_free;
6124 }
6125
Tom Zanussi30350d62018-01-15 20:51:49 -06006126 if (remove) {
Tom Zanussi4b147932018-01-15 20:51:58 -06006127 if (!have_hist_trigger_match(trigger_data, file))
6128 goto out_free;
6129
Tom Zanussi067fe032018-01-15 20:51:56 -06006130 if (hist_trigger_check_refs(trigger_data, file)) {
6131 ret = -EBUSY;
6132 goto out_free;
6133 }
6134
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006135 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
Tom Zanussi4b147932018-01-15 20:51:58 -06006136 se_name = trace_event_name(file->event_call);
6137 se = find_synth_event(se_name);
6138 if (se)
6139 se->ref--;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006140 ret = 0;
6141 goto out_free;
6142 }
6143
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006144 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
6145 /*
6146 * The above returns on success the # of triggers registered,
6147 * but if it didn't register any it returns zero. Consider no
6148 * triggers registered a failure too.
6149 */
6150 if (!ret) {
Tom Zanussie86ae9b2016-03-03 12:54:47 -06006151 if (!(attrs->pause || attrs->cont || attrs->clear))
Tom Zanussi83e99912016-03-03 12:54:46 -06006152 ret = -ENOENT;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006153 goto out_free;
6154 } else if (ret < 0)
6155 goto out_free;
Tom Zanussi067fe032018-01-15 20:51:56 -06006156
6157 if (get_named_trigger_data(trigger_data))
6158 goto enable;
6159
6160 if (has_hist_vars(hist_data))
6161 save_hist_vars(hist_data);
6162
Tom Zanussi7d18a102019-02-13 17:42:41 -06006163 ret = create_actions(hist_data);
Tom Zanussi0212e2a2018-01-15 20:51:57 -06006164 if (ret)
6165 goto out_unreg;
6166
Tom Zanussi067fe032018-01-15 20:51:56 -06006167 ret = tracing_map_init(hist_data->map);
6168 if (ret)
6169 goto out_unreg;
6170enable:
6171 ret = hist_trigger_enable(trigger_data, file);
6172 if (ret)
6173 goto out_unreg;
6174
Tom Zanussi4b147932018-01-15 20:51:58 -06006175 se_name = trace_event_name(file->event_call);
6176 se = find_synth_event(se_name);
6177 if (se)
6178 se->ref++;
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006179 /* Just return zero, not the number of registered triggers */
6180 ret = 0;
6181 out:
Tom Zanussif404da62018-01-15 20:52:05 -06006182 if (ret == 0)
6183 hist_err_clear();
6184
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006185 return ret;
Tom Zanussi067fe032018-01-15 20:51:56 -06006186 out_unreg:
6187 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006188 out_free:
6189 if (cmd_ops->set_filter)
6190 cmd_ops->set_filter(NULL, trigger_data, NULL);
6191
Tom Zanussi067fe032018-01-15 20:51:56 -06006192 remove_hist_vars(hist_data);
6193
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006194 kfree(trigger_data);
6195
6196 destroy_hist_data(hist_data);
6197 goto out;
6198}
6199
6200static struct event_command trigger_hist_cmd = {
6201 .name = "hist",
6202 .trigger_type = ETT_EVENT_HIST,
6203 .flags = EVENT_CMD_FL_NEEDS_REC,
6204 .func = event_hist_trigger_func,
6205 .reg = hist_register_trigger,
Tom Zanussi52a7f162016-03-03 12:54:57 -06006206 .unreg = hist_unregister_trigger,
6207 .unreg_all = hist_unreg_all,
Tom Zanussi7ef224d2016-03-03 12:54:42 -06006208 .get_trigger_ops = event_hist_get_trigger_ops,
6209 .set_filter = set_trigger_filter,
6210};
6211
6212__init int register_trigger_hist_cmd(void)
6213{
6214 int ret;
6215
6216 ret = register_event_command(&trigger_hist_cmd);
6217 WARN_ON(ret < 0);
6218
6219 return ret;
6220}
Tom Zanussid0bad492016-03-03 12:54:55 -06006221
6222static void
Tom Zanussi1ac4f512018-01-15 20:51:42 -06006223hist_enable_trigger(struct event_trigger_data *data, void *rec,
6224 struct ring_buffer_event *event)
Tom Zanussid0bad492016-03-03 12:54:55 -06006225{
6226 struct enable_trigger_data *enable_data = data->private_data;
6227 struct event_trigger_data *test;
6228
6229 list_for_each_entry_rcu(test, &enable_data->file->triggers, list) {
6230 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6231 if (enable_data->enable)
6232 test->paused = false;
6233 else
6234 test->paused = true;
Tom Zanussid0bad492016-03-03 12:54:55 -06006235 }
6236 }
6237}
6238
6239static void
Tom Zanussi1ac4f512018-01-15 20:51:42 -06006240hist_enable_count_trigger(struct event_trigger_data *data, void *rec,
6241 struct ring_buffer_event *event)
Tom Zanussid0bad492016-03-03 12:54:55 -06006242{
6243 if (!data->count)
6244 return;
6245
6246 if (data->count != -1)
6247 (data->count)--;
6248
Tom Zanussi1ac4f512018-01-15 20:51:42 -06006249 hist_enable_trigger(data, rec, event);
Tom Zanussid0bad492016-03-03 12:54:55 -06006250}
6251
6252static struct event_trigger_ops hist_enable_trigger_ops = {
6253 .func = hist_enable_trigger,
6254 .print = event_enable_trigger_print,
6255 .init = event_trigger_init,
6256 .free = event_enable_trigger_free,
6257};
6258
6259static struct event_trigger_ops hist_enable_count_trigger_ops = {
6260 .func = hist_enable_count_trigger,
6261 .print = event_enable_trigger_print,
6262 .init = event_trigger_init,
6263 .free = event_enable_trigger_free,
6264};
6265
6266static struct event_trigger_ops hist_disable_trigger_ops = {
6267 .func = hist_enable_trigger,
6268 .print = event_enable_trigger_print,
6269 .init = event_trigger_init,
6270 .free = event_enable_trigger_free,
6271};
6272
6273static struct event_trigger_ops hist_disable_count_trigger_ops = {
6274 .func = hist_enable_count_trigger,
6275 .print = event_enable_trigger_print,
6276 .init = event_trigger_init,
6277 .free = event_enable_trigger_free,
6278};
6279
6280static struct event_trigger_ops *
6281hist_enable_get_trigger_ops(char *cmd, char *param)
6282{
6283 struct event_trigger_ops *ops;
6284 bool enable;
6285
6286 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
6287
6288 if (enable)
6289 ops = param ? &hist_enable_count_trigger_ops :
6290 &hist_enable_trigger_ops;
6291 else
6292 ops = param ? &hist_disable_count_trigger_ops :
6293 &hist_disable_trigger_ops;
6294
6295 return ops;
6296}
6297
Tom Zanussi52a7f162016-03-03 12:54:57 -06006298static void hist_enable_unreg_all(struct trace_event_file *file)
6299{
Steven Rostedt47c18562016-06-29 19:55:59 -05006300 struct event_trigger_data *test, *n;
Tom Zanussi52a7f162016-03-03 12:54:57 -06006301
Steven Rostedt47c18562016-06-29 19:55:59 -05006302 list_for_each_entry_safe(test, n, &file->triggers, list) {
Tom Zanussi52a7f162016-03-03 12:54:57 -06006303 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
6304 list_del_rcu(&test->list);
6305 update_cond_flag(file);
6306 trace_event_trigger_enable_disable(file, 0);
6307 if (test->ops->free)
6308 test->ops->free(test->ops, test);
6309 }
6310 }
6311}
6312
Tom Zanussid0bad492016-03-03 12:54:55 -06006313static struct event_command trigger_hist_enable_cmd = {
6314 .name = ENABLE_HIST_STR,
6315 .trigger_type = ETT_HIST_ENABLE,
6316 .func = event_enable_trigger_func,
6317 .reg = event_enable_register_trigger,
6318 .unreg = event_enable_unregister_trigger,
Tom Zanussi52a7f162016-03-03 12:54:57 -06006319 .unreg_all = hist_enable_unreg_all,
Tom Zanussid0bad492016-03-03 12:54:55 -06006320 .get_trigger_ops = hist_enable_get_trigger_ops,
6321 .set_filter = set_trigger_filter,
6322};
6323
6324static struct event_command trigger_hist_disable_cmd = {
6325 .name = DISABLE_HIST_STR,
6326 .trigger_type = ETT_HIST_ENABLE,
6327 .func = event_enable_trigger_func,
6328 .reg = event_enable_register_trigger,
6329 .unreg = event_enable_unregister_trigger,
Tom Zanussi52a7f162016-03-03 12:54:57 -06006330 .unreg_all = hist_enable_unreg_all,
Tom Zanussid0bad492016-03-03 12:54:55 -06006331 .get_trigger_ops = hist_enable_get_trigger_ops,
6332 .set_filter = set_trigger_filter,
6333};
6334
6335static __init void unregister_trigger_hist_enable_disable_cmds(void)
6336{
6337 unregister_event_command(&trigger_hist_enable_cmd);
6338 unregister_event_command(&trigger_hist_disable_cmd);
6339}
6340
6341__init int register_trigger_hist_enable_disable_cmds(void)
6342{
6343 int ret;
6344
6345 ret = register_event_command(&trigger_hist_enable_cmd);
6346 if (WARN_ON(ret < 0))
6347 return ret;
6348 ret = register_event_command(&trigger_hist_disable_cmd);
6349 if (WARN_ON(ret < 0))
6350 unregister_trigger_hist_enable_disable_cmds();
6351
6352 return ret;
6353}
Tom Zanussi4b147932018-01-15 20:51:58 -06006354
6355static __init int trace_events_hist_init(void)
6356{
6357 struct dentry *entry = NULL;
6358 struct dentry *d_tracer;
6359 int err = 0;
6360
Masami Hiramatsu7bbab382018-11-05 18:03:33 +09006361 err = dyn_event_register(&synth_event_ops);
6362 if (err) {
6363 pr_warn("Could not register synth_event_ops\n");
6364 return err;
6365 }
6366
Tom Zanussi4b147932018-01-15 20:51:58 -06006367 d_tracer = tracing_init_dentry();
6368 if (IS_ERR(d_tracer)) {
6369 err = PTR_ERR(d_tracer);
6370 goto err;
6371 }
6372
6373 entry = tracefs_create_file("synthetic_events", 0644, d_tracer,
6374 NULL, &synth_events_fops);
6375 if (!entry) {
6376 err = -ENODEV;
6377 goto err;
6378 }
6379
6380 return err;
6381 err:
6382 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
6383
6384 return err;
6385}
6386
6387fs_initcall(trace_events_hist_init);