1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * trace_events_hist - trace event hist triggers
4  *
5  * 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/security.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
13 #include <linux/stacktrace.h>
14 #include <linux/rculist.h>
15 #include <linux/tracefs.h>
16 
17 /* for gfp flag names */
18 #include <linux/trace_events.h>
19 #include <trace/events/mmflags.h>
20 
21 #include "tracing_map.h"
22 #include "trace_synth.h"
23 
24 #define ERRORS								\
25 	C(NONE,			"No error"),				\
26 	C(DUPLICATE_VAR,	"Variable already defined"),		\
27 	C(VAR_NOT_UNIQUE,	"Variable name not unique, need to use fully qualified name (subsys.event.var) for variable"), \
28 	C(TOO_MANY_VARS,	"Too many variables defined"),		\
29 	C(MALFORMED_ASSIGNMENT,	"Malformed assignment"),		\
30 	C(NAMED_MISMATCH,	"Named hist trigger doesn't match existing named trigger (includes variables)"), \
31 	C(TRIGGER_EEXIST,	"Hist trigger already exists"),		\
32 	C(TRIGGER_ENOENT_CLEAR,	"Can't clear or continue a nonexistent hist trigger"), \
33 	C(SET_CLOCK_FAIL,	"Couldn't set trace_clock"),		\
34 	C(BAD_FIELD_MODIFIER,	"Invalid field modifier"),		\
35 	C(TOO_MANY_SUBEXPR,	"Too many subexpressions (3 max)"),	\
36 	C(TIMESTAMP_MISMATCH,	"Timestamp units in expression don't match"), \
37 	C(TOO_MANY_FIELD_VARS,	"Too many field variables defined"),	\
38 	C(EVENT_FILE_NOT_FOUND,	"Event file not found"),		\
39 	C(HIST_NOT_FOUND,	"Matching event histogram not found"),	\
40 	C(HIST_CREATE_FAIL,	"Couldn't create histogram for field"),	\
41 	C(SYNTH_VAR_NOT_FOUND,	"Couldn't find synthetic variable"),	\
42 	C(SYNTH_EVENT_NOT_FOUND,"Couldn't find synthetic event"),	\
43 	C(SYNTH_TYPE_MISMATCH,	"Param type doesn't match synthetic event field type"), \
44 	C(SYNTH_COUNT_MISMATCH,	"Param count doesn't match synthetic event field count"), \
45 	C(FIELD_VAR_PARSE_FAIL,	"Couldn't parse field variable"),	\
46 	C(VAR_CREATE_FIND_FAIL,	"Couldn't create or find variable"),	\
47 	C(ONX_NOT_VAR,		"For onmax(x) or onchange(x), x must be a variable"), \
48 	C(ONX_VAR_NOT_FOUND,	"Couldn't find onmax or onchange variable"), \
49 	C(ONX_VAR_CREATE_FAIL,	"Couldn't create onmax or onchange variable"), \
50 	C(FIELD_VAR_CREATE_FAIL,"Couldn't create field variable"),	\
51 	C(TOO_MANY_PARAMS,	"Too many action params"),		\
52 	C(PARAM_NOT_FOUND,	"Couldn't find param"),			\
53 	C(INVALID_PARAM,	"Invalid action param"),		\
54 	C(ACTION_NOT_FOUND,	"No action found"),			\
55 	C(NO_SAVE_PARAMS,	"No params found for save()"),		\
56 	C(TOO_MANY_SAVE_ACTIONS,"Can't have more than one save() action per hist"), \
57 	C(ACTION_MISMATCH,	"Handler doesn't support action"),	\
58 	C(NO_CLOSING_PAREN,	"No closing paren found"),		\
59 	C(SUBSYS_NOT_FOUND,	"Missing subsystem"),			\
60 	C(INVALID_SUBSYS_EVENT,	"Invalid subsystem or event name"),	\
61 	C(INVALID_REF_KEY,	"Using variable references in keys not supported"), \
62 	C(VAR_NOT_FOUND,	"Couldn't find variable"),		\
63 	C(FIELD_NOT_FOUND,	"Couldn't find field"),			\
64 	C(EMPTY_ASSIGNMENT,	"Empty assignment"),			\
65 	C(INVALID_SORT_MODIFIER,"Invalid sort modifier"),		\
66 	C(EMPTY_SORT_FIELD,	"Empty sort field"),			\
67 	C(TOO_MANY_SORT_FIELDS,	"Too many sort fields (Max = 2)"),	\
68 	C(INVALID_SORT_FIELD,	"Sort field must be a key or a val"),	\
69 	C(INVALID_STR_OPERAND,	"String type can not be an operand in expression"), \
70 	C(EXPECT_NUMBER,	"Expecting numeric literal"),		\
71 	C(UNARY_MINUS_SUBEXPR,	"Unary minus not supported in sub-expressions"), \
72 	C(DIVISION_BY_ZERO,	"Division by zero"),			\
73 	C(NEED_NOHC_VAL,	"Non-hitcount value is required for 'nohitcount'"),
74 
75 #undef C
76 #define C(a, b)		HIST_ERR_##a
77 
78 enum { ERRORS };
79 
80 #undef C
81 #define C(a, b)		b
82 
83 static const char *err_text[] = { ERRORS };
84 
85 struct hist_field;
86 
87 typedef u64 (*hist_field_fn_t) (struct hist_field *field,
88 				struct tracing_map_elt *elt,
89 				struct trace_buffer *buffer,
90 				struct ring_buffer_event *rbe,
91 				void *event);
92 
93 #define HIST_FIELD_OPERANDS_MAX	2
94 #define HIST_FIELDS_MAX		(TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
95 #define HIST_ACTIONS_MAX	8
96 #define HIST_CONST_DIGITS_MAX	21
97 #define HIST_DIV_SHIFT		20  /* For optimizing division by constants */
98 
99 enum field_op_id {
100 	FIELD_OP_NONE,
101 	FIELD_OP_PLUS,
102 	FIELD_OP_MINUS,
103 	FIELD_OP_UNARY_MINUS,
104 	FIELD_OP_DIV,
105 	FIELD_OP_MULT,
106 };
107 
108 enum hist_field_fn {
109 	HIST_FIELD_FN_NOP,
110 	HIST_FIELD_FN_VAR_REF,
111 	HIST_FIELD_FN_COUNTER,
112 	HIST_FIELD_FN_CONST,
113 	HIST_FIELD_FN_LOG2,
114 	HIST_FIELD_FN_BUCKET,
115 	HIST_FIELD_FN_TIMESTAMP,
116 	HIST_FIELD_FN_CPU,
117 	HIST_FIELD_FN_STRING,
118 	HIST_FIELD_FN_DYNSTRING,
119 	HIST_FIELD_FN_RELDYNSTRING,
120 	HIST_FIELD_FN_PSTRING,
121 	HIST_FIELD_FN_S64,
122 	HIST_FIELD_FN_U64,
123 	HIST_FIELD_FN_S32,
124 	HIST_FIELD_FN_U32,
125 	HIST_FIELD_FN_S16,
126 	HIST_FIELD_FN_U16,
127 	HIST_FIELD_FN_S8,
128 	HIST_FIELD_FN_U8,
129 	HIST_FIELD_FN_UMINUS,
130 	HIST_FIELD_FN_MINUS,
131 	HIST_FIELD_FN_PLUS,
132 	HIST_FIELD_FN_DIV,
133 	HIST_FIELD_FN_MULT,
134 	HIST_FIELD_FN_DIV_POWER2,
135 	HIST_FIELD_FN_DIV_NOT_POWER2,
136 	HIST_FIELD_FN_DIV_MULT_SHIFT,
137 	HIST_FIELD_FN_EXECNAME,
138 };
139 
140 /*
141  * A hist_var (histogram variable) contains variable information for
142  * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF
143  * flag set.  A hist_var has a variable name e.g. ts0, and is
144  * associated with a given histogram trigger, as specified by
145  * hist_data.  The hist_var idx is the unique index assigned to the
146  * variable by the hist trigger's tracing_map.  The idx is what is
147  * used to set a variable's value and, by a variable reference, to
148  * retrieve it.
149  */
150 struct hist_var {
151 	char				*name;
152 	struct hist_trigger_data	*hist_data;
153 	unsigned int			idx;
154 };
155 
156 struct hist_field {
157 	struct ftrace_event_field	*field;
158 	unsigned long			flags;
159 	unsigned long			buckets;
160 	const char			*type;
161 	struct hist_field		*operands[HIST_FIELD_OPERANDS_MAX];
162 	struct hist_trigger_data	*hist_data;
163 	enum hist_field_fn		fn_num;
164 	unsigned int			ref;
165 	unsigned int			size;
166 	unsigned int			offset;
167 	unsigned int                    is_signed;
168 
169 	/*
170 	 * Variable fields contain variable-specific info in var.
171 	 */
172 	struct hist_var			var;
173 	enum field_op_id		operator;
174 	char				*system;
175 	char				*event_name;
176 
177 	/*
178 	 * The name field is used for EXPR and VAR_REF fields.  VAR
179 	 * fields contain the variable name in var.name.
180 	 */
181 	char				*name;
182 
183 	/*
184 	 * When a histogram trigger is hit, if it has any references
185 	 * to variables, the values of those variables are collected
186 	 * into a var_ref_vals array by resolve_var_refs().  The
187 	 * current value of each variable is read from the tracing_map
188 	 * using the hist field's hist_var.idx and entered into the
189 	 * var_ref_idx entry i.e. var_ref_vals[var_ref_idx].
190 	 */
191 	unsigned int			var_ref_idx;
192 	bool                            read_once;
193 
194 	unsigned int			var_str_idx;
195 
196 	/* Numeric literals are represented as u64 */
197 	u64				constant;
198 	/* Used to optimize division by constants */
199 	u64				div_multiplier;
200 };
201 
202 static u64 hist_fn_call(struct hist_field *hist_field,
203 			struct tracing_map_elt *elt,
204 			struct trace_buffer *buffer,
205 			struct ring_buffer_event *rbe,
206 			void *event);
207 
208 static u64 hist_field_const(struct hist_field *field,
209 			   struct tracing_map_elt *elt,
210 			   struct trace_buffer *buffer,
211 			   struct ring_buffer_event *rbe,
212 			   void *event)
213 {
214 	return field->constant;
215 }
216 
217 static u64 hist_field_counter(struct hist_field *field,
218 			      struct tracing_map_elt *elt,
219 			      struct trace_buffer *buffer,
220 			      struct ring_buffer_event *rbe,
221 			      void *event)
222 {
223 	return 1;
224 }
225 
226 static u64 hist_field_string(struct hist_field *hist_field,
227 			     struct tracing_map_elt *elt,
228 			     struct trace_buffer *buffer,
229 			     struct ring_buffer_event *rbe,
230 			     void *event)
231 {
232 	char *addr = (char *)(event + hist_field->field->offset);
233 
234 	return (u64)(unsigned long)addr;
235 }
236 
237 static u64 hist_field_dynstring(struct hist_field *hist_field,
238 				struct tracing_map_elt *elt,
239 				struct trace_buffer *buffer,
240 				struct ring_buffer_event *rbe,
241 				void *event)
242 {
243 	u32 str_item = *(u32 *)(event + hist_field->field->offset);
244 	int str_loc = str_item & 0xffff;
245 	char *addr = (char *)(event + str_loc);
246 
247 	return (u64)(unsigned long)addr;
248 }
249 
250 static u64 hist_field_reldynstring(struct hist_field *hist_field,
251 				   struct tracing_map_elt *elt,
252 				   struct trace_buffer *buffer,
253 				   struct ring_buffer_event *rbe,
254 				   void *event)
255 {
256 	u32 *item = event + hist_field->field->offset;
257 	u32 str_item = *item;
258 	int str_loc = str_item & 0xffff;
259 	char *addr = (char *)&item[1] + str_loc;
260 
261 	return (u64)(unsigned long)addr;
262 }
263 
264 static u64 hist_field_pstring(struct hist_field *hist_field,
265 			      struct tracing_map_elt *elt,
266 			      struct trace_buffer *buffer,
267 			      struct ring_buffer_event *rbe,
268 			      void *event)
269 {
270 	char **addr = (char **)(event + hist_field->field->offset);
271 
272 	return (u64)(unsigned long)*addr;
273 }
274 
275 static u64 hist_field_log2(struct hist_field *hist_field,
276 			   struct tracing_map_elt *elt,
277 			   struct trace_buffer *buffer,
278 			   struct ring_buffer_event *rbe,
279 			   void *event)
280 {
281 	struct hist_field *operand = hist_field->operands[0];
282 
283 	u64 val = hist_fn_call(operand, elt, buffer, rbe, event);
284 
285 	return (u64) ilog2(roundup_pow_of_two(val));
286 }
287 
288 static u64 hist_field_bucket(struct hist_field *hist_field,
289 			     struct tracing_map_elt *elt,
290 			     struct trace_buffer *buffer,
291 			     struct ring_buffer_event *rbe,
292 			     void *event)
293 {
294 	struct hist_field *operand = hist_field->operands[0];
295 	unsigned long buckets = hist_field->buckets;
296 
297 	u64 val = hist_fn_call(operand, elt, buffer, rbe, event);
298 
299 	if (WARN_ON_ONCE(!buckets))
300 		return val;
301 
302 	if (val >= LONG_MAX)
303 		val = div64_ul(val, buckets);
304 	else
305 		val = (u64)((unsigned long)val / buckets);
306 	return val * buckets;
307 }
308 
309 static u64 hist_field_plus(struct hist_field *hist_field,
310 			   struct tracing_map_elt *elt,
311 			   struct trace_buffer *buffer,
312 			   struct ring_buffer_event *rbe,
313 			   void *event)
314 {
315 	struct hist_field *operand1 = hist_field->operands[0];
316 	struct hist_field *operand2 = hist_field->operands[1];
317 
318 	u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
319 	u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
320 
321 	return val1 + val2;
322 }
323 
324 static u64 hist_field_minus(struct hist_field *hist_field,
325 			    struct tracing_map_elt *elt,
326 			    struct trace_buffer *buffer,
327 			    struct ring_buffer_event *rbe,
328 			    void *event)
329 {
330 	struct hist_field *operand1 = hist_field->operands[0];
331 	struct hist_field *operand2 = hist_field->operands[1];
332 
333 	u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
334 	u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
335 
336 	return val1 - val2;
337 }
338 
339 static u64 hist_field_div(struct hist_field *hist_field,
340 			   struct tracing_map_elt *elt,
341 			   struct trace_buffer *buffer,
342 			   struct ring_buffer_event *rbe,
343 			   void *event)
344 {
345 	struct hist_field *operand1 = hist_field->operands[0];
346 	struct hist_field *operand2 = hist_field->operands[1];
347 
348 	u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
349 	u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
350 
351 	/* Return -1 for the undefined case */
352 	if (!val2)
353 		return -1;
354 
355 	/* Use shift if the divisor is a power of 2 */
356 	if (!(val2 & (val2 - 1)))
357 		return val1 >> __ffs64(val2);
358 
359 	return div64_u64(val1, val2);
360 }
361 
362 static u64 div_by_power_of_two(struct hist_field *hist_field,
363 				struct tracing_map_elt *elt,
364 				struct trace_buffer *buffer,
365 				struct ring_buffer_event *rbe,
366 				void *event)
367 {
368 	struct hist_field *operand1 = hist_field->operands[0];
369 	struct hist_field *operand2 = hist_field->operands[1];
370 
371 	u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
372 
373 	return val1 >> __ffs64(operand2->constant);
374 }
375 
376 static u64 div_by_not_power_of_two(struct hist_field *hist_field,
377 				struct tracing_map_elt *elt,
378 				struct trace_buffer *buffer,
379 				struct ring_buffer_event *rbe,
380 				void *event)
381 {
382 	struct hist_field *operand1 = hist_field->operands[0];
383 	struct hist_field *operand2 = hist_field->operands[1];
384 
385 	u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
386 
387 	return div64_u64(val1, operand2->constant);
388 }
389 
390 static u64 div_by_mult_and_shift(struct hist_field *hist_field,
391 				struct tracing_map_elt *elt,
392 				struct trace_buffer *buffer,
393 				struct ring_buffer_event *rbe,
394 				void *event)
395 {
396 	struct hist_field *operand1 = hist_field->operands[0];
397 	struct hist_field *operand2 = hist_field->operands[1];
398 
399 	u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
400 
401 	/*
402 	 * If the divisor is a constant, do a multiplication and shift instead.
403 	 *
404 	 * Choose Z = some power of 2. If Y <= Z, then:
405 	 *     X / Y = (X * (Z / Y)) / Z
406 	 *
407 	 * (Z / Y) is a constant (mult) which is calculated at parse time, so:
408 	 *     X / Y = (X * mult) / Z
409 	 *
410 	 * The division by Z can be replaced by a shift since Z is a power of 2:
411 	 *     X / Y = (X * mult) >> HIST_DIV_SHIFT
412 	 *
413 	 * As long, as X < Z the results will not be off by more than 1.
414 	 */
415 	if (val1 < (1 << HIST_DIV_SHIFT)) {
416 		u64 mult = operand2->div_multiplier;
417 
418 		return (val1 * mult + ((1 << HIST_DIV_SHIFT) - 1)) >> HIST_DIV_SHIFT;
419 	}
420 
421 	return div64_u64(val1, operand2->constant);
422 }
423 
424 static u64 hist_field_mult(struct hist_field *hist_field,
425 			   struct tracing_map_elt *elt,
426 			   struct trace_buffer *buffer,
427 			   struct ring_buffer_event *rbe,
428 			   void *event)
429 {
430 	struct hist_field *operand1 = hist_field->operands[0];
431 	struct hist_field *operand2 = hist_field->operands[1];
432 
433 	u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
434 	u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
435 
436 	return val1 * val2;
437 }
438 
439 static u64 hist_field_unary_minus(struct hist_field *hist_field,
440 				  struct tracing_map_elt *elt,
441 				  struct trace_buffer *buffer,
442 				  struct ring_buffer_event *rbe,
443 				  void *event)
444 {
445 	struct hist_field *operand = hist_field->operands[0];
446 
447 	s64 sval = (s64)hist_fn_call(operand, elt, buffer, rbe, event);
448 	u64 val = (u64)-sval;
449 
450 	return val;
451 }
452 
453 #define DEFINE_HIST_FIELD_FN(type)					\
454 	static u64 hist_field_##type(struct hist_field *hist_field,	\
455 				     struct tracing_map_elt *elt,	\
456 				     struct trace_buffer *buffer,	\
457 				     struct ring_buffer_event *rbe,	\
458 				     void *event)			\
459 {									\
460 	type *addr = (type *)(event + hist_field->field->offset);	\
461 									\
462 	return (u64)(unsigned long)*addr;				\
463 }
464 
465 DEFINE_HIST_FIELD_FN(s64);
466 DEFINE_HIST_FIELD_FN(u64);
467 DEFINE_HIST_FIELD_FN(s32);
468 DEFINE_HIST_FIELD_FN(u32);
469 DEFINE_HIST_FIELD_FN(s16);
470 DEFINE_HIST_FIELD_FN(u16);
471 DEFINE_HIST_FIELD_FN(s8);
472 DEFINE_HIST_FIELD_FN(u8);
473 
474 #define for_each_hist_field(i, hist_data)	\
475 	for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
476 
477 #define for_each_hist_val_field(i, hist_data)	\
478 	for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
479 
480 #define for_each_hist_key_field(i, hist_data)	\
481 	for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
482 
483 #define HIST_STACKTRACE_DEPTH	16
484 #define HIST_STACKTRACE_SIZE	(HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
485 #define HIST_STACKTRACE_SKIP	5
486 
487 #define HITCOUNT_IDX		0
488 #define HIST_KEY_SIZE_MAX	(MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
489 
490 enum hist_field_flags {
491 	HIST_FIELD_FL_HITCOUNT		= 1 << 0,
492 	HIST_FIELD_FL_KEY		= 1 << 1,
493 	HIST_FIELD_FL_STRING		= 1 << 2,
494 	HIST_FIELD_FL_HEX		= 1 << 3,
495 	HIST_FIELD_FL_SYM		= 1 << 4,
496 	HIST_FIELD_FL_SYM_OFFSET	= 1 << 5,
497 	HIST_FIELD_FL_EXECNAME		= 1 << 6,
498 	HIST_FIELD_FL_SYSCALL		= 1 << 7,
499 	HIST_FIELD_FL_STACKTRACE	= 1 << 8,
500 	HIST_FIELD_FL_LOG2		= 1 << 9,
501 	HIST_FIELD_FL_TIMESTAMP		= 1 << 10,
502 	HIST_FIELD_FL_TIMESTAMP_USECS	= 1 << 11,
503 	HIST_FIELD_FL_VAR		= 1 << 12,
504 	HIST_FIELD_FL_EXPR		= 1 << 13,
505 	HIST_FIELD_FL_VAR_REF		= 1 << 14,
506 	HIST_FIELD_FL_CPU		= 1 << 15,
507 	HIST_FIELD_FL_ALIAS		= 1 << 16,
508 	HIST_FIELD_FL_BUCKET		= 1 << 17,
509 	HIST_FIELD_FL_CONST		= 1 << 18,
510 	HIST_FIELD_FL_PERCENT		= 1 << 19,
511 	HIST_FIELD_FL_GRAPH		= 1 << 20,
512 };
513 
514 struct var_defs {
515 	unsigned int	n_vars;
516 	char		*name[TRACING_MAP_VARS_MAX];
517 	char		*expr[TRACING_MAP_VARS_MAX];
518 };
519 
520 struct hist_trigger_attrs {
521 	char		*keys_str;
522 	char		*vals_str;
523 	char		*sort_key_str;
524 	char		*name;
525 	char		*clock;
526 	bool		pause;
527 	bool		cont;
528 	bool		clear;
529 	bool		ts_in_usecs;
530 	bool		no_hitcount;
531 	unsigned int	map_bits;
532 
533 	char		*assignment_str[TRACING_MAP_VARS_MAX];
534 	unsigned int	n_assignments;
535 
536 	char		*action_str[HIST_ACTIONS_MAX];
537 	unsigned int	n_actions;
538 
539 	struct var_defs	var_defs;
540 };
541 
542 struct field_var {
543 	struct hist_field	*var;
544 	struct hist_field	*val;
545 };
546 
547 struct field_var_hist {
548 	struct hist_trigger_data	*hist_data;
549 	char				*cmd;
550 };
551 
552 struct hist_trigger_data {
553 	struct hist_field               *fields[HIST_FIELDS_MAX];
554 	unsigned int			n_vals;
555 	unsigned int			n_keys;
556 	unsigned int			n_fields;
557 	unsigned int			n_vars;
558 	unsigned int			n_var_str;
559 	unsigned int			key_size;
560 	struct tracing_map_sort_key	sort_keys[TRACING_MAP_SORT_KEYS_MAX];
561 	unsigned int			n_sort_keys;
562 	struct trace_event_file		*event_file;
563 	struct hist_trigger_attrs	*attrs;
564 	struct tracing_map		*map;
565 	bool				enable_timestamps;
566 	bool				remove;
567 	struct hist_field               *var_refs[TRACING_MAP_VARS_MAX];
568 	unsigned int			n_var_refs;
569 
570 	struct action_data		*actions[HIST_ACTIONS_MAX];
571 	unsigned int			n_actions;
572 
573 	struct field_var		*field_vars[SYNTH_FIELDS_MAX];
574 	unsigned int			n_field_vars;
575 	unsigned int			n_field_var_str;
576 	struct field_var_hist		*field_var_hists[SYNTH_FIELDS_MAX];
577 	unsigned int			n_field_var_hists;
578 
579 	struct field_var		*save_vars[SYNTH_FIELDS_MAX];
580 	unsigned int			n_save_vars;
581 	unsigned int			n_save_var_str;
582 };
583 
584 struct action_data;
585 
586 typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
587 			     struct tracing_map_elt *elt,
588 			     struct trace_buffer *buffer, void *rec,
589 			     struct ring_buffer_event *rbe, void *key,
590 			     struct action_data *data, u64 *var_ref_vals);
591 
592 typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val);
593 
594 enum handler_id {
595 	HANDLER_ONMATCH = 1,
596 	HANDLER_ONMAX,
597 	HANDLER_ONCHANGE,
598 };
599 
600 enum action_id {
601 	ACTION_SAVE = 1,
602 	ACTION_TRACE,
603 	ACTION_SNAPSHOT,
604 };
605 
606 struct action_data {
607 	enum handler_id		handler;
608 	enum action_id		action;
609 	char			*action_name;
610 	action_fn_t		fn;
611 
612 	unsigned int		n_params;
613 	char			*params[SYNTH_FIELDS_MAX];
614 
615 	/*
616 	 * When a histogram trigger is hit, the values of any
617 	 * references to variables, including variables being passed
618 	 * as parameters to synthetic events, are collected into a
619 	 * var_ref_vals array.  This var_ref_idx array is an array of
620 	 * indices into the var_ref_vals array, one for each synthetic
621 	 * event param, and is passed to the synthetic event
622 	 * invocation.
623 	 */
624 	unsigned int		var_ref_idx[SYNTH_FIELDS_MAX];
625 	struct synth_event	*synth_event;
626 	bool			use_trace_keyword;
627 	char			*synth_event_name;
628 
629 	union {
630 		struct {
631 			char			*event;
632 			char			*event_system;
633 		} match_data;
634 
635 		struct {
636 			/*
637 			 * var_str contains the $-unstripped variable
638 			 * name referenced by var_ref, and used when
639 			 * printing the action.  Because var_ref
640 			 * creation is deferred to create_actions(),
641 			 * we need a per-action way to save it until
642 			 * then, thus var_str.
643 			 */
644 			char			*var_str;
645 
646 			/*
647 			 * var_ref refers to the variable being
648 			 * tracked e.g onmax($var).
649 			 */
650 			struct hist_field	*var_ref;
651 
652 			/*
653 			 * track_var contains the 'invisible' tracking
654 			 * variable created to keep the current
655 			 * e.g. max value.
656 			 */
657 			struct hist_field	*track_var;
658 
659 			check_track_val_fn_t	check_val;
660 			action_fn_t		save_data;
661 		} track_data;
662 	};
663 };
664 
665 struct track_data {
666 	u64				track_val;
667 	bool				updated;
668 
669 	unsigned int			key_len;
670 	void				*key;
671 	struct tracing_map_elt		elt;
672 
673 	struct action_data		*action_data;
674 	struct hist_trigger_data	*hist_data;
675 };
676 
677 struct hist_elt_data {
678 	char *comm;
679 	u64 *var_ref_vals;
680 	char **field_var_str;
681 	int n_field_var_str;
682 };
683 
684 struct snapshot_context {
685 	struct tracing_map_elt	*elt;
686 	void			*key;
687 };
688 
689 /*
690  * Returns the specific division function to use if the divisor
691  * is constant. This avoids extra branches when the trigger is hit.
692  */
693 static enum hist_field_fn hist_field_get_div_fn(struct hist_field *divisor)
694 {
695 	u64 div = divisor->constant;
696 
697 	if (!(div & (div - 1)))
698 		return HIST_FIELD_FN_DIV_POWER2;
699 
700 	/* If the divisor is too large, do a regular division */
701 	if (div > (1 << HIST_DIV_SHIFT))
702 		return HIST_FIELD_FN_DIV_NOT_POWER2;
703 
704 	divisor->div_multiplier = div64_u64((u64)(1 << HIST_DIV_SHIFT), div);
705 	return HIST_FIELD_FN_DIV_MULT_SHIFT;
706 }
707 
708 static void track_data_free(struct track_data *track_data)
709 {
710 	struct hist_elt_data *elt_data;
711 
712 	if (!track_data)
713 		return;
714 
715 	kfree(track_data->key);
716 
717 	elt_data = track_data->elt.private_data;
718 	if (elt_data) {
719 		kfree(elt_data->comm);
720 		kfree(elt_data);
721 	}
722 
723 	kfree(track_data);
724 }
725 
726 static struct track_data *track_data_alloc(unsigned int key_len,
727 					   struct action_data *action_data,
728 					   struct hist_trigger_data *hist_data)
729 {
730 	struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
731 	struct hist_elt_data *elt_data;
732 
733 	if (!data)
734 		return ERR_PTR(-ENOMEM);
735 
736 	data->key = kzalloc(key_len, GFP_KERNEL);
737 	if (!data->key) {
738 		track_data_free(data);
739 		return ERR_PTR(-ENOMEM);
740 	}
741 
742 	data->key_len = key_len;
743 	data->action_data = action_data;
744 	data->hist_data = hist_data;
745 
746 	elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
747 	if (!elt_data) {
748 		track_data_free(data);
749 		return ERR_PTR(-ENOMEM);
750 	}
751 
752 	data->elt.private_data = elt_data;
753 
754 	elt_data->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL);
755 	if (!elt_data->comm) {
756 		track_data_free(data);
757 		return ERR_PTR(-ENOMEM);
758 	}
759 
760 	return data;
761 }
762 
763 #define HIST_PREFIX "hist:"
764 
765 static char *last_cmd;
766 static char last_cmd_loc[MAX_FILTER_STR_VAL];
767 
768 static int errpos(char *str)
769 {
770 	if (!str || !last_cmd)
771 		return 0;
772 
773 	return err_pos(last_cmd, str);
774 }
775 
776 static void last_cmd_set(struct trace_event_file *file, char *str)
777 {
778 	const char *system = NULL, *name = NULL;
779 	struct trace_event_call *call;
780 	int len;
781 
782 	if (!str)
783 		return;
784 
785 	/* sizeof() contains the nul byte */
786 	len = sizeof(HIST_PREFIX) + strlen(str);
787 	kfree(last_cmd);
788 	last_cmd = kzalloc(len, GFP_KERNEL);
789 	if (!last_cmd)
790 		return;
791 
792 	strcpy(last_cmd, HIST_PREFIX);
793 	/* Again, sizeof() contains the nul byte */
794 	len -= sizeof(HIST_PREFIX);
795 	strncat(last_cmd, str, len);
796 
797 	if (file) {
798 		call = file->event_call;
799 		system = call->class->system;
800 		if (system) {
801 			name = trace_event_name(call);
802 			if (!name)
803 				system = NULL;
804 		}
805 	}
806 
807 	if (system)
808 		snprintf(last_cmd_loc, MAX_FILTER_STR_VAL, HIST_PREFIX "%s:%s", system, name);
809 }
810 
811 static void hist_err(struct trace_array *tr, u8 err_type, u16 err_pos)
812 {
813 	if (!last_cmd)
814 		return;
815 
816 	tracing_log_err(tr, last_cmd_loc, last_cmd, err_text,
817 			err_type, err_pos);
818 }
819 
820 static void hist_err_clear(void)
821 {
822 	if (last_cmd)
823 		last_cmd[0] = '\0';
824 	last_cmd_loc[0] = '\0';
825 }
826 
827 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
828 				    unsigned int *var_ref_idx);
829 
830 static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
831 			       unsigned int *var_ref_idx)
832 {
833 	struct tracepoint *tp = event->tp;
834 
835 	if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
836 		struct tracepoint_func *probe_func_ptr;
837 		synth_probe_func_t probe_func;
838 		void *__data;
839 
840 		if (!(cpu_online(raw_smp_processor_id())))
841 			return;
842 
843 		probe_func_ptr = rcu_dereference_sched((tp)->funcs);
844 		if (probe_func_ptr) {
845 			do {
846 				probe_func = probe_func_ptr->func;
847 				__data = probe_func_ptr->data;
848 				probe_func(__data, var_ref_vals, var_ref_idx);
849 			} while ((++probe_func_ptr)->func);
850 		}
851 	}
852 }
853 
854 static void action_trace(struct hist_trigger_data *hist_data,
855 			 struct tracing_map_elt *elt,
856 			 struct trace_buffer *buffer, void *rec,
857 			 struct ring_buffer_event *rbe, void *key,
858 			 struct action_data *data, u64 *var_ref_vals)
859 {
860 	struct synth_event *event = data->synth_event;
861 
862 	trace_synth(event, var_ref_vals, data->var_ref_idx);
863 }
864 
865 struct hist_var_data {
866 	struct list_head list;
867 	struct hist_trigger_data *hist_data;
868 };
869 
870 static u64 hist_field_timestamp(struct hist_field *hist_field,
871 				struct tracing_map_elt *elt,
872 				struct trace_buffer *buffer,
873 				struct ring_buffer_event *rbe,
874 				void *event)
875 {
876 	struct hist_trigger_data *hist_data = hist_field->hist_data;
877 	struct trace_array *tr = hist_data->event_file->tr;
878 
879 	u64 ts = ring_buffer_event_time_stamp(buffer, rbe);
880 
881 	if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
882 		ts = ns2usecs(ts);
883 
884 	return ts;
885 }
886 
887 static u64 hist_field_cpu(struct hist_field *hist_field,
888 			  struct tracing_map_elt *elt,
889 			  struct trace_buffer *buffer,
890 			  struct ring_buffer_event *rbe,
891 			  void *event)
892 {
893 	int cpu = smp_processor_id();
894 
895 	return cpu;
896 }
897 
898 /**
899  * check_field_for_var_ref - Check if a VAR_REF field references a variable
900  * @hist_field: The VAR_REF field to check
901  * @var_data: The hist trigger that owns the variable
902  * @var_idx: The trigger variable identifier
903  *
904  * Check the given VAR_REF field to see whether or not it references
905  * the given variable associated with the given trigger.
906  *
907  * Return: The VAR_REF field if it does reference the variable, NULL if not
908  */
909 static struct hist_field *
910 check_field_for_var_ref(struct hist_field *hist_field,
911 			struct hist_trigger_data *var_data,
912 			unsigned int var_idx)
913 {
914 	WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
915 
916 	if (hist_field && hist_field->var.idx == var_idx &&
917 	    hist_field->var.hist_data == var_data)
918 		return hist_field;
919 
920 	return NULL;
921 }
922 
923 /**
924  * find_var_ref - Check if a trigger has a reference to a trigger variable
925  * @hist_data: The hist trigger that might have a reference to the variable
926  * @var_data: The hist trigger that owns the variable
927  * @var_idx: The trigger variable identifier
928  *
929  * Check the list of var_refs[] on the first hist trigger to see
930  * whether any of them are references to the variable on the second
931  * trigger.
932  *
933  * Return: The VAR_REF field referencing the variable if so, NULL if not
934  */
935 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
936 				       struct hist_trigger_data *var_data,
937 				       unsigned int var_idx)
938 {
939 	struct hist_field *hist_field;
940 	unsigned int i;
941 
942 	for (i = 0; i < hist_data->n_var_refs; i++) {
943 		hist_field = hist_data->var_refs[i];
944 		if (check_field_for_var_ref(hist_field, var_data, var_idx))
945 			return hist_field;
946 	}
947 
948 	return NULL;
949 }
950 
951 /**
952  * find_any_var_ref - Check if there is a reference to a given trigger variable
953  * @hist_data: The hist trigger
954  * @var_idx: The trigger variable identifier
955  *
956  * Check to see whether the given variable is currently referenced by
957  * any other trigger.
958  *
959  * The trigger the variable is defined on is explicitly excluded - the
960  * assumption being that a self-reference doesn't prevent a trigger
961  * from being removed.
962  *
963  * Return: The VAR_REF field referencing the variable if so, NULL if not
964  */
965 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
966 					   unsigned int var_idx)
967 {
968 	struct trace_array *tr = hist_data->event_file->tr;
969 	struct hist_field *found = NULL;
970 	struct hist_var_data *var_data;
971 
972 	list_for_each_entry(var_data, &tr->hist_vars, list) {
973 		if (var_data->hist_data == hist_data)
974 			continue;
975 		found = find_var_ref(var_data->hist_data, hist_data, var_idx);
976 		if (found)
977 			break;
978 	}
979 
980 	return found;
981 }
982 
983 /**
984  * check_var_refs - Check if there is a reference to any of trigger's variables
985  * @hist_data: The hist trigger
986  *
987  * A trigger can define one or more variables.  If any one of them is
988  * currently referenced by any other trigger, this function will
989  * determine that.
990  *
991  * Typically used to determine whether or not a trigger can be removed
992  * - if there are any references to a trigger's variables, it cannot.
993  *
994  * Return: True if there is a reference to any of trigger's variables
995  */
996 static bool check_var_refs(struct hist_trigger_data *hist_data)
997 {
998 	struct hist_field *field;
999 	bool found = false;
1000 	int i;
1001 
1002 	for_each_hist_field(i, hist_data) {
1003 		field = hist_data->fields[i];
1004 		if (field && field->flags & HIST_FIELD_FL_VAR) {
1005 			if (find_any_var_ref(hist_data, field->var.idx)) {
1006 				found = true;
1007 				break;
1008 			}
1009 		}
1010 	}
1011 
1012 	return found;
1013 }
1014 
1015 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
1016 {
1017 	struct trace_array *tr = hist_data->event_file->tr;
1018 	struct hist_var_data *var_data, *found = NULL;
1019 
1020 	list_for_each_entry(var_data, &tr->hist_vars, list) {
1021 		if (var_data->hist_data == hist_data) {
1022 			found = var_data;
1023 			break;
1024 		}
1025 	}
1026 
1027 	return found;
1028 }
1029 
1030 static bool field_has_hist_vars(struct hist_field *hist_field,
1031 				unsigned int level)
1032 {
1033 	int i;
1034 
1035 	if (level > 3)
1036 		return false;
1037 
1038 	if (!hist_field)
1039 		return false;
1040 
1041 	if (hist_field->flags & HIST_FIELD_FL_VAR ||
1042 	    hist_field->flags & HIST_FIELD_FL_VAR_REF)
1043 		return true;
1044 
1045 	for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
1046 		struct hist_field *operand;
1047 
1048 		operand = hist_field->operands[i];
1049 		if (field_has_hist_vars(operand, level + 1))
1050 			return true;
1051 	}
1052 
1053 	return false;
1054 }
1055 
1056 static bool has_hist_vars(struct hist_trigger_data *hist_data)
1057 {
1058 	struct hist_field *hist_field;
1059 	int i;
1060 
1061 	for_each_hist_field(i, hist_data) {
1062 		hist_field = hist_data->fields[i];
1063 		if (field_has_hist_vars(hist_field, 0))
1064 			return true;
1065 	}
1066 
1067 	return false;
1068 }
1069 
1070 static int save_hist_vars(struct hist_trigger_data *hist_data)
1071 {
1072 	struct trace_array *tr = hist_data->event_file->tr;
1073 	struct hist_var_data *var_data;
1074 
1075 	var_data = find_hist_vars(hist_data);
1076 	if (var_data)
1077 		return 0;
1078 
1079 	if (tracing_check_open_get_tr(tr))
1080 		return -ENODEV;
1081 
1082 	var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
1083 	if (!var_data) {
1084 		trace_array_put(tr);
1085 		return -ENOMEM;
1086 	}
1087 
1088 	var_data->hist_data = hist_data;
1089 	list_add(&var_data->list, &tr->hist_vars);
1090 
1091 	return 0;
1092 }
1093 
1094 static void remove_hist_vars(struct hist_trigger_data *hist_data)
1095 {
1096 	struct trace_array *tr = hist_data->event_file->tr;
1097 	struct hist_var_data *var_data;
1098 
1099 	var_data = find_hist_vars(hist_data);
1100 	if (!var_data)
1101 		return;
1102 
1103 	if (WARN_ON(check_var_refs(hist_data)))
1104 		return;
1105 
1106 	list_del(&var_data->list);
1107 
1108 	kfree(var_data);
1109 
1110 	trace_array_put(tr);
1111 }
1112 
1113 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
1114 					 const char *var_name)
1115 {
1116 	struct hist_field *hist_field, *found = NULL;
1117 	int i;
1118 
1119 	for_each_hist_field(i, hist_data) {
1120 		hist_field = hist_data->fields[i];
1121 		if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
1122 		    strcmp(hist_field->var.name, var_name) == 0) {
1123 			found = hist_field;
1124 			break;
1125 		}
1126 	}
1127 
1128 	return found;
1129 }
1130 
1131 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
1132 				   struct trace_event_file *file,
1133 				   const char *var_name)
1134 {
1135 	struct hist_trigger_data *test_data;
1136 	struct event_trigger_data *test;
1137 	struct hist_field *hist_field;
1138 
1139 	lockdep_assert_held(&event_mutex);
1140 
1141 	hist_field = find_var_field(hist_data, var_name);
1142 	if (hist_field)
1143 		return hist_field;
1144 
1145 	list_for_each_entry(test, &file->triggers, list) {
1146 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1147 			test_data = test->private_data;
1148 			hist_field = find_var_field(test_data, var_name);
1149 			if (hist_field)
1150 				return hist_field;
1151 		}
1152 	}
1153 
1154 	return NULL;
1155 }
1156 
1157 static struct trace_event_file *find_var_file(struct trace_array *tr,
1158 					      char *system,
1159 					      char *event_name,
1160 					      char *var_name)
1161 {
1162 	struct hist_trigger_data *var_hist_data;
1163 	struct hist_var_data *var_data;
1164 	struct trace_event_file *file, *found = NULL;
1165 
1166 	if (system)
1167 		return find_event_file(tr, system, event_name);
1168 
1169 	list_for_each_entry(var_data, &tr->hist_vars, list) {
1170 		var_hist_data = var_data->hist_data;
1171 		file = var_hist_data->event_file;
1172 		if (file == found)
1173 			continue;
1174 
1175 		if (find_var_field(var_hist_data, var_name)) {
1176 			if (found) {
1177 				hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name));
1178 				return NULL;
1179 			}
1180 
1181 			found = file;
1182 		}
1183 	}
1184 
1185 	return found;
1186 }
1187 
1188 static struct hist_field *find_file_var(struct trace_event_file *file,
1189 					const char *var_name)
1190 {
1191 	struct hist_trigger_data *test_data;
1192 	struct event_trigger_data *test;
1193 	struct hist_field *hist_field;
1194 
1195 	lockdep_assert_held(&event_mutex);
1196 
1197 	list_for_each_entry(test, &file->triggers, list) {
1198 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1199 			test_data = test->private_data;
1200 			hist_field = find_var_field(test_data, var_name);
1201 			if (hist_field)
1202 				return hist_field;
1203 		}
1204 	}
1205 
1206 	return NULL;
1207 }
1208 
1209 static struct hist_field *
1210 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
1211 {
1212 	struct trace_array *tr = hist_data->event_file->tr;
1213 	struct hist_field *hist_field, *found = NULL;
1214 	struct trace_event_file *file;
1215 	unsigned int i;
1216 
1217 	for (i = 0; i < hist_data->n_actions; i++) {
1218 		struct action_data *data = hist_data->actions[i];
1219 
1220 		if (data->handler == HANDLER_ONMATCH) {
1221 			char *system = data->match_data.event_system;
1222 			char *event_name = data->match_data.event;
1223 
1224 			file = find_var_file(tr, system, event_name, var_name);
1225 			if (!file)
1226 				continue;
1227 			hist_field = find_file_var(file, var_name);
1228 			if (hist_field) {
1229 				if (found) {
1230 					hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE,
1231 						 errpos(var_name));
1232 					return ERR_PTR(-EINVAL);
1233 				}
1234 
1235 				found = hist_field;
1236 			}
1237 		}
1238 	}
1239 	return found;
1240 }
1241 
1242 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
1243 					 char *system,
1244 					 char *event_name,
1245 					 char *var_name)
1246 {
1247 	struct trace_array *tr = hist_data->event_file->tr;
1248 	struct hist_field *hist_field = NULL;
1249 	struct trace_event_file *file;
1250 
1251 	if (!system || !event_name) {
1252 		hist_field = find_match_var(hist_data, var_name);
1253 		if (IS_ERR(hist_field))
1254 			return NULL;
1255 		if (hist_field)
1256 			return hist_field;
1257 	}
1258 
1259 	file = find_var_file(tr, system, event_name, var_name);
1260 	if (!file)
1261 		return NULL;
1262 
1263 	hist_field = find_file_var(file, var_name);
1264 
1265 	return hist_field;
1266 }
1267 
1268 static u64 hist_field_var_ref(struct hist_field *hist_field,
1269 			      struct tracing_map_elt *elt,
1270 			      struct trace_buffer *buffer,
1271 			      struct ring_buffer_event *rbe,
1272 			      void *event)
1273 {
1274 	struct hist_elt_data *elt_data;
1275 	u64 var_val = 0;
1276 
1277 	if (WARN_ON_ONCE(!elt))
1278 		return var_val;
1279 
1280 	elt_data = elt->private_data;
1281 	var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
1282 
1283 	return var_val;
1284 }
1285 
1286 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
1287 			     u64 *var_ref_vals, bool self)
1288 {
1289 	struct hist_trigger_data *var_data;
1290 	struct tracing_map_elt *var_elt;
1291 	struct hist_field *hist_field;
1292 	unsigned int i, var_idx;
1293 	bool resolved = true;
1294 	u64 var_val = 0;
1295 
1296 	for (i = 0; i < hist_data->n_var_refs; i++) {
1297 		hist_field = hist_data->var_refs[i];
1298 		var_idx = hist_field->var.idx;
1299 		var_data = hist_field->var.hist_data;
1300 
1301 		if (var_data == NULL) {
1302 			resolved = false;
1303 			break;
1304 		}
1305 
1306 		if ((self && var_data != hist_data) ||
1307 		    (!self && var_data == hist_data))
1308 			continue;
1309 
1310 		var_elt = tracing_map_lookup(var_data->map, key);
1311 		if (!var_elt) {
1312 			resolved = false;
1313 			break;
1314 		}
1315 
1316 		if (!tracing_map_var_set(var_elt, var_idx)) {
1317 			resolved = false;
1318 			break;
1319 		}
1320 
1321 		if (self || !hist_field->read_once)
1322 			var_val = tracing_map_read_var(var_elt, var_idx);
1323 		else
1324 			var_val = tracing_map_read_var_once(var_elt, var_idx);
1325 
1326 		var_ref_vals[i] = var_val;
1327 	}
1328 
1329 	return resolved;
1330 }
1331 
1332 static const char *hist_field_name(struct hist_field *field,
1333 				   unsigned int level)
1334 {
1335 	const char *field_name = "";
1336 
1337 	if (level > 1)
1338 		return field_name;
1339 
1340 	if (field->field)
1341 		field_name = field->field->name;
1342 	else if (field->flags & HIST_FIELD_FL_LOG2 ||
1343 		 field->flags & HIST_FIELD_FL_ALIAS ||
1344 		 field->flags & HIST_FIELD_FL_BUCKET)
1345 		field_name = hist_field_name(field->operands[0], ++level);
1346 	else if (field->flags & HIST_FIELD_FL_CPU)
1347 		field_name = "common_cpu";
1348 	else if (field->flags & HIST_FIELD_FL_EXPR ||
1349 		 field->flags & HIST_FIELD_FL_VAR_REF) {
1350 		if (field->system) {
1351 			static char full_name[MAX_FILTER_STR_VAL];
1352 
1353 			strcat(full_name, field->system);
1354 			strcat(full_name, ".");
1355 			strcat(full_name, field->event_name);
1356 			strcat(full_name, ".");
1357 			strcat(full_name, field->name);
1358 			field_name = full_name;
1359 		} else
1360 			field_name = field->name;
1361 	} else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1362 		field_name = "common_timestamp";
1363 	else if (field->flags & HIST_FIELD_FL_HITCOUNT)
1364 		field_name = "hitcount";
1365 
1366 	if (field_name == NULL)
1367 		field_name = "";
1368 
1369 	return field_name;
1370 }
1371 
1372 static enum hist_field_fn select_value_fn(int field_size, int field_is_signed)
1373 {
1374 	switch (field_size) {
1375 	case 8:
1376 		if (field_is_signed)
1377 			return HIST_FIELD_FN_S64;
1378 		else
1379 			return HIST_FIELD_FN_U64;
1380 	case 4:
1381 		if (field_is_signed)
1382 			return HIST_FIELD_FN_S32;
1383 		else
1384 			return HIST_FIELD_FN_U32;
1385 	case 2:
1386 		if (field_is_signed)
1387 			return HIST_FIELD_FN_S16;
1388 		else
1389 			return HIST_FIELD_FN_U16;
1390 	case 1:
1391 		if (field_is_signed)
1392 			return HIST_FIELD_FN_S8;
1393 		else
1394 			return HIST_FIELD_FN_U8;
1395 	}
1396 
1397 	return HIST_FIELD_FN_NOP;
1398 }
1399 
1400 static int parse_map_size(char *str)
1401 {
1402 	unsigned long size, map_bits;
1403 	int ret;
1404 
1405 	ret = kstrtoul(str, 0, &size);
1406 	if (ret)
1407 		goto out;
1408 
1409 	map_bits = ilog2(roundup_pow_of_two(size));
1410 	if (map_bits < TRACING_MAP_BITS_MIN ||
1411 	    map_bits > TRACING_MAP_BITS_MAX)
1412 		ret = -EINVAL;
1413 	else
1414 		ret = map_bits;
1415  out:
1416 	return ret;
1417 }
1418 
1419 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
1420 {
1421 	unsigned int i;
1422 
1423 	if (!attrs)
1424 		return;
1425 
1426 	for (i = 0; i < attrs->n_assignments; i++)
1427 		kfree(attrs->assignment_str[i]);
1428 
1429 	for (i = 0; i < attrs->n_actions; i++)
1430 		kfree(attrs->action_str[i]);
1431 
1432 	kfree(attrs->name);
1433 	kfree(attrs->sort_key_str);
1434 	kfree(attrs->keys_str);
1435 	kfree(attrs->vals_str);
1436 	kfree(attrs->clock);
1437 	kfree(attrs);
1438 }
1439 
1440 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
1441 {
1442 	int ret = -EINVAL;
1443 
1444 	if (attrs->n_actions >= HIST_ACTIONS_MAX)
1445 		return ret;
1446 
1447 	if ((str_has_prefix(str, "onmatch(")) ||
1448 	    (str_has_prefix(str, "onmax(")) ||
1449 	    (str_has_prefix(str, "onchange("))) {
1450 		attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
1451 		if (!attrs->action_str[attrs->n_actions]) {
1452 			ret = -ENOMEM;
1453 			return ret;
1454 		}
1455 		attrs->n_actions++;
1456 		ret = 0;
1457 	}
1458 	return ret;
1459 }
1460 
1461 static int parse_assignment(struct trace_array *tr,
1462 			    char *str, struct hist_trigger_attrs *attrs)
1463 {
1464 	int len, ret = 0;
1465 
1466 	if ((len = str_has_prefix(str, "key=")) ||
1467 	    (len = str_has_prefix(str, "keys="))) {
1468 		attrs->keys_str = kstrdup(str + len, GFP_KERNEL);
1469 		if (!attrs->keys_str) {
1470 			ret = -ENOMEM;
1471 			goto out;
1472 		}
1473 	} else if ((len = str_has_prefix(str, "val=")) ||
1474 		   (len = str_has_prefix(str, "vals=")) ||
1475 		   (len = str_has_prefix(str, "values="))) {
1476 		attrs->vals_str = kstrdup(str + len, GFP_KERNEL);
1477 		if (!attrs->vals_str) {
1478 			ret = -ENOMEM;
1479 			goto out;
1480 		}
1481 	} else if ((len = str_has_prefix(str, "sort="))) {
1482 		attrs->sort_key_str = kstrdup(str + len, GFP_KERNEL);
1483 		if (!attrs->sort_key_str) {
1484 			ret = -ENOMEM;
1485 			goto out;
1486 		}
1487 	} else if (str_has_prefix(str, "name=")) {
1488 		attrs->name = kstrdup(str, GFP_KERNEL);
1489 		if (!attrs->name) {
1490 			ret = -ENOMEM;
1491 			goto out;
1492 		}
1493 	} else if ((len = str_has_prefix(str, "clock="))) {
1494 		str += len;
1495 
1496 		str = strstrip(str);
1497 		attrs->clock = kstrdup(str, GFP_KERNEL);
1498 		if (!attrs->clock) {
1499 			ret = -ENOMEM;
1500 			goto out;
1501 		}
1502 	} else if ((len = str_has_prefix(str, "size="))) {
1503 		int map_bits = parse_map_size(str + len);
1504 
1505 		if (map_bits < 0) {
1506 			ret = map_bits;
1507 			goto out;
1508 		}
1509 		attrs->map_bits = map_bits;
1510 	} else {
1511 		char *assignment;
1512 
1513 		if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
1514 			hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str));
1515 			ret = -EINVAL;
1516 			goto out;
1517 		}
1518 
1519 		assignment = kstrdup(str, GFP_KERNEL);
1520 		if (!assignment) {
1521 			ret = -ENOMEM;
1522 			goto out;
1523 		}
1524 
1525 		attrs->assignment_str[attrs->n_assignments++] = assignment;
1526 	}
1527  out:
1528 	return ret;
1529 }
1530 
1531 static struct hist_trigger_attrs *
1532 parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str)
1533 {
1534 	struct hist_trigger_attrs *attrs;
1535 	int ret = 0;
1536 
1537 	attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1538 	if (!attrs)
1539 		return ERR_PTR(-ENOMEM);
1540 
1541 	while (trigger_str) {
1542 		char *str = strsep(&trigger_str, ":");
1543 		char *rhs;
1544 
1545 		rhs = strchr(str, '=');
1546 		if (rhs) {
1547 			if (!strlen(++rhs)) {
1548 				ret = -EINVAL;
1549 				hist_err(tr, HIST_ERR_EMPTY_ASSIGNMENT, errpos(str));
1550 				goto free;
1551 			}
1552 			ret = parse_assignment(tr, str, attrs);
1553 			if (ret)
1554 				goto free;
1555 		} else if (strcmp(str, "nohitcount") == 0 ||
1556 			   strcmp(str, "NOHC") == 0)
1557 			attrs->no_hitcount = true;
1558 		else if (strcmp(str, "pause") == 0)
1559 			attrs->pause = true;
1560 		else if ((strcmp(str, "cont") == 0) ||
1561 			 (strcmp(str, "continue") == 0))
1562 			attrs->cont = true;
1563 		else if (strcmp(str, "clear") == 0)
1564 			attrs->clear = true;
1565 		else {
1566 			ret = parse_action(str, attrs);
1567 			if (ret)
1568 				goto free;
1569 		}
1570 	}
1571 
1572 	if (!attrs->keys_str) {
1573 		ret = -EINVAL;
1574 		goto free;
1575 	}
1576 
1577 	if (!attrs->clock) {
1578 		attrs->clock = kstrdup("global", GFP_KERNEL);
1579 		if (!attrs->clock) {
1580 			ret = -ENOMEM;
1581 			goto free;
1582 		}
1583 	}
1584 
1585 	return attrs;
1586  free:
1587 	destroy_hist_trigger_attrs(attrs);
1588 
1589 	return ERR_PTR(ret);
1590 }
1591 
1592 static inline void save_comm(char *comm, struct task_struct *task)
1593 {
1594 	if (!task->pid) {
1595 		strcpy(comm, "<idle>");
1596 		return;
1597 	}
1598 
1599 	if (WARN_ON_ONCE(task->pid < 0)) {
1600 		strcpy(comm, "<XXX>");
1601 		return;
1602 	}
1603 
1604 	strncpy(comm, task->comm, TASK_COMM_LEN);
1605 }
1606 
1607 static void hist_elt_data_free(struct hist_elt_data *elt_data)
1608 {
1609 	unsigned int i;
1610 
1611 	for (i = 0; i < elt_data->n_field_var_str; i++)
1612 		kfree(elt_data->field_var_str[i]);
1613 
1614 	kfree(elt_data->field_var_str);
1615 
1616 	kfree(elt_data->comm);
1617 	kfree(elt_data);
1618 }
1619 
1620 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
1621 {
1622 	struct hist_elt_data *elt_data = elt->private_data;
1623 
1624 	hist_elt_data_free(elt_data);
1625 }
1626 
1627 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
1628 {
1629 	struct hist_trigger_data *hist_data = elt->map->private_data;
1630 	unsigned int size = TASK_COMM_LEN;
1631 	struct hist_elt_data *elt_data;
1632 	struct hist_field *hist_field;
1633 	unsigned int i, n_str;
1634 
1635 	elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
1636 	if (!elt_data)
1637 		return -ENOMEM;
1638 
1639 	for_each_hist_field(i, hist_data) {
1640 		hist_field = hist_data->fields[i];
1641 
1642 		if (hist_field->flags & HIST_FIELD_FL_EXECNAME) {
1643 			elt_data->comm = kzalloc(size, GFP_KERNEL);
1644 			if (!elt_data->comm) {
1645 				kfree(elt_data);
1646 				return -ENOMEM;
1647 			}
1648 			break;
1649 		}
1650 	}
1651 
1652 	n_str = hist_data->n_field_var_str + hist_data->n_save_var_str +
1653 		hist_data->n_var_str;
1654 	if (n_str > SYNTH_FIELDS_MAX) {
1655 		hist_elt_data_free(elt_data);
1656 		return -EINVAL;
1657 	}
1658 
1659 	BUILD_BUG_ON(STR_VAR_LEN_MAX & (sizeof(u64) - 1));
1660 
1661 	size = STR_VAR_LEN_MAX;
1662 
1663 	elt_data->field_var_str = kcalloc(n_str, sizeof(char *), GFP_KERNEL);
1664 	if (!elt_data->field_var_str) {
1665 		hist_elt_data_free(elt_data);
1666 		return -EINVAL;
1667 	}
1668 	elt_data->n_field_var_str = n_str;
1669 
1670 	for (i = 0; i < n_str; i++) {
1671 		elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
1672 		if (!elt_data->field_var_str[i]) {
1673 			hist_elt_data_free(elt_data);
1674 			return -ENOMEM;
1675 		}
1676 	}
1677 
1678 	elt->private_data = elt_data;
1679 
1680 	return 0;
1681 }
1682 
1683 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
1684 {
1685 	struct hist_elt_data *elt_data = elt->private_data;
1686 
1687 	if (elt_data->comm)
1688 		save_comm(elt_data->comm, current);
1689 }
1690 
1691 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
1692 	.elt_alloc	= hist_trigger_elt_data_alloc,
1693 	.elt_free	= hist_trigger_elt_data_free,
1694 	.elt_init	= hist_trigger_elt_data_init,
1695 };
1696 
1697 static const char *get_hist_field_flags(struct hist_field *hist_field)
1698 {
1699 	const char *flags_str = NULL;
1700 
1701 	if (hist_field->flags & HIST_FIELD_FL_HEX)
1702 		flags_str = "hex";
1703 	else if (hist_field->flags & HIST_FIELD_FL_SYM)
1704 		flags_str = "sym";
1705 	else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
1706 		flags_str = "sym-offset";
1707 	else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
1708 		flags_str = "execname";
1709 	else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
1710 		flags_str = "syscall";
1711 	else if (hist_field->flags & HIST_FIELD_FL_LOG2)
1712 		flags_str = "log2";
1713 	else if (hist_field->flags & HIST_FIELD_FL_BUCKET)
1714 		flags_str = "buckets";
1715 	else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
1716 		flags_str = "usecs";
1717 	else if (hist_field->flags & HIST_FIELD_FL_PERCENT)
1718 		flags_str = "percent";
1719 	else if (hist_field->flags & HIST_FIELD_FL_GRAPH)
1720 		flags_str = "graph";
1721 
1722 	return flags_str;
1723 }
1724 
1725 static void expr_field_str(struct hist_field *field, char *expr)
1726 {
1727 	if (field->flags & HIST_FIELD_FL_VAR_REF)
1728 		strcat(expr, "$");
1729 	else if (field->flags & HIST_FIELD_FL_CONST) {
1730 		char str[HIST_CONST_DIGITS_MAX];
1731 
1732 		snprintf(str, HIST_CONST_DIGITS_MAX, "%llu", field->constant);
1733 		strcat(expr, str);
1734 	}
1735 
1736 	strcat(expr, hist_field_name(field, 0));
1737 
1738 	if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
1739 		const char *flags_str = get_hist_field_flags(field);
1740 
1741 		if (flags_str) {
1742 			strcat(expr, ".");
1743 			strcat(expr, flags_str);
1744 		}
1745 	}
1746 }
1747 
1748 static char *expr_str(struct hist_field *field, unsigned int level)
1749 {
1750 	char *expr;
1751 
1752 	if (level > 1)
1753 		return NULL;
1754 
1755 	expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
1756 	if (!expr)
1757 		return NULL;
1758 
1759 	if (!field->operands[0]) {
1760 		expr_field_str(field, expr);
1761 		return expr;
1762 	}
1763 
1764 	if (field->operator == FIELD_OP_UNARY_MINUS) {
1765 		char *subexpr;
1766 
1767 		strcat(expr, "-(");
1768 		subexpr = expr_str(field->operands[0], ++level);
1769 		if (!subexpr) {
1770 			kfree(expr);
1771 			return NULL;
1772 		}
1773 		strcat(expr, subexpr);
1774 		strcat(expr, ")");
1775 
1776 		kfree(subexpr);
1777 
1778 		return expr;
1779 	}
1780 
1781 	expr_field_str(field->operands[0], expr);
1782 
1783 	switch (field->operator) {
1784 	case FIELD_OP_MINUS:
1785 		strcat(expr, "-");
1786 		break;
1787 	case FIELD_OP_PLUS:
1788 		strcat(expr, "+");
1789 		break;
1790 	case FIELD_OP_DIV:
1791 		strcat(expr, "/");
1792 		break;
1793 	case FIELD_OP_MULT:
1794 		strcat(expr, "*");
1795 		break;
1796 	default:
1797 		kfree(expr);
1798 		return NULL;
1799 	}
1800 
1801 	expr_field_str(field->operands[1], expr);
1802 
1803 	return expr;
1804 }
1805 
1806 /*
1807  * If field_op != FIELD_OP_NONE, *sep points to the root operator
1808  * of the expression tree to be evaluated.
1809  */
1810 static int contains_operator(char *str, char **sep)
1811 {
1812 	enum field_op_id field_op = FIELD_OP_NONE;
1813 	char *minus_op, *plus_op, *div_op, *mult_op;
1814 
1815 
1816 	/*
1817 	 * Report the last occurrence of the operators first, so that the
1818 	 * expression is evaluated left to right. This is important since
1819 	 * subtraction and division are not associative.
1820 	 *
1821 	 *	e.g
1822 	 *		64/8/4/2 is 1, i.e 64/8/4/2 = ((64/8)/4)/2
1823 	 *		14-7-5-2 is 0, i.e 14-7-5-2 = ((14-7)-5)-2
1824 	 */
1825 
1826 	/*
1827 	 * First, find lower precedence addition and subtraction
1828 	 * since the expression will be evaluated recursively.
1829 	 */
1830 	minus_op = strrchr(str, '-');
1831 	if (minus_op) {
1832 		/*
1833 		 * Unary minus is not supported in sub-expressions. If
1834 		 * present, it is always the next root operator.
1835 		 */
1836 		if (minus_op == str) {
1837 			field_op = FIELD_OP_UNARY_MINUS;
1838 			goto out;
1839 		}
1840 
1841 		field_op = FIELD_OP_MINUS;
1842 	}
1843 
1844 	plus_op = strrchr(str, '+');
1845 	if (plus_op || minus_op) {
1846 		/*
1847 		 * For operators of the same precedence use to rightmost as the
1848 		 * root, so that the expression is evaluated left to right.
1849 		 */
1850 		if (plus_op > minus_op)
1851 			field_op = FIELD_OP_PLUS;
1852 		goto out;
1853 	}
1854 
1855 	/*
1856 	 * Multiplication and division have higher precedence than addition and
1857 	 * subtraction.
1858 	 */
1859 	div_op = strrchr(str, '/');
1860 	if (div_op)
1861 		field_op = FIELD_OP_DIV;
1862 
1863 	mult_op = strrchr(str, '*');
1864 	/*
1865 	 * For operators of the same precedence use to rightmost as the
1866 	 * root, so that the expression is evaluated left to right.
1867 	 */
1868 	if (mult_op > div_op)
1869 		field_op = FIELD_OP_MULT;
1870 
1871 out:
1872 	if (sep) {
1873 		switch (field_op) {
1874 		case FIELD_OP_UNARY_MINUS:
1875 		case FIELD_OP_MINUS:
1876 			*sep = minus_op;
1877 			break;
1878 		case FIELD_OP_PLUS:
1879 			*sep = plus_op;
1880 			break;
1881 		case FIELD_OP_DIV:
1882 			*sep = div_op;
1883 			break;
1884 		case FIELD_OP_MULT:
1885 			*sep = mult_op;
1886 			break;
1887 		case FIELD_OP_NONE:
1888 		default:
1889 			*sep = NULL;
1890 			break;
1891 		}
1892 	}
1893 
1894 	return field_op;
1895 }
1896 
1897 static void get_hist_field(struct hist_field *hist_field)
1898 {
1899 	hist_field->ref++;
1900 }
1901 
1902 static void __destroy_hist_field(struct hist_field *hist_field)
1903 {
1904 	if (--hist_field->ref > 1)
1905 		return;
1906 
1907 	kfree(hist_field->var.name);
1908 	kfree(hist_field->name);
1909 
1910 	/* Can likely be a const */
1911 	kfree_const(hist_field->type);
1912 
1913 	kfree(hist_field->system);
1914 	kfree(hist_field->event_name);
1915 
1916 	kfree(hist_field);
1917 }
1918 
1919 static void destroy_hist_field(struct hist_field *hist_field,
1920 			       unsigned int level)
1921 {
1922 	unsigned int i;
1923 
1924 	if (level > 3)
1925 		return;
1926 
1927 	if (!hist_field)
1928 		return;
1929 
1930 	if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
1931 		return; /* var refs will be destroyed separately */
1932 
1933 	for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
1934 		destroy_hist_field(hist_field->operands[i], level + 1);
1935 
1936 	__destroy_hist_field(hist_field);
1937 }
1938 
1939 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
1940 					    struct ftrace_event_field *field,
1941 					    unsigned long flags,
1942 					    char *var_name)
1943 {
1944 	struct hist_field *hist_field;
1945 
1946 	if (field && is_function_field(field))
1947 		return NULL;
1948 
1949 	hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
1950 	if (!hist_field)
1951 		return NULL;
1952 
1953 	hist_field->ref = 1;
1954 
1955 	hist_field->hist_data = hist_data;
1956 
1957 	if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
1958 		goto out; /* caller will populate */
1959 
1960 	if (flags & HIST_FIELD_FL_VAR_REF) {
1961 		hist_field->fn_num = HIST_FIELD_FN_VAR_REF;
1962 		goto out;
1963 	}
1964 
1965 	if (flags & HIST_FIELD_FL_HITCOUNT) {
1966 		hist_field->fn_num = HIST_FIELD_FN_COUNTER;
1967 		hist_field->size = sizeof(u64);
1968 		hist_field->type = "u64";
1969 		goto out;
1970 	}
1971 
1972 	if (flags & HIST_FIELD_FL_CONST) {
1973 		hist_field->fn_num = HIST_FIELD_FN_CONST;
1974 		hist_field->size = sizeof(u64);
1975 		hist_field->type = kstrdup("u64", GFP_KERNEL);
1976 		if (!hist_field->type)
1977 			goto free;
1978 		goto out;
1979 	}
1980 
1981 	if (flags & HIST_FIELD_FL_STACKTRACE) {
1982 		hist_field->fn_num = HIST_FIELD_FN_NOP;
1983 		goto out;
1984 	}
1985 
1986 	if (flags & (HIST_FIELD_FL_LOG2 | HIST_FIELD_FL_BUCKET)) {
1987 		unsigned long fl = flags & ~(HIST_FIELD_FL_LOG2 | HIST_FIELD_FL_BUCKET);
1988 		hist_field->fn_num = flags & HIST_FIELD_FL_LOG2 ? HIST_FIELD_FN_LOG2 :
1989 			HIST_FIELD_FN_BUCKET;
1990 		hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
1991 		hist_field->size = hist_field->operands[0]->size;
1992 		hist_field->type = kstrdup_const(hist_field->operands[0]->type, GFP_KERNEL);
1993 		if (!hist_field->type)
1994 			goto free;
1995 		goto out;
1996 	}
1997 
1998 	if (flags & HIST_FIELD_FL_TIMESTAMP) {
1999 		hist_field->fn_num = HIST_FIELD_FN_TIMESTAMP;
2000 		hist_field->size = sizeof(u64);
2001 		hist_field->type = "u64";
2002 		goto out;
2003 	}
2004 
2005 	if (flags & HIST_FIELD_FL_CPU) {
2006 		hist_field->fn_num = HIST_FIELD_FN_CPU;
2007 		hist_field->size = sizeof(int);
2008 		hist_field->type = "unsigned int";
2009 		goto out;
2010 	}
2011 
2012 	if (WARN_ON_ONCE(!field))
2013 		goto out;
2014 
2015 	/* Pointers to strings are just pointers and dangerous to dereference */
2016 	if (is_string_field(field) &&
2017 	    (field->filter_type != FILTER_PTR_STRING)) {
2018 		flags |= HIST_FIELD_FL_STRING;
2019 
2020 		hist_field->size = MAX_FILTER_STR_VAL;
2021 		hist_field->type = kstrdup_const(field->type, GFP_KERNEL);
2022 		if (!hist_field->type)
2023 			goto free;
2024 
2025 		if (field->filter_type == FILTER_STATIC_STRING) {
2026 			hist_field->fn_num = HIST_FIELD_FN_STRING;
2027 			hist_field->size = field->size;
2028 		} else if (field->filter_type == FILTER_DYN_STRING) {
2029 			hist_field->fn_num = HIST_FIELD_FN_DYNSTRING;
2030 		} else if (field->filter_type == FILTER_RDYN_STRING)
2031 			hist_field->fn_num = HIST_FIELD_FN_RELDYNSTRING;
2032 		else
2033 			hist_field->fn_num = HIST_FIELD_FN_PSTRING;
2034 	} else {
2035 		hist_field->size = field->size;
2036 		hist_field->is_signed = field->is_signed;
2037 		hist_field->type = kstrdup_const(field->type, GFP_KERNEL);
2038 		if (!hist_field->type)
2039 			goto free;
2040 
2041 		hist_field->fn_num = select_value_fn(field->size,
2042 						     field->is_signed);
2043 		if (hist_field->fn_num == HIST_FIELD_FN_NOP) {
2044 			destroy_hist_field(hist_field, 0);
2045 			return NULL;
2046 		}
2047 	}
2048  out:
2049 	hist_field->field = field;
2050 	hist_field->flags = flags;
2051 
2052 	if (var_name) {
2053 		hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
2054 		if (!hist_field->var.name)
2055 			goto free;
2056 	}
2057 
2058 	return hist_field;
2059  free:
2060 	destroy_hist_field(hist_field, 0);
2061 	return NULL;
2062 }
2063 
2064 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
2065 {
2066 	unsigned int i;
2067 
2068 	for (i = 0; i < HIST_FIELDS_MAX; i++) {
2069 		if (hist_data->fields[i]) {
2070 			destroy_hist_field(hist_data->fields[i], 0);
2071 			hist_data->fields[i] = NULL;
2072 		}
2073 	}
2074 
2075 	for (i = 0; i < hist_data->n_var_refs; i++) {
2076 		WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
2077 		__destroy_hist_field(hist_data->var_refs[i]);
2078 		hist_data->var_refs[i] = NULL;
2079 	}
2080 }
2081 
2082 static int init_var_ref(struct hist_field *ref_field,
2083 			struct hist_field *var_field,
2084 			char *system, char *event_name)
2085 {
2086 	int err = 0;
2087 
2088 	ref_field->var.idx = var_field->var.idx;
2089 	ref_field->var.hist_data = var_field->hist_data;
2090 	ref_field->size = var_field->size;
2091 	ref_field->is_signed = var_field->is_signed;
2092 	ref_field->flags |= var_field->flags &
2093 		(HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2094 
2095 	if (system) {
2096 		ref_field->system = kstrdup(system, GFP_KERNEL);
2097 		if (!ref_field->system)
2098 			return -ENOMEM;
2099 	}
2100 
2101 	if (event_name) {
2102 		ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
2103 		if (!ref_field->event_name) {
2104 			err = -ENOMEM;
2105 			goto free;
2106 		}
2107 	}
2108 
2109 	if (var_field->var.name) {
2110 		ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
2111 		if (!ref_field->name) {
2112 			err = -ENOMEM;
2113 			goto free;
2114 		}
2115 	} else if (var_field->name) {
2116 		ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
2117 		if (!ref_field->name) {
2118 			err = -ENOMEM;
2119 			goto free;
2120 		}
2121 	}
2122 
2123 	ref_field->type = kstrdup_const(var_field->type, GFP_KERNEL);
2124 	if (!ref_field->type) {
2125 		err = -ENOMEM;
2126 		goto free;
2127 	}
2128  out:
2129 	return err;
2130  free:
2131 	kfree(ref_field->system);
2132 	ref_field->system = NULL;
2133 	kfree(ref_field->event_name);
2134 	ref_field->event_name = NULL;
2135 	kfree(ref_field->name);
2136 	ref_field->name = NULL;
2137 
2138 	goto out;
2139 }
2140 
2141 static int find_var_ref_idx(struct hist_trigger_data *hist_data,
2142 			    struct hist_field *var_field)
2143 {
2144 	struct hist_field *ref_field;
2145 	int i;
2146 
2147 	for (i = 0; i < hist_data->n_var_refs; i++) {
2148 		ref_field = hist_data->var_refs[i];
2149 		if (ref_field->var.idx == var_field->var.idx &&
2150 		    ref_field->var.hist_data == var_field->hist_data)
2151 			return i;
2152 	}
2153 
2154 	return -ENOENT;
2155 }
2156 
2157 /**
2158  * create_var_ref - Create a variable reference and attach it to trigger
2159  * @hist_data: The trigger that will be referencing the variable
2160  * @var_field: The VAR field to create a reference to
2161  * @system: The optional system string
2162  * @event_name: The optional event_name string
2163  *
2164  * Given a variable hist_field, create a VAR_REF hist_field that
2165  * represents a reference to it.
2166  *
2167  * This function also adds the reference to the trigger that
2168  * now references the variable.
2169  *
2170  * Return: The VAR_REF field if successful, NULL if not
2171  */
2172 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
2173 					 struct hist_field *var_field,
2174 					 char *system, char *event_name)
2175 {
2176 	unsigned long flags = HIST_FIELD_FL_VAR_REF;
2177 	struct hist_field *ref_field;
2178 	int i;
2179 
2180 	/* Check if the variable already exists */
2181 	for (i = 0; i < hist_data->n_var_refs; i++) {
2182 		ref_field = hist_data->var_refs[i];
2183 		if (ref_field->var.idx == var_field->var.idx &&
2184 		    ref_field->var.hist_data == var_field->hist_data) {
2185 			get_hist_field(ref_field);
2186 			return ref_field;
2187 		}
2188 	}
2189 	/* Sanity check to avoid out-of-bound write on 'hist_data->var_refs' */
2190 	if (hist_data->n_var_refs >= TRACING_MAP_VARS_MAX)
2191 		return NULL;
2192 	ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
2193 	if (ref_field) {
2194 		if (init_var_ref(ref_field, var_field, system, event_name)) {
2195 			destroy_hist_field(ref_field, 0);
2196 			return NULL;
2197 		}
2198 
2199 		hist_data->var_refs[hist_data->n_var_refs] = ref_field;
2200 		ref_field->var_ref_idx = hist_data->n_var_refs++;
2201 	}
2202 
2203 	return ref_field;
2204 }
2205 
2206 static bool is_var_ref(char *var_name)
2207 {
2208 	if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
2209 		return false;
2210 
2211 	return true;
2212 }
2213 
2214 static char *field_name_from_var(struct hist_trigger_data *hist_data,
2215 				 char *var_name)
2216 {
2217 	char *name, *field;
2218 	unsigned int i;
2219 
2220 	for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
2221 		name = hist_data->attrs->var_defs.name[i];
2222 
2223 		if (strcmp(var_name, name) == 0) {
2224 			field = hist_data->attrs->var_defs.expr[i];
2225 			if (contains_operator(field, NULL) || is_var_ref(field))
2226 				continue;
2227 			return field;
2228 		}
2229 	}
2230 
2231 	return NULL;
2232 }
2233 
2234 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
2235 				 char *system, char *event_name,
2236 				 char *var_name)
2237 {
2238 	struct trace_event_call *call;
2239 
2240 	if (system && event_name) {
2241 		call = hist_data->event_file->event_call;
2242 
2243 		if (strcmp(system, call->class->system) != 0)
2244 			return NULL;
2245 
2246 		if (strcmp(event_name, trace_event_name(call)) != 0)
2247 			return NULL;
2248 	}
2249 
2250 	if (!!system != !!event_name)
2251 		return NULL;
2252 
2253 	if (!is_var_ref(var_name))
2254 		return NULL;
2255 
2256 	var_name++;
2257 
2258 	return field_name_from_var(hist_data, var_name);
2259 }
2260 
2261 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
2262 					char *system, char *event_name,
2263 					char *var_name)
2264 {
2265 	struct hist_field *var_field = NULL, *ref_field = NULL;
2266 	struct trace_array *tr = hist_data->event_file->tr;
2267 
2268 	if (!is_var_ref(var_name))
2269 		return NULL;
2270 
2271 	var_name++;
2272 
2273 	var_field = find_event_var(hist_data, system, event_name, var_name);
2274 	if (var_field)
2275 		ref_field = create_var_ref(hist_data, var_field,
2276 					   system, event_name);
2277 
2278 	if (!ref_field)
2279 		hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name));
2280 
2281 	return ref_field;
2282 }
2283 
2284 static struct ftrace_event_field *
2285 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
2286 	    char *field_str, unsigned long *flags, unsigned long *buckets)
2287 {
2288 	struct ftrace_event_field *field = NULL;
2289 	char *field_name, *modifier, *str;
2290 	struct trace_array *tr = file->tr;
2291 
2292 	modifier = str = kstrdup(field_str, GFP_KERNEL);
2293 	if (!modifier)
2294 		return ERR_PTR(-ENOMEM);
2295 
2296 	field_name = strsep(&modifier, ".");
2297 	if (modifier) {
2298 		if (strcmp(modifier, "hex") == 0)
2299 			*flags |= HIST_FIELD_FL_HEX;
2300 		else if (strcmp(modifier, "sym") == 0)
2301 			*flags |= HIST_FIELD_FL_SYM;
2302 		/*
2303 		 * 'sym-offset' occurrences in the trigger string are modified
2304 		 * to 'symXoffset' to simplify arithmetic expression parsing.
2305 		 */
2306 		else if (strcmp(modifier, "symXoffset") == 0)
2307 			*flags |= HIST_FIELD_FL_SYM_OFFSET;
2308 		else if ((strcmp(modifier, "execname") == 0) &&
2309 			 (strcmp(field_name, "common_pid") == 0))
2310 			*flags |= HIST_FIELD_FL_EXECNAME;
2311 		else if (strcmp(modifier, "syscall") == 0)
2312 			*flags |= HIST_FIELD_FL_SYSCALL;
2313 		else if (strcmp(modifier, "log2") == 0)
2314 			*flags |= HIST_FIELD_FL_LOG2;
2315 		else if (strcmp(modifier, "usecs") == 0)
2316 			*flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
2317 		else if (strncmp(modifier, "bucket", 6) == 0) {
2318 			int ret;
2319 
2320 			modifier += 6;
2321 
2322 			if (*modifier == 's')
2323 				modifier++;
2324 			if (*modifier != '=')
2325 				goto error;
2326 			modifier++;
2327 			ret = kstrtoul(modifier, 0, buckets);
2328 			if (ret || !(*buckets))
2329 				goto error;
2330 			*flags |= HIST_FIELD_FL_BUCKET;
2331 		} else if (strncmp(modifier, "percent", 7) == 0) {
2332 			if (*flags & (HIST_FIELD_FL_VAR | HIST_FIELD_FL_KEY))
2333 				goto error;
2334 			*flags |= HIST_FIELD_FL_PERCENT;
2335 		} else if (strncmp(modifier, "graph", 5) == 0) {
2336 			if (*flags & (HIST_FIELD_FL_VAR | HIST_FIELD_FL_KEY))
2337 				goto error;
2338 			*flags |= HIST_FIELD_FL_GRAPH;
2339 		} else {
2340  error:
2341 			hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier));
2342 			field = ERR_PTR(-EINVAL);
2343 			goto out;
2344 		}
2345 	}
2346 
2347 	if (strcmp(field_name, "common_timestamp") == 0) {
2348 		*flags |= HIST_FIELD_FL_TIMESTAMP;
2349 		hist_data->enable_timestamps = true;
2350 		if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2351 			hist_data->attrs->ts_in_usecs = true;
2352 	} else if (strcmp(field_name, "common_cpu") == 0)
2353 		*flags |= HIST_FIELD_FL_CPU;
2354 	else if (strcmp(field_name, "hitcount") == 0)
2355 		*flags |= HIST_FIELD_FL_HITCOUNT;
2356 	else {
2357 		field = trace_find_event_field(file->event_call, field_name);
2358 		if (!field || !field->size) {
2359 			/*
2360 			 * For backward compatibility, if field_name
2361 			 * was "cpu", then we treat this the same as
2362 			 * common_cpu. This also works for "CPU".
2363 			 */
2364 			if (field && field->filter_type == FILTER_CPU) {
2365 				*flags |= HIST_FIELD_FL_CPU;
2366 			} else {
2367 				hist_err(tr, HIST_ERR_FIELD_NOT_FOUND,
2368 					 errpos(field_name));
2369 				field = ERR_PTR(-EINVAL);
2370 				goto out;
2371 			}
2372 		}
2373 	}
2374  out:
2375 	kfree(str);
2376 
2377 	return field;
2378 }
2379 
2380 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2381 				       struct hist_field *var_ref,
2382 				       char *var_name)
2383 {
2384 	struct hist_field *alias = NULL;
2385 	unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2386 
2387 	alias = create_hist_field(hist_data, NULL, flags, var_name);
2388 	if (!alias)
2389 		return NULL;
2390 
2391 	alias->fn_num = var_ref->fn_num;
2392 	alias->operands[0] = var_ref;
2393 
2394 	if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2395 		destroy_hist_field(alias, 0);
2396 		return NULL;
2397 	}
2398 
2399 	alias->var_ref_idx = var_ref->var_ref_idx;
2400 
2401 	return alias;
2402 }
2403 
2404 static struct hist_field *parse_const(struct hist_trigger_data *hist_data,
2405 				      char *str, char *var_name,
2406 				      unsigned long *flags)
2407 {
2408 	struct trace_array *tr = hist_data->event_file->tr;
2409 	struct hist_field *field = NULL;
2410 	u64 constant;
2411 
2412 	if (kstrtoull(str, 0, &constant)) {
2413 		hist_err(tr, HIST_ERR_EXPECT_NUMBER, errpos(str));
2414 		return NULL;
2415 	}
2416 
2417 	*flags |= HIST_FIELD_FL_CONST;
2418 	field = create_hist_field(hist_data, NULL, *flags, var_name);
2419 	if (!field)
2420 		return NULL;
2421 
2422 	field->constant = constant;
2423 
2424 	return field;
2425 }
2426 
2427 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2428 				     struct trace_event_file *file, char *str,
2429 				     unsigned long *flags, char *var_name)
2430 {
2431 	char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
2432 	struct ftrace_event_field *field = NULL;
2433 	struct hist_field *hist_field = NULL;
2434 	unsigned long buckets = 0;
2435 	int ret = 0;
2436 
2437 	if (isdigit(str[0])) {
2438 		hist_field = parse_const(hist_data, str, var_name, flags);
2439 		if (!hist_field) {
2440 			ret = -EINVAL;
2441 			goto out;
2442 		}
2443 		return hist_field;
2444 	}
2445 
2446 	s = strchr(str, '.');
2447 	if (s) {
2448 		s = strchr(++s, '.');
2449 		if (s) {
2450 			ref_system = strsep(&str, ".");
2451 			if (!str) {
2452 				ret = -EINVAL;
2453 				goto out;
2454 			}
2455 			ref_event = strsep(&str, ".");
2456 			if (!str) {
2457 				ret = -EINVAL;
2458 				goto out;
2459 			}
2460 			ref_var = str;
2461 		}
2462 	}
2463 
2464 	s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2465 	if (!s) {
2466 		hist_field = parse_var_ref(hist_data, ref_system,
2467 					   ref_event, ref_var);
2468 		if (hist_field) {
2469 			if (var_name) {
2470 				hist_field = create_alias(hist_data, hist_field, var_name);
2471 				if (!hist_field) {
2472 					ret = -ENOMEM;
2473 					goto out;
2474 				}
2475 			}
2476 			return hist_field;
2477 		}
2478 	} else
2479 		str = s;
2480 
2481 	field = parse_field(hist_data, file, str, flags, &buckets);
2482 	if (IS_ERR(field)) {
2483 		ret = PTR_ERR(field);
2484 		goto out;
2485 	}
2486 
2487 	hist_field = create_hist_field(hist_data, field, *flags, var_name);
2488 	if (!hist_field) {
2489 		ret = -ENOMEM;
2490 		goto out;
2491 	}
2492 	hist_field->buckets = buckets;
2493 
2494 	return hist_field;
2495  out:
2496 	return ERR_PTR(ret);
2497 }
2498 
2499 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2500 				     struct trace_event_file *file,
2501 				     char *str, unsigned long flags,
2502 				     char *var_name, unsigned int *n_subexprs);
2503 
2504 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2505 				      struct trace_event_file *file,
2506 				      char *str, unsigned long flags,
2507 				      char *var_name, unsigned int *n_subexprs)
2508 {
2509 	struct hist_field *operand1, *expr = NULL;
2510 	unsigned long operand_flags;
2511 	int ret = 0;
2512 	char *s;
2513 
2514 	/* Unary minus operator, increment n_subexprs */
2515 	++*n_subexprs;
2516 
2517 	/* we support only -(xxx) i.e. explicit parens required */
2518 
2519 	if (*n_subexprs > 3) {
2520 		hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2521 		ret = -EINVAL;
2522 		goto free;
2523 	}
2524 
2525 	str++; /* skip leading '-' */
2526 
2527 	s = strchr(str, '(');
2528 	if (s)
2529 		str++;
2530 	else {
2531 		ret = -EINVAL;
2532 		goto free;
2533 	}
2534 
2535 	s = strrchr(str, ')');
2536 	if (s) {
2537 		 /* unary minus not supported in sub-expressions */
2538 		if (*(s+1) != '\0') {
2539 			hist_err(file->tr, HIST_ERR_UNARY_MINUS_SUBEXPR,
2540 				 errpos(str));
2541 			ret = -EINVAL;
2542 			goto free;
2543 		}
2544 		*s = '\0';
2545 	}
2546 	else {
2547 		ret = -EINVAL; /* no closing ')' */
2548 		goto free;
2549 	}
2550 
2551 	flags |= HIST_FIELD_FL_EXPR;
2552 	expr = create_hist_field(hist_data, NULL, flags, var_name);
2553 	if (!expr) {
2554 		ret = -ENOMEM;
2555 		goto free;
2556 	}
2557 
2558 	operand_flags = 0;
2559 	operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, n_subexprs);
2560 	if (IS_ERR(operand1)) {
2561 		ret = PTR_ERR(operand1);
2562 		goto free;
2563 	}
2564 	if (operand1->flags & HIST_FIELD_FL_STRING) {
2565 		/* String type can not be the operand of unary operator. */
2566 		hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
2567 		destroy_hist_field(operand1, 0);
2568 		ret = -EINVAL;
2569 		goto free;
2570 	}
2571 
2572 	expr->flags |= operand1->flags &
2573 		(HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2574 	expr->fn_num = HIST_FIELD_FN_UMINUS;
2575 	expr->operands[0] = operand1;
2576 	expr->size = operand1->size;
2577 	expr->is_signed = operand1->is_signed;
2578 	expr->operator = FIELD_OP_UNARY_MINUS;
2579 	expr->name = expr_str(expr, 0);
2580 	expr->type = kstrdup_const(operand1->type, GFP_KERNEL);
2581 	if (!expr->type) {
2582 		ret = -ENOMEM;
2583 		goto free;
2584 	}
2585 
2586 	return expr;
2587  free:
2588 	destroy_hist_field(expr, 0);
2589 	return ERR_PTR(ret);
2590 }
2591 
2592 /*
2593  * If the operands are var refs, return pointers the
2594  * variable(s) referenced in var1 and var2, else NULL.
2595  */
2596 static int check_expr_operands(struct trace_array *tr,
2597 			       struct hist_field *operand1,
2598 			       struct hist_field *operand2,
2599 			       struct hist_field **var1,
2600 			       struct hist_field **var2)
2601 {
2602 	unsigned long operand1_flags = operand1->flags;
2603 	unsigned long operand2_flags = operand2->flags;
2604 
2605 	if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2606 	    (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2607 		struct hist_field *var;
2608 
2609 		var = find_var_field(operand1->var.hist_data, operand1->name);
2610 		if (!var)
2611 			return -EINVAL;
2612 		operand1_flags = var->flags;
2613 		*var1 = var;
2614 	}
2615 
2616 	if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2617 	    (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2618 		struct hist_field *var;
2619 
2620 		var = find_var_field(operand2->var.hist_data, operand2->name);
2621 		if (!var)
2622 			return -EINVAL;
2623 		operand2_flags = var->flags;
2624 		*var2 = var;
2625 	}
2626 
2627 	if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
2628 	    (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
2629 		hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0);
2630 		return -EINVAL;
2631 	}
2632 
2633 	return 0;
2634 }
2635 
2636 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2637 				     struct trace_event_file *file,
2638 				     char *str, unsigned long flags,
2639 				     char *var_name, unsigned int *n_subexprs)
2640 {
2641 	struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2642 	struct hist_field *var1 = NULL, *var2 = NULL;
2643 	unsigned long operand_flags, operand2_flags;
2644 	int field_op, ret = -EINVAL;
2645 	char *sep, *operand1_str;
2646 	enum hist_field_fn op_fn;
2647 	bool combine_consts;
2648 
2649 	if (*n_subexprs > 3) {
2650 		hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2651 		return ERR_PTR(-EINVAL);
2652 	}
2653 
2654 	field_op = contains_operator(str, &sep);
2655 
2656 	if (field_op == FIELD_OP_NONE)
2657 		return parse_atom(hist_data, file, str, &flags, var_name);
2658 
2659 	if (field_op == FIELD_OP_UNARY_MINUS)
2660 		return parse_unary(hist_data, file, str, flags, var_name, n_subexprs);
2661 
2662 	/* Binary operator found, increment n_subexprs */
2663 	++*n_subexprs;
2664 
2665 	/* Split the expression string at the root operator */
2666 	if (!sep)
2667 		return ERR_PTR(-EINVAL);
2668 
2669 	*sep = '\0';
2670 	operand1_str = str;
2671 	str = sep+1;
2672 
2673 	/* Binary operator requires both operands */
2674 	if (*operand1_str == '\0' || *str == '\0')
2675 		return ERR_PTR(-EINVAL);
2676 
2677 	operand_flags = 0;
2678 
2679 	/* LHS of string is an expression e.g. a+b in a+b+c */
2680 	operand1 = parse_expr(hist_data, file, operand1_str, operand_flags, NULL, n_subexprs);
2681 	if (IS_ERR(operand1))
2682 		return ERR_CAST(operand1);
2683 
2684 	if (operand1->flags & HIST_FIELD_FL_STRING) {
2685 		hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(operand1_str));
2686 		ret = -EINVAL;
2687 		goto free_op1;
2688 	}
2689 
2690 	/* RHS of string is another expression e.g. c in a+b+c */
2691 	operand_flags = 0;
2692 	operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, n_subexprs);
2693 	if (IS_ERR(operand2)) {
2694 		ret = PTR_ERR(operand2);
2695 		goto free_op1;
2696 	}
2697 	if (operand2->flags & HIST_FIELD_FL_STRING) {
2698 		hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
2699 		ret = -EINVAL;
2700 		goto free_operands;
2701 	}
2702 
2703 	switch (field_op) {
2704 	case FIELD_OP_MINUS:
2705 		op_fn = HIST_FIELD_FN_MINUS;
2706 		break;
2707 	case FIELD_OP_PLUS:
2708 		op_fn = HIST_FIELD_FN_PLUS;
2709 		break;
2710 	case FIELD_OP_DIV:
2711 		op_fn = HIST_FIELD_FN_DIV;
2712 		break;
2713 	case FIELD_OP_MULT:
2714 		op_fn = HIST_FIELD_FN_MULT;
2715 		break;
2716 	default:
2717 		ret = -EINVAL;
2718 		goto free_operands;
2719 	}
2720 
2721 	ret = check_expr_operands(file->tr, operand1, operand2, &var1, &var2);
2722 	if (ret)
2723 		goto free_operands;
2724 
2725 	operand_flags = var1 ? var1->flags : operand1->flags;
2726 	operand2_flags = var2 ? var2->flags : operand2->flags;
2727 
2728 	/*
2729 	 * If both operands are constant, the expression can be
2730 	 * collapsed to a single constant.
2731 	 */
2732 	combine_consts = operand_flags & operand2_flags & HIST_FIELD_FL_CONST;
2733 
2734 	flags |= combine_consts ? HIST_FIELD_FL_CONST : HIST_FIELD_FL_EXPR;
2735 
2736 	flags |= operand1->flags &
2737 		(HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2738 
2739 	expr = create_hist_field(hist_data, NULL, flags, var_name);
2740 	if (!expr) {
2741 		ret = -ENOMEM;
2742 		goto free_operands;
2743 	}
2744 
2745 	operand1->read_once = true;
2746 	operand2->read_once = true;
2747 
2748 	/* The operands are now owned and free'd by 'expr' */
2749 	expr->operands[0] = operand1;
2750 	expr->operands[1] = operand2;
2751 
2752 	if (field_op == FIELD_OP_DIV &&
2753 			operand2_flags & HIST_FIELD_FL_CONST) {
2754 		u64 divisor = var2 ? var2->constant : operand2->constant;
2755 
2756 		if (!divisor) {
2757 			hist_err(file->tr, HIST_ERR_DIVISION_BY_ZERO, errpos(str));
2758 			ret = -EDOM;
2759 			goto free_expr;
2760 		}
2761 
2762 		/*
2763 		 * Copy the divisor here so we don't have to look it up
2764 		 * later if this is a var ref
2765 		 */
2766 		operand2->constant = divisor;
2767 		op_fn = hist_field_get_div_fn(operand2);
2768 	}
2769 
2770 	expr->fn_num = op_fn;
2771 
2772 	if (combine_consts) {
2773 		if (var1)
2774 			expr->operands[0] = var1;
2775 		if (var2)
2776 			expr->operands[1] = var2;
2777 
2778 		expr->constant = hist_fn_call(expr, NULL, NULL, NULL, NULL);
2779 		expr->fn_num = HIST_FIELD_FN_CONST;
2780 
2781 		expr->operands[0] = NULL;
2782 		expr->operands[1] = NULL;
2783 
2784 		/*
2785 		 * var refs won't be destroyed immediately
2786 		 * See: destroy_hist_field()
2787 		 */
2788 		destroy_hist_field(operand2, 0);
2789 		destroy_hist_field(operand1, 0);
2790 
2791 		expr->name = expr_str(expr, 0);
2792 	} else {
2793 		/* The operand sizes should be the same, so just pick one */
2794 		expr->size = operand1->size;
2795 		expr->is_signed = operand1->is_signed;
2796 
2797 		expr->operator = field_op;
2798 		expr->type = kstrdup_const(operand1->type, GFP_KERNEL);
2799 		if (!expr->type) {
2800 			ret = -ENOMEM;
2801 			goto free_expr;
2802 		}
2803 
2804 		expr->name = expr_str(expr, 0);
2805 	}
2806 
2807 	return expr;
2808 
2809 free_operands:
2810 	destroy_hist_field(operand2, 0);
2811 free_op1:
2812 	destroy_hist_field(operand1, 0);
2813 	return ERR_PTR(ret);
2814 
2815 free_expr:
2816 	destroy_hist_field(expr, 0);
2817 	return ERR_PTR(ret);
2818 }
2819 
2820 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
2821 				 struct trace_event_file *file)
2822 {
2823 	struct event_trigger_data *test;
2824 
2825 	lockdep_assert_held(&event_mutex);
2826 
2827 	list_for_each_entry(test, &file->triggers, list) {
2828 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2829 			if (test->private_data == hist_data)
2830 				return test->filter_str;
2831 		}
2832 	}
2833 
2834 	return NULL;
2835 }
2836 
2837 static struct event_command trigger_hist_cmd;
2838 static int event_hist_trigger_parse(struct event_command *cmd_ops,
2839 				    struct trace_event_file *file,
2840 				    char *glob, char *cmd,
2841 				    char *param_and_filter);
2842 
2843 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
2844 			    struct hist_trigger_data *hist_data,
2845 			    unsigned int n_keys)
2846 {
2847 	struct hist_field *target_hist_field, *hist_field;
2848 	unsigned int n, i, j;
2849 
2850 	if (hist_data->n_fields - hist_data->n_vals != n_keys)
2851 		return false;
2852 
2853 	i = hist_data->n_vals;
2854 	j = target_hist_data->n_vals;
2855 
2856 	for (n = 0; n < n_keys; n++) {
2857 		hist_field = hist_data->fields[i + n];
2858 		target_hist_field = target_hist_data->fields[j + n];
2859 
2860 		if (strcmp(hist_field->type, target_hist_field->type) != 0)
2861 			return false;
2862 		if (hist_field->size != target_hist_field->size)
2863 			return false;
2864 		if (hist_field->is_signed != target_hist_field->is_signed)
2865 			return false;
2866 	}
2867 
2868 	return true;
2869 }
2870 
2871 static struct hist_trigger_data *
2872 find_compatible_hist(struct hist_trigger_data *target_hist_data,
2873 		     struct trace_event_file *file)
2874 {
2875 	struct hist_trigger_data *hist_data;
2876 	struct event_trigger_data *test;
2877 	unsigned int n_keys;
2878 
2879 	lockdep_assert_held(&event_mutex);
2880 
2881 	n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
2882 
2883 	list_for_each_entry(test, &file->triggers, list) {
2884 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2885 			hist_data = test->private_data;
2886 
2887 			if (compatible_keys(target_hist_data, hist_data, n_keys))
2888 				return hist_data;
2889 		}
2890 	}
2891 
2892 	return NULL;
2893 }
2894 
2895 static struct trace_event_file *event_file(struct trace_array *tr,
2896 					   char *system, char *event_name)
2897 {
2898 	struct trace_event_file *file;
2899 
2900 	file = __find_event_file(tr, system, event_name);
2901 	if (!file)
2902 		return ERR_PTR(-EINVAL);
2903 
2904 	return file;
2905 }
2906 
2907 static struct hist_field *
2908 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
2909 			 char *system, char *event_name, char *field_name)
2910 {
2911 	struct hist_field *event_var;
2912 	char *synthetic_name;
2913 
2914 	synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2915 	if (!synthetic_name)
2916 		return ERR_PTR(-ENOMEM);
2917 
2918 	strcpy(synthetic_name, "synthetic_");
2919 	strcat(synthetic_name, field_name);
2920 
2921 	event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
2922 
2923 	kfree(synthetic_name);
2924 
2925 	return event_var;
2926 }
2927 
2928 /**
2929  * create_field_var_hist - Automatically create a histogram and var for a field
2930  * @target_hist_data: The target hist trigger
2931  * @subsys_name: Optional subsystem name
2932  * @event_name: Optional event name
2933  * @field_name: The name of the field (and the resulting variable)
2934  *
2935  * Hist trigger actions fetch data from variables, not directly from
2936  * events.  However, for convenience, users are allowed to directly
2937  * specify an event field in an action, which will be automatically
2938  * converted into a variable on their behalf.
2939  *
2940  * If a user specifies a field on an event that isn't the event the
2941  * histogram currently being defined (the target event histogram), the
2942  * only way that can be accomplished is if a new hist trigger is
2943  * created and the field variable defined on that.
2944  *
2945  * This function creates a new histogram compatible with the target
2946  * event (meaning a histogram with the same key as the target
2947  * histogram), and creates a variable for the specified field, but
2948  * with 'synthetic_' prepended to the variable name in order to avoid
2949  * collision with normal field variables.
2950  *
2951  * Return: The variable created for the field.
2952  */
2953 static struct hist_field *
2954 create_field_var_hist(struct hist_trigger_data *target_hist_data,
2955 		      char *subsys_name, char *event_name, char *field_name)
2956 {
2957 	struct trace_array *tr = target_hist_data->event_file->tr;
2958 	struct hist_trigger_data *hist_data;
2959 	unsigned int i, n, first = true;
2960 	struct field_var_hist *var_hist;
2961 	struct trace_event_file *file;
2962 	struct hist_field *key_field;
2963 	struct hist_field *event_var;
2964 	char *saved_filter;
2965 	char *cmd;
2966 	int ret;
2967 
2968 	if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
2969 		hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
2970 		return ERR_PTR(-EINVAL);
2971 	}
2972 
2973 	file = event_file(tr, subsys_name, event_name);
2974 
2975 	if (IS_ERR(file)) {
2976 		hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name));
2977 		ret = PTR_ERR(file);
2978 		return ERR_PTR(ret);
2979 	}
2980 
2981 	/*
2982 	 * Look for a histogram compatible with target.  We'll use the
2983 	 * found histogram specification to create a new matching
2984 	 * histogram with our variable on it.  target_hist_data is not
2985 	 * yet a registered histogram so we can't use that.
2986 	 */
2987 	hist_data = find_compatible_hist(target_hist_data, file);
2988 	if (!hist_data) {
2989 		hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name));
2990 		return ERR_PTR(-EINVAL);
2991 	}
2992 
2993 	/* See if a synthetic field variable has already been created */
2994 	event_var = find_synthetic_field_var(target_hist_data, subsys_name,
2995 					     event_name, field_name);
2996 	if (!IS_ERR_OR_NULL(event_var))
2997 		return event_var;
2998 
2999 	var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
3000 	if (!var_hist)
3001 		return ERR_PTR(-ENOMEM);
3002 
3003 	cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3004 	if (!cmd) {
3005 		kfree(var_hist);
3006 		return ERR_PTR(-ENOMEM);
3007 	}
3008 
3009 	/* Use the same keys as the compatible histogram */
3010 	strcat(cmd, "keys=");
3011 
3012 	for_each_hist_key_field(i, hist_data) {
3013 		key_field = hist_data->fields[i];
3014 		if (!first)
3015 			strcat(cmd, ",");
3016 		strcat(cmd, key_field->field->name);
3017 		first = false;
3018 	}
3019 
3020 	/* Create the synthetic field variable specification */
3021 	strcat(cmd, ":synthetic_");
3022 	strcat(cmd, field_name);
3023 	strcat(cmd, "=");
3024 	strcat(cmd, field_name);
3025 
3026 	/* Use the same filter as the compatible histogram */
3027 	saved_filter = find_trigger_filter(hist_data, file);
3028 	if (saved_filter) {
3029 		strcat(cmd, " if ");
3030 		strcat(cmd, saved_filter);
3031 	}
3032 
3033 	var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
3034 	if (!var_hist->cmd) {
3035 		kfree(cmd);
3036 		kfree(var_hist);
3037 		return ERR_PTR(-ENOMEM);
3038 	}
3039 
3040 	/* Save the compatible histogram information */
3041 	var_hist->hist_data = hist_data;
3042 
3043 	/* Create the new histogram with our variable */
3044 	ret = event_hist_trigger_parse(&trigger_hist_cmd, file,
3045 				       "", "hist", cmd);
3046 	if (ret) {
3047 		kfree(cmd);
3048 		kfree(var_hist->cmd);
3049 		kfree(var_hist);
3050 		hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name));
3051 		return ERR_PTR(ret);
3052 	}
3053 
3054 	kfree(cmd);
3055 
3056 	/* If we can't find the variable, something went wrong */
3057 	event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3058 					     event_name, field_name);
3059 	if (IS_ERR_OR_NULL(event_var)) {
3060 		kfree(var_hist->cmd);
3061 		kfree(var_hist);
3062 		hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
3063 		return ERR_PTR(-EINVAL);
3064 	}
3065 
3066 	n = target_hist_data->n_field_var_hists;
3067 	target_hist_data->field_var_hists[n] = var_hist;
3068 	target_hist_data->n_field_var_hists++;
3069 
3070 	return event_var;
3071 }
3072 
3073 static struct hist_field *
3074 find_target_event_var(struct hist_trigger_data *hist_data,
3075 		      char *subsys_name, char *event_name, char *var_name)
3076 {
3077 	struct trace_event_file *file = hist_data->event_file;
3078 	struct hist_field *hist_field = NULL;
3079 
3080 	if (subsys_name) {
3081 		struct trace_event_call *call;
3082 
3083 		if (!event_name)
3084 			return NULL;
3085 
3086 		call = file->event_call;
3087 
3088 		if (strcmp(subsys_name, call->class->system) != 0)
3089 			return NULL;
3090 
3091 		if (strcmp(event_name, trace_event_name(call)) != 0)
3092 			return NULL;
3093 	}
3094 
3095 	hist_field = find_var_field(hist_data, var_name);
3096 
3097 	return hist_field;
3098 }
3099 
3100 static inline void __update_field_vars(struct tracing_map_elt *elt,
3101 				       struct trace_buffer *buffer,
3102 				       struct ring_buffer_event *rbe,
3103 				       void *rec,
3104 				       struct field_var **field_vars,
3105 				       unsigned int n_field_vars,
3106 				       unsigned int field_var_str_start)
3107 {
3108 	struct hist_elt_data *elt_data = elt->private_data;
3109 	unsigned int i, j, var_idx;
3110 	u64 var_val;
3111 
3112 	for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
3113 		struct field_var *field_var = field_vars[i];
3114 		struct hist_field *var = field_var->var;
3115 		struct hist_field *val = field_var->val;
3116 
3117 		var_val = hist_fn_call(val, elt, buffer, rbe, rec);
3118 		var_idx = var->var.idx;
3119 
3120 		if (val->flags & HIST_FIELD_FL_STRING) {
3121 			char *str = elt_data->field_var_str[j++];
3122 			char *val_str = (char *)(uintptr_t)var_val;
3123 			unsigned int size;
3124 
3125 			size = min(val->size, STR_VAR_LEN_MAX);
3126 			strscpy(str, val_str, size);
3127 			var_val = (u64)(uintptr_t)str;
3128 		}
3129 		tracing_map_set_var(elt, var_idx, var_val);
3130 	}
3131 }
3132 
3133 static void update_field_vars(struct hist_trigger_data *hist_data,
3134 			      struct tracing_map_elt *elt,
3135 			      struct trace_buffer *buffer,
3136 			      struct ring_buffer_event *rbe,
3137 			      void *rec)
3138 {
3139 	__update_field_vars(elt, buffer, rbe, rec, hist_data->field_vars,
3140 			    hist_data->n_field_vars, 0);
3141 }
3142 
3143 static void save_track_data_vars(struct hist_trigger_data *hist_data,
3144 				 struct tracing_map_elt *elt,
3145 				 struct trace_buffer *buffer,  void *rec,
3146 				 struct ring_buffer_event *rbe, void *key,
3147 				 struct action_data *data, u64 *var_ref_vals)
3148 {
3149 	__update_field_vars(elt, buffer, rbe, rec, hist_data->save_vars,
3150 			    hist_data->n_save_vars, hist_data->n_field_var_str);
3151 }
3152 
3153 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
3154 				     struct trace_event_file *file,
3155 				     char *name, int size, const char *type)
3156 {
3157 	struct hist_field *var;
3158 	int idx;
3159 
3160 	if (find_var(hist_data, file, name) && !hist_data->remove) {
3161 		var = ERR_PTR(-EINVAL);
3162 		goto out;
3163 	}
3164 
3165 	var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3166 	if (!var) {
3167 		var = ERR_PTR(-ENOMEM);
3168 		goto out;
3169 	}
3170 
3171 	idx = tracing_map_add_var(hist_data->map);
3172 	if (idx < 0) {
3173 		kfree(var);
3174 		var = ERR_PTR(-EINVAL);
3175 		goto out;
3176 	}
3177 
3178 	var->ref = 1;
3179 	var->flags = HIST_FIELD_FL_VAR;
3180 	var->var.idx = idx;
3181 	var->var.hist_data = var->hist_data = hist_data;
3182 	var->size = size;
3183 	var->var.name = kstrdup(name, GFP_KERNEL);
3184 	var->type = kstrdup_const(type, GFP_KERNEL);
3185 	if (!var->var.name || !var->type) {
3186 		kfree_const(var->type);
3187 		kfree(var->var.name);
3188 		kfree(var);
3189 		var = ERR_PTR(-ENOMEM);
3190 	}
3191  out:
3192 	return var;
3193 }
3194 
3195 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
3196 					  struct trace_event_file *file,
3197 					  char *field_name)
3198 {
3199 	struct hist_field *val = NULL, *var = NULL;
3200 	unsigned long flags = HIST_FIELD_FL_VAR;
3201 	struct trace_array *tr = file->tr;
3202 	struct field_var *field_var;
3203 	int ret = 0;
3204 
3205 	if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
3206 		hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
3207 		ret = -EINVAL;
3208 		goto err;
3209 	}
3210 
3211 	val = parse_atom(hist_data, file, field_name, &flags, NULL);
3212 	if (IS_ERR(val)) {
3213 		hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name));
3214 		ret = PTR_ERR(val);
3215 		goto err;
3216 	}
3217 
3218 	var = create_var(hist_data, file, field_name, val->size, val->type);
3219 	if (IS_ERR(var)) {
3220 		hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name));
3221 		kfree(val);
3222 		ret = PTR_ERR(var);
3223 		goto err;
3224 	}
3225 
3226 	field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
3227 	if (!field_var) {
3228 		kfree(val);
3229 		kfree(var);
3230 		ret =  -ENOMEM;
3231 		goto err;
3232 	}
3233 
3234 	field_var->var = var;
3235 	field_var->val = val;
3236  out:
3237 	return field_var;
3238  err:
3239 	field_var = ERR_PTR(ret);
3240 	goto out;
3241 }
3242 
3243 /**
3244  * create_target_field_var - Automatically create a variable for a field
3245  * @target_hist_data: The target hist trigger
3246  * @subsys_name: Optional subsystem name
3247  * @event_name: Optional event name
3248  * @var_name: The name of the field (and the resulting variable)
3249  *
3250  * Hist trigger actions fetch data from variables, not directly from
3251  * events.  However, for convenience, users are allowed to directly
3252  * specify an event field in an action, which will be automatically
3253  * converted into a variable on their behalf.
3254  *
3255  * This function creates a field variable with the name var_name on
3256  * the hist trigger currently being defined on the target event.  If
3257  * subsys_name and event_name are specified, this function simply
3258  * verifies that they do in fact match the target event subsystem and
3259  * event name.
3260  *
3261  * Return: The variable created for the field.
3262  */
3263 static struct field_var *
3264 create_target_field_var(struct hist_trigger_data *target_hist_data,
3265 			char *subsys_name, char *event_name, char *var_name)
3266 {
3267 	struct trace_event_file *file = target_hist_data->event_file;
3268 
3269 	if (subsys_name) {
3270 		struct trace_event_call *call;
3271 
3272 		if (!event_name)
3273 			return NULL;
3274 
3275 		call = file->event_call;
3276 
3277 		if (strcmp(subsys_name, call->class->system) != 0)
3278 			return NULL;
3279 
3280 		if (strcmp(event_name, trace_event_name(call)) != 0)
3281 			return NULL;
3282 	}
3283 
3284 	return create_field_var(target_hist_data, file, var_name);
3285 }
3286 
3287 static bool check_track_val_max(u64 track_val, u64 var_val)
3288 {
3289 	if (var_val <= track_val)
3290 		return false;
3291 
3292 	return true;
3293 }
3294 
3295 static bool check_track_val_changed(u64 track_val, u64 var_val)
3296 {
3297 	if (var_val == track_val)
3298 		return false;
3299 
3300 	return true;
3301 }
3302 
3303 static u64 get_track_val(struct hist_trigger_data *hist_data,
3304 			 struct tracing_map_elt *elt,
3305 			 struct action_data *data)
3306 {
3307 	unsigned int track_var_idx = data->track_data.track_var->var.idx;
3308 	u64 track_val;
3309 
3310 	track_val = tracing_map_read_var(elt, track_var_idx);
3311 
3312 	return track_val;
3313 }
3314 
3315 static void save_track_val(struct hist_trigger_data *hist_data,
3316 			   struct tracing_map_elt *elt,
3317 			   struct action_data *data, u64 var_val)
3318 {
3319 	unsigned int track_var_idx = data->track_data.track_var->var.idx;
3320 
3321 	tracing_map_set_var(elt, track_var_idx, var_val);
3322 }
3323 
3324 static void save_track_data(struct hist_trigger_data *hist_data,
3325 			    struct tracing_map_elt *elt,
3326 			    struct trace_buffer *buffer, void *rec,
3327 			    struct ring_buffer_event *rbe, void *key,
3328 			    struct action_data *data, u64 *var_ref_vals)
3329 {
3330 	if (data->track_data.save_data)
3331 		data->track_data.save_data(hist_data, elt, buffer, rec, rbe,
3332 					   key, data, var_ref_vals);
3333 }
3334 
3335 static bool check_track_val(struct tracing_map_elt *elt,
3336 			    struct action_data *data,
3337 			    u64 var_val)
3338 {
3339 	struct hist_trigger_data *hist_data;
3340 	u64 track_val;
3341 
3342 	hist_data = data->track_data.track_var->hist_data;
3343 	track_val = get_track_val(hist_data, elt, data);
3344 
3345 	return data->track_data.check_val(track_val, var_val);
3346 }
3347 
3348 #ifdef CONFIG_TRACER_SNAPSHOT
3349 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3350 {
3351 	/* called with tr->max_lock held */
3352 	struct track_data *track_data = tr->cond_snapshot->cond_data;
3353 	struct hist_elt_data *elt_data, *track_elt_data;
3354 	struct snapshot_context *context = cond_data;
3355 	struct action_data *action;
3356 	u64 track_val;
3357 
3358 	if (!track_data)
3359 		return false;
3360 
3361 	action = track_data->action_data;
3362 
3363 	track_val = get_track_val(track_data->hist_data, context->elt,
3364 				  track_data->action_data);
3365 
3366 	if (!action->track_data.check_val(track_data->track_val, track_val))
3367 		return false;
3368 
3369 	track_data->track_val = track_val;
3370 	memcpy(track_data->key, context->key, track_data->key_len);
3371 
3372 	elt_data = context->elt->private_data;
3373 	track_elt_data = track_data->elt.private_data;
3374 	if (elt_data->comm)
3375 		strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
3376 
3377 	track_data->updated = true;
3378 
3379 	return true;
3380 }
3381 
3382 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3383 				     struct tracing_map_elt *elt,
3384 				     struct trace_buffer *buffer, void *rec,
3385 				     struct ring_buffer_event *rbe, void *key,
3386 				     struct action_data *data,
3387 				     u64 *var_ref_vals)
3388 {
3389 	struct trace_event_file *file = hist_data->event_file;
3390 	struct snapshot_context context;
3391 
3392 	context.elt = elt;
3393 	context.key = key;
3394 
3395 	tracing_snapshot_cond(file->tr, &context);
3396 }
3397 
3398 static void hist_trigger_print_key(struct seq_file *m,
3399 				   struct hist_trigger_data *hist_data,
3400 				   void *key,
3401 				   struct tracing_map_elt *elt);
3402 
3403 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
3404 {
3405 	unsigned int i;
3406 
3407 	if (!hist_data->n_actions)
3408 		return NULL;
3409 
3410 	for (i = 0; i < hist_data->n_actions; i++) {
3411 		struct action_data *data = hist_data->actions[i];
3412 
3413 		if (data->action == ACTION_SNAPSHOT)
3414 			return data;
3415 	}
3416 
3417 	return NULL;
3418 }
3419 
3420 static void track_data_snapshot_print(struct seq_file *m,
3421 				      struct hist_trigger_data *hist_data)
3422 {
3423 	struct trace_event_file *file = hist_data->event_file;
3424 	struct track_data *track_data;
3425 	struct action_data *action;
3426 
3427 	track_data = tracing_cond_snapshot_data(file->tr);
3428 	if (!track_data)
3429 		return;
3430 
3431 	if (!track_data->updated)
3432 		return;
3433 
3434 	action = snapshot_action(hist_data);
3435 	if (!action)
3436 		return;
3437 
3438 	seq_puts(m, "\nSnapshot taken (see tracing/snapshot).  Details:\n");
3439 	seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
3440 		   action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
3441 		   action->track_data.var_str, track_data->track_val);
3442 
3443 	seq_puts(m, "\ttriggered by event with key: ");
3444 	hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
3445 	seq_putc(m, '\n');
3446 }
3447 #else
3448 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3449 {
3450 	return false;
3451 }
3452 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3453 				     struct tracing_map_elt *elt,
3454 				     struct trace_buffer *buffer, void *rec,
3455 				     struct ring_buffer_event *rbe, void *key,
3456 				     struct action_data *data,
3457 				     u64 *var_ref_vals) {}
3458 static void track_data_snapshot_print(struct seq_file *m,
3459 				      struct hist_trigger_data *hist_data) {}
3460 #endif /* CONFIG_TRACER_SNAPSHOT */
3461 
3462 static void track_data_print(struct seq_file *m,
3463 			     struct hist_trigger_data *hist_data,
3464 			     struct tracing_map_elt *elt,
3465 			     struct action_data *data)
3466 {
3467 	u64 track_val = get_track_val(hist_data, elt, data);
3468 	unsigned int i, save_var_idx;
3469 
3470 	if (data->handler == HANDLER_ONMAX)
3471 		seq_printf(m, "\n\tmax: %10llu", track_val);
3472 	else if (data->handler == HANDLER_ONCHANGE)
3473 		seq_printf(m, "\n\tchanged: %10llu", track_val);
3474 
3475 	if (data->action == ACTION_SNAPSHOT)
3476 		return;
3477 
3478 	for (i = 0; i < hist_data->n_save_vars; i++) {
3479 		struct hist_field *save_val = hist_data->save_vars[i]->val;
3480 		struct hist_field *save_var = hist_data->save_vars[i]->var;
3481 		u64 val;
3482 
3483 		save_var_idx = save_var->var.idx;
3484 
3485 		val = tracing_map_read_var(elt, save_var_idx);
3486 
3487 		if (save_val->flags & HIST_FIELD_FL_STRING) {
3488 			seq_printf(m, "  %s: %-32s", save_var->var.name,
3489 				   (char *)(uintptr_t)(val));
3490 		} else
3491 			seq_printf(m, "  %s: %10llu", save_var->var.name, val);
3492 	}
3493 }
3494 
3495 static void ontrack_action(struct hist_trigger_data *hist_data,
3496 			   struct tracing_map_elt *elt,
3497 			   struct trace_buffer *buffer, void *rec,
3498 			   struct ring_buffer_event *rbe, void *key,
3499 			   struct action_data *data, u64 *var_ref_vals)
3500 {
3501 	u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
3502 
3503 	if (check_track_val(elt, data, var_val)) {
3504 		save_track_val(hist_data, elt, data, var_val);
3505 		save_track_data(hist_data, elt, buffer, rec, rbe,
3506 				key, data, var_ref_vals);
3507 	}
3508 }
3509 
3510 static void action_data_destroy(struct action_data *data)
3511 {
3512 	unsigned int i;
3513 
3514 	lockdep_assert_held(&event_mutex);
3515 
3516 	kfree(data->action_name);
3517 
3518 	for (i = 0; i < data->n_params; i++)
3519 		kfree(data->params[i]);
3520 
3521 	if (data->synth_event)
3522 		data->synth_event->ref--;
3523 
3524 	kfree(data->synth_event_name);
3525 
3526 	kfree(data);
3527 }
3528 
3529 static void track_data_destroy(struct hist_trigger_data *hist_data,
3530 			       struct action_data *data)
3531 {
3532 	struct trace_event_file *file = hist_data->event_file;
3533 
3534 	destroy_hist_field(data->track_data.track_var, 0);
3535 
3536 	if (data->action == ACTION_SNAPSHOT) {
3537 		struct track_data *track_data;
3538 
3539 		track_data = tracing_cond_snapshot_data(file->tr);
3540 		if (track_data && track_data->hist_data == hist_data) {
3541 			tracing_snapshot_cond_disable(file->tr);
3542 			track_data_free(track_data);
3543 		}
3544 	}
3545 
3546 	kfree(data->track_data.var_str);
3547 
3548 	action_data_destroy(data);
3549 }
3550 
3551 static int action_create(struct hist_trigger_data *hist_data,
3552 			 struct action_data *data);
3553 
3554 static int track_data_create(struct hist_trigger_data *hist_data,
3555 			     struct action_data *data)
3556 {
3557 	struct hist_field *var_field, *ref_field, *track_var = NULL;
3558 	struct trace_event_file *file = hist_data->event_file;
3559 	struct trace_array *tr = file->tr;
3560 	char *track_data_var_str;
3561 	int ret = 0;
3562 
3563 	track_data_var_str = data->track_data.var_str;
3564 	if (track_data_var_str[0] != '$') {
3565 		hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str));
3566 		return -EINVAL;
3567 	}
3568 	track_data_var_str++;
3569 
3570 	var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
3571 	if (!var_field) {
3572 		hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str));
3573 		return -EINVAL;
3574 	}
3575 
3576 	ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
3577 	if (!ref_field)
3578 		return -ENOMEM;
3579 
3580 	data->track_data.var_ref = ref_field;
3581 
3582 	if (data->handler == HANDLER_ONMAX)
3583 		track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
3584 	if (IS_ERR(track_var)) {
3585 		hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3586 		ret = PTR_ERR(track_var);
3587 		goto out;
3588 	}
3589 
3590 	if (data->handler == HANDLER_ONCHANGE)
3591 		track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
3592 	if (IS_ERR(track_var)) {
3593 		hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3594 		ret = PTR_ERR(track_var);
3595 		goto out;
3596 	}
3597 	data->track_data.track_var = track_var;
3598 
3599 	ret = action_create(hist_data, data);
3600  out:
3601 	return ret;
3602 }
3603 
3604 static int parse_action_params(struct trace_array *tr, char *params,
3605 			       struct action_data *data)
3606 {
3607 	char *param, *saved_param;
3608 	bool first_param = true;
3609 	int ret = 0;
3610 
3611 	while (params) {
3612 		if (data->n_params >= SYNTH_FIELDS_MAX) {
3613 			hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0);
3614 			ret = -EINVAL;
3615 			goto out;
3616 		}
3617 
3618 		param = strsep(&params, ",");
3619 		if (!param) {
3620 			hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0);
3621 			ret = -EINVAL;
3622 			goto out;
3623 		}
3624 
3625 		param = strstrip(param);
3626 		if (strlen(param) < 2) {
3627 			hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param));
3628 			ret = -EINVAL;
3629 			goto out;
3630 		}
3631 
3632 		saved_param = kstrdup(param, GFP_KERNEL);
3633 		if (!saved_param) {
3634 			ret = -ENOMEM;
3635 			goto out;
3636 		}
3637 
3638 		if (first_param && data->use_trace_keyword) {
3639 			data->synth_event_name = saved_param;
3640 			first_param = false;
3641 			continue;
3642 		}
3643 		first_param = false;
3644 
3645 		data->params[data->n_params++] = saved_param;
3646 	}
3647  out:
3648 	return ret;
3649 }
3650 
3651 static int action_parse(struct trace_array *tr, char *str, struct action_data *data,
3652 			enum handler_id handler)
3653 {
3654 	char *action_name;
3655 	int ret = 0;
3656 
3657 	strsep(&str, ".");
3658 	if (!str) {
3659 		hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3660 		ret = -EINVAL;
3661 		goto out;
3662 	}
3663 
3664 	action_name = strsep(&str, "(");
3665 	if (!action_name || !str) {
3666 		hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3667 		ret = -EINVAL;
3668 		goto out;
3669 	}
3670 
3671 	if (str_has_prefix(action_name, "save")) {
3672 		char *params = strsep(&str, ")");
3673 
3674 		if (!params) {
3675 			hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0);
3676 			ret = -EINVAL;
3677 			goto out;
3678 		}
3679 
3680 		ret = parse_action_params(tr, params, data);
3681 		if (ret)
3682 			goto out;
3683 
3684 		if (handler == HANDLER_ONMAX)
3685 			data->track_data.check_val = check_track_val_max;
3686 		else if (handler == HANDLER_ONCHANGE)
3687 			data->track_data.check_val = check_track_val_changed;
3688 		else {
3689 			hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3690 			ret = -EINVAL;
3691 			goto out;
3692 		}
3693 
3694 		data->track_data.save_data = save_track_data_vars;
3695 		data->fn = ontrack_action;
3696 		data->action = ACTION_SAVE;
3697 	} else if (str_has_prefix(action_name, "snapshot")) {
3698 		char *params = strsep(&str, ")");
3699 
3700 		if (!str) {
3701 			hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params));
3702 			ret = -EINVAL;
3703 			goto out;
3704 		}
3705 
3706 		if (handler == HANDLER_ONMAX)
3707 			data->track_data.check_val = check_track_val_max;
3708 		else if (handler == HANDLER_ONCHANGE)
3709 			data->track_data.check_val = check_track_val_changed;
3710 		else {
3711 			hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3712 			ret = -EINVAL;
3713 			goto out;
3714 		}
3715 
3716 		data->track_data.save_data = save_track_data_snapshot;
3717 		data->fn = ontrack_action;
3718 		data->action = ACTION_SNAPSHOT;
3719 	} else {
3720 		char *params = strsep(&str, ")");
3721 
3722 		if (str_has_prefix(action_name, "trace"))
3723 			data->use_trace_keyword = true;
3724 
3725 		if (params) {
3726 			ret = parse_action_params(tr, params, data);
3727 			if (ret)
3728 				goto out;
3729 		}
3730 
3731 		if (handler == HANDLER_ONMAX)
3732 			data->track_data.check_val = check_track_val_max;
3733 		else if (handler == HANDLER_ONCHANGE)
3734 			data->track_data.check_val = check_track_val_changed;
3735 
3736 		if (handler != HANDLER_ONMATCH) {
3737 			data->track_data.save_data = action_trace;
3738 			data->fn = ontrack_action;
3739 		} else
3740 			data->fn = action_trace;
3741 
3742 		data->action = ACTION_TRACE;
3743 	}
3744 
3745 	data->action_name = kstrdup(action_name, GFP_KERNEL);
3746 	if (!data->action_name) {
3747 		ret = -ENOMEM;
3748 		goto out;
3749 	}
3750 
3751 	data->handler = handler;
3752  out:
3753 	return ret;
3754 }
3755 
3756 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
3757 					    char *str, enum handler_id handler)
3758 {
3759 	struct action_data *data;
3760 	int ret = -EINVAL;
3761 	char *var_str;
3762 
3763 	data = kzalloc(sizeof(*data), GFP_KERNEL);
3764 	if (!data)
3765 		return ERR_PTR(-ENOMEM);
3766 
3767 	var_str = strsep(&str, ")");
3768 	if (!var_str || !str) {
3769 		ret = -EINVAL;
3770 		goto free;
3771 	}
3772 
3773 	data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
3774 	if (!data->track_data.var_str) {
3775 		ret = -ENOMEM;
3776 		goto free;
3777 	}
3778 
3779 	ret = action_parse(hist_data->event_file->tr, str, data, handler);
3780 	if (ret)
3781 		goto free;
3782  out:
3783 	return data;
3784  free:
3785 	track_data_destroy(hist_data, data);
3786 	data = ERR_PTR(ret);
3787 	goto out;
3788 }
3789 
3790 static void onmatch_destroy(struct action_data *data)
3791 {
3792 	kfree(data->match_data.event);
3793 	kfree(data->match_data.event_system);
3794 
3795 	action_data_destroy(data);
3796 }
3797 
3798 static void destroy_field_var(struct field_var *field_var)
3799 {
3800 	if (!field_var)
3801 		return;
3802 
3803 	destroy_hist_field(field_var->var, 0);
3804 	destroy_hist_field(field_var->val, 0);
3805 
3806 	kfree(field_var);
3807 }
3808 
3809 static void destroy_field_vars(struct hist_trigger_data *hist_data)
3810 {
3811 	unsigned int i;
3812 
3813 	for (i = 0; i < hist_data->n_field_vars; i++)
3814 		destroy_field_var(hist_data->field_vars[i]);
3815 
3816 	for (i = 0; i < hist_data->n_save_vars; i++)
3817 		destroy_field_var(hist_data->save_vars[i]);
3818 }
3819 
3820 static void save_field_var(struct hist_trigger_data *hist_data,
3821 			   struct field_var *field_var)
3822 {
3823 	hist_data->field_vars[hist_data->n_field_vars++] = field_var;
3824 
3825 	if (field_var->val->flags & HIST_FIELD_FL_STRING)
3826 		hist_data->n_field_var_str++;
3827 }
3828 
3829 
3830 static int check_synth_field(struct synth_event *event,
3831 			     struct hist_field *hist_field,
3832 			     unsigned int field_pos)
3833 {
3834 	struct synth_field *field;
3835 
3836 	if (field_pos >= event->n_fields)
3837 		return -EINVAL;
3838 
3839 	field = event->fields[field_pos];
3840 
3841 	/*
3842 	 * A dynamic string synth field can accept static or
3843 	 * dynamic. A static string synth field can only accept a
3844 	 * same-sized static string, which is checked for later.
3845 	 */
3846 	if (strstr(hist_field->type, "char[") && field->is_string
3847 	    && field->is_dynamic)
3848 		return 0;
3849 
3850 	if (strcmp(field->type, hist_field->type) != 0) {
3851 		if (field->size != hist_field->size ||
3852 		    (!field->is_string && field->is_signed != hist_field->is_signed))
3853 			return -EINVAL;
3854 	}
3855 
3856 	return 0;
3857 }
3858 
3859 static struct hist_field *
3860 trace_action_find_var(struct hist_trigger_data *hist_data,
3861 		      struct action_data *data,
3862 		      char *system, char *event, char *var)
3863 {
3864 	struct trace_array *tr = hist_data->event_file->tr;
3865 	struct hist_field *hist_field;
3866 
3867 	var++; /* skip '$' */
3868 
3869 	hist_field = find_target_event_var(hist_data, system, event, var);
3870 	if (!hist_field) {
3871 		if (!system && data->handler == HANDLER_ONMATCH) {
3872 			system = data->match_data.event_system;
3873 			event = data->match_data.event;
3874 		}
3875 
3876 		hist_field = find_event_var(hist_data, system, event, var);
3877 	}
3878 
3879 	if (!hist_field)
3880 		hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var));
3881 
3882 	return hist_field;
3883 }
3884 
3885 static struct hist_field *
3886 trace_action_create_field_var(struct hist_trigger_data *hist_data,
3887 			      struct action_data *data, char *system,
3888 			      char *event, char *var)
3889 {
3890 	struct hist_field *hist_field = NULL;
3891 	struct field_var *field_var;
3892 
3893 	/*
3894 	 * First try to create a field var on the target event (the
3895 	 * currently being defined).  This will create a variable for
3896 	 * unqualified fields on the target event, or if qualified,
3897 	 * target fields that have qualified names matching the target.
3898 	 */
3899 	field_var = create_target_field_var(hist_data, system, event, var);
3900 
3901 	if (field_var && !IS_ERR(field_var)) {
3902 		save_field_var(hist_data, field_var);
3903 		hist_field = field_var->var;
3904 	} else {
3905 		field_var = NULL;
3906 		/*
3907 		 * If no explicit system.event is specified, default to
3908 		 * looking for fields on the onmatch(system.event.xxx)
3909 		 * event.
3910 		 */
3911 		if (!system && data->handler == HANDLER_ONMATCH) {
3912 			system = data->match_data.event_system;
3913 			event = data->match_data.event;
3914 		}
3915 
3916 		if (!event)
3917 			goto free;
3918 		/*
3919 		 * At this point, we're looking at a field on another
3920 		 * event.  Because we can't modify a hist trigger on
3921 		 * another event to add a variable for a field, we need
3922 		 * to create a new trigger on that event and create the
3923 		 * variable at the same time.
3924 		 */
3925 		hist_field = create_field_var_hist(hist_data, system, event, var);
3926 		if (IS_ERR(hist_field))
3927 			goto free;
3928 	}
3929  out:
3930 	return hist_field;
3931  free:
3932 	destroy_field_var(field_var);
3933 	hist_field = NULL;
3934 	goto out;
3935 }
3936 
3937 static int trace_action_create(struct hist_trigger_data *hist_data,
3938 			       struct action_data *data)
3939 {
3940 	struct trace_array *tr = hist_data->event_file->tr;
3941 	char *event_name, *param, *system = NULL;
3942 	struct hist_field *hist_field, *var_ref;
3943 	unsigned int i;
3944 	unsigned int field_pos = 0;
3945 	struct synth_event *event;
3946 	char *synth_event_name;
3947 	int var_ref_idx, ret = 0;
3948 
3949 	lockdep_assert_held(&event_mutex);
3950 
3951 	/* Sanity check to avoid out-of-bound write on 'data->var_ref_idx' */
3952 	if (data->n_params > SYNTH_FIELDS_MAX)
3953 		return -EINVAL;
3954 
3955 	if (data->use_trace_keyword)
3956 		synth_event_name = data->synth_event_name;
3957 	else
3958 		synth_event_name = data->action_name;
3959 
3960 	event = find_synth_event(synth_event_name);
3961 	if (!event) {
3962 		hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name));
3963 		return -EINVAL;
3964 	}
3965 
3966 	event->ref++;
3967 
3968 	for (i = 0; i < data->n_params; i++) {
3969 		char *p;
3970 
3971 		p = param = kstrdup(data->params[i], GFP_KERNEL);
3972 		if (!param) {
3973 			ret = -ENOMEM;
3974 			goto err;
3975 		}
3976 
3977 		system = strsep(&param, ".");
3978 		if (!param) {
3979 			param = (char *)system;
3980 			system = event_name = NULL;
3981 		} else {
3982 			event_name = strsep(&param, ".");
3983 			if (!param) {
3984 				kfree(p);
3985 				ret = -EINVAL;
3986 				goto err;
3987 			}
3988 		}
3989 
3990 		if (param[0] == '$')
3991 			hist_field = trace_action_find_var(hist_data, data,
3992 							   system, event_name,
3993 							   param);
3994 		else
3995 			hist_field = trace_action_create_field_var(hist_data,
3996 								   data,
3997 								   system,
3998 								   event_name,
3999 								   param);
4000 
4001 		if (!hist_field) {
4002 			kfree(p);
4003 			ret = -EINVAL;
4004 			goto err;
4005 		}
4006 
4007 		if (check_synth_field(event, hist_field, field_pos) == 0) {
4008 			var_ref = create_var_ref(hist_data, hist_field,
4009 						 system, event_name);
4010 			if (!var_ref) {
4011 				kfree(p);
4012 				ret = -ENOMEM;
4013 				goto err;
4014 			}
4015 
4016 			var_ref_idx = find_var_ref_idx(hist_data, var_ref);
4017 			if (WARN_ON(var_ref_idx < 0)) {
4018 				kfree(p);
4019 				ret = var_ref_idx;
4020 				goto err;
4021 			}
4022 
4023 			data->var_ref_idx[i] = var_ref_idx;
4024 
4025 			field_pos++;
4026 			kfree(p);
4027 			continue;
4028 		}
4029 
4030 		hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param));
4031 		kfree(p);
4032 		ret = -EINVAL;
4033 		goto err;
4034 	}
4035 
4036 	if (field_pos != event->n_fields) {
4037 		hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name));
4038 		ret = -EINVAL;
4039 		goto err;
4040 	}
4041 
4042 	data->synth_event = event;
4043  out:
4044 	return ret;
4045  err:
4046 	event->ref--;
4047 
4048 	goto out;
4049 }
4050 
4051 static int action_create(struct hist_trigger_data *hist_data,
4052 			 struct action_data *data)
4053 {
4054 	struct trace_event_file *file = hist_data->event_file;
4055 	struct trace_array *tr = file->tr;
4056 	struct track_data *track_data;
4057 	struct field_var *field_var;
4058 	unsigned int i;
4059 	char *param;
4060 	int ret = 0;
4061 
4062 	if (data->action == ACTION_TRACE)
4063 		return trace_action_create(hist_data, data);
4064 
4065 	if (data->action == ACTION_SNAPSHOT) {
4066 		track_data = track_data_alloc(hist_data->key_size, data, hist_data);
4067 		if (IS_ERR(track_data)) {
4068 			ret = PTR_ERR(track_data);
4069 			goto out;
4070 		}
4071 
4072 		ret = tracing_snapshot_cond_enable(file->tr, track_data,
4073 						   cond_snapshot_update);
4074 		if (ret)
4075 			track_data_free(track_data);
4076 
4077 		goto out;
4078 	}
4079 
4080 	if (data->action == ACTION_SAVE) {
4081 		if (hist_data->n_save_vars) {
4082 			ret = -EEXIST;
4083 			hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0);
4084 			goto out;
4085 		}
4086 
4087 		for (i = 0; i < data->n_params; i++) {
4088 			param = kstrdup(data->params[i], GFP_KERNEL);
4089 			if (!param) {
4090 				ret = -ENOMEM;
4091 				goto out;
4092 			}
4093 
4094 			field_var = create_target_field_var(hist_data, NULL, NULL, param);
4095 			if (IS_ERR(field_var)) {
4096 				hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL,
4097 					 errpos(param));
4098 				ret = PTR_ERR(field_var);
4099 				kfree(param);
4100 				goto out;
4101 			}
4102 
4103 			hist_data->save_vars[hist_data->n_save_vars++] = field_var;
4104 			if (field_var->val->flags & HIST_FIELD_FL_STRING)
4105 				hist_data->n_save_var_str++;
4106 			kfree(param);
4107 		}
4108 	}
4109  out:
4110 	return ret;
4111 }
4112 
4113 static int onmatch_create(struct hist_trigger_data *hist_data,
4114 			  struct action_data *data)
4115 {
4116 	return action_create(hist_data, data);
4117 }
4118 
4119 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
4120 {
4121 	char *match_event, *match_event_system;
4122 	struct action_data *data;
4123 	int ret = -EINVAL;
4124 
4125 	data = kzalloc(sizeof(*data), GFP_KERNEL);
4126 	if (!data)
4127 		return ERR_PTR(-ENOMEM);
4128 
4129 	match_event = strsep(&str, ")");
4130 	if (!match_event || !str) {
4131 		hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event));
4132 		goto free;
4133 	}
4134 
4135 	match_event_system = strsep(&match_event, ".");
4136 	if (!match_event) {
4137 		hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system));
4138 		goto free;
4139 	}
4140 
4141 	if (IS_ERR(event_file(tr, match_event_system, match_event))) {
4142 		hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event));
4143 		goto free;
4144 	}
4145 
4146 	data->match_data.event = kstrdup(match_event, GFP_KERNEL);
4147 	if (!data->match_data.event) {
4148 		ret = -ENOMEM;
4149 		goto free;
4150 	}
4151 
4152 	data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
4153 	if (!data->match_data.event_system) {
4154 		ret = -ENOMEM;
4155 		goto free;
4156 	}
4157 
4158 	ret = action_parse(tr, str, data, HANDLER_ONMATCH);
4159 	if (ret)
4160 		goto free;
4161  out:
4162 	return data;
4163  free:
4164 	onmatch_destroy(data);
4165 	data = ERR_PTR(ret);
4166 	goto out;
4167 }
4168 
4169 static int create_hitcount_val(struct hist_trigger_data *hist_data)
4170 {
4171 	hist_data->fields[HITCOUNT_IDX] =
4172 		create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
4173 	if (!hist_data->fields[HITCOUNT_IDX])
4174 		return -ENOMEM;
4175 
4176 	hist_data->n_vals++;
4177 	hist_data->n_fields++;
4178 
4179 	if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
4180 		return -EINVAL;
4181 
4182 	return 0;
4183 }
4184 
4185 static int __create_val_field(struct hist_trigger_data *hist_data,
4186 			      unsigned int val_idx,
4187 			      struct trace_event_file *file,
4188 			      char *var_name, char *field_str,
4189 			      unsigned long flags)
4190 {
4191 	struct hist_field *hist_field;
4192 	int ret = 0, n_subexprs = 0;
4193 
4194 	hist_field = parse_expr(hist_data, file, field_str, flags, var_name, &n_subexprs);
4195 	if (IS_ERR(hist_field)) {
4196 		ret = PTR_ERR(hist_field);
4197 		goto out;
4198 	}
4199 
4200 	hist_data->fields[val_idx] = hist_field;
4201 
4202 	++hist_data->n_vals;
4203 	++hist_data->n_fields;
4204 
4205 	if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4206 		ret = -EINVAL;
4207  out:
4208 	return ret;
4209 }
4210 
4211 static int create_val_field(struct hist_trigger_data *hist_data,
4212 			    unsigned int val_idx,
4213 			    struct trace_event_file *file,
4214 			    char *field_str)
4215 {
4216 	if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
4217 		return -EINVAL;
4218 
4219 	return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
4220 }
4221 
4222 static const char no_comm[] = "(no comm)";
4223 
4224 static u64 hist_field_execname(struct hist_field *hist_field,
4225 			       struct tracing_map_elt *elt,
4226 			       struct trace_buffer *buffer,
4227 			       struct ring_buffer_event *rbe,
4228 			       void *event)
4229 {
4230 	struct hist_elt_data *elt_data;
4231 
4232 	if (WARN_ON_ONCE(!elt))
4233 		return (u64)(unsigned long)no_comm;
4234 
4235 	elt_data = elt->private_data;
4236 
4237 	if (WARN_ON_ONCE(!elt_data->comm))
4238 		return (u64)(unsigned long)no_comm;
4239 
4240 	return (u64)(unsigned long)(elt_data->comm);
4241 }
4242 
4243 static u64 hist_fn_call(struct hist_field *hist_field,
4244 			struct tracing_map_elt *elt,
4245 			struct trace_buffer *buffer,
4246 			struct ring_buffer_event *rbe,
4247 			void *event)
4248 {
4249 	switch (hist_field->fn_num) {
4250 	case HIST_FIELD_FN_VAR_REF:
4251 		return hist_field_var_ref(hist_field, elt, buffer, rbe, event);
4252 	case HIST_FIELD_FN_COUNTER:
4253 		return hist_field_counter(hist_field, elt, buffer, rbe, event);
4254 	case HIST_FIELD_FN_CONST:
4255 		return hist_field_const(hist_field, elt, buffer, rbe, event);
4256 	case HIST_FIELD_FN_LOG2:
4257 		return hist_field_log2(hist_field, elt, buffer, rbe, event);
4258 	case HIST_FIELD_FN_BUCKET:
4259 		return hist_field_bucket(hist_field, elt, buffer, rbe, event);
4260 	case HIST_FIELD_FN_TIMESTAMP:
4261 		return hist_field_timestamp(hist_field, elt, buffer, rbe, event);
4262 	case HIST_FIELD_FN_CPU:
4263 		return hist_field_cpu(hist_field, elt, buffer, rbe, event);
4264 	case HIST_FIELD_FN_STRING:
4265 		return hist_field_string(hist_field, elt, buffer, rbe, event);
4266 	case HIST_FIELD_FN_DYNSTRING:
4267 		return hist_field_dynstring(hist_field, elt, buffer, rbe, event);
4268 	case HIST_FIELD_FN_RELDYNSTRING:
4269 		return hist_field_reldynstring(hist_field, elt, buffer, rbe, event);
4270 	case HIST_FIELD_FN_PSTRING:
4271 		return hist_field_pstring(hist_field, elt, buffer, rbe, event);
4272 	case HIST_FIELD_FN_S64:
4273 		return hist_field_s64(hist_field, elt, buffer, rbe, event);
4274 	case HIST_FIELD_FN_U64:
4275 		return hist_field_u64(hist_field, elt, buffer, rbe, event);
4276 	case HIST_FIELD_FN_S32:
4277 		return hist_field_s32(hist_field, elt, buffer, rbe, event);
4278 	case HIST_FIELD_FN_U32:
4279 		return hist_field_u32(hist_field, elt, buffer, rbe, event);
4280 	case HIST_FIELD_FN_S16:
4281 		return hist_field_s16(hist_field, elt, buffer, rbe, event);
4282 	case HIST_FIELD_FN_U16:
4283 		return hist_field_u16(hist_field, elt, buffer, rbe, event);
4284 	case HIST_FIELD_FN_S8:
4285 		return hist_field_s8(hist_field, elt, buffer, rbe, event);
4286 	case HIST_FIELD_FN_U8:
4287 		return hist_field_u8(hist_field, elt, buffer, rbe, event);
4288 	case HIST_FIELD_FN_UMINUS:
4289 		return hist_field_unary_minus(hist_field, elt, buffer, rbe, event);
4290 	case HIST_FIELD_FN_MINUS:
4291 		return hist_field_minus(hist_field, elt, buffer, rbe, event);
4292 	case HIST_FIELD_FN_PLUS:
4293 		return hist_field_plus(hist_field, elt, buffer, rbe, event);
4294 	case HIST_FIELD_FN_DIV:
4295 		return hist_field_div(hist_field, elt, buffer, rbe, event);
4296 	case HIST_FIELD_FN_MULT:
4297 		return hist_field_mult(hist_field, elt, buffer, rbe, event);
4298 	case HIST_FIELD_FN_DIV_POWER2:
4299 		return div_by_power_of_two(hist_field, elt, buffer, rbe, event);
4300 	case HIST_FIELD_FN_DIV_NOT_POWER2:
4301 		return div_by_not_power_of_two(hist_field, elt, buffer, rbe, event);
4302 	case HIST_FIELD_FN_DIV_MULT_SHIFT:
4303 		return div_by_mult_and_shift(hist_field, elt, buffer, rbe, event);
4304 	case HIST_FIELD_FN_EXECNAME:
4305 		return hist_field_execname(hist_field, elt, buffer, rbe, event);
4306 	default:
4307 		return 0;
4308 	}
4309 }
4310 
4311 /* Convert a var that points to common_pid.execname to a string */
4312 static void update_var_execname(struct hist_field *hist_field)
4313 {
4314 	hist_field->flags = HIST_FIELD_FL_STRING | HIST_FIELD_FL_VAR |
4315 		HIST_FIELD_FL_EXECNAME;
4316 	hist_field->size = MAX_FILTER_STR_VAL;
4317 	hist_field->is_signed = 0;
4318 
4319 	kfree_const(hist_field->type);
4320 	hist_field->type = "char[]";
4321 
4322 	hist_field->fn_num = HIST_FIELD_FN_EXECNAME;
4323 }
4324 
4325 static int create_var_field(struct hist_trigger_data *hist_data,
4326 			    unsigned int val_idx,
4327 			    struct trace_event_file *file,
4328 			    char *var_name, char *expr_str)
4329 {
4330 	struct trace_array *tr = hist_data->event_file->tr;
4331 	unsigned long flags = 0;
4332 	int ret;
4333 
4334 	if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4335 		return -EINVAL;
4336 
4337 	if (find_var(hist_data, file, var_name) && !hist_data->remove) {
4338 		hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name));
4339 		return -EINVAL;
4340 	}
4341 
4342 	flags |= HIST_FIELD_FL_VAR;
4343 	hist_data->n_vars++;
4344 	if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
4345 		return -EINVAL;
4346 
4347 	ret = __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
4348 
4349 	if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_EXECNAME)
4350 		update_var_execname(hist_data->fields[val_idx]);
4351 
4352 	if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_STRING)
4353 		hist_data->fields[val_idx]->var_str_idx = hist_data->n_var_str++;
4354 
4355 	return ret;
4356 }
4357 
4358 static int create_val_fields(struct hist_trigger_data *hist_data,
4359 			     struct trace_event_file *file)
4360 {
4361 	unsigned int i, j = 1, n_hitcount = 0;
4362 	char *fields_str, *field_str;
4363 	int ret;
4364 
4365 	ret = create_hitcount_val(hist_data);
4366 	if (ret)
4367 		goto out;
4368 
4369 	fields_str = hist_data->attrs->vals_str;
4370 	if (!fields_str)
4371 		goto out;
4372 
4373 	for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
4374 		     j < TRACING_MAP_VALS_MAX; i++) {
4375 		field_str = strsep(&fields_str, ",");
4376 		if (!field_str)
4377 			break;
4378 
4379 		if (strcmp(field_str, "hitcount") == 0) {
4380 			if (!n_hitcount++)
4381 				continue;
4382 		}
4383 
4384 		ret = create_val_field(hist_data, j++, file, field_str);
4385 		if (ret)
4386 			goto out;
4387 	}
4388 
4389 	if (fields_str && (strcmp(fields_str, "hitcount") != 0))
4390 		ret = -EINVAL;
4391  out:
4392 	/* There is only raw hitcount but nohitcount suppresses it. */
4393 	if (j == 1 && hist_data->attrs->no_hitcount) {
4394 		hist_err(hist_data->event_file->tr, HIST_ERR_NEED_NOHC_VAL, 0);
4395 		ret = -ENOENT;
4396 	}
4397 
4398 	return ret;
4399 }
4400 
4401 static int create_key_field(struct hist_trigger_data *hist_data,
4402 			    unsigned int key_idx,
4403 			    unsigned int key_offset,
4404 			    struct trace_event_file *file,
4405 			    char *field_str)
4406 {
4407 	struct trace_array *tr = hist_data->event_file->tr;
4408 	struct hist_field *hist_field = NULL;
4409 	unsigned long flags = 0;
4410 	unsigned int key_size;
4411 	int ret = 0, n_subexprs = 0;
4412 
4413 	if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
4414 		return -EINVAL;
4415 
4416 	flags |= HIST_FIELD_FL_KEY;
4417 
4418 	if (strcmp(field_str, "stacktrace") == 0) {
4419 		flags |= HIST_FIELD_FL_STACKTRACE;
4420 		key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
4421 		hist_field = create_hist_field(hist_data, NULL, flags, NULL);
4422 	} else {
4423 		hist_field = parse_expr(hist_data, file, field_str, flags,
4424 					NULL, &n_subexprs);
4425 		if (IS_ERR(hist_field)) {
4426 			ret = PTR_ERR(hist_field);
4427 			goto out;
4428 		}
4429 
4430 		if (field_has_hist_vars(hist_field, 0))	{
4431 			hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str));
4432 			destroy_hist_field(hist_field, 0);
4433 			ret = -EINVAL;
4434 			goto out;
4435 		}
4436 
4437 		key_size = hist_field->size;
4438 	}
4439 
4440 	hist_data->fields[key_idx] = hist_field;
4441 
4442 	key_size = ALIGN(key_size, sizeof(u64));
4443 	hist_data->fields[key_idx]->size = key_size;
4444 	hist_data->fields[key_idx]->offset = key_offset;
4445 
4446 	hist_data->key_size += key_size;
4447 
4448 	if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
4449 		ret = -EINVAL;
4450 		goto out;
4451 	}
4452 
4453 	hist_data->n_keys++;
4454 	hist_data->n_fields++;
4455 
4456 	if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
4457 		return -EINVAL;
4458 
4459 	ret = key_size;
4460  out:
4461 	return ret;
4462 }
4463 
4464 static int create_key_fields(struct hist_trigger_data *hist_data,
4465 			     struct trace_event_file *file)
4466 {
4467 	unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
4468 	char *fields_str, *field_str;
4469 	int ret = -EINVAL;
4470 
4471 	fields_str = hist_data->attrs->keys_str;
4472 	if (!fields_str)
4473 		goto out;
4474 
4475 	for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
4476 		field_str = strsep(&fields_str, ",");
4477 		if (!field_str)
4478 			break;
4479 		ret = create_key_field(hist_data, i, key_offset,
4480 				       file, field_str);
4481 		if (ret < 0)
4482 			goto out;
4483 		key_offset += ret;
4484 	}
4485 	if (fields_str) {
4486 		ret = -EINVAL;
4487 		goto out;
4488 	}
4489 	ret = 0;
4490  out:
4491 	return ret;
4492 }
4493 
4494 static int create_var_fields(struct hist_trigger_data *hist_data,
4495 			     struct trace_event_file *file)
4496 {
4497 	unsigned int i, j = hist_data->n_vals;
4498 	int ret = 0;
4499 
4500 	unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
4501 
4502 	for (i = 0; i < n_vars; i++) {
4503 		char *var_name = hist_data->attrs->var_defs.name[i];
4504 		char *expr = hist_data->attrs->var_defs.expr[i];
4505 
4506 		ret = create_var_field(hist_data, j++, file, var_name, expr);
4507 		if (ret)
4508 			goto out;
4509 	}
4510  out:
4511 	return ret;
4512 }
4513 
4514 static void free_var_defs(struct hist_trigger_data *hist_data)
4515 {
4516 	unsigned int i;
4517 
4518 	for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
4519 		kfree(hist_data->attrs->var_defs.name[i]);
4520 		kfree(hist_data->attrs->var_defs.expr[i]);
4521 	}
4522 
4523 	hist_data->attrs->var_defs.n_vars = 0;
4524 }
4525 
4526 static int parse_var_defs(struct hist_trigger_data *hist_data)
4527 {
4528 	struct trace_array *tr = hist_data->event_file->tr;
4529 	char *s, *str, *var_name, *field_str;
4530 	unsigned int i, j, n_vars = 0;
4531 	int ret = 0;
4532 
4533 	for (i = 0; i < hist_data->attrs->n_assignments; i++) {
4534 		str = hist_data->attrs->assignment_str[i];
4535 		for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
4536 			field_str = strsep(&str, ",");
4537 			if (!field_str)
4538 				break;
4539 
4540 			var_name = strsep(&field_str, "=");
4541 			if (!var_name || !field_str) {
4542 				hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT,
4543 					 errpos(var_name));
4544 				ret = -EINVAL;
4545 				goto free;
4546 			}
4547 
4548 			if (n_vars == TRACING_MAP_VARS_MAX) {
4549 				hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name));
4550 				ret = -EINVAL;
4551 				goto free;
4552 			}
4553 
4554 			s = kstrdup(var_name, GFP_KERNEL);
4555 			if (!s) {
4556 				ret = -ENOMEM;
4557 				goto free;
4558 			}
4559 			hist_data->attrs->var_defs.name[n_vars] = s;
4560 
4561 			s = kstrdup(field_str, GFP_KERNEL);
4562 			if (!s) {
4563 				kfree(hist_data->attrs->var_defs.name[n_vars]);
4564 				hist_data->attrs->var_defs.name[n_vars] = NULL;
4565 				ret = -ENOMEM;
4566 				goto free;
4567 			}
4568 			hist_data->attrs->var_defs.expr[n_vars++] = s;
4569 
4570 			hist_data->attrs->var_defs.n_vars = n_vars;
4571 		}
4572 	}
4573 
4574 	return ret;
4575  free:
4576 	free_var_defs(hist_data);
4577 
4578 	return ret;
4579 }
4580 
4581 static int create_hist_fields(struct hist_trigger_data *hist_data,
4582 			      struct trace_event_file *file)
4583 {
4584 	int ret;
4585 
4586 	ret = parse_var_defs(hist_data);
4587 	if (ret)
4588 		return ret;
4589 
4590 	ret = create_val_fields(hist_data, file);
4591 	if (ret)
4592 		goto out;
4593 
4594 	ret = create_var_fields(hist_data, file);
4595 	if (ret)
4596 		goto out;
4597 
4598 	ret = create_key_fields(hist_data, file);
4599 
4600  out:
4601 	free_var_defs(hist_data);
4602 
4603 	return ret;
4604 }
4605 
4606 static int is_descending(struct trace_array *tr, const char *str)
4607 {
4608 	if (!str)
4609 		return 0;
4610 
4611 	if (strcmp(str, "descending") == 0)
4612 		return 1;
4613 
4614 	if (strcmp(str, "ascending") == 0)
4615 		return 0;
4616 
4617 	hist_err(tr, HIST_ERR_INVALID_SORT_MODIFIER, errpos((char *)str));
4618 
4619 	return -EINVAL;
4620 }
4621 
4622 static int create_sort_keys(struct hist_trigger_data *hist_data)
4623 {
4624 	struct trace_array *tr = hist_data->event_file->tr;
4625 	char *fields_str = hist_data->attrs->sort_key_str;
4626 	struct tracing_map_sort_key *sort_key;
4627 	int descending, ret = 0;
4628 	unsigned int i, j, k;
4629 
4630 	hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
4631 
4632 	if (!fields_str)
4633 		goto out;
4634 
4635 	for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
4636 		struct hist_field *hist_field;
4637 		char *field_str, *field_name;
4638 		const char *test_name;
4639 
4640 		sort_key = &hist_data->sort_keys[i];
4641 
4642 		field_str = strsep(&fields_str, ",");
4643 		if (!field_str)
4644 			break;
4645 
4646 		if (!*field_str) {
4647 			ret = -EINVAL;
4648 			hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4649 			break;
4650 		}
4651 
4652 		if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4653 			hist_err(tr, HIST_ERR_TOO_MANY_SORT_FIELDS, errpos("sort="));
4654 			ret = -EINVAL;
4655 			break;
4656 		}
4657 
4658 		field_name = strsep(&field_str, ".");
4659 		if (!field_name || !*field_name) {
4660 			ret = -EINVAL;
4661 			hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4662 			break;
4663 		}
4664 
4665 		if (strcmp(field_name, "hitcount") == 0) {
4666 			descending = is_descending(tr, field_str);
4667 			if (descending < 0) {
4668 				ret = descending;
4669 				break;
4670 			}
4671 			sort_key->descending = descending;
4672 			continue;
4673 		}
4674 
4675 		for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4676 			unsigned int idx;
4677 
4678 			hist_field = hist_data->fields[j];
4679 			if (hist_field->flags & HIST_FIELD_FL_VAR)
4680 				continue;
4681 
4682 			idx = k++;
4683 
4684 			test_name = hist_field_name(hist_field, 0);
4685 
4686 			if (strcmp(field_name, test_name) == 0) {
4687 				sort_key->field_idx = idx;
4688 				descending = is_descending(tr, field_str);
4689 				if (descending < 0) {
4690 					ret = descending;
4691 					goto out;
4692 				}
4693 				sort_key->descending = descending;
4694 				break;
4695 			}
4696 		}
4697 		if (j == hist_data->n_fields) {
4698 			ret = -EINVAL;
4699 			hist_err(tr, HIST_ERR_INVALID_SORT_FIELD, errpos(field_name));
4700 			break;
4701 		}
4702 	}
4703 
4704 	hist_data->n_sort_keys = i;
4705  out:
4706 	return ret;
4707 }
4708 
4709 static void destroy_actions(struct hist_trigger_data *hist_data)
4710 {
4711 	unsigned int i;
4712 
4713 	for (i = 0; i < hist_data->n_actions; i++) {
4714 		struct action_data *data = hist_data->actions[i];
4715 
4716 		if (data->handler == HANDLER_ONMATCH)
4717 			onmatch_destroy(data);
4718 		else if (data->handler == HANDLER_ONMAX ||
4719 			 data->handler == HANDLER_ONCHANGE)
4720 			track_data_destroy(hist_data, data);
4721 		else
4722 			kfree(data);
4723 	}
4724 }
4725 
4726 static int parse_actions(struct hist_trigger_data *hist_data)
4727 {
4728 	struct trace_array *tr = hist_data->event_file->tr;
4729 	struct action_data *data;
4730 	unsigned int i;
4731 	int ret = 0;
4732 	char *str;
4733 	int len;
4734 
4735 	for (i = 0; i < hist_data->attrs->n_actions; i++) {
4736 		str = hist_data->attrs->action_str[i];
4737 
4738 		if ((len = str_has_prefix(str, "onmatch("))) {
4739 			char *action_str = str + len;
4740 
4741 			data = onmatch_parse(tr, action_str);
4742 			if (IS_ERR(data)) {
4743 				ret = PTR_ERR(data);
4744 				break;
4745 			}
4746 		} else if ((len = str_has_prefix(str, "onmax("))) {
4747 			char *action_str = str + len;
4748 
4749 			data = track_data_parse(hist_data, action_str,
4750 						HANDLER_ONMAX);
4751 			if (IS_ERR(data)) {
4752 				ret = PTR_ERR(data);
4753 				break;
4754 			}
4755 		} else if ((len = str_has_prefix(str, "onchange("))) {
4756 			char *action_str = str + len;
4757 
4758 			data = track_data_parse(hist_data, action_str,
4759 						HANDLER_ONCHANGE);
4760 			if (IS_ERR(data)) {
4761 				ret = PTR_ERR(data);
4762 				break;
4763 			}
4764 		} else {
4765 			ret = -EINVAL;
4766 			break;
4767 		}
4768 
4769 		hist_data->actions[hist_data->n_actions++] = data;
4770 	}
4771 
4772 	return ret;
4773 }
4774 
4775 static int create_actions(struct hist_trigger_data *hist_data)
4776 {
4777 	struct action_data *data;
4778 	unsigned int i;
4779 	int ret = 0;
4780 
4781 	for (i = 0; i < hist_data->attrs->n_actions; i++) {
4782 		data = hist_data->actions[i];
4783 
4784 		if (data->handler == HANDLER_ONMATCH) {
4785 			ret = onmatch_create(hist_data, data);
4786 			if (ret)
4787 				break;
4788 		} else if (data->handler == HANDLER_ONMAX ||
4789 			   data->handler == HANDLER_ONCHANGE) {
4790 			ret = track_data_create(hist_data, data);
4791 			if (ret)
4792 				break;
4793 		} else {
4794 			ret = -EINVAL;
4795 			break;
4796 		}
4797 	}
4798 
4799 	return ret;
4800 }
4801 
4802 static void print_actions(struct seq_file *m,
4803 			  struct hist_trigger_data *hist_data,
4804 			  struct tracing_map_elt *elt)
4805 {
4806 	unsigned int i;
4807 
4808 	for (i = 0; i < hist_data->n_actions; i++) {
4809 		struct action_data *data = hist_data->actions[i];
4810 
4811 		if (data->action == ACTION_SNAPSHOT)
4812 			continue;
4813 
4814 		if (data->handler == HANDLER_ONMAX ||
4815 		    data->handler == HANDLER_ONCHANGE)
4816 			track_data_print(m, hist_data, elt, data);
4817 	}
4818 }
4819 
4820 static void print_action_spec(struct seq_file *m,
4821 			      struct hist_trigger_data *hist_data,
4822 			      struct action_data *data)
4823 {
4824 	unsigned int i;
4825 
4826 	if (data->action == ACTION_SAVE) {
4827 		for (i = 0; i < hist_data->n_save_vars; i++) {
4828 			seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
4829 			if (i < hist_data->n_save_vars - 1)
4830 				seq_puts(m, ",");
4831 		}
4832 	} else if (data->action == ACTION_TRACE) {
4833 		if (data->use_trace_keyword)
4834 			seq_printf(m, "%s", data->synth_event_name);
4835 		for (i = 0; i < data->n_params; i++) {
4836 			if (i || data->use_trace_keyword)
4837 				seq_puts(m, ",");
4838 			seq_printf(m, "%s", data->params[i]);
4839 		}
4840 	}
4841 }
4842 
4843 static void print_track_data_spec(struct seq_file *m,
4844 				  struct hist_trigger_data *hist_data,
4845 				  struct action_data *data)
4846 {
4847 	if (data->handler == HANDLER_ONMAX)
4848 		seq_puts(m, ":onmax(");
4849 	else if (data->handler == HANDLER_ONCHANGE)
4850 		seq_puts(m, ":onchange(");
4851 	seq_printf(m, "%s", data->track_data.var_str);
4852 	seq_printf(m, ").%s(", data->action_name);
4853 
4854 	print_action_spec(m, hist_data, data);
4855 
4856 	seq_puts(m, ")");
4857 }
4858 
4859 static void print_onmatch_spec(struct seq_file *m,
4860 			       struct hist_trigger_data *hist_data,
4861 			       struct action_data *data)
4862 {
4863 	seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
4864 		   data->match_data.event);
4865 
4866 	seq_printf(m, "%s(", data->action_name);
4867 
4868 	print_action_spec(m, hist_data, data);
4869 
4870 	seq_puts(m, ")");
4871 }
4872 
4873 static bool actions_match(struct hist_trigger_data *hist_data,
4874 			  struct hist_trigger_data *hist_data_test)
4875 {
4876 	unsigned int i, j;
4877 
4878 	if (hist_data->n_actions != hist_data_test->n_actions)
4879 		return false;
4880 
4881 	for (i = 0; i < hist_data->n_actions; i++) {
4882 		struct action_data *data = hist_data->actions[i];
4883 		struct action_data *data_test = hist_data_test->actions[i];
4884 		char *action_name, *action_name_test;
4885 
4886 		if (data->handler != data_test->handler)
4887 			return false;
4888 		if (data->action != data_test->action)
4889 			return false;
4890 
4891 		if (data->n_params != data_test->n_params)
4892 			return false;
4893 
4894 		for (j = 0; j < data->n_params; j++) {
4895 			if (strcmp(data->params[j], data_test->params[j]) != 0)
4896 				return false;
4897 		}
4898 
4899 		if (data->use_trace_keyword)
4900 			action_name = data->synth_event_name;
4901 		else
4902 			action_name = data->action_name;
4903 
4904 		if (data_test->use_trace_keyword)
4905 			action_name_test = data_test->synth_event_name;
4906 		else
4907 			action_name_test = data_test->action_name;
4908 
4909 		if (strcmp(action_name, action_name_test) != 0)
4910 			return false;
4911 
4912 		if (data->handler == HANDLER_ONMATCH) {
4913 			if (strcmp(data->match_data.event_system,
4914 				   data_test->match_data.event_system) != 0)
4915 				return false;
4916 			if (strcmp(data->match_data.event,
4917 				   data_test->match_data.event) != 0)
4918 				return false;
4919 		} else if (data->handler == HANDLER_ONMAX ||
4920 			   data->handler == HANDLER_ONCHANGE) {
4921 			if (strcmp(data->track_data.var_str,
4922 				   data_test->track_data.var_str) != 0)
4923 				return false;
4924 		}
4925 	}
4926 
4927 	return true;
4928 }
4929 
4930 
4931 static void print_actions_spec(struct seq_file *m,
4932 			       struct hist_trigger_data *hist_data)
4933 {
4934 	unsigned int i;
4935 
4936 	for (i = 0; i < hist_data->n_actions; i++) {
4937 		struct action_data *data = hist_data->actions[i];
4938 
4939 		if (data->handler == HANDLER_ONMATCH)
4940 			print_onmatch_spec(m, hist_data, data);
4941 		else if (data->handler == HANDLER_ONMAX ||
4942 			 data->handler == HANDLER_ONCHANGE)
4943 			print_track_data_spec(m, hist_data, data);
4944 	}
4945 }
4946 
4947 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
4948 {
4949 	unsigned int i;
4950 
4951 	for (i = 0; i < hist_data->n_field_var_hists; i++) {
4952 		kfree(hist_data->field_var_hists[i]->cmd);
4953 		kfree(hist_data->field_var_hists[i]);
4954 	}
4955 }
4956 
4957 static void destroy_hist_data(struct hist_trigger_data *hist_data)
4958 {
4959 	if (!hist_data)
4960 		return;
4961 
4962 	destroy_hist_trigger_attrs(hist_data->attrs);
4963 	destroy_hist_fields(hist_data);
4964 	tracing_map_destroy(hist_data->map);
4965 
4966 	destroy_actions(hist_data);
4967 	destroy_field_vars(hist_data);
4968 	destroy_field_var_hists(hist_data);
4969 
4970 	kfree(hist_data);
4971 }
4972 
4973 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
4974 {
4975 	struct tracing_map *map = hist_data->map;
4976 	struct ftrace_event_field *field;
4977 	struct hist_field *hist_field;
4978 	int i, idx = 0;
4979 
4980 	for_each_hist_field(i, hist_data) {
4981 		hist_field = hist_data->fields[i];
4982 		if (hist_field->flags & HIST_FIELD_FL_KEY) {
4983 			tracing_map_cmp_fn_t cmp_fn;
4984 
4985 			field = hist_field->field;
4986 
4987 			if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
4988 				cmp_fn = tracing_map_cmp_none;
4989 			else if (!field || hist_field->flags & HIST_FIELD_FL_CPU)
4990 				cmp_fn = tracing_map_cmp_num(hist_field->size,
4991 							     hist_field->is_signed);
4992 			else if (is_string_field(field))
4993 				cmp_fn = tracing_map_cmp_string;
4994 			else
4995 				cmp_fn = tracing_map_cmp_num(field->size,
4996 							     field->is_signed);
4997 			idx = tracing_map_add_key_field(map,
4998 							hist_field->offset,
4999 							cmp_fn);
5000 		} else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
5001 			idx = tracing_map_add_sum_field(map);
5002 
5003 		if (idx < 0)
5004 			return idx;
5005 
5006 		if (hist_field->flags & HIST_FIELD_FL_VAR) {
5007 			idx = tracing_map_add_var(map);
5008 			if (idx < 0)
5009 				return idx;
5010 			hist_field->var.idx = idx;
5011 			hist_field->var.hist_data = hist_data;
5012 		}
5013 	}
5014 
5015 	return 0;
5016 }
5017 
5018 static struct hist_trigger_data *
5019 create_hist_data(unsigned int map_bits,
5020 		 struct hist_trigger_attrs *attrs,
5021 		 struct trace_event_file *file,
5022 		 bool remove)
5023 {
5024 	const struct tracing_map_ops *map_ops = NULL;
5025 	struct hist_trigger_data *hist_data;
5026 	int ret = 0;
5027 
5028 	hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
5029 	if (!hist_data)
5030 		return ERR_PTR(-ENOMEM);
5031 
5032 	hist_data->attrs = attrs;
5033 	hist_data->remove = remove;
5034 	hist_data->event_file = file;
5035 
5036 	ret = parse_actions(hist_data);
5037 	if (ret)
5038 		goto free;
5039 
5040 	ret = create_hist_fields(hist_data, file);
5041 	if (ret)
5042 		goto free;
5043 
5044 	ret = create_sort_keys(hist_data);
5045 	if (ret)
5046 		goto free;
5047 
5048 	map_ops = &hist_trigger_elt_data_ops;
5049 
5050 	hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
5051 					    map_ops, hist_data);
5052 	if (IS_ERR(hist_data->map)) {
5053 		ret = PTR_ERR(hist_data->map);
5054 		hist_data->map = NULL;
5055 		goto free;
5056 	}
5057 
5058 	ret = create_tracing_map_fields(hist_data);
5059 	if (ret)
5060 		goto free;
5061  out:
5062 	return hist_data;
5063  free:
5064 	hist_data->attrs = NULL;
5065 
5066 	destroy_hist_data(hist_data);
5067 
5068 	hist_data = ERR_PTR(ret);
5069 
5070 	goto out;
5071 }
5072 
5073 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
5074 				    struct tracing_map_elt *elt,
5075 				    struct trace_buffer *buffer, void *rec,
5076 				    struct ring_buffer_event *rbe,
5077 				    u64 *var_ref_vals)
5078 {
5079 	struct hist_elt_data *elt_data;
5080 	struct hist_field *hist_field;
5081 	unsigned int i, var_idx;
5082 	u64 hist_val;
5083 
5084 	elt_data = elt->private_data;
5085 	elt_data->var_ref_vals = var_ref_vals;
5086 
5087 	for_each_hist_val_field(i, hist_data) {
5088 		hist_field = hist_data->fields[i];
5089 		hist_val = hist_fn_call(hist_field, elt, buffer, rbe, rec);
5090 		if (hist_field->flags & HIST_FIELD_FL_VAR) {
5091 			var_idx = hist_field->var.idx;
5092 
5093 			if (hist_field->flags & HIST_FIELD_FL_STRING) {
5094 				unsigned int str_start, var_str_idx, idx;
5095 				char *str, *val_str;
5096 				unsigned int size;
5097 
5098 				str_start = hist_data->n_field_var_str +
5099 					hist_data->n_save_var_str;
5100 				var_str_idx = hist_field->var_str_idx;
5101 				idx = str_start + var_str_idx;
5102 
5103 				str = elt_data->field_var_str[idx];
5104 				val_str = (char *)(uintptr_t)hist_val;
5105 
5106 				size = min(hist_field->size, STR_VAR_LEN_MAX);
5107 				strscpy(str, val_str, size);
5108 
5109 				hist_val = (u64)(uintptr_t)str;
5110 			}
5111 			tracing_map_set_var(elt, var_idx, hist_val);
5112 			continue;
5113 		}
5114 		tracing_map_update_sum(elt, i, hist_val);
5115 	}
5116 
5117 	for_each_hist_key_field(i, hist_data) {
5118 		hist_field = hist_data->fields[i];
5119 		if (hist_field->flags & HIST_FIELD_FL_VAR) {
5120 			hist_val = hist_fn_call(hist_field, elt, buffer, rbe, rec);
5121 			var_idx = hist_field->var.idx;
5122 			tracing_map_set_var(elt, var_idx, hist_val);
5123 		}
5124 	}
5125 
5126 	update_field_vars(hist_data, elt, buffer, rbe, rec);
5127 }
5128 
5129 static inline void add_to_key(char *compound_key, void *key,
5130 			      struct hist_field *key_field, void *rec)
5131 {
5132 	size_t size = key_field->size;
5133 
5134 	if (key_field->flags & HIST_FIELD_FL_STRING) {
5135 		struct ftrace_event_field *field;
5136 
5137 		field = key_field->field;
5138 		if (field->filter_type == FILTER_DYN_STRING ||
5139 		    field->filter_type == FILTER_RDYN_STRING)
5140 			size = *(u32 *)(rec + field->offset) >> 16;
5141 		else if (field->filter_type == FILTER_STATIC_STRING)
5142 			size = field->size;
5143 
5144 		/* ensure NULL-termination */
5145 		if (size > key_field->size - 1)
5146 			size = key_field->size - 1;
5147 
5148 		strncpy(compound_key + key_field->offset, (char *)key, size);
5149 	} else
5150 		memcpy(compound_key + key_field->offset, key, size);
5151 }
5152 
5153 static void
5154 hist_trigger_actions(struct hist_trigger_data *hist_data,
5155 		     struct tracing_map_elt *elt,
5156 		     struct trace_buffer *buffer, void *rec,
5157 		     struct ring_buffer_event *rbe, void *key,
5158 		     u64 *var_ref_vals)
5159 {
5160 	struct action_data *data;
5161 	unsigned int i;
5162 
5163 	for (i = 0; i < hist_data->n_actions; i++) {
5164 		data = hist_data->actions[i];
5165 		data->fn(hist_data, elt, buffer, rec, rbe, key, data, var_ref_vals);
5166 	}
5167 }
5168 
5169 static void event_hist_trigger(struct event_trigger_data *data,
5170 			       struct trace_buffer *buffer, void *rec,
5171 			       struct ring_buffer_event *rbe)
5172 {
5173 	struct hist_trigger_data *hist_data = data->private_data;
5174 	bool use_compound_key = (hist_data->n_keys > 1);
5175 	unsigned long entries[HIST_STACKTRACE_DEPTH];
5176 	u64 var_ref_vals[TRACING_MAP_VARS_MAX];
5177 	char compound_key[HIST_KEY_SIZE_MAX];
5178 	struct tracing_map_elt *elt = NULL;
5179 	struct hist_field *key_field;
5180 	u64 field_contents;
5181 	void *key = NULL;
5182 	unsigned int i;
5183 
5184 	if (unlikely(!rbe))
5185 		return;
5186 
5187 	memset(compound_key, 0, hist_data->key_size);
5188 
5189 	for_each_hist_key_field(i, hist_data) {
5190 		key_field = hist_data->fields[i];
5191 
5192 		if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5193 			memset(entries, 0, HIST_STACKTRACE_SIZE);
5194 			stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
5195 					 HIST_STACKTRACE_SKIP);
5196 			key = entries;
5197 		} else {
5198 			field_contents = hist_fn_call(key_field, elt, buffer, rbe, rec);
5199 			if (key_field->flags & HIST_FIELD_FL_STRING) {
5200 				key = (void *)(unsigned long)field_contents;
5201 				use_compound_key = true;
5202 			} else
5203 				key = (void *)&field_contents;
5204 		}
5205 
5206 		if (use_compound_key)
5207 			add_to_key(compound_key, key, key_field, rec);
5208 	}
5209 
5210 	if (use_compound_key)
5211 		key = compound_key;
5212 
5213 	if (hist_data->n_var_refs &&
5214 	    !resolve_var_refs(hist_data, key, var_ref_vals, false))
5215 		return;
5216 
5217 	elt = tracing_map_insert(hist_data->map, key);
5218 	if (!elt)
5219 		return;
5220 
5221 	hist_trigger_elt_update(hist_data, elt, buffer, rec, rbe, var_ref_vals);
5222 
5223 	if (resolve_var_refs(hist_data, key, var_ref_vals, true))
5224 		hist_trigger_actions(hist_data, elt, buffer, rec, rbe, key, var_ref_vals);
5225 }
5226 
5227 static void hist_trigger_stacktrace_print(struct seq_file *m,
5228 					  unsigned long *stacktrace_entries,
5229 					  unsigned int max_entries)
5230 {
5231 	unsigned int spaces = 8;
5232 	unsigned int i;
5233 
5234 	for (i = 0; i < max_entries; i++) {
5235 		if (!stacktrace_entries[i])
5236 			return;
5237 
5238 		seq_printf(m, "%*c", 1 + spaces, ' ');
5239 		seq_printf(m, "%pS\n", (void*)stacktrace_entries[i]);
5240 	}
5241 }
5242 
5243 static void hist_trigger_print_key(struct seq_file *m,
5244 				   struct hist_trigger_data *hist_data,
5245 				   void *key,
5246 				   struct tracing_map_elt *elt)
5247 {
5248 	struct hist_field *key_field;
5249 	bool multiline = false;
5250 	const char *field_name;
5251 	unsigned int i;
5252 	u64 uval;
5253 
5254 	seq_puts(m, "{ ");
5255 
5256 	for_each_hist_key_field(i, hist_data) {
5257 		key_field = hist_data->fields[i];
5258 
5259 		if (i > hist_data->n_vals)
5260 			seq_puts(m, ", ");
5261 
5262 		field_name = hist_field_name(key_field, 0);
5263 
5264 		if (key_field->flags & HIST_FIELD_FL_HEX) {
5265 			uval = *(u64 *)(key + key_field->offset);
5266 			seq_printf(m, "%s: %llx", field_name, uval);
5267 		} else if (key_field->flags & HIST_FIELD_FL_SYM) {
5268 			uval = *(u64 *)(key + key_field->offset);
5269 			seq_printf(m, "%s: [%llx] %-45ps", field_name,
5270 				   uval, (void *)(uintptr_t)uval);
5271 		} else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
5272 			uval = *(u64 *)(key + key_field->offset);
5273 			seq_printf(m, "%s: [%llx] %-55pS", field_name,
5274 				   uval, (void *)(uintptr_t)uval);
5275 		} else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
5276 			struct hist_elt_data *elt_data = elt->private_data;
5277 			char *comm;
5278 
5279 			if (WARN_ON_ONCE(!elt_data))
5280 				return;
5281 
5282 			comm = elt_data->comm;
5283 
5284 			uval = *(u64 *)(key + key_field->offset);
5285 			seq_printf(m, "%s: %-16s[%10llu]", field_name,
5286 				   comm, uval);
5287 		} else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
5288 			const char *syscall_name;
5289 
5290 			uval = *(u64 *)(key + key_field->offset);
5291 			syscall_name = get_syscall_name(uval);
5292 			if (!syscall_name)
5293 				syscall_name = "unknown_syscall";
5294 
5295 			seq_printf(m, "%s: %-30s[%3llu]", field_name,
5296 				   syscall_name, uval);
5297 		} else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5298 			seq_puts(m, "stacktrace:\n");
5299 			hist_trigger_stacktrace_print(m,
5300 						      key + key_field->offset,
5301 						      HIST_STACKTRACE_DEPTH);
5302 			multiline = true;
5303 		} else if (key_field->flags & HIST_FIELD_FL_LOG2) {
5304 			seq_printf(m, "%s: ~ 2^%-2llu", field_name,
5305 				   *(u64 *)(key + key_field->offset));
5306 		} else if (key_field->flags & HIST_FIELD_FL_BUCKET) {
5307 			unsigned long buckets = key_field->buckets;
5308 			uval = *(u64 *)(key + key_field->offset);
5309 			seq_printf(m, "%s: ~ %llu-%llu", field_name,
5310 				   uval, uval + buckets -1);
5311 		} else if (key_field->flags & HIST_FIELD_FL_STRING) {
5312 			seq_printf(m, "%s: %-50s", field_name,
5313 				   (char *)(key + key_field->offset));
5314 		} else {
5315 			uval = *(u64 *)(key + key_field->offset);
5316 			seq_printf(m, "%s: %10llu", field_name, uval);
5317 		}
5318 	}
5319 
5320 	if (!multiline)
5321 		seq_puts(m, " ");
5322 
5323 	seq_puts(m, "}");
5324 }
5325 
5326 /* Get the 100 times of the percentage of @val in @total */
5327 static inline unsigned int __get_percentage(u64 val, u64 total)
5328 {
5329 	if (!total)
5330 		goto div0;
5331 
5332 	if (val < (U64_MAX / 10000))
5333 		return (unsigned int)div64_ul(val * 10000, total);
5334 
5335 	total = div64_u64(total, 10000);
5336 	if (!total)
5337 		goto div0;
5338 
5339 	return (unsigned int)div64_ul(val, total);
5340 div0:
5341 	return val ? UINT_MAX : 0;
5342 }
5343 
5344 #define BAR_CHAR '#'
5345 
5346 static inline const char *__fill_bar_str(char *buf, int size, u64 val, u64 max)
5347 {
5348 	unsigned int len = __get_percentage(val, max);
5349 	int i;
5350 
5351 	if (len == UINT_MAX) {
5352 		snprintf(buf, size, "[ERROR]");
5353 		return buf;
5354 	}
5355 
5356 	len = len * size / 10000;
5357 	for (i = 0; i < len && i < size; i++)
5358 		buf[i] = BAR_CHAR;
5359 	while (i < size)
5360 		buf[i++] = ' ';
5361 	buf[size] = '\0';
5362 
5363 	return buf;
5364 }
5365 
5366 struct hist_val_stat {
5367 	u64 max;
5368 	u64 total;
5369 };
5370 
5371 static void hist_trigger_print_val(struct seq_file *m, unsigned int idx,
5372 				   const char *field_name, unsigned long flags,
5373 				   struct hist_val_stat *stats,
5374 				   struct tracing_map_elt *elt)
5375 {
5376 	u64 val = tracing_map_read_sum(elt, idx);
5377 	unsigned int pc;
5378 	char bar[21];
5379 
5380 	if (flags & HIST_FIELD_FL_PERCENT) {
5381 		pc = __get_percentage(val, stats[idx].total);
5382 		if (pc == UINT_MAX)
5383 			seq_printf(m, " %s (%%):[ERROR]", field_name);
5384 		else
5385 			seq_printf(m, " %s (%%): %3u.%02u", field_name,
5386 					pc / 100, pc % 100);
5387 	} else if (flags & HIST_FIELD_FL_GRAPH) {
5388 		seq_printf(m, " %s: %20s", field_name,
5389 			   __fill_bar_str(bar, 20, val, stats[idx].max));
5390 	} else if (flags & HIST_FIELD_FL_HEX) {
5391 		seq_printf(m, " %s: %10llx", field_name, val);
5392 	} else {
5393 		seq_printf(m, " %s: %10llu", field_name, val);
5394 	}
5395 }
5396 
5397 static void hist_trigger_entry_print(struct seq_file *m,
5398 				     struct hist_trigger_data *hist_data,
5399 				     struct hist_val_stat *stats,
5400 				     void *key,
5401 				     struct tracing_map_elt *elt)
5402 {
5403 	const char *field_name;
5404 	unsigned int i = HITCOUNT_IDX;
5405 	unsigned long flags;
5406 
5407 	hist_trigger_print_key(m, hist_data, key, elt);
5408 
5409 	/* At first, show the raw hitcount if !nohitcount */
5410 	if (!hist_data->attrs->no_hitcount)
5411 		hist_trigger_print_val(m, i, "hitcount", 0, stats, elt);
5412 
5413 	for (i = 1; i < hist_data->n_vals; i++) {
5414 		field_name = hist_field_name(hist_data->fields[i], 0);
5415 		flags = hist_data->fields[i]->flags;
5416 		if (flags & HIST_FIELD_FL_VAR || flags & HIST_FIELD_FL_EXPR)
5417 			continue;
5418 
5419 		seq_puts(m, " ");
5420 		hist_trigger_print_val(m, i, field_name, flags, stats, elt);
5421 	}
5422 
5423 	print_actions(m, hist_data, elt);
5424 
5425 	seq_puts(m, "\n");
5426 }
5427 
5428 static int print_entries(struct seq_file *m,
5429 			 struct hist_trigger_data *hist_data)
5430 {
5431 	struct tracing_map_sort_entry **sort_entries = NULL;
5432 	struct tracing_map *map = hist_data->map;
5433 	int i, j, n_entries;
5434 	struct hist_val_stat *stats = NULL;
5435 	u64 val;
5436 
5437 	n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
5438 					     hist_data->n_sort_keys,
5439 					     &sort_entries);
5440 	if (n_entries < 0)
5441 		return n_entries;
5442 
5443 	/* Calculate the max and the total for each field if needed. */
5444 	for (j = 0; j < hist_data->n_vals; j++) {
5445 		if (!(hist_data->fields[j]->flags &
5446 			(HIST_FIELD_FL_PERCENT | HIST_FIELD_FL_GRAPH)))
5447 			continue;
5448 		if (!stats) {
5449 			stats = kcalloc(hist_data->n_vals, sizeof(*stats),
5450 				       GFP_KERNEL);
5451 			if (!stats) {
5452 				n_entries = -ENOMEM;
5453 				goto out;
5454 			}
5455 		}
5456 		for (i = 0; i < n_entries; i++) {
5457 			val = tracing_map_read_sum(sort_entries[i]->elt, j);
5458 			stats[j].total += val;
5459 			if (stats[j].max < val)
5460 				stats[j].max = val;
5461 		}
5462 	}
5463 
5464 	for (i = 0; i < n_entries; i++)
5465 		hist_trigger_entry_print(m, hist_data, stats,
5466 					 sort_entries[i]->key,
5467 					 sort_entries[i]->elt);
5468 
5469 	kfree(stats);
5470 out:
5471 	tracing_map_destroy_sort_entries(sort_entries, n_entries);
5472 
5473 	return n_entries;
5474 }
5475 
5476 static void hist_trigger_show(struct seq_file *m,
5477 			      struct event_trigger_data *data, int n)
5478 {
5479 	struct hist_trigger_data *hist_data;
5480 	int n_entries;
5481 
5482 	if (n > 0)
5483 		seq_puts(m, "\n\n");
5484 
5485 	seq_puts(m, "# event histogram\n#\n# trigger info: ");
5486 	data->ops->print(m, data);
5487 	seq_puts(m, "#\n\n");
5488 
5489 	hist_data = data->private_data;
5490 	n_entries = print_entries(m, hist_data);
5491 	if (n_entries < 0)
5492 		n_entries = 0;
5493 
5494 	track_data_snapshot_print(m, hist_data);
5495 
5496 	seq_printf(m, "\nTotals:\n    Hits: %llu\n    Entries: %u\n    Dropped: %llu\n",
5497 		   (u64)atomic64_read(&hist_data->map->hits),
5498 		   n_entries, (u64)atomic64_read(&hist_data->map->drops));
5499 }
5500 
5501 static int hist_show(struct seq_file *m, void *v)
5502 {
5503 	struct event_trigger_data *data;
5504 	struct trace_event_file *event_file;
5505 	int n = 0, ret = 0;
5506 
5507 	mutex_lock(&event_mutex);
5508 
5509 	event_file = event_file_data(m->private);
5510 	if (unlikely(!event_file)) {
5511 		ret = -ENODEV;
5512 		goto out_unlock;
5513 	}
5514 
5515 	list_for_each_entry(data, &event_file->triggers, list) {
5516 		if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5517 			hist_trigger_show(m, data, n++);
5518 	}
5519 
5520  out_unlock:
5521 	mutex_unlock(&event_mutex);
5522 
5523 	return ret;
5524 }
5525 
5526 static int event_hist_open(struct inode *inode, struct file *file)
5527 {
5528 	int ret;
5529 
5530 	ret = security_locked_down(LOCKDOWN_TRACEFS);
5531 	if (ret)
5532 		return ret;
5533 
5534 	return single_open(file, hist_show, file);
5535 }
5536 
5537 const struct file_operations event_hist_fops = {
5538 	.open = event_hist_open,
5539 	.read = seq_read,
5540 	.llseek = seq_lseek,
5541 	.release = single_release,
5542 };
5543 
5544 #ifdef CONFIG_HIST_TRIGGERS_DEBUG
5545 static void hist_field_debug_show_flags(struct seq_file *m,
5546 					unsigned long flags)
5547 {
5548 	seq_puts(m, "      flags:\n");
5549 
5550 	if (flags & HIST_FIELD_FL_KEY)
5551 		seq_puts(m, "        HIST_FIELD_FL_KEY\n");
5552 	else if (flags & HIST_FIELD_FL_HITCOUNT)
5553 		seq_puts(m, "        VAL: HIST_FIELD_FL_HITCOUNT\n");
5554 	else if (flags & HIST_FIELD_FL_VAR)
5555 		seq_puts(m, "        HIST_FIELD_FL_VAR\n");
5556 	else if (flags & HIST_FIELD_FL_VAR_REF)
5557 		seq_puts(m, "        HIST_FIELD_FL_VAR_REF\n");
5558 	else
5559 		seq_puts(m, "        VAL: normal u64 value\n");
5560 
5561 	if (flags & HIST_FIELD_FL_ALIAS)
5562 		seq_puts(m, "        HIST_FIELD_FL_ALIAS\n");
5563 	else if (flags & HIST_FIELD_FL_CONST)
5564 		seq_puts(m, "        HIST_FIELD_FL_CONST\n");
5565 }
5566 
5567 static int hist_field_debug_show(struct seq_file *m,
5568 				 struct hist_field *field, unsigned long flags)
5569 {
5570 	if ((field->flags & flags) != flags) {
5571 		seq_printf(m, "ERROR: bad flags - %lx\n", flags);
5572 		return -EINVAL;
5573 	}
5574 
5575 	hist_field_debug_show_flags(m, field->flags);
5576 	if (field->field)
5577 		seq_printf(m, "      ftrace_event_field name: %s\n",
5578 			   field->field->name);
5579 
5580 	if (field->flags & HIST_FIELD_FL_VAR) {
5581 		seq_printf(m, "      var.name: %s\n", field->var.name);
5582 		seq_printf(m, "      var.idx (into tracing_map_elt.vars[]): %u\n",
5583 			   field->var.idx);
5584 	}
5585 
5586 	if (field->flags & HIST_FIELD_FL_CONST)
5587 		seq_printf(m, "      constant: %llu\n", field->constant);
5588 
5589 	if (field->flags & HIST_FIELD_FL_ALIAS)
5590 		seq_printf(m, "      var_ref_idx (into hist_data->var_refs[]): %u\n",
5591 			   field->var_ref_idx);
5592 
5593 	if (field->flags & HIST_FIELD_FL_VAR_REF) {
5594 		seq_printf(m, "      name: %s\n", field->name);
5595 		seq_printf(m, "      var.idx (into tracing_map_elt.vars[]): %u\n",
5596 			   field->var.idx);
5597 		seq_printf(m, "      var.hist_data: %p\n", field->var.hist_data);
5598 		seq_printf(m, "      var_ref_idx (into hist_data->var_refs[]): %u\n",
5599 			   field->var_ref_idx);
5600 		if (field->system)
5601 			seq_printf(m, "      system: %s\n", field->system);
5602 		if (field->event_name)
5603 			seq_printf(m, "      event_name: %s\n", field->event_name);
5604 	}
5605 
5606 	seq_printf(m, "      type: %s\n", field->type);
5607 	seq_printf(m, "      size: %u\n", field->size);
5608 	seq_printf(m, "      is_signed: %u\n", field->is_signed);
5609 
5610 	return 0;
5611 }
5612 
5613 static int field_var_debug_show(struct seq_file *m,
5614 				struct field_var *field_var, unsigned int i,
5615 				bool save_vars)
5616 {
5617 	const char *vars_name = save_vars ? "save_vars" : "field_vars";
5618 	struct hist_field *field;
5619 	int ret = 0;
5620 
5621 	seq_printf(m, "\n    hist_data->%s[%d]:\n", vars_name, i);
5622 
5623 	field = field_var->var;
5624 
5625 	seq_printf(m, "\n      %s[%d].var:\n", vars_name, i);
5626 
5627 	hist_field_debug_show_flags(m, field->flags);
5628 	seq_printf(m, "      var.name: %s\n", field->var.name);
5629 	seq_printf(m, "      var.idx (into tracing_map_elt.vars[]): %u\n",
5630 		   field->var.idx);
5631 
5632 	field = field_var->val;
5633 
5634 	seq_printf(m, "\n      %s[%d].val:\n", vars_name, i);
5635 	if (field->field)
5636 		seq_printf(m, "      ftrace_event_field name: %s\n",
5637 			   field->field->name);
5638 	else {
5639 		ret = -EINVAL;
5640 		goto out;
5641 	}
5642 
5643 	seq_printf(m, "      type: %s\n", field->type);
5644 	seq_printf(m, "      size: %u\n", field->size);
5645 	seq_printf(m, "      is_signed: %u\n", field->is_signed);
5646 out:
5647 	return ret;
5648 }
5649 
5650 static int hist_action_debug_show(struct seq_file *m,
5651 				  struct action_data *data, int i)
5652 {
5653 	int ret = 0;
5654 
5655 	if (data->handler == HANDLER_ONMAX ||
5656 	    data->handler == HANDLER_ONCHANGE) {
5657 		seq_printf(m, "\n    hist_data->actions[%d].track_data.var_ref:\n", i);
5658 		ret = hist_field_debug_show(m, data->track_data.var_ref,
5659 					    HIST_FIELD_FL_VAR_REF);
5660 		if (ret)
5661 			goto out;
5662 
5663 		seq_printf(m, "\n    hist_data->actions[%d].track_data.track_var:\n", i);
5664 		ret = hist_field_debug_show(m, data->track_data.track_var,
5665 					    HIST_FIELD_FL_VAR);
5666 		if (ret)
5667 			goto out;
5668 	}
5669 
5670 	if (data->handler == HANDLER_ONMATCH) {
5671 		seq_printf(m, "\n    hist_data->actions[%d].match_data.event_system: %s\n",
5672 			   i, data->match_data.event_system);
5673 		seq_printf(m, "    hist_data->actions[%d].match_data.event: %s\n",
5674 			   i, data->match_data.event);
5675 	}
5676 out:
5677 	return ret;
5678 }
5679 
5680 static int hist_actions_debug_show(struct seq_file *m,
5681 				   struct hist_trigger_data *hist_data)
5682 {
5683 	int i, ret = 0;
5684 
5685 	if (hist_data->n_actions)
5686 		seq_puts(m, "\n  action tracking variables (for onmax()/onchange()/onmatch()):\n");
5687 
5688 	for (i = 0; i < hist_data->n_actions; i++) {
5689 		struct action_data *action = hist_data->actions[i];
5690 
5691 		ret = hist_action_debug_show(m, action, i);
5692 		if (ret)
5693 			goto out;
5694 	}
5695 
5696 	if (hist_data->n_save_vars)
5697 		seq_puts(m, "\n  save action variables (save() params):\n");
5698 
5699 	for (i = 0; i < hist_data->n_save_vars; i++) {
5700 		ret = field_var_debug_show(m, hist_data->save_vars[i], i, true);
5701 		if (ret)
5702 			goto out;
5703 	}
5704 out:
5705 	return ret;
5706 }
5707 
5708 static void hist_trigger_debug_show(struct seq_file *m,
5709 				    struct event_trigger_data *data, int n)
5710 {
5711 	struct hist_trigger_data *hist_data;
5712 	int i, ret;
5713 
5714 	if (n > 0)
5715 		seq_puts(m, "\n\n");
5716 
5717 	seq_puts(m, "# event histogram\n#\n# trigger info: ");
5718 	data->ops->print(m, data);
5719 	seq_puts(m, "#\n\n");
5720 
5721 	hist_data = data->private_data;
5722 
5723 	seq_printf(m, "hist_data: %p\n\n", hist_data);
5724 	seq_printf(m, "  n_vals: %u\n", hist_data->n_vals);
5725 	seq_printf(m, "  n_keys: %u\n", hist_data->n_keys);
5726 	seq_printf(m, "  n_fields: %u\n", hist_data->n_fields);
5727 
5728 	seq_puts(m, "\n  val fields:\n\n");
5729 
5730 	seq_puts(m, "    hist_data->fields[0]:\n");
5731 	ret = hist_field_debug_show(m, hist_data->fields[0],
5732 				    HIST_FIELD_FL_HITCOUNT);
5733 	if (ret)
5734 		return;
5735 
5736 	for (i = 1; i < hist_data->n_vals; i++) {
5737 		seq_printf(m, "\n    hist_data->fields[%d]:\n", i);
5738 		ret = hist_field_debug_show(m, hist_data->fields[i], 0);
5739 		if (ret)
5740 			return;
5741 	}
5742 
5743 	seq_puts(m, "\n  key fields:\n");
5744 
5745 	for (i = hist_data->n_vals; i < hist_data->n_fields; i++) {
5746 		seq_printf(m, "\n    hist_data->fields[%d]:\n", i);
5747 		ret = hist_field_debug_show(m, hist_data->fields[i],
5748 					    HIST_FIELD_FL_KEY);
5749 		if (ret)
5750 			return;
5751 	}
5752 
5753 	if (hist_data->n_var_refs)
5754 		seq_puts(m, "\n  variable reference fields:\n");
5755 
5756 	for (i = 0; i < hist_data->n_var_refs; i++) {
5757 		seq_printf(m, "\n    hist_data->var_refs[%d]:\n", i);
5758 		ret = hist_field_debug_show(m, hist_data->var_refs[i],
5759 					    HIST_FIELD_FL_VAR_REF);
5760 		if (ret)
5761 			return;
5762 	}
5763 
5764 	if (hist_data->n_field_vars)
5765 		seq_puts(m, "\n  field variables:\n");
5766 
5767 	for (i = 0; i < hist_data->n_field_vars; i++) {
5768 		ret = field_var_debug_show(m, hist_data->field_vars[i], i, false);
5769 		if (ret)
5770 			return;
5771 	}
5772 
5773 	ret = hist_actions_debug_show(m, hist_data);
5774 	if (ret)
5775 		return;
5776 }
5777 
5778 static int hist_debug_show(struct seq_file *m, void *v)
5779 {
5780 	struct event_trigger_data *data;
5781 	struct trace_event_file *event_file;
5782 	int n = 0, ret = 0;
5783 
5784 	mutex_lock(&event_mutex);
5785 
5786 	event_file = event_file_data(m->private);
5787 	if (unlikely(!event_file)) {
5788 		ret = -ENODEV;
5789 		goto out_unlock;
5790 	}
5791 
5792 	list_for_each_entry(data, &event_file->triggers, list) {
5793 		if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5794 			hist_trigger_debug_show(m, data, n++);
5795 	}
5796 
5797  out_unlock:
5798 	mutex_unlock(&event_mutex);
5799 
5800 	return ret;
5801 }
5802 
5803 static int event_hist_debug_open(struct inode *inode, struct file *file)
5804 {
5805 	int ret;
5806 
5807 	ret = security_locked_down(LOCKDOWN_TRACEFS);
5808 	if (ret)
5809 		return ret;
5810 
5811 	return single_open(file, hist_debug_show, file);
5812 }
5813 
5814 const struct file_operations event_hist_debug_fops = {
5815 	.open = event_hist_debug_open,
5816 	.read = seq_read,
5817 	.llseek = seq_lseek,
5818 	.release = single_release,
5819 };
5820 #endif
5821 
5822 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
5823 {
5824 	const char *field_name = hist_field_name(hist_field, 0);
5825 
5826 	if (hist_field->var.name)
5827 		seq_printf(m, "%s=", hist_field->var.name);
5828 
5829 	if (hist_field->flags & HIST_FIELD_FL_CPU)
5830 		seq_puts(m, "common_cpu");
5831 	else if (hist_field->flags & HIST_FIELD_FL_CONST)
5832 		seq_printf(m, "%llu", hist_field->constant);
5833 	else if (field_name) {
5834 		if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
5835 		    hist_field->flags & HIST_FIELD_FL_ALIAS)
5836 			seq_putc(m, '$');
5837 		seq_printf(m, "%s", field_name);
5838 	} else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
5839 		seq_puts(m, "common_timestamp");
5840 
5841 	if (hist_field->flags) {
5842 		if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
5843 		    !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
5844 			const char *flags = get_hist_field_flags(hist_field);
5845 
5846 			if (flags)
5847 				seq_printf(m, ".%s", flags);
5848 		}
5849 	}
5850 	if (hist_field->buckets)
5851 		seq_printf(m, "=%ld", hist_field->buckets);
5852 }
5853 
5854 static int event_hist_trigger_print(struct seq_file *m,
5855 				    struct event_trigger_data *data)
5856 {
5857 	struct hist_trigger_data *hist_data = data->private_data;
5858 	struct hist_field *field;
5859 	bool have_var = false;
5860 	bool show_val = false;
5861 	unsigned int i;
5862 
5863 	seq_puts(m, HIST_PREFIX);
5864 
5865 	if (data->name)
5866 		seq_printf(m, "%s:", data->name);
5867 
5868 	seq_puts(m, "keys=");
5869 
5870 	for_each_hist_key_field(i, hist_data) {
5871 		field = hist_data->fields[i];
5872 
5873 		if (i > hist_data->n_vals)
5874 			seq_puts(m, ",");
5875 
5876 		if (field->flags & HIST_FIELD_FL_STACKTRACE)
5877 			seq_puts(m, "stacktrace");
5878 		else
5879 			hist_field_print(m, field);
5880 	}
5881 
5882 	seq_puts(m, ":vals=");
5883 
5884 	for_each_hist_val_field(i, hist_data) {
5885 		field = hist_data->fields[i];
5886 		if (field->flags & HIST_FIELD_FL_VAR) {
5887 			have_var = true;
5888 			continue;
5889 		}
5890 
5891 		if (i == HITCOUNT_IDX) {
5892 			if (hist_data->attrs->no_hitcount)
5893 				continue;
5894 			seq_puts(m, "hitcount");
5895 		} else {
5896 			if (show_val)
5897 				seq_puts(m, ",");
5898 			hist_field_print(m, field);
5899 		}
5900 		show_val = true;
5901 	}
5902 
5903 	if (have_var) {
5904 		unsigned int n = 0;
5905 
5906 		seq_puts(m, ":");
5907 
5908 		for_each_hist_val_field(i, hist_data) {
5909 			field = hist_data->fields[i];
5910 
5911 			if (field->flags & HIST_FIELD_FL_VAR) {
5912 				if (n++)
5913 					seq_puts(m, ",");
5914 				hist_field_print(m, field);
5915 			}
5916 		}
5917 	}
5918 
5919 	seq_puts(m, ":sort=");
5920 
5921 	for (i = 0; i < hist_data->n_sort_keys; i++) {
5922 		struct tracing_map_sort_key *sort_key;
5923 		unsigned int idx, first_key_idx;
5924 
5925 		/* skip VAR vals */
5926 		first_key_idx = hist_data->n_vals - hist_data->n_vars;
5927 
5928 		sort_key = &hist_data->sort_keys[i];
5929 		idx = sort_key->field_idx;
5930 
5931 		if (WARN_ON(idx >= HIST_FIELDS_MAX))
5932 			return -EINVAL;
5933 
5934 		if (i > 0)
5935 			seq_puts(m, ",");
5936 
5937 		if (idx == HITCOUNT_IDX)
5938 			seq_puts(m, "hitcount");
5939 		else {
5940 			if (idx >= first_key_idx)
5941 				idx += hist_data->n_vars;
5942 			hist_field_print(m, hist_data->fields[idx]);
5943 		}
5944 
5945 		if (sort_key->descending)
5946 			seq_puts(m, ".descending");
5947 	}
5948 	seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
5949 	if (hist_data->enable_timestamps)
5950 		seq_printf(m, ":clock=%s", hist_data->attrs->clock);
5951 	if (hist_data->attrs->no_hitcount)
5952 		seq_puts(m, ":nohitcount");
5953 
5954 	print_actions_spec(m, hist_data);
5955 
5956 	if (data->filter_str)
5957 		seq_printf(m, " if %s", data->filter_str);
5958 
5959 	if (data->paused)
5960 		seq_puts(m, " [paused]");
5961 	else
5962 		seq_puts(m, " [active]");
5963 
5964 	seq_putc(m, '\n');
5965 
5966 	return 0;
5967 }
5968 
5969 static int event_hist_trigger_init(struct event_trigger_data *data)
5970 {
5971 	struct hist_trigger_data *hist_data = data->private_data;
5972 
5973 	if (!data->ref && hist_data->attrs->name)
5974 		save_named_trigger(hist_data->attrs->name, data);
5975 
5976 	data->ref++;
5977 
5978 	return 0;
5979 }
5980 
5981 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
5982 {
5983 	struct trace_event_file *file;
5984 	unsigned int i;
5985 	char *cmd;
5986 	int ret;
5987 
5988 	for (i = 0; i < hist_data->n_field_var_hists; i++) {
5989 		file = hist_data->field_var_hists[i]->hist_data->event_file;
5990 		cmd = hist_data->field_var_hists[i]->cmd;
5991 		ret = event_hist_trigger_parse(&trigger_hist_cmd, file,
5992 					       "!hist", "hist", cmd);
5993 		WARN_ON_ONCE(ret < 0);
5994 	}
5995 }
5996 
5997 static void event_hist_trigger_free(struct event_trigger_data *data)
5998 {
5999 	struct hist_trigger_data *hist_data = data->private_data;
6000 
6001 	if (WARN_ON_ONCE(data->ref <= 0))
6002 		return;
6003 
6004 	data->ref--;
6005 	if (!data->ref) {
6006 		if (data->name)
6007 			del_named_trigger(data);
6008 
6009 		trigger_data_free(data);
6010 
6011 		remove_hist_vars(hist_data);
6012 
6013 		unregister_field_var_hists(hist_data);
6014 
6015 		destroy_hist_data(hist_data);
6016 	}
6017 }
6018 
6019 static struct event_trigger_ops event_hist_trigger_ops = {
6020 	.trigger		= event_hist_trigger,
6021 	.print			= event_hist_trigger_print,
6022 	.init			= event_hist_trigger_init,
6023 	.free			= event_hist_trigger_free,
6024 };
6025 
6026 static int event_hist_trigger_named_init(struct event_trigger_data *data)
6027 {
6028 	data->ref++;
6029 
6030 	save_named_trigger(data->named_data->name, data);
6031 
6032 	event_hist_trigger_init(data->named_data);
6033 
6034 	return 0;
6035 }
6036 
6037 static void event_hist_trigger_named_free(struct event_trigger_data *data)
6038 {
6039 	if (WARN_ON_ONCE(data->ref <= 0))
6040 		return;
6041 
6042 	event_hist_trigger_free(data->named_data);
6043 
6044 	data->ref--;
6045 	if (!data->ref) {
6046 		del_named_trigger(data);
6047 		trigger_data_free(data);
6048 	}
6049 }
6050 
6051 static struct event_trigger_ops event_hist_trigger_named_ops = {
6052 	.trigger		= event_hist_trigger,
6053 	.print			= event_hist_trigger_print,
6054 	.init			= event_hist_trigger_named_init,
6055 	.free			= event_hist_trigger_named_free,
6056 };
6057 
6058 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
6059 							    char *param)
6060 {
6061 	return &event_hist_trigger_ops;
6062 }
6063 
6064 static void hist_clear(struct event_trigger_data *data)
6065 {
6066 	struct hist_trigger_data *hist_data = data->private_data;
6067 
6068 	if (data->name)
6069 		pause_named_trigger(data);
6070 
6071 	tracepoint_synchronize_unregister();
6072 
6073 	tracing_map_clear(hist_data->map);
6074 
6075 	if (data->name)
6076 		unpause_named_trigger(data);
6077 }
6078 
6079 static bool compatible_field(struct ftrace_event_field *field,
6080 			     struct ftrace_event_field *test_field)
6081 {
6082 	if (field == test_field)
6083 		return true;
6084 	if (field == NULL || test_field == NULL)
6085 		return false;
6086 	if (strcmp(field->name, test_field->name) != 0)
6087 		return false;
6088 	if (strcmp(field->type, test_field->type) != 0)
6089 		return false;
6090 	if (field->size != test_field->size)
6091 		return false;
6092 	if (field->is_signed != test_field->is_signed)
6093 		return false;
6094 
6095 	return true;
6096 }
6097 
6098 static bool hist_trigger_match(struct event_trigger_data *data,
6099 			       struct event_trigger_data *data_test,
6100 			       struct event_trigger_data *named_data,
6101 			       bool ignore_filter)
6102 {
6103 	struct tracing_map_sort_key *sort_key, *sort_key_test;
6104 	struct hist_trigger_data *hist_data, *hist_data_test;
6105 	struct hist_field *key_field, *key_field_test;
6106 	unsigned int i;
6107 
6108 	if (named_data && (named_data != data_test) &&
6109 	    (named_data != data_test->named_data))
6110 		return false;
6111 
6112 	if (!named_data && is_named_trigger(data_test))
6113 		return false;
6114 
6115 	hist_data = data->private_data;
6116 	hist_data_test = data_test->private_data;
6117 
6118 	if (hist_data->n_vals != hist_data_test->n_vals ||
6119 	    hist_data->n_fields != hist_data_test->n_fields ||
6120 	    hist_data->n_sort_keys != hist_data_test->n_sort_keys)
6121 		return false;
6122 
6123 	if (!ignore_filter) {
6124 		if ((data->filter_str && !data_test->filter_str) ||
6125 		   (!data->filter_str && data_test->filter_str))
6126 			return false;
6127 	}
6128 
6129 	for_each_hist_field(i, hist_data) {
6130 		key_field = hist_data->fields[i];
6131 		key_field_test = hist_data_test->fields[i];
6132 
6133 		if (key_field->flags != key_field_test->flags)
6134 			return false;
6135 		if (!compatible_field(key_field->field, key_field_test->field))
6136 			return false;
6137 		if (key_field->offset != key_field_test->offset)
6138 			return false;
6139 		if (key_field->size != key_field_test->size)
6140 			return false;
6141 		if (key_field->is_signed != key_field_test->is_signed)
6142 			return false;
6143 		if (!!key_field->var.name != !!key_field_test->var.name)
6144 			return false;
6145 		if (key_field->var.name &&
6146 		    strcmp(key_field->var.name, key_field_test->var.name) != 0)
6147 			return false;
6148 	}
6149 
6150 	for (i = 0; i < hist_data->n_sort_keys; i++) {
6151 		sort_key = &hist_data->sort_keys[i];
6152 		sort_key_test = &hist_data_test->sort_keys[i];
6153 
6154 		if (sort_key->field_idx != sort_key_test->field_idx ||
6155 		    sort_key->descending != sort_key_test->descending)
6156 			return false;
6157 	}
6158 
6159 	if (!ignore_filter && data->filter_str &&
6160 	    (strcmp(data->filter_str, data_test->filter_str) != 0))
6161 		return false;
6162 
6163 	if (!actions_match(hist_data, hist_data_test))
6164 		return false;
6165 
6166 	return true;
6167 }
6168 
6169 static bool existing_hist_update_only(char *glob,
6170 				      struct event_trigger_data *data,
6171 				      struct trace_event_file *file)
6172 {
6173 	struct hist_trigger_data *hist_data = data->private_data;
6174 	struct event_trigger_data *test, *named_data = NULL;
6175 	bool updated = false;
6176 
6177 	if (!hist_data->attrs->pause && !hist_data->attrs->cont &&
6178 	    !hist_data->attrs->clear)
6179 		goto out;
6180 
6181 	if (hist_data->attrs->name) {
6182 		named_data = find_named_trigger(hist_data->attrs->name);
6183 		if (named_data) {
6184 			if (!hist_trigger_match(data, named_data, named_data,
6185 						true))
6186 				goto out;
6187 		}
6188 	}
6189 
6190 	if (hist_data->attrs->name && !named_data)
6191 		goto out;
6192 
6193 	list_for_each_entry(test, &file->triggers, list) {
6194 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6195 			if (!hist_trigger_match(data, test, named_data, false))
6196 				continue;
6197 			if (hist_data->attrs->pause)
6198 				test->paused = true;
6199 			else if (hist_data->attrs->cont)
6200 				test->paused = false;
6201 			else if (hist_data->attrs->clear)
6202 				hist_clear(test);
6203 			updated = true;
6204 			goto out;
6205 		}
6206 	}
6207  out:
6208 	return updated;
6209 }
6210 
6211 static int hist_register_trigger(char *glob,
6212 				 struct event_trigger_data *data,
6213 				 struct trace_event_file *file)
6214 {
6215 	struct hist_trigger_data *hist_data = data->private_data;
6216 	struct event_trigger_data *test, *named_data = NULL;
6217 	struct trace_array *tr = file->tr;
6218 	int ret = 0;
6219 
6220 	if (hist_data->attrs->name) {
6221 		named_data = find_named_trigger(hist_data->attrs->name);
6222 		if (named_data) {
6223 			if (!hist_trigger_match(data, named_data, named_data,
6224 						true)) {
6225 				hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name));
6226 				ret = -EINVAL;
6227 				goto out;
6228 			}
6229 		}
6230 	}
6231 
6232 	if (hist_data->attrs->name && !named_data)
6233 		goto new;
6234 
6235 	lockdep_assert_held(&event_mutex);
6236 
6237 	list_for_each_entry(test, &file->triggers, list) {
6238 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6239 			if (hist_trigger_match(data, test, named_data, false)) {
6240 				hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
6241 				ret = -EEXIST;
6242 				goto out;
6243 			}
6244 		}
6245 	}
6246  new:
6247 	if (hist_data->attrs->cont || hist_data->attrs->clear) {
6248 		hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0);
6249 		ret = -ENOENT;
6250 		goto out;
6251 	}
6252 
6253 	if (hist_data->attrs->pause)
6254 		data->paused = true;
6255 
6256 	if (named_data) {
6257 		data->private_data = named_data->private_data;
6258 		set_named_trigger_data(data, named_data);
6259 		data->ops = &event_hist_trigger_named_ops;
6260 	}
6261 
6262 	if (data->ops->init) {
6263 		ret = data->ops->init(data);
6264 		if (ret < 0)
6265 			goto out;
6266 	}
6267 
6268 	if (hist_data->enable_timestamps) {
6269 		char *clock = hist_data->attrs->clock;
6270 
6271 		ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
6272 		if (ret) {
6273 			hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock));
6274 			goto out;
6275 		}
6276 
6277 		tracing_set_filter_buffering(file->tr, true);
6278 	}
6279 
6280 	if (named_data)
6281 		destroy_hist_data(hist_data);
6282  out:
6283 	return ret;
6284 }
6285 
6286 static int hist_trigger_enable(struct event_trigger_data *data,
6287 			       struct trace_event_file *file)
6288 {
6289 	int ret = 0;
6290 
6291 	list_add_tail_rcu(&data->list, &file->triggers);
6292 
6293 	update_cond_flag(file);
6294 
6295 	if (trace_event_trigger_enable_disable(file, 1) < 0) {
6296 		list_del_rcu(&data->list);
6297 		update_cond_flag(file);
6298 		ret--;
6299 	}
6300 
6301 	return ret;
6302 }
6303 
6304 static bool have_hist_trigger_match(struct event_trigger_data *data,
6305 				    struct trace_event_file *file)
6306 {
6307 	struct hist_trigger_data *hist_data = data->private_data;
6308 	struct event_trigger_data *test, *named_data = NULL;
6309 	bool match = false;
6310 
6311 	lockdep_assert_held(&event_mutex);
6312 
6313 	if (hist_data->attrs->name)
6314 		named_data = find_named_trigger(hist_data->attrs->name);
6315 
6316 	list_for_each_entry(test, &file->triggers, list) {
6317 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6318 			if (hist_trigger_match(data, test, named_data, false)) {
6319 				match = true;
6320 				break;
6321 			}
6322 		}
6323 	}
6324 
6325 	return match;
6326 }
6327 
6328 static bool hist_trigger_check_refs(struct event_trigger_data *data,
6329 				    struct trace_event_file *file)
6330 {
6331 	struct hist_trigger_data *hist_data = data->private_data;
6332 	struct event_trigger_data *test, *named_data = NULL;
6333 
6334 	lockdep_assert_held(&event_mutex);
6335 
6336 	if (hist_data->attrs->name)
6337 		named_data = find_named_trigger(hist_data->attrs->name);
6338 
6339 	list_for_each_entry(test, &file->triggers, list) {
6340 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6341 			if (!hist_trigger_match(data, test, named_data, false))
6342 				continue;
6343 			hist_data = test->private_data;
6344 			if (check_var_refs(hist_data))
6345 				return true;
6346 			break;
6347 		}
6348 	}
6349 
6350 	return false;
6351 }
6352 
6353 static void hist_unregister_trigger(char *glob,
6354 				    struct event_trigger_data *data,
6355 				    struct trace_event_file *file)
6356 {
6357 	struct event_trigger_data *test = NULL, *iter, *named_data = NULL;
6358 	struct hist_trigger_data *hist_data = data->private_data;
6359 
6360 	lockdep_assert_held(&event_mutex);
6361 
6362 	if (hist_data->attrs->name)
6363 		named_data = find_named_trigger(hist_data->attrs->name);
6364 
6365 	list_for_each_entry(iter, &file->triggers, list) {
6366 		if (iter->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6367 			if (!hist_trigger_match(data, iter, named_data, false))
6368 				continue;
6369 			test = iter;
6370 			list_del_rcu(&test->list);
6371 			trace_event_trigger_enable_disable(file, 0);
6372 			update_cond_flag(file);
6373 			break;
6374 		}
6375 	}
6376 
6377 	if (test && test->ops->free)
6378 		test->ops->free(test);
6379 
6380 	if (hist_data->enable_timestamps) {
6381 		if (!hist_data->remove || test)
6382 			tracing_set_filter_buffering(file->tr, false);
6383 	}
6384 }
6385 
6386 static bool hist_file_check_refs(struct trace_event_file *file)
6387 {
6388 	struct hist_trigger_data *hist_data;
6389 	struct event_trigger_data *test;
6390 
6391 	lockdep_assert_held(&event_mutex);
6392 
6393 	list_for_each_entry(test, &file->triggers, list) {
6394 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6395 			hist_data = test->private_data;
6396 			if (check_var_refs(hist_data))
6397 				return true;
6398 		}
6399 	}
6400 
6401 	return false;
6402 }
6403 
6404 static void hist_unreg_all(struct trace_event_file *file)
6405 {
6406 	struct event_trigger_data *test, *n;
6407 	struct hist_trigger_data *hist_data;
6408 	struct synth_event *se;
6409 	const char *se_name;
6410 
6411 	lockdep_assert_held(&event_mutex);
6412 
6413 	if (hist_file_check_refs(file))
6414 		return;
6415 
6416 	list_for_each_entry_safe(test, n, &file->triggers, list) {
6417 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6418 			hist_data = test->private_data;
6419 			list_del_rcu(&test->list);
6420 			trace_event_trigger_enable_disable(file, 0);
6421 
6422 			se_name = trace_event_name(file->event_call);
6423 			se = find_synth_event(se_name);
6424 			if (se)
6425 				se->ref--;
6426 
6427 			update_cond_flag(file);
6428 			if (hist_data->enable_timestamps)
6429 				tracing_set_filter_buffering(file->tr, false);
6430 			if (test->ops->free)
6431 				test->ops->free(test);
6432 		}
6433 	}
6434 }
6435 
6436 static int event_hist_trigger_parse(struct event_command *cmd_ops,
6437 				    struct trace_event_file *file,
6438 				    char *glob, char *cmd,
6439 				    char *param_and_filter)
6440 {
6441 	unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
6442 	struct event_trigger_data *trigger_data;
6443 	struct hist_trigger_attrs *attrs;
6444 	struct hist_trigger_data *hist_data;
6445 	char *param, *filter, *p, *start;
6446 	struct synth_event *se;
6447 	const char *se_name;
6448 	bool remove;
6449 	int ret = 0;
6450 
6451 	lockdep_assert_held(&event_mutex);
6452 
6453 	if (WARN_ON(!glob))
6454 		return -EINVAL;
6455 
6456 	if (glob[0]) {
6457 		hist_err_clear();
6458 		last_cmd_set(file, param_and_filter);
6459 	}
6460 
6461 	remove = event_trigger_check_remove(glob);
6462 
6463 	if (event_trigger_empty_param(param_and_filter))
6464 		return -EINVAL;
6465 
6466 	/*
6467 	 * separate the trigger from the filter (k:v [if filter])
6468 	 * allowing for whitespace in the trigger
6469 	 */
6470 	p = param = param_and_filter;
6471 	do {
6472 		p = strstr(p, "if");
6473 		if (!p)
6474 			break;
6475 		if (p == param_and_filter)
6476 			return -EINVAL;
6477 		if (*(p - 1) != ' ' && *(p - 1) != '\t') {
6478 			p++;
6479 			continue;
6480 		}
6481 		if (p >= param_and_filter + strlen(param_and_filter) - (sizeof("if") - 1) - 1)
6482 			return -EINVAL;
6483 		if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
6484 			p++;
6485 			continue;
6486 		}
6487 		break;
6488 	} while (1);
6489 
6490 	if (!p)
6491 		filter = NULL;
6492 	else {
6493 		*(p - 1) = '\0';
6494 		filter = strstrip(p);
6495 		param = strstrip(param);
6496 	}
6497 
6498 	/*
6499 	 * To simplify arithmetic expression parsing, replace occurrences of
6500 	 * '.sym-offset' modifier with '.symXoffset'
6501 	 */
6502 	start = strstr(param, ".sym-offset");
6503 	while (start) {
6504 		*(start + 4) = 'X';
6505 		start = strstr(start + 11, ".sym-offset");
6506 	}
6507 
6508 	attrs = parse_hist_trigger_attrs(file->tr, param);
6509 	if (IS_ERR(attrs))
6510 		return PTR_ERR(attrs);
6511 
6512 	if (attrs->map_bits)
6513 		hist_trigger_bits = attrs->map_bits;
6514 
6515 	hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
6516 	if (IS_ERR(hist_data)) {
6517 		destroy_hist_trigger_attrs(attrs);
6518 		return PTR_ERR(hist_data);
6519 	}
6520 
6521 	trigger_data = event_trigger_alloc(cmd_ops, cmd, param, hist_data);
6522 	if (!trigger_data) {
6523 		ret = -ENOMEM;
6524 		goto out_free;
6525 	}
6526 
6527 	ret = event_trigger_set_filter(cmd_ops, file, filter, trigger_data);
6528 	if (ret < 0)
6529 		goto out_free;
6530 
6531 	if (remove) {
6532 		if (!have_hist_trigger_match(trigger_data, file))
6533 			goto out_free;
6534 
6535 		if (hist_trigger_check_refs(trigger_data, file)) {
6536 			ret = -EBUSY;
6537 			goto out_free;
6538 		}
6539 
6540 		event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
6541 		se_name = trace_event_name(file->event_call);
6542 		se = find_synth_event(se_name);
6543 		if (se)
6544 			se->ref--;
6545 		ret = 0;
6546 		goto out_free;
6547 	}
6548 
6549 	if (existing_hist_update_only(glob, trigger_data, file))
6550 		goto out_free;
6551 
6552 	ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
6553 	if (ret < 0)
6554 		goto out_free;
6555 
6556 	if (get_named_trigger_data(trigger_data))
6557 		goto enable;
6558 
6559 	if (has_hist_vars(hist_data))
6560 		save_hist_vars(hist_data);
6561 
6562 	ret = create_actions(hist_data);
6563 	if (ret)
6564 		goto out_unreg;
6565 
6566 	ret = tracing_map_init(hist_data->map);
6567 	if (ret)
6568 		goto out_unreg;
6569 enable:
6570 	ret = hist_trigger_enable(trigger_data, file);
6571 	if (ret)
6572 		goto out_unreg;
6573 
6574 	se_name = trace_event_name(file->event_call);
6575 	se = find_synth_event(se_name);
6576 	if (se)
6577 		se->ref++;
6578  out:
6579 	if (ret == 0 && glob[0])
6580 		hist_err_clear();
6581 
6582 	return ret;
6583  out_unreg:
6584 	event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
6585  out_free:
6586 	event_trigger_reset_filter(cmd_ops, trigger_data);
6587 
6588 	remove_hist_vars(hist_data);
6589 
6590 	kfree(trigger_data);
6591 
6592 	destroy_hist_data(hist_data);
6593 	goto out;
6594 }
6595 
6596 static struct event_command trigger_hist_cmd = {
6597 	.name			= "hist",
6598 	.trigger_type		= ETT_EVENT_HIST,
6599 	.flags			= EVENT_CMD_FL_NEEDS_REC,
6600 	.parse			= event_hist_trigger_parse,
6601 	.reg			= hist_register_trigger,
6602 	.unreg			= hist_unregister_trigger,
6603 	.unreg_all		= hist_unreg_all,
6604 	.get_trigger_ops	= event_hist_get_trigger_ops,
6605 	.set_filter		= set_trigger_filter,
6606 };
6607 
6608 __init int register_trigger_hist_cmd(void)
6609 {
6610 	int ret;
6611 
6612 	ret = register_event_command(&trigger_hist_cmd);
6613 	WARN_ON(ret < 0);
6614 
6615 	return ret;
6616 }
6617 
6618 static void
6619 hist_enable_trigger(struct event_trigger_data *data,
6620 		    struct trace_buffer *buffer,  void *rec,
6621 		    struct ring_buffer_event *event)
6622 {
6623 	struct enable_trigger_data *enable_data = data->private_data;
6624 	struct event_trigger_data *test;
6625 
6626 	list_for_each_entry_rcu(test, &enable_data->file->triggers, list,
6627 				lockdep_is_held(&event_mutex)) {
6628 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6629 			if (enable_data->enable)
6630 				test->paused = false;
6631 			else
6632 				test->paused = true;
6633 		}
6634 	}
6635 }
6636 
6637 static void
6638 hist_enable_count_trigger(struct event_trigger_data *data,
6639 			  struct trace_buffer *buffer,  void *rec,
6640 			  struct ring_buffer_event *event)
6641 {
6642 	if (!data->count)
6643 		return;
6644 
6645 	if (data->count != -1)
6646 		(data->count)--;
6647 
6648 	hist_enable_trigger(data, buffer, rec, event);
6649 }
6650 
6651 static struct event_trigger_ops hist_enable_trigger_ops = {
6652 	.trigger		= hist_enable_trigger,
6653 	.print			= event_enable_trigger_print,
6654 	.init			= event_trigger_init,
6655 	.free			= event_enable_trigger_free,
6656 };
6657 
6658 static struct event_trigger_ops hist_enable_count_trigger_ops = {
6659 	.trigger		= hist_enable_count_trigger,
6660 	.print			= event_enable_trigger_print,
6661 	.init			= event_trigger_init,
6662 	.free			= event_enable_trigger_free,
6663 };
6664 
6665 static struct event_trigger_ops hist_disable_trigger_ops = {
6666 	.trigger		= hist_enable_trigger,
6667 	.print			= event_enable_trigger_print,
6668 	.init			= event_trigger_init,
6669 	.free			= event_enable_trigger_free,
6670 };
6671 
6672 static struct event_trigger_ops hist_disable_count_trigger_ops = {
6673 	.trigger		= hist_enable_count_trigger,
6674 	.print			= event_enable_trigger_print,
6675 	.init			= event_trigger_init,
6676 	.free			= event_enable_trigger_free,
6677 };
6678 
6679 static struct event_trigger_ops *
6680 hist_enable_get_trigger_ops(char *cmd, char *param)
6681 {
6682 	struct event_trigger_ops *ops;
6683 	bool enable;
6684 
6685 	enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
6686 
6687 	if (enable)
6688 		ops = param ? &hist_enable_count_trigger_ops :
6689 			&hist_enable_trigger_ops;
6690 	else
6691 		ops = param ? &hist_disable_count_trigger_ops :
6692 			&hist_disable_trigger_ops;
6693 
6694 	return ops;
6695 }
6696 
6697 static void hist_enable_unreg_all(struct trace_event_file *file)
6698 {
6699 	struct event_trigger_data *test, *n;
6700 
6701 	list_for_each_entry_safe(test, n, &file->triggers, list) {
6702 		if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
6703 			list_del_rcu(&test->list);
6704 			update_cond_flag(file);
6705 			trace_event_trigger_enable_disable(file, 0);
6706 			if (test->ops->free)
6707 				test->ops->free(test);
6708 		}
6709 	}
6710 }
6711 
6712 static struct event_command trigger_hist_enable_cmd = {
6713 	.name			= ENABLE_HIST_STR,
6714 	.trigger_type		= ETT_HIST_ENABLE,
6715 	.parse			= event_enable_trigger_parse,
6716 	.reg			= event_enable_register_trigger,
6717 	.unreg			= event_enable_unregister_trigger,
6718 	.unreg_all		= hist_enable_unreg_all,
6719 	.get_trigger_ops	= hist_enable_get_trigger_ops,
6720 	.set_filter		= set_trigger_filter,
6721 };
6722 
6723 static struct event_command trigger_hist_disable_cmd = {
6724 	.name			= DISABLE_HIST_STR,
6725 	.trigger_type		= ETT_HIST_ENABLE,
6726 	.parse			= event_enable_trigger_parse,
6727 	.reg			= event_enable_register_trigger,
6728 	.unreg			= event_enable_unregister_trigger,
6729 	.unreg_all		= hist_enable_unreg_all,
6730 	.get_trigger_ops	= hist_enable_get_trigger_ops,
6731 	.set_filter		= set_trigger_filter,
6732 };
6733 
6734 static __init void unregister_trigger_hist_enable_disable_cmds(void)
6735 {
6736 	unregister_event_command(&trigger_hist_enable_cmd);
6737 	unregister_event_command(&trigger_hist_disable_cmd);
6738 }
6739 
6740 __init int register_trigger_hist_enable_disable_cmds(void)
6741 {
6742 	int ret;
6743 
6744 	ret = register_event_command(&trigger_hist_enable_cmd);
6745 	if (WARN_ON(ret < 0))
6746 		return ret;
6747 	ret = register_event_command(&trigger_hist_disable_cmd);
6748 	if (WARN_ON(ret < 0))
6749 		unregister_trigger_hist_enable_disable_cmds();
6750 
6751 	return ret;
6752 }
6753