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