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