xref: /linux/kernel/trace/trace_events_hist.c (revision 9466b6ae6befa87b27cc13ee7263e01b9c48efc0)
1bcea3f96SSteven Rostedt (VMware) // SPDX-License-Identifier: GPL-2.0
27ef224d1STom Zanussi /*
37ef224d1STom Zanussi  * trace_events_hist - trace event hist triggers
47ef224d1STom Zanussi  *
57ef224d1STom Zanussi  * Copyright (C) 2015 Tom Zanussi <tom.zanussi@linux.intel.com>
67ef224d1STom Zanussi  */
77ef224d1STom Zanussi 
87ef224d1STom Zanussi #include <linux/module.h>
97ef224d1STom Zanussi #include <linux/kallsyms.h>
1017911ff3SSteven Rostedt (VMware) #include <linux/security.h>
117ef224d1STom Zanussi #include <linux/mutex.h>
127ef224d1STom Zanussi #include <linux/slab.h>
137ef224d1STom Zanussi #include <linux/stacktrace.h>
14b2d09103SIngo Molnar #include <linux/rculist.h>
154b147936STom Zanussi #include <linux/tracefs.h>
167ef224d1STom Zanussi 
17ac681546SZhengjun Xing /* for gfp flag names */
18ac681546SZhengjun Xing #include <linux/trace_events.h>
19ac681546SZhengjun Xing #include <trace/events/mmflags.h>
20ac681546SZhengjun Xing 
217ef224d1STom Zanussi #include "tracing_map.h"
22726721a5STom Zanussi #include "trace_synth.h"
234b147936STom Zanussi 
24d566c5e9STom Zanussi #define ERRORS								\
25d566c5e9STom Zanussi 	C(NONE,			"No error"),				\
26d566c5e9STom Zanussi 	C(DUPLICATE_VAR,	"Variable already defined"),		\
27d566c5e9STom Zanussi 	C(VAR_NOT_UNIQUE,	"Variable name not unique, need to use fully qualified name (subsys.event.var) for variable"), \
28d566c5e9STom Zanussi 	C(TOO_MANY_VARS,	"Too many variables defined"),		\
29d566c5e9STom Zanussi 	C(MALFORMED_ASSIGNMENT,	"Malformed assignment"),		\
30d566c5e9STom Zanussi 	C(NAMED_MISMATCH,	"Named hist trigger doesn't match existing named trigger (includes variables)"), \
31d566c5e9STom Zanussi 	C(TRIGGER_EEXIST,	"Hist trigger already exists"),		\
32d566c5e9STom Zanussi 	C(TRIGGER_ENOENT_CLEAR,	"Can't clear or continue a nonexistent hist trigger"), \
33d566c5e9STom Zanussi 	C(SET_CLOCK_FAIL,	"Couldn't set trace_clock"),		\
34d566c5e9STom Zanussi 	C(BAD_FIELD_MODIFIER,	"Invalid field modifier"),		\
35d566c5e9STom Zanussi 	C(TOO_MANY_SUBEXPR,	"Too many subexpressions (3 max)"),	\
36d566c5e9STom Zanussi 	C(TIMESTAMP_MISMATCH,	"Timestamp units in expression don't match"), \
37d566c5e9STom Zanussi 	C(TOO_MANY_FIELD_VARS,	"Too many field variables defined"),	\
38d566c5e9STom Zanussi 	C(EVENT_FILE_NOT_FOUND,	"Event file not found"),		\
39d566c5e9STom Zanussi 	C(HIST_NOT_FOUND,	"Matching event histogram not found"),	\
40d566c5e9STom Zanussi 	C(HIST_CREATE_FAIL,	"Couldn't create histogram for field"),	\
41d566c5e9STom Zanussi 	C(SYNTH_VAR_NOT_FOUND,	"Couldn't find synthetic variable"),	\
42d566c5e9STom Zanussi 	C(SYNTH_EVENT_NOT_FOUND,"Couldn't find synthetic event"),	\
43d566c5e9STom Zanussi 	C(SYNTH_TYPE_MISMATCH,	"Param type doesn't match synthetic event field type"), \
44d566c5e9STom Zanussi 	C(SYNTH_COUNT_MISMATCH,	"Param count doesn't match synthetic event field count"), \
45d566c5e9STom Zanussi 	C(FIELD_VAR_PARSE_FAIL,	"Couldn't parse field variable"),	\
46d566c5e9STom Zanussi 	C(VAR_CREATE_FIND_FAIL,	"Couldn't create or find variable"),	\
47d566c5e9STom Zanussi 	C(ONX_NOT_VAR,		"For onmax(x) or onchange(x), x must be a variable"), \
48d566c5e9STom Zanussi 	C(ONX_VAR_NOT_FOUND,	"Couldn't find onmax or onchange variable"), \
49d566c5e9STom Zanussi 	C(ONX_VAR_CREATE_FAIL,	"Couldn't create onmax or onchange variable"), \
50d566c5e9STom Zanussi 	C(FIELD_VAR_CREATE_FAIL,"Couldn't create field variable"),	\
51d566c5e9STom Zanussi 	C(TOO_MANY_PARAMS,	"Too many action params"),		\
52d566c5e9STom Zanussi 	C(PARAM_NOT_FOUND,	"Couldn't find param"),			\
53d566c5e9STom Zanussi 	C(INVALID_PARAM,	"Invalid action param"),		\
54d566c5e9STom Zanussi 	C(ACTION_NOT_FOUND,	"No action found"),			\
55d566c5e9STom Zanussi 	C(NO_SAVE_PARAMS,	"No params found for save()"),		\
56d566c5e9STom Zanussi 	C(TOO_MANY_SAVE_ACTIONS,"Can't have more than one save() action per hist"), \
57d566c5e9STom Zanussi 	C(ACTION_MISMATCH,	"Handler doesn't support action"),	\
58d566c5e9STom Zanussi 	C(NO_CLOSING_PAREN,	"No closing paren found"),		\
59d566c5e9STom Zanussi 	C(SUBSYS_NOT_FOUND,	"Missing subsystem"),			\
60d566c5e9STom Zanussi 	C(INVALID_SUBSYS_EVENT,	"Invalid subsystem or event name"),	\
61c8d94a18STom Zanussi 	C(INVALID_REF_KEY,	"Using variable references in keys not supported"), \
62d566c5e9STom Zanussi 	C(VAR_NOT_FOUND,	"Couldn't find variable"),		\
634de26c8cSTom Zanussi 	C(FIELD_NOT_FOUND,	"Couldn't find field"),			\
644de26c8cSTom Zanussi 	C(EMPTY_ASSIGNMENT,	"Empty assignment"),			\
654de26c8cSTom Zanussi 	C(INVALID_SORT_MODIFIER,"Invalid sort modifier"),		\
664de26c8cSTom Zanussi 	C(EMPTY_SORT_FIELD,	"Empty sort field"),			\
674de26c8cSTom Zanussi 	C(TOO_MANY_SORT_FIELDS,	"Too many sort fields (Max = 2)"),	\
68a9d10ca4SMasami Hiramatsu 	C(INVALID_SORT_FIELD,	"Sort field must be a key or a val"),	\
6952cfb373SKalesh Singh 	C(INVALID_STR_OPERAND,	"String type can not be an operand in expression"), \
709710b2f3SKalesh Singh 	C(EXPECT_NUMBER,	"Expecting numeric literal"),		\
718b5d46fdSKalesh Singh 	C(UNARY_MINUS_SUBEXPR,	"Unary minus not supported in sub-expressions"), \
72ccf47f5cSMasami Hiramatsu (Google) 	C(DIVISION_BY_ZERO,	"Division by zero"),			\
73ccf47f5cSMasami Hiramatsu (Google) 	C(NEED_NOHC_VAL,	"Non-hitcount value is required for 'nohitcount'"),
74d566c5e9STom Zanussi 
75d566c5e9STom Zanussi #undef C
76d566c5e9STom Zanussi #define C(a, b)		HIST_ERR_##a
77d566c5e9STom Zanussi 
78d566c5e9STom Zanussi enum { ERRORS };
79d566c5e9STom Zanussi 
80d566c5e9STom Zanussi #undef C
81d566c5e9STom Zanussi #define C(a, b)		b
82d566c5e9STom Zanussi 
83d566c5e9STom Zanussi static const char *err_text[] = { ERRORS };
84d566c5e9STom Zanussi 
857ef224d1STom Zanussi struct hist_field;
867ef224d1STom Zanussi 
87df35d93bSTom Zanussi typedef u64 (*hist_field_fn_t) (struct hist_field *field,
88df35d93bSTom Zanussi 				struct tracing_map_elt *elt,
89b47e3302SSteven Rostedt (VMware) 				struct trace_buffer *buffer,
90df35d93bSTom Zanussi 				struct ring_buffer_event *rbe,
91df35d93bSTom Zanussi 				void *event);
927ef224d1STom Zanussi 
935819eaddSTom Zanussi #define HIST_FIELD_OPERANDS_MAX	2
9430350d65STom Zanussi #define HIST_FIELDS_MAX		(TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
950212e2aaSTom Zanussi #define HIST_ACTIONS_MAX	8
9652cfb373SKalesh Singh #define HIST_CONST_DIGITS_MAX	21
978b5d46fdSKalesh Singh #define HIST_DIV_SHIFT		20  /* For optimizing division by constants */
9830350d65STom Zanussi 
99100719dcSTom Zanussi enum field_op_id {
100100719dcSTom Zanussi 	FIELD_OP_NONE,
101100719dcSTom Zanussi 	FIELD_OP_PLUS,
102100719dcSTom Zanussi 	FIELD_OP_MINUS,
103100719dcSTom Zanussi 	FIELD_OP_UNARY_MINUS,
104bcef0441SKalesh Singh 	FIELD_OP_DIV,
105bcef0441SKalesh Singh 	FIELD_OP_MULT,
106100719dcSTom Zanussi };
107100719dcSTom Zanussi 
10886087383SSteven Rostedt (Google) enum hist_field_fn {
10986087383SSteven Rostedt (Google) 	HIST_FIELD_FN_NOP,
11086087383SSteven Rostedt (Google) 	HIST_FIELD_FN_VAR_REF,
11186087383SSteven Rostedt (Google) 	HIST_FIELD_FN_COUNTER,
11286087383SSteven Rostedt (Google) 	HIST_FIELD_FN_CONST,
11386087383SSteven Rostedt (Google) 	HIST_FIELD_FN_LOG2,
11486087383SSteven Rostedt (Google) 	HIST_FIELD_FN_BUCKET,
11586087383SSteven Rostedt (Google) 	HIST_FIELD_FN_TIMESTAMP,
11686087383SSteven Rostedt (Google) 	HIST_FIELD_FN_CPU,
11786087383SSteven Rostedt (Google) 	HIST_FIELD_FN_STRING,
11886087383SSteven Rostedt (Google) 	HIST_FIELD_FN_DYNSTRING,
11986087383SSteven Rostedt (Google) 	HIST_FIELD_FN_RELDYNSTRING,
12086087383SSteven Rostedt (Google) 	HIST_FIELD_FN_PSTRING,
12186087383SSteven Rostedt (Google) 	HIST_FIELD_FN_S64,
12286087383SSteven Rostedt (Google) 	HIST_FIELD_FN_U64,
12386087383SSteven Rostedt (Google) 	HIST_FIELD_FN_S32,
12486087383SSteven Rostedt (Google) 	HIST_FIELD_FN_U32,
12586087383SSteven Rostedt (Google) 	HIST_FIELD_FN_S16,
12686087383SSteven Rostedt (Google) 	HIST_FIELD_FN_U16,
12786087383SSteven Rostedt (Google) 	HIST_FIELD_FN_S8,
12886087383SSteven Rostedt (Google) 	HIST_FIELD_FN_U8,
12986087383SSteven Rostedt (Google) 	HIST_FIELD_FN_UMINUS,
13086087383SSteven Rostedt (Google) 	HIST_FIELD_FN_MINUS,
13186087383SSteven Rostedt (Google) 	HIST_FIELD_FN_PLUS,
13286087383SSteven Rostedt (Google) 	HIST_FIELD_FN_DIV,
13386087383SSteven Rostedt (Google) 	HIST_FIELD_FN_MULT,
13486087383SSteven Rostedt (Google) 	HIST_FIELD_FN_DIV_POWER2,
13586087383SSteven Rostedt (Google) 	HIST_FIELD_FN_DIV_NOT_POWER2,
13686087383SSteven Rostedt (Google) 	HIST_FIELD_FN_DIV_MULT_SHIFT,
13786087383SSteven Rostedt (Google) 	HIST_FIELD_FN_EXECNAME,
138f5914b30STom Zanussi 	HIST_FIELD_FN_STACK,
13986087383SSteven Rostedt (Google) };
14086087383SSteven Rostedt (Google) 
14105ddb25cSTom Zanussi /*
14205ddb25cSTom Zanussi  * A hist_var (histogram variable) contains variable information for
14305ddb25cSTom Zanussi  * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF
14405ddb25cSTom Zanussi  * flag set.  A hist_var has a variable name e.g. ts0, and is
14505ddb25cSTom Zanussi  * associated with a given histogram trigger, as specified by
14605ddb25cSTom Zanussi  * hist_data.  The hist_var idx is the unique index assigned to the
14705ddb25cSTom Zanussi  * variable by the hist trigger's tracing_map.  The idx is what is
14805ddb25cSTom Zanussi  * used to set a variable's value and, by a variable reference, to
14905ddb25cSTom Zanussi  * retrieve it.
15005ddb25cSTom Zanussi  */
15130350d65STom Zanussi struct hist_var {
15230350d65STom Zanussi 	char				*name;
15330350d65STom Zanussi 	struct hist_trigger_data	*hist_data;
15430350d65STom Zanussi 	unsigned int			idx;
15530350d65STom Zanussi };
1565819eaddSTom Zanussi 
1577ef224d1STom Zanussi struct hist_field {
1587ef224d1STom Zanussi 	struct ftrace_event_field	*field;
1597ef224d1STom Zanussi 	unsigned long			flags;
160de9a48a3SSteven Rostedt (VMware) 	unsigned long			buckets;
16119a9facdSTom Zanussi 	const char			*type;
1625819eaddSTom Zanussi 	struct hist_field		*operands[HIST_FIELD_OPERANDS_MAX];
163b559d003STom Zanussi 	struct hist_trigger_data	*hist_data;
16486087383SSteven Rostedt (Google) 	enum hist_field_fn		fn_num;
16586087383SSteven Rostedt (Google) 	unsigned int			ref;
16686087383SSteven Rostedt (Google) 	unsigned int			size;
16786087383SSteven Rostedt (Google) 	unsigned int			offset;
16886087383SSteven Rostedt (Google) 	unsigned int                    is_signed;
16905ddb25cSTom Zanussi 
17005ddb25cSTom Zanussi 	/*
17105ddb25cSTom Zanussi 	 * Variable fields contain variable-specific info in var.
17205ddb25cSTom Zanussi 	 */
17330350d65STom Zanussi 	struct hist_var			var;
174100719dcSTom Zanussi 	enum field_op_id		operator;
175067fe038STom Zanussi 	char				*system;
176067fe038STom Zanussi 	char				*event_name;
17705ddb25cSTom Zanussi 
17805ddb25cSTom Zanussi 	/*
17905ddb25cSTom Zanussi 	 * The name field is used for EXPR and VAR_REF fields.  VAR
18005ddb25cSTom Zanussi 	 * fields contain the variable name in var.name.
18105ddb25cSTom Zanussi 	 */
182100719dcSTom Zanussi 	char				*name;
18305ddb25cSTom Zanussi 
18405ddb25cSTom Zanussi 	/*
18505ddb25cSTom Zanussi 	 * When a histogram trigger is hit, if it has any references
18605ddb25cSTom Zanussi 	 * to variables, the values of those variables are collected
18705ddb25cSTom Zanussi 	 * into a var_ref_vals array by resolve_var_refs().  The
18805ddb25cSTom Zanussi 	 * current value of each variable is read from the tracing_map
18905ddb25cSTom Zanussi 	 * using the hist field's hist_var.idx and entered into the
19005ddb25cSTom Zanussi 	 * var_ref_idx entry i.e. var_ref_vals[var_ref_idx].
19105ddb25cSTom Zanussi 	 */
192067fe038STom Zanussi 	unsigned int			var_ref_idx;
193067fe038STom Zanussi 	bool                            read_once;
19463a1e5deSTom Zanussi 
19563a1e5deSTom Zanussi 	unsigned int			var_str_idx;
19652cfb373SKalesh Singh 
19752cfb373SKalesh Singh 	/* Numeric literals are represented as u64 */
19852cfb373SKalesh Singh 	u64				constant;
1998b5d46fdSKalesh Singh 	/* Used to optimize division by constants */
2008b5d46fdSKalesh Singh 	u64				div_multiplier;
2017ef224d1STom Zanussi };
2027ef224d1STom Zanussi 
20386087383SSteven Rostedt (Google) static u64 hist_fn_call(struct hist_field *hist_field,
204df35d93bSTom Zanussi 			struct tracing_map_elt *elt,
205b47e3302SSteven Rostedt (VMware) 			struct trace_buffer *buffer,
206df35d93bSTom Zanussi 			struct ring_buffer_event *rbe,
20786087383SSteven Rostedt (Google) 			void *event);
20869a0200cSTom Zanussi 
hist_field_const(struct hist_field * field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)20952cfb373SKalesh Singh static u64 hist_field_const(struct hist_field *field,
21052cfb373SKalesh Singh 			   struct tracing_map_elt *elt,
21152cfb373SKalesh Singh 			   struct trace_buffer *buffer,
21252cfb373SKalesh Singh 			   struct ring_buffer_event *rbe,
21352cfb373SKalesh Singh 			   void *event)
21452cfb373SKalesh Singh {
21552cfb373SKalesh Singh 	return field->constant;
21652cfb373SKalesh Singh }
21752cfb373SKalesh Singh 
hist_field_counter(struct hist_field * field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)218df35d93bSTom Zanussi static u64 hist_field_counter(struct hist_field *field,
219df35d93bSTom Zanussi 			      struct tracing_map_elt *elt,
220b47e3302SSteven Rostedt (VMware) 			      struct trace_buffer *buffer,
221df35d93bSTom Zanussi 			      struct ring_buffer_event *rbe,
222df35d93bSTom Zanussi 			      void *event)
2237ef224d1STom Zanussi {
2247ef224d1STom Zanussi 	return 1;
2257ef224d1STom Zanussi }
2267ef224d1STom Zanussi 
hist_field_string(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)227df35d93bSTom Zanussi static u64 hist_field_string(struct hist_field *hist_field,
228df35d93bSTom Zanussi 			     struct tracing_map_elt *elt,
229b47e3302SSteven Rostedt (VMware) 			     struct trace_buffer *buffer,
230df35d93bSTom Zanussi 			     struct ring_buffer_event *rbe,
231df35d93bSTom Zanussi 			     void *event)
2327ef224d1STom Zanussi {
2337ef224d1STom Zanussi 	char *addr = (char *)(event + hist_field->field->offset);
2347ef224d1STom Zanussi 
2357ef224d1STom Zanussi 	return (u64)(unsigned long)addr;
2367ef224d1STom Zanussi }
2377ef224d1STom Zanussi 
hist_field_dynstring(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)238df35d93bSTom Zanussi static u64 hist_field_dynstring(struct hist_field *hist_field,
239df35d93bSTom Zanussi 				struct tracing_map_elt *elt,
240b47e3302SSteven Rostedt (VMware) 				struct trace_buffer *buffer,
241df35d93bSTom Zanussi 				struct ring_buffer_event *rbe,
242df35d93bSTom Zanussi 				void *event)
24379e577cbSNamhyung Kim {
24479e577cbSNamhyung Kim 	u32 str_item = *(u32 *)(event + hist_field->field->offset);
24579e577cbSNamhyung Kim 	int str_loc = str_item & 0xffff;
24679e577cbSNamhyung Kim 	char *addr = (char *)(event + str_loc);
24779e577cbSNamhyung Kim 
24879e577cbSNamhyung Kim 	return (u64)(unsigned long)addr;
24979e577cbSNamhyung Kim }
25079e577cbSNamhyung Kim 
hist_field_reldynstring(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)25105770dd0SMasami Hiramatsu static u64 hist_field_reldynstring(struct hist_field *hist_field,
25205770dd0SMasami Hiramatsu 				   struct tracing_map_elt *elt,
25305770dd0SMasami Hiramatsu 				   struct trace_buffer *buffer,
25405770dd0SMasami Hiramatsu 				   struct ring_buffer_event *rbe,
25505770dd0SMasami Hiramatsu 				   void *event)
25605770dd0SMasami Hiramatsu {
25705770dd0SMasami Hiramatsu 	u32 *item = event + hist_field->field->offset;
25805770dd0SMasami Hiramatsu 	u32 str_item = *item;
25905770dd0SMasami Hiramatsu 	int str_loc = str_item & 0xffff;
26005770dd0SMasami Hiramatsu 	char *addr = (char *)&item[1] + str_loc;
26105770dd0SMasami Hiramatsu 
26205770dd0SMasami Hiramatsu 	return (u64)(unsigned long)addr;
26305770dd0SMasami Hiramatsu }
26405770dd0SMasami Hiramatsu 
hist_field_pstring(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)265df35d93bSTom Zanussi static u64 hist_field_pstring(struct hist_field *hist_field,
266df35d93bSTom Zanussi 			      struct tracing_map_elt *elt,
267b47e3302SSteven Rostedt (VMware) 			      struct trace_buffer *buffer,
268df35d93bSTom Zanussi 			      struct ring_buffer_event *rbe,
269df35d93bSTom Zanussi 			      void *event)
27079e577cbSNamhyung Kim {
27179e577cbSNamhyung Kim 	char **addr = (char **)(event + hist_field->field->offset);
27279e577cbSNamhyung Kim 
27379e577cbSNamhyung Kim 	return (u64)(unsigned long)*addr;
27479e577cbSNamhyung Kim }
27579e577cbSNamhyung Kim 
hist_field_log2(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)276df35d93bSTom Zanussi static u64 hist_field_log2(struct hist_field *hist_field,
277df35d93bSTom Zanussi 			   struct tracing_map_elt *elt,
278b47e3302SSteven Rostedt (VMware) 			   struct trace_buffer *buffer,
279df35d93bSTom Zanussi 			   struct ring_buffer_event *rbe,
280df35d93bSTom Zanussi 			   void *event)
2814b94f5b7SNamhyung Kim {
2825819eaddSTom Zanussi 	struct hist_field *operand = hist_field->operands[0];
2835819eaddSTom Zanussi 
28486087383SSteven Rostedt (Google) 	u64 val = hist_fn_call(operand, elt, buffer, rbe, event);
2854b94f5b7SNamhyung Kim 
2864b94f5b7SNamhyung Kim 	return (u64) ilog2(roundup_pow_of_two(val));
2874b94f5b7SNamhyung Kim }
2884b94f5b7SNamhyung Kim 
hist_field_bucket(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)289de9a48a3SSteven Rostedt (VMware) static u64 hist_field_bucket(struct hist_field *hist_field,
290de9a48a3SSteven Rostedt (VMware) 			     struct tracing_map_elt *elt,
291de9a48a3SSteven Rostedt (VMware) 			     struct trace_buffer *buffer,
292de9a48a3SSteven Rostedt (VMware) 			     struct ring_buffer_event *rbe,
293de9a48a3SSteven Rostedt (VMware) 			     void *event)
294de9a48a3SSteven Rostedt (VMware) {
295de9a48a3SSteven Rostedt (VMware) 	struct hist_field *operand = hist_field->operands[0];
296de9a48a3SSteven Rostedt (VMware) 	unsigned long buckets = hist_field->buckets;
297de9a48a3SSteven Rostedt (VMware) 
29886087383SSteven Rostedt (Google) 	u64 val = hist_fn_call(operand, elt, buffer, rbe, event);
299de9a48a3SSteven Rostedt (VMware) 
300de9a48a3SSteven Rostedt (VMware) 	if (WARN_ON_ONCE(!buckets))
301de9a48a3SSteven Rostedt (VMware) 		return val;
302de9a48a3SSteven Rostedt (VMware) 
303de9a48a3SSteven Rostedt (VMware) 	if (val >= LONG_MAX)
304de9a48a3SSteven Rostedt (VMware) 		val = div64_ul(val, buckets);
305de9a48a3SSteven Rostedt (VMware) 	else
306de9a48a3SSteven Rostedt (VMware) 		val = (u64)((unsigned long)val / buckets);
307de9a48a3SSteven Rostedt (VMware) 	return val * buckets;
308de9a48a3SSteven Rostedt (VMware) }
309de9a48a3SSteven Rostedt (VMware) 
hist_field_plus(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)310df35d93bSTom Zanussi static u64 hist_field_plus(struct hist_field *hist_field,
311df35d93bSTom Zanussi 			   struct tracing_map_elt *elt,
312b47e3302SSteven Rostedt (VMware) 			   struct trace_buffer *buffer,
313df35d93bSTom Zanussi 			   struct ring_buffer_event *rbe,
314df35d93bSTom Zanussi 			   void *event)
315100719dcSTom Zanussi {
316100719dcSTom Zanussi 	struct hist_field *operand1 = hist_field->operands[0];
317100719dcSTom Zanussi 	struct hist_field *operand2 = hist_field->operands[1];
318100719dcSTom Zanussi 
31986087383SSteven Rostedt (Google) 	u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
32086087383SSteven Rostedt (Google) 	u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
321100719dcSTom Zanussi 
322100719dcSTom Zanussi 	return val1 + val2;
323100719dcSTom Zanussi }
324100719dcSTom Zanussi 
hist_field_minus(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)325df35d93bSTom Zanussi static u64 hist_field_minus(struct hist_field *hist_field,
326df35d93bSTom Zanussi 			    struct tracing_map_elt *elt,
327b47e3302SSteven Rostedt (VMware) 			    struct trace_buffer *buffer,
328df35d93bSTom Zanussi 			    struct ring_buffer_event *rbe,
329df35d93bSTom Zanussi 			    void *event)
330100719dcSTom Zanussi {
331100719dcSTom Zanussi 	struct hist_field *operand1 = hist_field->operands[0];
332100719dcSTom Zanussi 	struct hist_field *operand2 = hist_field->operands[1];
333100719dcSTom Zanussi 
33486087383SSteven Rostedt (Google) 	u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
33586087383SSteven Rostedt (Google) 	u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
336100719dcSTom Zanussi 
337100719dcSTom Zanussi 	return val1 - val2;
338100719dcSTom Zanussi }
339100719dcSTom Zanussi 
hist_field_div(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)340bcef0441SKalesh Singh static u64 hist_field_div(struct hist_field *hist_field,
341bcef0441SKalesh Singh 			   struct tracing_map_elt *elt,
342bcef0441SKalesh Singh 			   struct trace_buffer *buffer,
343bcef0441SKalesh Singh 			   struct ring_buffer_event *rbe,
344bcef0441SKalesh Singh 			   void *event)
345bcef0441SKalesh Singh {
346bcef0441SKalesh Singh 	struct hist_field *operand1 = hist_field->operands[0];
347bcef0441SKalesh Singh 	struct hist_field *operand2 = hist_field->operands[1];
348bcef0441SKalesh Singh 
34986087383SSteven Rostedt (Google) 	u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
35086087383SSteven Rostedt (Google) 	u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
351bcef0441SKalesh Singh 
352bcef0441SKalesh Singh 	/* Return -1 for the undefined case */
353bcef0441SKalesh Singh 	if (!val2)
354bcef0441SKalesh Singh 		return -1;
355bcef0441SKalesh Singh 
356722eddaaSKalesh Singh 	/* Use shift if the divisor is a power of 2 */
357722eddaaSKalesh Singh 	if (!(val2 & (val2 - 1)))
358722eddaaSKalesh Singh 		return val1 >> __ffs64(val2);
359722eddaaSKalesh Singh 
360bcef0441SKalesh Singh 	return div64_u64(val1, val2);
361bcef0441SKalesh Singh }
362bcef0441SKalesh Singh 
div_by_power_of_two(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)3638b5d46fdSKalesh Singh static u64 div_by_power_of_two(struct hist_field *hist_field,
3648b5d46fdSKalesh Singh 				struct tracing_map_elt *elt,
3658b5d46fdSKalesh Singh 				struct trace_buffer *buffer,
3668b5d46fdSKalesh Singh 				struct ring_buffer_event *rbe,
3678b5d46fdSKalesh Singh 				void *event)
3688b5d46fdSKalesh Singh {
3698b5d46fdSKalesh Singh 	struct hist_field *operand1 = hist_field->operands[0];
3708b5d46fdSKalesh Singh 	struct hist_field *operand2 = hist_field->operands[1];
3718b5d46fdSKalesh Singh 
37286087383SSteven Rostedt (Google) 	u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
3738b5d46fdSKalesh Singh 
3748b5d46fdSKalesh Singh 	return val1 >> __ffs64(operand2->constant);
3758b5d46fdSKalesh Singh }
3768b5d46fdSKalesh Singh 
div_by_not_power_of_two(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)3778b5d46fdSKalesh Singh static u64 div_by_not_power_of_two(struct hist_field *hist_field,
3788b5d46fdSKalesh Singh 				struct tracing_map_elt *elt,
3798b5d46fdSKalesh Singh 				struct trace_buffer *buffer,
3808b5d46fdSKalesh Singh 				struct ring_buffer_event *rbe,
3818b5d46fdSKalesh Singh 				void *event)
3828b5d46fdSKalesh Singh {
3838b5d46fdSKalesh Singh 	struct hist_field *operand1 = hist_field->operands[0];
3848b5d46fdSKalesh Singh 	struct hist_field *operand2 = hist_field->operands[1];
3858b5d46fdSKalesh Singh 
38686087383SSteven Rostedt (Google) 	u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
3878b5d46fdSKalesh Singh 
3888b5d46fdSKalesh Singh 	return div64_u64(val1, operand2->constant);
3898b5d46fdSKalesh Singh }
3908b5d46fdSKalesh Singh 
div_by_mult_and_shift(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)3918b5d46fdSKalesh Singh static u64 div_by_mult_and_shift(struct hist_field *hist_field,
3928b5d46fdSKalesh Singh 				struct tracing_map_elt *elt,
3938b5d46fdSKalesh Singh 				struct trace_buffer *buffer,
3948b5d46fdSKalesh Singh 				struct ring_buffer_event *rbe,
3958b5d46fdSKalesh Singh 				void *event)
3968b5d46fdSKalesh Singh {
3978b5d46fdSKalesh Singh 	struct hist_field *operand1 = hist_field->operands[0];
3988b5d46fdSKalesh Singh 	struct hist_field *operand2 = hist_field->operands[1];
3998b5d46fdSKalesh Singh 
40086087383SSteven Rostedt (Google) 	u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
4018b5d46fdSKalesh Singh 
4028b5d46fdSKalesh Singh 	/*
4038b5d46fdSKalesh Singh 	 * If the divisor is a constant, do a multiplication and shift instead.
4048b5d46fdSKalesh Singh 	 *
4058b5d46fdSKalesh Singh 	 * Choose Z = some power of 2. If Y <= Z, then:
4068b5d46fdSKalesh Singh 	 *     X / Y = (X * (Z / Y)) / Z
4078b5d46fdSKalesh Singh 	 *
4088b5d46fdSKalesh Singh 	 * (Z / Y) is a constant (mult) which is calculated at parse time, so:
4098b5d46fdSKalesh Singh 	 *     X / Y = (X * mult) / Z
4108b5d46fdSKalesh Singh 	 *
4118b5d46fdSKalesh Singh 	 * The division by Z can be replaced by a shift since Z is a power of 2:
4128b5d46fdSKalesh Singh 	 *     X / Y = (X * mult) >> HIST_DIV_SHIFT
4138b5d46fdSKalesh Singh 	 *
4148b5d46fdSKalesh Singh 	 * As long, as X < Z the results will not be off by more than 1.
4158b5d46fdSKalesh Singh 	 */
4168b5d46fdSKalesh Singh 	if (val1 < (1 << HIST_DIV_SHIFT)) {
4178b5d46fdSKalesh Singh 		u64 mult = operand2->div_multiplier;
4188b5d46fdSKalesh Singh 
4198b5d46fdSKalesh Singh 		return (val1 * mult + ((1 << HIST_DIV_SHIFT) - 1)) >> HIST_DIV_SHIFT;
4208b5d46fdSKalesh Singh 	}
4218b5d46fdSKalesh Singh 
4228b5d46fdSKalesh Singh 	return div64_u64(val1, operand2->constant);
4238b5d46fdSKalesh Singh }
4248b5d46fdSKalesh Singh 
hist_field_mult(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)425bcef0441SKalesh Singh static u64 hist_field_mult(struct hist_field *hist_field,
426bcef0441SKalesh Singh 			   struct tracing_map_elt *elt,
427bcef0441SKalesh Singh 			   struct trace_buffer *buffer,
428bcef0441SKalesh Singh 			   struct ring_buffer_event *rbe,
429bcef0441SKalesh Singh 			   void *event)
430bcef0441SKalesh Singh {
431bcef0441SKalesh Singh 	struct hist_field *operand1 = hist_field->operands[0];
432bcef0441SKalesh Singh 	struct hist_field *operand2 = hist_field->operands[1];
433bcef0441SKalesh Singh 
43486087383SSteven Rostedt (Google) 	u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
43586087383SSteven Rostedt (Google) 	u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
436bcef0441SKalesh Singh 
437bcef0441SKalesh Singh 	return val1 * val2;
438bcef0441SKalesh Singh }
439bcef0441SKalesh Singh 
hist_field_unary_minus(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)440df35d93bSTom Zanussi static u64 hist_field_unary_minus(struct hist_field *hist_field,
441df35d93bSTom Zanussi 				  struct tracing_map_elt *elt,
442b47e3302SSteven Rostedt (VMware) 				  struct trace_buffer *buffer,
443df35d93bSTom Zanussi 				  struct ring_buffer_event *rbe,
444df35d93bSTom Zanussi 				  void *event)
445100719dcSTom Zanussi {
446100719dcSTom Zanussi 	struct hist_field *operand = hist_field->operands[0];
447100719dcSTom Zanussi 
44886087383SSteven Rostedt (Google) 	s64 sval = (s64)hist_fn_call(operand, elt, buffer, rbe, event);
449100719dcSTom Zanussi 	u64 val = (u64)-sval;
450100719dcSTom Zanussi 
451100719dcSTom Zanussi 	return val;
452100719dcSTom Zanussi }
453100719dcSTom Zanussi 
4547ef224d1STom Zanussi #define DEFINE_HIST_FIELD_FN(type)					\
455fbd302cbSTom Zanussi 	static u64 hist_field_##type(struct hist_field *hist_field,	\
456df35d93bSTom Zanussi 				     struct tracing_map_elt *elt,	\
457b47e3302SSteven Rostedt (VMware) 				     struct trace_buffer *buffer,	\
458df35d93bSTom Zanussi 				     struct ring_buffer_event *rbe,	\
459df35d93bSTom Zanussi 				     void *event)			\
4607ef224d1STom Zanussi {									\
4617ef224d1STom Zanussi 	type *addr = (type *)(event + hist_field->field->offset);	\
4627ef224d1STom Zanussi 									\
46379e577cbSNamhyung Kim 	return (u64)(unsigned long)*addr;				\
4647ef224d1STom Zanussi }
4657ef224d1STom Zanussi 
4667ef224d1STom Zanussi DEFINE_HIST_FIELD_FN(s64);
4677ef224d1STom Zanussi DEFINE_HIST_FIELD_FN(u64);
4687ef224d1STom Zanussi DEFINE_HIST_FIELD_FN(s32);
4697ef224d1STom Zanussi DEFINE_HIST_FIELD_FN(u32);
4707ef224d1STom Zanussi DEFINE_HIST_FIELD_FN(s16);
4717ef224d1STom Zanussi DEFINE_HIST_FIELD_FN(u16);
4727ef224d1STom Zanussi DEFINE_HIST_FIELD_FN(s8);
4737ef224d1STom Zanussi DEFINE_HIST_FIELD_FN(u8);
4747ef224d1STom Zanussi 
4757ef224d1STom Zanussi #define for_each_hist_field(i, hist_data)	\
4767ef224d1STom Zanussi 	for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
4777ef224d1STom Zanussi 
4787ef224d1STom Zanussi #define for_each_hist_val_field(i, hist_data)	\
4797ef224d1STom Zanussi 	for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
4807ef224d1STom Zanussi 
4817ef224d1STom Zanussi #define for_each_hist_key_field(i, hist_data)	\
4827ef224d1STom Zanussi 	for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
4837ef224d1STom Zanussi 
4847ef224d1STom Zanussi #define HITCOUNT_IDX		0
48569a0200cSTom Zanussi #define HIST_KEY_SIZE_MAX	(MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
4867ef224d1STom Zanussi 
4877ef224d1STom Zanussi enum hist_field_flags {
4880d7a8325STom Zanussi 	HIST_FIELD_FL_HITCOUNT		= 1 << 0,
4890d7a8325STom Zanussi 	HIST_FIELD_FL_KEY		= 1 << 1,
4900d7a8325STom Zanussi 	HIST_FIELD_FL_STRING		= 1 << 2,
4910d7a8325STom Zanussi 	HIST_FIELD_FL_HEX		= 1 << 3,
4920d7a8325STom Zanussi 	HIST_FIELD_FL_SYM		= 1 << 4,
4930d7a8325STom Zanussi 	HIST_FIELD_FL_SYM_OFFSET	= 1 << 5,
4940d7a8325STom Zanussi 	HIST_FIELD_FL_EXECNAME		= 1 << 6,
4950d7a8325STom Zanussi 	HIST_FIELD_FL_SYSCALL		= 1 << 7,
4960d7a8325STom Zanussi 	HIST_FIELD_FL_STACKTRACE	= 1 << 8,
4970d7a8325STom Zanussi 	HIST_FIELD_FL_LOG2		= 1 << 9,
498ad42febeSTom Zanussi 	HIST_FIELD_FL_TIMESTAMP		= 1 << 10,
499860f9f6bSTom Zanussi 	HIST_FIELD_FL_TIMESTAMP_USECS	= 1 << 11,
50030350d65STom Zanussi 	HIST_FIELD_FL_VAR		= 1 << 12,
501100719dcSTom Zanussi 	HIST_FIELD_FL_EXPR		= 1 << 13,
502067fe038STom Zanussi 	HIST_FIELD_FL_VAR_REF		= 1 << 14,
5038b7622bfSTom Zanussi 	HIST_FIELD_FL_CPU		= 1 << 15,
5047e8b88a3STom Zanussi 	HIST_FIELD_FL_ALIAS		= 1 << 16,
505de9a48a3SSteven Rostedt (VMware) 	HIST_FIELD_FL_BUCKET		= 1 << 17,
50652cfb373SKalesh Singh 	HIST_FIELD_FL_CONST		= 1 << 18,
507abaa5258SMasami Hiramatsu (Google) 	HIST_FIELD_FL_PERCENT		= 1 << 19,
508a2c54256SMasami Hiramatsu (Google) 	HIST_FIELD_FL_GRAPH		= 1 << 20,
50930350d65STom Zanussi };
51030350d65STom Zanussi 
51130350d65STom Zanussi struct var_defs {
51230350d65STom Zanussi 	unsigned int	n_vars;
51330350d65STom Zanussi 	char		*name[TRACING_MAP_VARS_MAX];
51430350d65STom Zanussi 	char		*expr[TRACING_MAP_VARS_MAX];
5157ef224d1STom Zanussi };
5167ef224d1STom Zanussi 
5177ef224d1STom Zanussi struct hist_trigger_attrs {
5187ef224d1STom Zanussi 	char		*keys_str;
519f2606835STom Zanussi 	char		*vals_str;
520e62347d2STom Zanussi 	char		*sort_key_str;
5215463bfdaSTom Zanussi 	char		*name;
522a4072fe8STom Zanussi 	char		*clock;
52383e99914STom Zanussi 	bool		pause;
52483e99914STom Zanussi 	bool		cont;
525e86ae9baSTom Zanussi 	bool		clear;
526860f9f6bSTom Zanussi 	bool		ts_in_usecs;
527ccf47f5cSMasami Hiramatsu (Google) 	bool		no_hitcount;
5287ef224d1STom Zanussi 	unsigned int	map_bits;
52930350d65STom Zanussi 
53030350d65STom Zanussi 	char		*assignment_str[TRACING_MAP_VARS_MAX];
53130350d65STom Zanussi 	unsigned int	n_assignments;
53230350d65STom Zanussi 
5330212e2aaSTom Zanussi 	char		*action_str[HIST_ACTIONS_MAX];
5340212e2aaSTom Zanussi 	unsigned int	n_actions;
5350212e2aaSTom Zanussi 
53630350d65STom Zanussi 	struct var_defs	var_defs;
5377ef224d1STom Zanussi };
5387ef224d1STom Zanussi 
53902205a67STom Zanussi struct field_var {
54002205a67STom Zanussi 	struct hist_field	*var;
54102205a67STom Zanussi 	struct hist_field	*val;
54202205a67STom Zanussi };
54302205a67STom Zanussi 
54402205a67STom Zanussi struct field_var_hist {
54502205a67STom Zanussi 	struct hist_trigger_data	*hist_data;
54602205a67STom Zanussi 	char				*cmd;
54702205a67STom Zanussi };
54802205a67STom Zanussi 
5497ef224d1STom Zanussi struct hist_trigger_data {
55030350d65STom Zanussi 	struct hist_field               *fields[HIST_FIELDS_MAX];
5517ef224d1STom Zanussi 	unsigned int			n_vals;
5527ef224d1STom Zanussi 	unsigned int			n_keys;
5537ef224d1STom Zanussi 	unsigned int			n_fields;
55430350d65STom Zanussi 	unsigned int			n_vars;
55563a1e5deSTom Zanussi 	unsigned int			n_var_str;
5567ef224d1STom Zanussi 	unsigned int			key_size;
5577ef224d1STom Zanussi 	struct tracing_map_sort_key	sort_keys[TRACING_MAP_SORT_KEYS_MAX];
5587ef224d1STom Zanussi 	unsigned int			n_sort_keys;
5597ef224d1STom Zanussi 	struct trace_event_file		*event_file;
5607ef224d1STom Zanussi 	struct hist_trigger_attrs	*attrs;
5617ef224d1STom Zanussi 	struct tracing_map		*map;
562ad42febeSTom Zanussi 	bool				enable_timestamps;
56330350d65STom Zanussi 	bool				remove;
564067fe038STom Zanussi 	struct hist_field               *var_refs[TRACING_MAP_VARS_MAX];
565067fe038STom Zanussi 	unsigned int			n_var_refs;
5660212e2aaSTom Zanussi 
5670212e2aaSTom Zanussi 	struct action_data		*actions[HIST_ACTIONS_MAX];
5680212e2aaSTom Zanussi 	unsigned int			n_actions;
56902205a67STom Zanussi 
57002205a67STom Zanussi 	struct field_var		*field_vars[SYNTH_FIELDS_MAX];
57102205a67STom Zanussi 	unsigned int			n_field_vars;
57202205a67STom Zanussi 	unsigned int			n_field_var_str;
57302205a67STom Zanussi 	struct field_var_hist		*field_var_hists[SYNTH_FIELDS_MAX];
57402205a67STom Zanussi 	unsigned int			n_field_var_hists;
57550450603STom Zanussi 
5767d18a10cSTom Zanussi 	struct field_var		*save_vars[SYNTH_FIELDS_MAX];
5777d18a10cSTom Zanussi 	unsigned int			n_save_vars;
5787d18a10cSTom Zanussi 	unsigned int			n_save_var_str;
5790212e2aaSTom Zanussi };
5800212e2aaSTom Zanussi 
5810212e2aaSTom Zanussi struct action_data;
5820212e2aaSTom Zanussi 
5830212e2aaSTom Zanussi typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
584b47e3302SSteven Rostedt (VMware) 			     struct tracing_map_elt *elt,
585b47e3302SSteven Rostedt (VMware) 			     struct trace_buffer *buffer, void *rec,
5867d18a10cSTom Zanussi 			     struct ring_buffer_event *rbe, void *key,
5870212e2aaSTom Zanussi 			     struct action_data *data, u64 *var_ref_vals);
5880212e2aaSTom Zanussi 
589466f4528STom Zanussi typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val);
590466f4528STom Zanussi 
5917d18a10cSTom Zanussi enum handler_id {
5927d18a10cSTom Zanussi 	HANDLER_ONMATCH = 1,
5937d18a10cSTom Zanussi 	HANDLER_ONMAX,
594dff81f55STom Zanussi 	HANDLER_ONCHANGE,
5957d18a10cSTom Zanussi };
5967d18a10cSTom Zanussi 
5977d18a10cSTom Zanussi enum action_id {
5987d18a10cSTom Zanussi 	ACTION_SAVE = 1,
5997d18a10cSTom Zanussi 	ACTION_TRACE,
600a3785b7eSTom Zanussi 	ACTION_SNAPSHOT,
6017d18a10cSTom Zanussi };
6027d18a10cSTom Zanussi 
6030212e2aaSTom Zanussi struct action_data {
6047d18a10cSTom Zanussi 	enum handler_id		handler;
6057d18a10cSTom Zanussi 	enum action_id		action;
6067d18a10cSTom Zanussi 	char			*action_name;
6070212e2aaSTom Zanussi 	action_fn_t		fn;
6087d18a10cSTom Zanussi 
609c282a386STom Zanussi 	unsigned int		n_params;
610c282a386STom Zanussi 	char			*params[SYNTH_FIELDS_MAX];
611c282a386STom Zanussi 
61205ddb25cSTom Zanussi 	/*
61305ddb25cSTom Zanussi 	 * When a histogram trigger is hit, the values of any
61405ddb25cSTom Zanussi 	 * references to variables, including variables being passed
61505ddb25cSTom Zanussi 	 * as parameters to synthetic events, are collected into a
616d380dcdeSTom Zanussi 	 * var_ref_vals array.  This var_ref_idx array is an array of
617d380dcdeSTom Zanussi 	 * indices into the var_ref_vals array, one for each synthetic
618d380dcdeSTom Zanussi 	 * event param, and is passed to the synthetic event
619d380dcdeSTom Zanussi 	 * invocation.
62005ddb25cSTom Zanussi 	 */
62182470f7dSZheng Yejian 	unsigned int		var_ref_idx[SYNTH_FIELDS_MAX];
622c282a386STom Zanussi 	struct synth_event	*synth_event;
623e91eefd7STom Zanussi 	bool			use_trace_keyword;
624e91eefd7STom Zanussi 	char			*synth_event_name;
625c3e49506STom Zanussi 
626c3e49506STom Zanussi 	union {
627c3e49506STom Zanussi 		struct {
628c3e49506STom Zanussi 			char			*event;
629c3e49506STom Zanussi 			char			*event_system;
630c3e49506STom Zanussi 		} match_data;
63150450603STom Zanussi 
63250450603STom Zanussi 		struct {
633466f4528STom Zanussi 			/*
634466f4528STom Zanussi 			 * var_str contains the $-unstripped variable
635466f4528STom Zanussi 			 * name referenced by var_ref, and used when
636466f4528STom Zanussi 			 * printing the action.  Because var_ref
637466f4528STom Zanussi 			 * creation is deferred to create_actions(),
638466f4528STom Zanussi 			 * we need a per-action way to save it until
639466f4528STom Zanussi 			 * then, thus var_str.
640466f4528STom Zanussi 			 */
64150450603STom Zanussi 			char			*var_str;
6427ef224d1STom Zanussi 
643466f4528STom Zanussi 			/*
644466f4528STom Zanussi 			 * var_ref refers to the variable being
645466f4528STom Zanussi 			 * tracked e.g onmax($var).
646466f4528STom Zanussi 			 */
647466f4528STom Zanussi 			struct hist_field	*var_ref;
648466f4528STom Zanussi 
649466f4528STom Zanussi 			/*
650466f4528STom Zanussi 			 * track_var contains the 'invisible' tracking
651466f4528STom Zanussi 			 * variable created to keep the current
652466f4528STom Zanussi 			 * e.g. max value.
653466f4528STom Zanussi 			 */
654466f4528STom Zanussi 			struct hist_field	*track_var;
655466f4528STom Zanussi 
656466f4528STom Zanussi 			check_track_val_fn_t	check_val;
657466f4528STom Zanussi 			action_fn_t		save_data;
658466f4528STom Zanussi 		} track_data;
659466f4528STom Zanussi 	};
660466f4528STom Zanussi };
661f404da6eSTom Zanussi 
662a3785b7eSTom Zanussi struct track_data {
663a3785b7eSTom Zanussi 	u64				track_val;
664a3785b7eSTom Zanussi 	bool				updated;
665a3785b7eSTom Zanussi 
666a3785b7eSTom Zanussi 	unsigned int			key_len;
667a3785b7eSTom Zanussi 	void				*key;
668a3785b7eSTom Zanussi 	struct tracing_map_elt		elt;
669a3785b7eSTom Zanussi 
670a3785b7eSTom Zanussi 	struct action_data		*action_data;
671a3785b7eSTom Zanussi 	struct hist_trigger_data	*hist_data;
672a3785b7eSTom Zanussi };
673a3785b7eSTom Zanussi 
674a3785b7eSTom Zanussi struct hist_elt_data {
675a3785b7eSTom Zanussi 	char *comm;
676a3785b7eSTom Zanussi 	u64 *var_ref_vals;
677c910db94STom Zanussi 	char **field_var_str;
678c910db94STom Zanussi 	int n_field_var_str;
679a3785b7eSTom Zanussi };
680a3785b7eSTom Zanussi 
681a3785b7eSTom Zanussi struct snapshot_context {
682a3785b7eSTom Zanussi 	struct tracing_map_elt	*elt;
683a3785b7eSTom Zanussi 	void			*key;
684a3785b7eSTom Zanussi };
685a3785b7eSTom Zanussi 
6868b5d46fdSKalesh Singh /*
6878b5d46fdSKalesh Singh  * Returns the specific division function to use if the divisor
6888b5d46fdSKalesh Singh  * is constant. This avoids extra branches when the trigger is hit.
6898b5d46fdSKalesh Singh  */
hist_field_get_div_fn(struct hist_field * divisor)69086087383SSteven Rostedt (Google) static enum hist_field_fn hist_field_get_div_fn(struct hist_field *divisor)
6918b5d46fdSKalesh Singh {
6928b5d46fdSKalesh Singh 	u64 div = divisor->constant;
6938b5d46fdSKalesh Singh 
6948b5d46fdSKalesh Singh 	if (!(div & (div - 1)))
69586087383SSteven Rostedt (Google) 		return HIST_FIELD_FN_DIV_POWER2;
6968b5d46fdSKalesh Singh 
6978b5d46fdSKalesh Singh 	/* If the divisor is too large, do a regular division */
6988b5d46fdSKalesh Singh 	if (div > (1 << HIST_DIV_SHIFT))
69986087383SSteven Rostedt (Google) 		return HIST_FIELD_FN_DIV_NOT_POWER2;
7008b5d46fdSKalesh Singh 
7018b5d46fdSKalesh Singh 	divisor->div_multiplier = div64_u64((u64)(1 << HIST_DIV_SHIFT), div);
70286087383SSteven Rostedt (Google) 	return HIST_FIELD_FN_DIV_MULT_SHIFT;
7038b5d46fdSKalesh Singh }
7048b5d46fdSKalesh Singh 
track_data_free(struct track_data * track_data)705a3785b7eSTom Zanussi static void track_data_free(struct track_data *track_data)
706a3785b7eSTom Zanussi {
707a3785b7eSTom Zanussi 	struct hist_elt_data *elt_data;
708a3785b7eSTom Zanussi 
709a3785b7eSTom Zanussi 	if (!track_data)
710a3785b7eSTom Zanussi 		return;
711a3785b7eSTom Zanussi 
712a3785b7eSTom Zanussi 	kfree(track_data->key);
713a3785b7eSTom Zanussi 
714a3785b7eSTom Zanussi 	elt_data = track_data->elt.private_data;
715a3785b7eSTom Zanussi 	if (elt_data) {
716a3785b7eSTom Zanussi 		kfree(elt_data->comm);
717a3785b7eSTom Zanussi 		kfree(elt_data);
718a3785b7eSTom Zanussi 	}
719a3785b7eSTom Zanussi 
720a3785b7eSTom Zanussi 	kfree(track_data);
721a3785b7eSTom Zanussi }
722a3785b7eSTom Zanussi 
track_data_alloc(unsigned int key_len,struct action_data * action_data,struct hist_trigger_data * hist_data)723a3785b7eSTom Zanussi static struct track_data *track_data_alloc(unsigned int key_len,
724a3785b7eSTom Zanussi 					   struct action_data *action_data,
725a3785b7eSTom Zanussi 					   struct hist_trigger_data *hist_data)
726a3785b7eSTom Zanussi {
727a3785b7eSTom Zanussi 	struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
728a3785b7eSTom Zanussi 	struct hist_elt_data *elt_data;
729a3785b7eSTom Zanussi 
730a3785b7eSTom Zanussi 	if (!data)
731a3785b7eSTom Zanussi 		return ERR_PTR(-ENOMEM);
732a3785b7eSTom Zanussi 
733a3785b7eSTom Zanussi 	data->key = kzalloc(key_len, GFP_KERNEL);
734a3785b7eSTom Zanussi 	if (!data->key) {
735a3785b7eSTom Zanussi 		track_data_free(data);
736a3785b7eSTom Zanussi 		return ERR_PTR(-ENOMEM);
737a3785b7eSTom Zanussi 	}
738a3785b7eSTom Zanussi 
739a3785b7eSTom Zanussi 	data->key_len = key_len;
740a3785b7eSTom Zanussi 	data->action_data = action_data;
741a3785b7eSTom Zanussi 	data->hist_data = hist_data;
742a3785b7eSTom Zanussi 
743a3785b7eSTom Zanussi 	elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
744a3785b7eSTom Zanussi 	if (!elt_data) {
745a3785b7eSTom Zanussi 		track_data_free(data);
746a3785b7eSTom Zanussi 		return ERR_PTR(-ENOMEM);
747a3785b7eSTom Zanussi 	}
748726721a5STom Zanussi 
749a3785b7eSTom Zanussi 	data->elt.private_data = elt_data;
750a3785b7eSTom Zanussi 
751a3785b7eSTom Zanussi 	elt_data->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL);
752a3785b7eSTom Zanussi 	if (!elt_data->comm) {
753a3785b7eSTom Zanussi 		track_data_free(data);
754a3785b7eSTom Zanussi 		return ERR_PTR(-ENOMEM);
755a3785b7eSTom Zanussi 	}
756a3785b7eSTom Zanussi 
757a3785b7eSTom Zanussi 	return data;
758a3785b7eSTom Zanussi }
759a3785b7eSTom Zanussi 
760edfeed31STom Zanussi #define HIST_PREFIX "hist:"
761edfeed31STom Zanussi 
762edfeed31STom Zanussi static char *last_cmd;
763a1a05bb4STom Zanussi static char last_cmd_loc[MAX_FILTER_STR_VAL];
764f404da6eSTom Zanussi 
errpos(char * str)765d566c5e9STom Zanussi static int errpos(char *str)
766f404da6eSTom Zanussi {
767edfeed31STom Zanussi 	if (!str || !last_cmd)
768edfeed31STom Zanussi 		return 0;
769edfeed31STom Zanussi 
770d566c5e9STom Zanussi 	return err_pos(last_cmd, str);
771f404da6eSTom Zanussi }
772f404da6eSTom Zanussi 
last_cmd_set(struct trace_event_file * file,char * str)773a1a05bb4STom Zanussi static void last_cmd_set(struct trace_event_file *file, char *str)
774f404da6eSTom Zanussi {
775a1a05bb4STom Zanussi 	const char *system = NULL, *name = NULL;
776a1a05bb4STom Zanussi 	struct trace_event_call *call;
777f404da6eSTom Zanussi 
778f404da6eSTom Zanussi 	if (!str)
779f404da6eSTom Zanussi 		return;
780f404da6eSTom Zanussi 
781edfeed31STom Zanussi 	kfree(last_cmd);
782545db7e2SChristophe JAILLET 
783545db7e2SChristophe JAILLET 	last_cmd = kasprintf(GFP_KERNEL, HIST_PREFIX "%s", str);
784edfeed31STom Zanussi 	if (!last_cmd)
785edfeed31STom Zanussi 		return;
786edfeed31STom Zanussi 
787a1a05bb4STom Zanussi 	if (file) {
788a1a05bb4STom Zanussi 		call = file->event_call;
789a1a05bb4STom Zanussi 		system = call->class->system;
790a1a05bb4STom Zanussi 		if (system) {
791a1a05bb4STom Zanussi 			name = trace_event_name(call);
792a1a05bb4STom Zanussi 			if (!name)
793a1a05bb4STom Zanussi 				system = NULL;
794a1a05bb4STom Zanussi 		}
795f404da6eSTom Zanussi 	}
796f404da6eSTom Zanussi 
797a1a05bb4STom Zanussi 	if (system)
798edfeed31STom Zanussi 		snprintf(last_cmd_loc, MAX_FILTER_STR_VAL, HIST_PREFIX "%s:%s", system, name);
799f404da6eSTom Zanussi }
800f404da6eSTom Zanussi 
hist_err(struct trace_array * tr,u8 err_type,u16 err_pos)801edfeed31STom Zanussi static void hist_err(struct trace_array *tr, u8 err_type, u16 err_pos)
802f404da6eSTom Zanussi {
803edfeed31STom Zanussi 	if (!last_cmd)
804edfeed31STom Zanussi 		return;
805edfeed31STom Zanussi 
8062f754e77SSteven Rostedt (VMware) 	tracing_log_err(tr, last_cmd_loc, last_cmd, err_text,
8072f754e77SSteven Rostedt (VMware) 			err_type, err_pos);
808f404da6eSTom Zanussi }
809f404da6eSTom Zanussi 
hist_err_clear(void)810f404da6eSTom Zanussi static void hist_err_clear(void)
811f404da6eSTom Zanussi {
812edfeed31STom Zanussi 	if (last_cmd)
813a1a05bb4STom Zanussi 		last_cmd[0] = '\0';
814a1a05bb4STom Zanussi 	last_cmd_loc[0] = '\0';
815f404da6eSTom Zanussi }
816f404da6eSTom Zanussi 
8174b147936STom Zanussi typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
818d380dcdeSTom Zanussi 				    unsigned int *var_ref_idx);
8194b147936STom Zanussi 
trace_synth(struct synth_event * event,u64 * var_ref_vals,unsigned int * var_ref_idx)8204b147936STom Zanussi static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
821d380dcdeSTom Zanussi 			       unsigned int *var_ref_idx)
8224b147936STom Zanussi {
8234b147936STom Zanussi 	struct tracepoint *tp = event->tp;
8244b147936STom Zanussi 
8254b147936STom Zanussi 	if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
8264b147936STom Zanussi 		struct tracepoint_func *probe_func_ptr;
8274b147936STom Zanussi 		synth_probe_func_t probe_func;
8284b147936STom Zanussi 		void *__data;
8294b147936STom Zanussi 
8304b147936STom Zanussi 		if (!(cpu_online(raw_smp_processor_id())))
8314b147936STom Zanussi 			return;
8324b147936STom Zanussi 
8334b147936STom Zanussi 		probe_func_ptr = rcu_dereference_sched((tp)->funcs);
8344b147936STom Zanussi 		if (probe_func_ptr) {
8354b147936STom Zanussi 			do {
8364b147936STom Zanussi 				probe_func = probe_func_ptr->func;
8374b147936STom Zanussi 				__data = probe_func_ptr->data;
8384b147936STom Zanussi 				probe_func(__data, var_ref_vals, var_ref_idx);
8394b147936STom Zanussi 			} while ((++probe_func_ptr)->func);
8404b147936STom Zanussi 		}
8414b147936STom Zanussi 	}
8424b147936STom Zanussi }
8434b147936STom Zanussi 
action_trace(struct hist_trigger_data * hist_data,struct tracing_map_elt * elt,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * rbe,void * key,struct action_data * data,u64 * var_ref_vals)844c282a386STom Zanussi static void action_trace(struct hist_trigger_data *hist_data,
845b47e3302SSteven Rostedt (VMware) 			 struct tracing_map_elt *elt,
846b47e3302SSteven Rostedt (VMware) 			 struct trace_buffer *buffer, void *rec,
8477d18a10cSTom Zanussi 			 struct ring_buffer_event *rbe, void *key,
848c282a386STom Zanussi 			 struct action_data *data, u64 *var_ref_vals)
849c282a386STom Zanussi {
850c3e49506STom Zanussi 	struct synth_event *event = data->synth_event;
851c282a386STom Zanussi 
852c3e49506STom Zanussi 	trace_synth(event, var_ref_vals, data->var_ref_idx);
853c282a386STom Zanussi }
854c282a386STom Zanussi 
855c282a386STom Zanussi struct hist_var_data {
856c282a386STom Zanussi 	struct list_head list;
857c282a386STom Zanussi 	struct hist_trigger_data *hist_data;
858c282a386STom Zanussi };
859c282a386STom Zanussi 
hist_field_timestamp(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)860df35d93bSTom Zanussi static u64 hist_field_timestamp(struct hist_field *hist_field,
861df35d93bSTom Zanussi 				struct tracing_map_elt *elt,
862b47e3302SSteven Rostedt (VMware) 				struct trace_buffer *buffer,
863df35d93bSTom Zanussi 				struct ring_buffer_event *rbe,
864df35d93bSTom Zanussi 				void *event)
865860f9f6bSTom Zanussi {
866860f9f6bSTom Zanussi 	struct hist_trigger_data *hist_data = hist_field->hist_data;
867860f9f6bSTom Zanussi 	struct trace_array *tr = hist_data->event_file->tr;
868860f9f6bSTom Zanussi 
869efe6196aSSteven Rostedt (VMware) 	u64 ts = ring_buffer_event_time_stamp(buffer, rbe);
870860f9f6bSTom Zanussi 
871860f9f6bSTom Zanussi 	if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
872860f9f6bSTom Zanussi 		ts = ns2usecs(ts);
873860f9f6bSTom Zanussi 
874860f9f6bSTom Zanussi 	return ts;
875860f9f6bSTom Zanussi }
876860f9f6bSTom Zanussi 
hist_field_cpu(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)8778b7622bfSTom Zanussi static u64 hist_field_cpu(struct hist_field *hist_field,
8788b7622bfSTom Zanussi 			  struct tracing_map_elt *elt,
879b47e3302SSteven Rostedt (VMware) 			  struct trace_buffer *buffer,
8808b7622bfSTom Zanussi 			  struct ring_buffer_event *rbe,
8818b7622bfSTom Zanussi 			  void *event)
8828b7622bfSTom Zanussi {
8838b7622bfSTom Zanussi 	int cpu = smp_processor_id();
8848b7622bfSTom Zanussi 
8858b7622bfSTom Zanussi 	return cpu;
8868b7622bfSTom Zanussi }
8878b7622bfSTom Zanussi 
888de40f033STom Zanussi /**
889de40f033STom Zanussi  * check_field_for_var_ref - Check if a VAR_REF field references a variable
890de40f033STom Zanussi  * @hist_field: The VAR_REF field to check
891de40f033STom Zanussi  * @var_data: The hist trigger that owns the variable
892de40f033STom Zanussi  * @var_idx: The trigger variable identifier
893de40f033STom Zanussi  *
894de40f033STom Zanussi  * Check the given VAR_REF field to see whether or not it references
895de40f033STom Zanussi  * the given variable associated with the given trigger.
896de40f033STom Zanussi  *
897de40f033STom Zanussi  * Return: The VAR_REF field if it does reference the variable, NULL if not
898de40f033STom Zanussi  */
899067fe038STom Zanussi static struct hist_field *
check_field_for_var_ref(struct hist_field * hist_field,struct hist_trigger_data * var_data,unsigned int var_idx)900067fe038STom Zanussi check_field_for_var_ref(struct hist_field *hist_field,
901067fe038STom Zanussi 			struct hist_trigger_data *var_data,
902067fe038STom Zanussi 			unsigned int var_idx)
903067fe038STom Zanussi {
904e4f6d245STom Zanussi 	WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
905067fe038STom Zanussi 
906e4f6d245STom Zanussi 	if (hist_field && hist_field->var.idx == var_idx &&
907e4f6d245STom Zanussi 	    hist_field->var.hist_data == var_data)
908e4f6d245STom Zanussi 		return hist_field;
909067fe038STom Zanussi 
910e4f6d245STom Zanussi 	return NULL;
911067fe038STom Zanussi }
912067fe038STom Zanussi 
913de40f033STom Zanussi /**
914de40f033STom Zanussi  * find_var_ref - Check if a trigger has a reference to a trigger variable
915de40f033STom Zanussi  * @hist_data: The hist trigger that might have a reference to the variable
916de40f033STom Zanussi  * @var_data: The hist trigger that owns the variable
917de40f033STom Zanussi  * @var_idx: The trigger variable identifier
918de40f033STom Zanussi  *
919de40f033STom Zanussi  * Check the list of var_refs[] on the first hist trigger to see
920de40f033STom Zanussi  * whether any of them are references to the variable on the second
921de40f033STom Zanussi  * trigger.
922de40f033STom Zanussi  *
923de40f033STom Zanussi  * Return: The VAR_REF field referencing the variable if so, NULL if not
924de40f033STom Zanussi  */
find_var_ref(struct hist_trigger_data * hist_data,struct hist_trigger_data * var_data,unsigned int var_idx)925067fe038STom Zanussi static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
926067fe038STom Zanussi 				       struct hist_trigger_data *var_data,
927067fe038STom Zanussi 				       unsigned int var_idx)
928067fe038STom Zanussi {
929e4f6d245STom Zanussi 	struct hist_field *hist_field;
930067fe038STom Zanussi 	unsigned int i;
931067fe038STom Zanussi 
932e4f6d245STom Zanussi 	for (i = 0; i < hist_data->n_var_refs; i++) {
933e4f6d245STom Zanussi 		hist_field = hist_data->var_refs[i];
934e4f6d245STom Zanussi 		if (check_field_for_var_ref(hist_field, var_data, var_idx))
935e4f6d245STom Zanussi 			return hist_field;
936067fe038STom Zanussi 	}
937067fe038STom Zanussi 
938e4f6d245STom Zanussi 	return NULL;
939067fe038STom Zanussi }
940067fe038STom Zanussi 
941de40f033STom Zanussi /**
942de40f033STom Zanussi  * find_any_var_ref - Check if there is a reference to a given trigger variable
943de40f033STom Zanussi  * @hist_data: The hist trigger
944de40f033STom Zanussi  * @var_idx: The trigger variable identifier
945de40f033STom Zanussi  *
946de40f033STom Zanussi  * Check to see whether the given variable is currently referenced by
947de40f033STom Zanussi  * any other trigger.
948de40f033STom Zanussi  *
949de40f033STom Zanussi  * The trigger the variable is defined on is explicitly excluded - the
950de40f033STom Zanussi  * assumption being that a self-reference doesn't prevent a trigger
951de40f033STom Zanussi  * from being removed.
952de40f033STom Zanussi  *
953de40f033STom Zanussi  * Return: The VAR_REF field referencing the variable if so, NULL if not
954de40f033STom Zanussi  */
find_any_var_ref(struct hist_trigger_data * hist_data,unsigned int var_idx)955067fe038STom Zanussi static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
956067fe038STom Zanussi 					   unsigned int var_idx)
957067fe038STom Zanussi {
958067fe038STom Zanussi 	struct trace_array *tr = hist_data->event_file->tr;
959067fe038STom Zanussi 	struct hist_field *found = NULL;
960067fe038STom Zanussi 	struct hist_var_data *var_data;
961067fe038STom Zanussi 
962067fe038STom Zanussi 	list_for_each_entry(var_data, &tr->hist_vars, list) {
963067fe038STom Zanussi 		if (var_data->hist_data == hist_data)
964067fe038STom Zanussi 			continue;
965067fe038STom Zanussi 		found = find_var_ref(var_data->hist_data, hist_data, var_idx);
966067fe038STom Zanussi 		if (found)
967067fe038STom Zanussi 			break;
968067fe038STom Zanussi 	}
969067fe038STom Zanussi 
970067fe038STom Zanussi 	return found;
971067fe038STom Zanussi }
972067fe038STom Zanussi 
973de40f033STom Zanussi /**
974de40f033STom Zanussi  * check_var_refs - Check if there is a reference to any of trigger's variables
975de40f033STom Zanussi  * @hist_data: The hist trigger
976de40f033STom Zanussi  *
977de40f033STom Zanussi  * A trigger can define one or more variables.  If any one of them is
978de40f033STom Zanussi  * currently referenced by any other trigger, this function will
979de40f033STom Zanussi  * determine that.
9800a068f4aSColin Ian King  *
981de40f033STom Zanussi  * Typically used to determine whether or not a trigger can be removed
982de40f033STom Zanussi  * - if there are any references to a trigger's variables, it cannot.
983de40f033STom Zanussi  *
984de40f033STom Zanussi  * Return: True if there is a reference to any of trigger's variables
985de40f033STom Zanussi  */
check_var_refs(struct hist_trigger_data * hist_data)986067fe038STom Zanussi static bool check_var_refs(struct hist_trigger_data *hist_data)
987067fe038STom Zanussi {
988067fe038STom Zanussi 	struct hist_field *field;
989067fe038STom Zanussi 	bool found = false;
990067fe038STom Zanussi 	int i;
991067fe038STom Zanussi 
992067fe038STom Zanussi 	for_each_hist_field(i, hist_data) {
993067fe038STom Zanussi 		field = hist_data->fields[i];
994067fe038STom Zanussi 		if (field && field->flags & HIST_FIELD_FL_VAR) {
995067fe038STom Zanussi 			if (find_any_var_ref(hist_data, field->var.idx)) {
996067fe038STom Zanussi 				found = true;
997067fe038STom Zanussi 				break;
998067fe038STom Zanussi 			}
999067fe038STom Zanussi 		}
1000067fe038STom Zanussi 	}
1001067fe038STom Zanussi 
1002067fe038STom Zanussi 	return found;
1003067fe038STom Zanussi }
1004067fe038STom Zanussi 
find_hist_vars(struct hist_trigger_data * hist_data)1005067fe038STom Zanussi static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
1006067fe038STom Zanussi {
1007067fe038STom Zanussi 	struct trace_array *tr = hist_data->event_file->tr;
1008067fe038STom Zanussi 	struct hist_var_data *var_data, *found = NULL;
1009067fe038STom Zanussi 
1010067fe038STom Zanussi 	list_for_each_entry(var_data, &tr->hist_vars, list) {
1011067fe038STom Zanussi 		if (var_data->hist_data == hist_data) {
1012067fe038STom Zanussi 			found = var_data;
1013067fe038STom Zanussi 			break;
1014067fe038STom Zanussi 		}
1015067fe038STom Zanussi 	}
1016067fe038STom Zanussi 
1017067fe038STom Zanussi 	return found;
1018067fe038STom Zanussi }
1019067fe038STom Zanussi 
field_has_hist_vars(struct hist_field * hist_field,unsigned int level)1020067fe038STom Zanussi static bool field_has_hist_vars(struct hist_field *hist_field,
1021067fe038STom Zanussi 				unsigned int level)
1022067fe038STom Zanussi {
1023067fe038STom Zanussi 	int i;
1024067fe038STom Zanussi 
1025067fe038STom Zanussi 	if (level > 3)
1026067fe038STom Zanussi 		return false;
1027067fe038STom Zanussi 
1028067fe038STom Zanussi 	if (!hist_field)
1029067fe038STom Zanussi 		return false;
1030067fe038STom Zanussi 
1031067fe038STom Zanussi 	if (hist_field->flags & HIST_FIELD_FL_VAR ||
1032067fe038STom Zanussi 	    hist_field->flags & HIST_FIELD_FL_VAR_REF)
1033067fe038STom Zanussi 		return true;
1034067fe038STom Zanussi 
1035067fe038STom Zanussi 	for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
1036067fe038STom Zanussi 		struct hist_field *operand;
1037067fe038STom Zanussi 
1038067fe038STom Zanussi 		operand = hist_field->operands[i];
1039067fe038STom Zanussi 		if (field_has_hist_vars(operand, level + 1))
1040067fe038STom Zanussi 			return true;
1041067fe038STom Zanussi 	}
1042067fe038STom Zanussi 
1043067fe038STom Zanussi 	return false;
1044067fe038STom Zanussi }
1045067fe038STom Zanussi 
has_hist_vars(struct hist_trigger_data * hist_data)1046067fe038STom Zanussi static bool has_hist_vars(struct hist_trigger_data *hist_data)
1047067fe038STom Zanussi {
1048067fe038STom Zanussi 	struct hist_field *hist_field;
1049067fe038STom Zanussi 	int i;
1050067fe038STom Zanussi 
1051067fe038STom Zanussi 	for_each_hist_field(i, hist_data) {
1052067fe038STom Zanussi 		hist_field = hist_data->fields[i];
1053067fe038STom Zanussi 		if (field_has_hist_vars(hist_field, 0))
1054067fe038STom Zanussi 			return true;
1055067fe038STom Zanussi 	}
1056067fe038STom Zanussi 
1057067fe038STom Zanussi 	return false;
1058067fe038STom Zanussi }
1059067fe038STom Zanussi 
save_hist_vars(struct hist_trigger_data * hist_data)1060067fe038STom Zanussi static int save_hist_vars(struct hist_trigger_data *hist_data)
1061067fe038STom Zanussi {
1062067fe038STom Zanussi 	struct trace_array *tr = hist_data->event_file->tr;
1063067fe038STom Zanussi 	struct hist_var_data *var_data;
1064067fe038STom Zanussi 
1065067fe038STom Zanussi 	var_data = find_hist_vars(hist_data);
1066067fe038STom Zanussi 	if (var_data)
1067067fe038STom Zanussi 		return 0;
1068067fe038STom Zanussi 
10698530dec6SSteven Rostedt (VMware) 	if (tracing_check_open_get_tr(tr))
1070067fe038STom Zanussi 		return -ENODEV;
1071067fe038STom Zanussi 
1072067fe038STom Zanussi 	var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
1073067fe038STom Zanussi 	if (!var_data) {
1074067fe038STom Zanussi 		trace_array_put(tr);
1075067fe038STom Zanussi 		return -ENOMEM;
1076067fe038STom Zanussi 	}
1077067fe038STom Zanussi 
1078067fe038STom Zanussi 	var_data->hist_data = hist_data;
1079067fe038STom Zanussi 	list_add(&var_data->list, &tr->hist_vars);
1080067fe038STom Zanussi 
1081067fe038STom Zanussi 	return 0;
1082067fe038STom Zanussi }
1083067fe038STom Zanussi 
remove_hist_vars(struct hist_trigger_data * hist_data)1084067fe038STom Zanussi static void remove_hist_vars(struct hist_trigger_data *hist_data)
1085067fe038STom Zanussi {
1086067fe038STom Zanussi 	struct trace_array *tr = hist_data->event_file->tr;
1087067fe038STom Zanussi 	struct hist_var_data *var_data;
1088067fe038STom Zanussi 
1089067fe038STom Zanussi 	var_data = find_hist_vars(hist_data);
1090067fe038STom Zanussi 	if (!var_data)
1091067fe038STom Zanussi 		return;
1092067fe038STom Zanussi 
1093067fe038STom Zanussi 	if (WARN_ON(check_var_refs(hist_data)))
1094067fe038STom Zanussi 		return;
1095067fe038STom Zanussi 
1096067fe038STom Zanussi 	list_del(&var_data->list);
1097067fe038STom Zanussi 
1098067fe038STom Zanussi 	kfree(var_data);
1099067fe038STom Zanussi 
1100067fe038STom Zanussi 	trace_array_put(tr);
1101067fe038STom Zanussi }
1102067fe038STom Zanussi 
find_var_field(struct hist_trigger_data * hist_data,const char * var_name)110330350d65STom Zanussi static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
110430350d65STom Zanussi 					 const char *var_name)
110530350d65STom Zanussi {
110630350d65STom Zanussi 	struct hist_field *hist_field, *found = NULL;
110730350d65STom Zanussi 	int i;
110830350d65STom Zanussi 
110930350d65STom Zanussi 	for_each_hist_field(i, hist_data) {
111030350d65STom Zanussi 		hist_field = hist_data->fields[i];
111130350d65STom Zanussi 		if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
111230350d65STom Zanussi 		    strcmp(hist_field->var.name, var_name) == 0) {
111330350d65STom Zanussi 			found = hist_field;
111430350d65STom Zanussi 			break;
111530350d65STom Zanussi 		}
111630350d65STom Zanussi 	}
111730350d65STom Zanussi 
111830350d65STom Zanussi 	return found;
111930350d65STom Zanussi }
112030350d65STom Zanussi 
find_var(struct hist_trigger_data * hist_data,struct trace_event_file * file,const char * var_name)112130350d65STom Zanussi static struct hist_field *find_var(struct hist_trigger_data *hist_data,
112230350d65STom Zanussi 				   struct trace_event_file *file,
112330350d65STom Zanussi 				   const char *var_name)
112430350d65STom Zanussi {
112530350d65STom Zanussi 	struct hist_trigger_data *test_data;
112630350d65STom Zanussi 	struct event_trigger_data *test;
112730350d65STom Zanussi 	struct hist_field *hist_field;
112830350d65STom Zanussi 
1129aeed8aa3SMasami Hiramatsu 	lockdep_assert_held(&event_mutex);
1130aeed8aa3SMasami Hiramatsu 
113130350d65STom Zanussi 	hist_field = find_var_field(hist_data, var_name);
113230350d65STom Zanussi 	if (hist_field)
113330350d65STom Zanussi 		return hist_field;
113430350d65STom Zanussi 
1135aeed8aa3SMasami Hiramatsu 	list_for_each_entry(test, &file->triggers, list) {
113630350d65STom Zanussi 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
113730350d65STom Zanussi 			test_data = test->private_data;
113830350d65STom Zanussi 			hist_field = find_var_field(test_data, var_name);
113930350d65STom Zanussi 			if (hist_field)
114030350d65STom Zanussi 				return hist_field;
114130350d65STom Zanussi 		}
114230350d65STom Zanussi 	}
114330350d65STom Zanussi 
114430350d65STom Zanussi 	return NULL;
114530350d65STom Zanussi }
114630350d65STom Zanussi 
find_var_file(struct trace_array * tr,char * system,char * event_name,char * var_name)1147067fe038STom Zanussi static struct trace_event_file *find_var_file(struct trace_array *tr,
1148067fe038STom Zanussi 					      char *system,
1149067fe038STom Zanussi 					      char *event_name,
1150067fe038STom Zanussi 					      char *var_name)
1151067fe038STom Zanussi {
1152067fe038STom Zanussi 	struct hist_trigger_data *var_hist_data;
1153067fe038STom Zanussi 	struct hist_var_data *var_data;
1154067fe038STom Zanussi 	struct trace_event_file *file, *found = NULL;
1155067fe038STom Zanussi 
1156067fe038STom Zanussi 	if (system)
1157067fe038STom Zanussi 		return find_event_file(tr, system, event_name);
1158067fe038STom Zanussi 
1159067fe038STom Zanussi 	list_for_each_entry(var_data, &tr->hist_vars, list) {
1160067fe038STom Zanussi 		var_hist_data = var_data->hist_data;
1161067fe038STom Zanussi 		file = var_hist_data->event_file;
1162067fe038STom Zanussi 		if (file == found)
1163067fe038STom Zanussi 			continue;
1164067fe038STom Zanussi 
1165067fe038STom Zanussi 		if (find_var_field(var_hist_data, var_name)) {
1166f404da6eSTom Zanussi 			if (found) {
1167d0cd871bSSteven Rostedt (VMware) 				hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name));
1168067fe038STom Zanussi 				return NULL;
1169f404da6eSTom Zanussi 			}
1170067fe038STom Zanussi 
1171067fe038STom Zanussi 			found = file;
1172067fe038STom Zanussi 		}
1173067fe038STom Zanussi 	}
1174067fe038STom Zanussi 
1175067fe038STom Zanussi 	return found;
1176067fe038STom Zanussi }
1177067fe038STom Zanussi 
find_file_var(struct trace_event_file * file,const char * var_name)1178067fe038STom Zanussi static struct hist_field *find_file_var(struct trace_event_file *file,
1179067fe038STom Zanussi 					const char *var_name)
1180067fe038STom Zanussi {
1181067fe038STom Zanussi 	struct hist_trigger_data *test_data;
1182067fe038STom Zanussi 	struct event_trigger_data *test;
1183067fe038STom Zanussi 	struct hist_field *hist_field;
1184067fe038STom Zanussi 
1185aeed8aa3SMasami Hiramatsu 	lockdep_assert_held(&event_mutex);
1186aeed8aa3SMasami Hiramatsu 
1187aeed8aa3SMasami Hiramatsu 	list_for_each_entry(test, &file->triggers, list) {
1188067fe038STom Zanussi 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1189067fe038STom Zanussi 			test_data = test->private_data;
1190067fe038STom Zanussi 			hist_field = find_var_field(test_data, var_name);
1191067fe038STom Zanussi 			if (hist_field)
1192067fe038STom Zanussi 				return hist_field;
1193067fe038STom Zanussi 		}
1194067fe038STom Zanussi 	}
1195067fe038STom Zanussi 
1196067fe038STom Zanussi 	return NULL;
1197067fe038STom Zanussi }
1198067fe038STom Zanussi 
1199c282a386STom Zanussi static struct hist_field *
find_match_var(struct hist_trigger_data * hist_data,char * var_name)1200c282a386STom Zanussi find_match_var(struct hist_trigger_data *hist_data, char *var_name)
1201c282a386STom Zanussi {
1202c282a386STom Zanussi 	struct trace_array *tr = hist_data->event_file->tr;
1203c282a386STom Zanussi 	struct hist_field *hist_field, *found = NULL;
1204c282a386STom Zanussi 	struct trace_event_file *file;
1205c282a386STom Zanussi 	unsigned int i;
1206c282a386STom Zanussi 
1207c282a386STom Zanussi 	for (i = 0; i < hist_data->n_actions; i++) {
1208c282a386STom Zanussi 		struct action_data *data = hist_data->actions[i];
1209c282a386STom Zanussi 
12107d18a10cSTom Zanussi 		if (data->handler == HANDLER_ONMATCH) {
1211c3e49506STom Zanussi 			char *system = data->match_data.event_system;
1212c3e49506STom Zanussi 			char *event_name = data->match_data.event;
1213c282a386STom Zanussi 
1214c282a386STom Zanussi 			file = find_var_file(tr, system, event_name, var_name);
1215c282a386STom Zanussi 			if (!file)
1216c282a386STom Zanussi 				continue;
1217c282a386STom Zanussi 			hist_field = find_file_var(file, var_name);
1218c282a386STom Zanussi 			if (hist_field) {
1219c282a386STom Zanussi 				if (found) {
1220d0cd871bSSteven Rostedt (VMware) 					hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE,
1221d0cd871bSSteven Rostedt (VMware) 						 errpos(var_name));
1222c282a386STom Zanussi 					return ERR_PTR(-EINVAL);
1223c282a386STom Zanussi 				}
1224c282a386STom Zanussi 
1225c282a386STom Zanussi 				found = hist_field;
1226c282a386STom Zanussi 			}
1227c282a386STom Zanussi 		}
1228c282a386STom Zanussi 	}
1229c282a386STom Zanussi 	return found;
1230c282a386STom Zanussi }
1231c282a386STom Zanussi 
find_event_var(struct hist_trigger_data * hist_data,char * system,char * event_name,char * var_name)1232067fe038STom Zanussi static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
1233067fe038STom Zanussi 					 char *system,
1234067fe038STom Zanussi 					 char *event_name,
1235067fe038STom Zanussi 					 char *var_name)
1236067fe038STom Zanussi {
1237067fe038STom Zanussi 	struct trace_array *tr = hist_data->event_file->tr;
1238067fe038STom Zanussi 	struct hist_field *hist_field = NULL;
1239067fe038STom Zanussi 	struct trace_event_file *file;
1240067fe038STom Zanussi 
1241c282a386STom Zanussi 	if (!system || !event_name) {
1242c282a386STom Zanussi 		hist_field = find_match_var(hist_data, var_name);
1243c282a386STom Zanussi 		if (IS_ERR(hist_field))
1244c282a386STom Zanussi 			return NULL;
1245c282a386STom Zanussi 		if (hist_field)
1246c282a386STom Zanussi 			return hist_field;
1247c282a386STom Zanussi 	}
1248c282a386STom Zanussi 
1249067fe038STom Zanussi 	file = find_var_file(tr, system, event_name, var_name);
1250067fe038STom Zanussi 	if (!file)
1251067fe038STom Zanussi 		return NULL;
1252067fe038STom Zanussi 
1253067fe038STom Zanussi 	hist_field = find_file_var(file, var_name);
1254067fe038STom Zanussi 
1255067fe038STom Zanussi 	return hist_field;
1256067fe038STom Zanussi }
1257067fe038STom Zanussi 
hist_field_var_ref(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)1258067fe038STom Zanussi static u64 hist_field_var_ref(struct hist_field *hist_field,
1259067fe038STom Zanussi 			      struct tracing_map_elt *elt,
1260b47e3302SSteven Rostedt (VMware) 			      struct trace_buffer *buffer,
1261067fe038STom Zanussi 			      struct ring_buffer_event *rbe,
1262067fe038STom Zanussi 			      void *event)
1263067fe038STom Zanussi {
1264067fe038STom Zanussi 	struct hist_elt_data *elt_data;
1265067fe038STom Zanussi 	u64 var_val = 0;
1266067fe038STom Zanussi 
126755267c88STom Zanussi 	if (WARN_ON_ONCE(!elt))
126855267c88STom Zanussi 		return var_val;
126955267c88STom Zanussi 
1270067fe038STom Zanussi 	elt_data = elt->private_data;
1271067fe038STom Zanussi 	var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
1272067fe038STom Zanussi 
1273067fe038STom Zanussi 	return var_val;
1274067fe038STom Zanussi }
1275067fe038STom Zanussi 
resolve_var_refs(struct hist_trigger_data * hist_data,void * key,u64 * var_ref_vals,bool self)1276067fe038STom Zanussi static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
1277067fe038STom Zanussi 			     u64 *var_ref_vals, bool self)
1278067fe038STom Zanussi {
1279067fe038STom Zanussi 	struct hist_trigger_data *var_data;
1280067fe038STom Zanussi 	struct tracing_map_elt *var_elt;
1281067fe038STom Zanussi 	struct hist_field *hist_field;
1282067fe038STom Zanussi 	unsigned int i, var_idx;
1283067fe038STom Zanussi 	bool resolved = true;
1284067fe038STom Zanussi 	u64 var_val = 0;
1285067fe038STom Zanussi 
1286067fe038STom Zanussi 	for (i = 0; i < hist_data->n_var_refs; i++) {
1287067fe038STom Zanussi 		hist_field = hist_data->var_refs[i];
1288067fe038STom Zanussi 		var_idx = hist_field->var.idx;
1289067fe038STom Zanussi 		var_data = hist_field->var.hist_data;
1290067fe038STom Zanussi 
1291067fe038STom Zanussi 		if (var_data == NULL) {
1292067fe038STom Zanussi 			resolved = false;
1293067fe038STom Zanussi 			break;
1294067fe038STom Zanussi 		}
1295067fe038STom Zanussi 
1296067fe038STom Zanussi 		if ((self && var_data != hist_data) ||
1297067fe038STom Zanussi 		    (!self && var_data == hist_data))
1298067fe038STom Zanussi 			continue;
1299067fe038STom Zanussi 
1300067fe038STom Zanussi 		var_elt = tracing_map_lookup(var_data->map, key);
1301067fe038STom Zanussi 		if (!var_elt) {
1302067fe038STom Zanussi 			resolved = false;
1303067fe038STom Zanussi 			break;
1304067fe038STom Zanussi 		}
1305067fe038STom Zanussi 
1306067fe038STom Zanussi 		if (!tracing_map_var_set(var_elt, var_idx)) {
1307067fe038STom Zanussi 			resolved = false;
1308067fe038STom Zanussi 			break;
1309067fe038STom Zanussi 		}
1310067fe038STom Zanussi 
1311067fe038STom Zanussi 		if (self || !hist_field->read_once)
1312067fe038STom Zanussi 			var_val = tracing_map_read_var(var_elt, var_idx);
1313067fe038STom Zanussi 		else
1314067fe038STom Zanussi 			var_val = tracing_map_read_var_once(var_elt, var_idx);
1315067fe038STom Zanussi 
1316067fe038STom Zanussi 		var_ref_vals[i] = var_val;
1317067fe038STom Zanussi 	}
1318067fe038STom Zanussi 
1319067fe038STom Zanussi 	return resolved;
1320067fe038STom Zanussi }
1321067fe038STom Zanussi 
hist_field_name(struct hist_field * field,unsigned int level)132285013256STom Zanussi static const char *hist_field_name(struct hist_field *field,
132385013256STom Zanussi 				   unsigned int level)
132485013256STom Zanussi {
132585013256STom Zanussi 	const char *field_name = "";
132685013256STom Zanussi 
13279f116f76SSteven Rostedt (Google) 	if (WARN_ON_ONCE(!field))
13289f116f76SSteven Rostedt (Google) 		return field_name;
13299f116f76SSteven Rostedt (Google) 
133085013256STom Zanussi 	if (level > 1)
133185013256STom Zanussi 		return field_name;
133285013256STom Zanussi 
133385013256STom Zanussi 	if (field->field)
133485013256STom Zanussi 		field_name = field->field->name;
13357e8b88a3STom Zanussi 	else if (field->flags & HIST_FIELD_FL_LOG2 ||
1336de9a48a3SSteven Rostedt (VMware) 		 field->flags & HIST_FIELD_FL_ALIAS ||
1337de9a48a3SSteven Rostedt (VMware) 		 field->flags & HIST_FIELD_FL_BUCKET)
13385819eaddSTom Zanussi 		field_name = hist_field_name(field->operands[0], ++level);
13398b7622bfSTom Zanussi 	else if (field->flags & HIST_FIELD_FL_CPU)
13401e3bac71SSteven Rostedt (VMware) 		field_name = "common_cpu";
1341067fe038STom Zanussi 	else if (field->flags & HIST_FIELD_FL_EXPR ||
1342067fe038STom Zanussi 		 field->flags & HIST_FIELD_FL_VAR_REF) {
1343067fe038STom Zanussi 		if (field->system) {
1344067fe038STom Zanussi 			static char full_name[MAX_FILTER_STR_VAL];
1345067fe038STom Zanussi 
1346067fe038STom Zanussi 			strcat(full_name, field->system);
1347067fe038STom Zanussi 			strcat(full_name, ".");
1348067fe038STom Zanussi 			strcat(full_name, field->event_name);
1349067fe038STom Zanussi 			strcat(full_name, ".");
1350067fe038STom Zanussi 			strcat(full_name, field->name);
1351067fe038STom Zanussi 			field_name = full_name;
1352067fe038STom Zanussi 		} else
1353100719dcSTom Zanussi 			field_name = field->name;
13540ae7961eSTom Zanussi 	} else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
13550ae7961eSTom Zanussi 		field_name = "common_timestamp";
13562bacfd9fSTom Zanussi 	else if (field->flags & HIST_FIELD_FL_STACKTRACE) {
13572bacfd9fSTom Zanussi 		if (field->field)
13582bacfd9fSTom Zanussi 			field_name = field->field->name;
13592bacfd9fSTom Zanussi 		else
13604b512860SSteven Rostedt (Google) 			field_name = "common_stacktrace";
13612bacfd9fSTom Zanussi 	} else if (field->flags & HIST_FIELD_FL_HITCOUNT)
13625f2e094eSTom Zanussi 		field_name = "hitcount";
136385013256STom Zanussi 
136485013256STom Zanussi 	if (field_name == NULL)
136585013256STom Zanussi 		field_name = "";
136685013256STom Zanussi 
136785013256STom Zanussi 	return field_name;
136885013256STom Zanussi }
136985013256STom Zanussi 
select_value_fn(int field_size,int field_is_signed)137086087383SSteven Rostedt (Google) static enum hist_field_fn select_value_fn(int field_size, int field_is_signed)
13717ef224d1STom Zanussi {
13727ef224d1STom Zanussi 	switch (field_size) {
13737ef224d1STom Zanussi 	case 8:
13747ef224d1STom Zanussi 		if (field_is_signed)
137586087383SSteven Rostedt (Google) 			return HIST_FIELD_FN_S64;
13767ef224d1STom Zanussi 		else
137786087383SSteven Rostedt (Google) 			return HIST_FIELD_FN_U64;
13787ef224d1STom Zanussi 	case 4:
13797ef224d1STom Zanussi 		if (field_is_signed)
138086087383SSteven Rostedt (Google) 			return HIST_FIELD_FN_S32;
13817ef224d1STom Zanussi 		else
138286087383SSteven Rostedt (Google) 			return HIST_FIELD_FN_U32;
13837ef224d1STom Zanussi 	case 2:
13847ef224d1STom Zanussi 		if (field_is_signed)
138586087383SSteven Rostedt (Google) 			return HIST_FIELD_FN_S16;
13867ef224d1STom Zanussi 		else
138786087383SSteven Rostedt (Google) 			return HIST_FIELD_FN_U16;
13887ef224d1STom Zanussi 	case 1:
13897ef224d1STom Zanussi 		if (field_is_signed)
139086087383SSteven Rostedt (Google) 			return HIST_FIELD_FN_S8;
13917ef224d1STom Zanussi 		else
139286087383SSteven Rostedt (Google) 			return HIST_FIELD_FN_U8;
13937ef224d1STom Zanussi 	}
13947ef224d1STom Zanussi 
139586087383SSteven Rostedt (Google) 	return HIST_FIELD_FN_NOP;
13967ef224d1STom Zanussi }
13977ef224d1STom Zanussi 
parse_map_size(char * str)13987ef224d1STom Zanussi static int parse_map_size(char *str)
13997ef224d1STom Zanussi {
14007ef224d1STom Zanussi 	unsigned long size, map_bits;
14017ef224d1STom Zanussi 	int ret;
14027ef224d1STom Zanussi 
14037ef224d1STom Zanussi 	ret = kstrtoul(str, 0, &size);
14047ef224d1STom Zanussi 	if (ret)
14057ef224d1STom Zanussi 		goto out;
14067ef224d1STom Zanussi 
14077ef224d1STom Zanussi 	map_bits = ilog2(roundup_pow_of_two(size));
14087ef224d1STom Zanussi 	if (map_bits < TRACING_MAP_BITS_MIN ||
14097ef224d1STom Zanussi 	    map_bits > TRACING_MAP_BITS_MAX)
14107ef224d1STom Zanussi 		ret = -EINVAL;
14117ef224d1STom Zanussi 	else
14127ef224d1STom Zanussi 		ret = map_bits;
14137ef224d1STom Zanussi  out:
14147ef224d1STom Zanussi 	return ret;
14157ef224d1STom Zanussi }
14167ef224d1STom Zanussi 
destroy_hist_trigger_attrs(struct hist_trigger_attrs * attrs)14177ef224d1STom Zanussi static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
14187ef224d1STom Zanussi {
141930350d65STom Zanussi 	unsigned int i;
142030350d65STom Zanussi 
14217ef224d1STom Zanussi 	if (!attrs)
14227ef224d1STom Zanussi 		return;
14237ef224d1STom Zanussi 
142430350d65STom Zanussi 	for (i = 0; i < attrs->n_assignments; i++)
142530350d65STom Zanussi 		kfree(attrs->assignment_str[i]);
142630350d65STom Zanussi 
14270212e2aaSTom Zanussi 	for (i = 0; i < attrs->n_actions; i++)
14280212e2aaSTom Zanussi 		kfree(attrs->action_str[i]);
14290212e2aaSTom Zanussi 
14305463bfdaSTom Zanussi 	kfree(attrs->name);
1431e62347d2STom Zanussi 	kfree(attrs->sort_key_str);
14327ef224d1STom Zanussi 	kfree(attrs->keys_str);
1433f2606835STom Zanussi 	kfree(attrs->vals_str);
1434a4072fe8STom Zanussi 	kfree(attrs->clock);
14357ef224d1STom Zanussi 	kfree(attrs);
14367ef224d1STom Zanussi }
14377ef224d1STom Zanussi 
parse_action(char * str,struct hist_trigger_attrs * attrs)14380212e2aaSTom Zanussi static int parse_action(char *str, struct hist_trigger_attrs *attrs)
14390212e2aaSTom Zanussi {
1440c282a386STom Zanussi 	int ret = -EINVAL;
14410212e2aaSTom Zanussi 
14420212e2aaSTom Zanussi 	if (attrs->n_actions >= HIST_ACTIONS_MAX)
14430212e2aaSTom Zanussi 		return ret;
14440212e2aaSTom Zanussi 
1445754481e6SSteven Rostedt (VMware) 	if ((str_has_prefix(str, "onmatch(")) ||
1446dff81f55STom Zanussi 	    (str_has_prefix(str, "onmax(")) ||
1447dff81f55STom Zanussi 	    (str_has_prefix(str, "onchange("))) {
1448c282a386STom Zanussi 		attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
1449c282a386STom Zanussi 		if (!attrs->action_str[attrs->n_actions]) {
1450c282a386STom Zanussi 			ret = -ENOMEM;
1451c282a386STom Zanussi 			return ret;
1452c282a386STom Zanussi 		}
1453c282a386STom Zanussi 		attrs->n_actions++;
1454c282a386STom Zanussi 		ret = 0;
1455c282a386STom Zanussi 	}
14560212e2aaSTom Zanussi 	return ret;
14570212e2aaSTom Zanussi }
14580212e2aaSTom Zanussi 
parse_assignment(struct trace_array * tr,char * str,struct hist_trigger_attrs * attrs)1459d0cd871bSSteven Rostedt (VMware) static int parse_assignment(struct trace_array *tr,
1460d0cd871bSSteven Rostedt (VMware) 			    char *str, struct hist_trigger_attrs *attrs)
14619b1ae035STom Zanussi {
1462b527b638STom Zanussi 	int len, ret = 0;
14639b1ae035STom Zanussi 
1464b527b638STom Zanussi 	if ((len = str_has_prefix(str, "key=")) ||
1465b527b638STom Zanussi 	    (len = str_has_prefix(str, "keys="))) {
1466b527b638STom Zanussi 		attrs->keys_str = kstrdup(str + len, GFP_KERNEL);
14679b1ae035STom Zanussi 		if (!attrs->keys_str) {
14689b1ae035STom Zanussi 			ret = -ENOMEM;
14699b1ae035STom Zanussi 			goto out;
14709b1ae035STom Zanussi 		}
1471b527b638STom Zanussi 	} else if ((len = str_has_prefix(str, "val=")) ||
1472b527b638STom Zanussi 		   (len = str_has_prefix(str, "vals=")) ||
1473b527b638STom Zanussi 		   (len = str_has_prefix(str, "values="))) {
1474b527b638STom Zanussi 		attrs->vals_str = kstrdup(str + len, GFP_KERNEL);
14759b1ae035STom Zanussi 		if (!attrs->vals_str) {
14769b1ae035STom Zanussi 			ret = -ENOMEM;
14779b1ae035STom Zanussi 			goto out;
14789b1ae035STom Zanussi 		}
1479b527b638STom Zanussi 	} else if ((len = str_has_prefix(str, "sort="))) {
1480b527b638STom Zanussi 		attrs->sort_key_str = kstrdup(str + len, GFP_KERNEL);
14819b1ae035STom Zanussi 		if (!attrs->sort_key_str) {
14829b1ae035STom Zanussi 			ret = -ENOMEM;
14839b1ae035STom Zanussi 			goto out;
14849b1ae035STom Zanussi 		}
1485754481e6SSteven Rostedt (VMware) 	} else if (str_has_prefix(str, "name=")) {
14869b1ae035STom Zanussi 		attrs->name = kstrdup(str, GFP_KERNEL);
14879b1ae035STom Zanussi 		if (!attrs->name) {
14889b1ae035STom Zanussi 			ret = -ENOMEM;
14899b1ae035STom Zanussi 			goto out;
14909b1ae035STom Zanussi 		}
1491b527b638STom Zanussi 	} else if ((len = str_has_prefix(str, "clock="))) {
1492b527b638STom Zanussi 		str += len;
1493a4072fe8STom Zanussi 
1494a4072fe8STom Zanussi 		str = strstrip(str);
1495a4072fe8STom Zanussi 		attrs->clock = kstrdup(str, GFP_KERNEL);
1496a4072fe8STom Zanussi 		if (!attrs->clock) {
1497a4072fe8STom Zanussi 			ret = -ENOMEM;
1498a4072fe8STom Zanussi 			goto out;
1499a4072fe8STom Zanussi 		}
1500b527b638STom Zanussi 	} else if ((len = str_has_prefix(str, "size="))) {
1501b527b638STom Zanussi 		int map_bits = parse_map_size(str + len);
15029b1ae035STom Zanussi 
15039b1ae035STom Zanussi 		if (map_bits < 0) {
15049b1ae035STom Zanussi 			ret = map_bits;
15059b1ae035STom Zanussi 			goto out;
15069b1ae035STom Zanussi 		}
15079b1ae035STom Zanussi 		attrs->map_bits = map_bits;
150830350d65STom Zanussi 	} else {
150930350d65STom Zanussi 		char *assignment;
151030350d65STom Zanussi 
151130350d65STom Zanussi 		if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
1512d0cd871bSSteven Rostedt (VMware) 			hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str));
15139b1ae035STom Zanussi 			ret = -EINVAL;
151430350d65STom Zanussi 			goto out;
151530350d65STom Zanussi 		}
151630350d65STom Zanussi 
151730350d65STom Zanussi 		assignment = kstrdup(str, GFP_KERNEL);
151830350d65STom Zanussi 		if (!assignment) {
151930350d65STom Zanussi 			ret = -ENOMEM;
152030350d65STom Zanussi 			goto out;
152130350d65STom Zanussi 		}
152230350d65STom Zanussi 
152330350d65STom Zanussi 		attrs->assignment_str[attrs->n_assignments++] = assignment;
152430350d65STom Zanussi 	}
15259b1ae035STom Zanussi  out:
15269b1ae035STom Zanussi 	return ret;
15279b1ae035STom Zanussi }
15289b1ae035STom Zanussi 
1529d0cd871bSSteven Rostedt (VMware) static struct hist_trigger_attrs *
parse_hist_trigger_attrs(struct trace_array * tr,char * trigger_str)1530d0cd871bSSteven Rostedt (VMware) parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str)
15317ef224d1STom Zanussi {
15327ef224d1STom Zanussi 	struct hist_trigger_attrs *attrs;
15337ef224d1STom Zanussi 	int ret = 0;
15347ef224d1STom Zanussi 
15357ef224d1STom Zanussi 	attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
15367ef224d1STom Zanussi 	if (!attrs)
15377ef224d1STom Zanussi 		return ERR_PTR(-ENOMEM);
15387ef224d1STom Zanussi 
15397ef224d1STom Zanussi 	while (trigger_str) {
15407ef224d1STom Zanussi 		char *str = strsep(&trigger_str, ":");
1541b527b638STom Zanussi 		char *rhs;
15427ef224d1STom Zanussi 
1543b527b638STom Zanussi 		rhs = strchr(str, '=');
1544b527b638STom Zanussi 		if (rhs) {
1545b527b638STom Zanussi 			if (!strlen(++rhs)) {
1546b527b638STom Zanussi 				ret = -EINVAL;
15474de26c8cSTom Zanussi 				hist_err(tr, HIST_ERR_EMPTY_ASSIGNMENT, errpos(str));
1548b527b638STom Zanussi 				goto free;
1549b527b638STom Zanussi 			}
1550d0cd871bSSteven Rostedt (VMware) 			ret = parse_assignment(tr, str, attrs);
15519b1ae035STom Zanussi 			if (ret)
15529b1ae035STom Zanussi 				goto free;
1553ccf47f5cSMasami Hiramatsu (Google) 		} else if (strcmp(str, "nohitcount") == 0 ||
1554ccf47f5cSMasami Hiramatsu (Google) 			   strcmp(str, "NOHC") == 0)
1555ccf47f5cSMasami Hiramatsu (Google) 			attrs->no_hitcount = true;
1556ccf47f5cSMasami Hiramatsu (Google) 		else if (strcmp(str, "pause") == 0)
155783e99914STom Zanussi 			attrs->pause = true;
155883e99914STom Zanussi 		else if ((strcmp(str, "cont") == 0) ||
155983e99914STom Zanussi 			 (strcmp(str, "continue") == 0))
156083e99914STom Zanussi 			attrs->cont = true;
1561e86ae9baSTom Zanussi 		else if (strcmp(str, "clear") == 0)
1562e86ae9baSTom Zanussi 			attrs->clear = true;
15639b1ae035STom Zanussi 		else {
15640212e2aaSTom Zanussi 			ret = parse_action(str, attrs);
15650212e2aaSTom Zanussi 			if (ret)
15667ef224d1STom Zanussi 				goto free;
15677ef224d1STom Zanussi 		}
15687ef224d1STom Zanussi 	}
15697ef224d1STom Zanussi 
15707ef224d1STom Zanussi 	if (!attrs->keys_str) {
15717ef224d1STom Zanussi 		ret = -EINVAL;
15727ef224d1STom Zanussi 		goto free;
15737ef224d1STom Zanussi 	}
15747ef224d1STom Zanussi 
1575a4072fe8STom Zanussi 	if (!attrs->clock) {
1576a4072fe8STom Zanussi 		attrs->clock = kstrdup("global", GFP_KERNEL);
1577a4072fe8STom Zanussi 		if (!attrs->clock) {
1578a4072fe8STom Zanussi 			ret = -ENOMEM;
1579a4072fe8STom Zanussi 			goto free;
1580a4072fe8STom Zanussi 		}
1581a4072fe8STom Zanussi 	}
1582a4072fe8STom Zanussi 
15837ef224d1STom Zanussi 	return attrs;
15847ef224d1STom Zanussi  free:
15857ef224d1STom Zanussi 	destroy_hist_trigger_attrs(attrs);
15867ef224d1STom Zanussi 
15877ef224d1STom Zanussi 	return ERR_PTR(ret);
15887ef224d1STom Zanussi }
15897ef224d1STom Zanussi 
save_comm(char * comm,struct task_struct * task)15906b4827adSTom Zanussi static inline void save_comm(char *comm, struct task_struct *task)
15916b4827adSTom Zanussi {
15926b4827adSTom Zanussi 	if (!task->pid) {
15936b4827adSTom Zanussi 		strcpy(comm, "<idle>");
15946b4827adSTom Zanussi 		return;
15956b4827adSTom Zanussi 	}
15966b4827adSTom Zanussi 
15976b4827adSTom Zanussi 	if (WARN_ON_ONCE(task->pid < 0)) {
15986b4827adSTom Zanussi 		strcpy(comm, "<XXX>");
15996b4827adSTom Zanussi 		return;
16006b4827adSTom Zanussi 	}
16016b4827adSTom Zanussi 
160227242c62STom Zanussi 	strncpy(comm, task->comm, TASK_COMM_LEN);
16036b4827adSTom Zanussi }
16046b4827adSTom Zanussi 
hist_elt_data_free(struct hist_elt_data * elt_data)1605af6a29bcSTom Zanussi static void hist_elt_data_free(struct hist_elt_data *elt_data)
16066b4827adSTom Zanussi {
160702205a67STom Zanussi 	unsigned int i;
160802205a67STom Zanussi 
1609c910db94STom Zanussi 	for (i = 0; i < elt_data->n_field_var_str; i++)
161002205a67STom Zanussi 		kfree(elt_data->field_var_str[i]);
161102205a67STom Zanussi 
1612c910db94STom Zanussi 	kfree(elt_data->field_var_str);
1613c910db94STom Zanussi 
1614af6a29bcSTom Zanussi 	kfree(elt_data->comm);
1615af6a29bcSTom Zanussi 	kfree(elt_data);
16166b4827adSTom Zanussi }
16176b4827adSTom Zanussi 
hist_trigger_elt_data_free(struct tracing_map_elt * elt)1618af6a29bcSTom Zanussi static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
1619af6a29bcSTom Zanussi {
1620af6a29bcSTom Zanussi 	struct hist_elt_data *elt_data = elt->private_data;
1621af6a29bcSTom Zanussi 
1622af6a29bcSTom Zanussi 	hist_elt_data_free(elt_data);
1623af6a29bcSTom Zanussi }
1624af6a29bcSTom Zanussi 
hist_trigger_elt_data_alloc(struct tracing_map_elt * elt)1625af6a29bcSTom Zanussi static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
16266b4827adSTom Zanussi {
16276b4827adSTom Zanussi 	struct hist_trigger_data *hist_data = elt->map->private_data;
1628af6a29bcSTom Zanussi 	unsigned int size = TASK_COMM_LEN;
1629af6a29bcSTom Zanussi 	struct hist_elt_data *elt_data;
1630ed2cf907SSteven Rostedt (VMware) 	struct hist_field *hist_field;
163102205a67STom Zanussi 	unsigned int i, n_str;
16326b4827adSTom Zanussi 
1633af6a29bcSTom Zanussi 	elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
1634af6a29bcSTom Zanussi 	if (!elt_data)
1635af6a29bcSTom Zanussi 		return -ENOMEM;
1636af6a29bcSTom Zanussi 
1637ed2cf907SSteven Rostedt (VMware) 	for_each_hist_field(i, hist_data) {
1638ed2cf907SSteven Rostedt (VMware) 		hist_field = hist_data->fields[i];
16396b4827adSTom Zanussi 
1640ed2cf907SSteven Rostedt (VMware) 		if (hist_field->flags & HIST_FIELD_FL_EXECNAME) {
1641af6a29bcSTom Zanussi 			elt_data->comm = kzalloc(size, GFP_KERNEL);
1642af6a29bcSTom Zanussi 			if (!elt_data->comm) {
1643af6a29bcSTom Zanussi 				kfree(elt_data);
16446b4827adSTom Zanussi 				return -ENOMEM;
1645af6a29bcSTom Zanussi 			}
16466b4827adSTom Zanussi 			break;
16476b4827adSTom Zanussi 		}
16486b4827adSTom Zanussi 	}
16496b4827adSTom Zanussi 
165063a1e5deSTom Zanussi 	n_str = hist_data->n_field_var_str + hist_data->n_save_var_str +
165163a1e5deSTom Zanussi 		hist_data->n_var_str;
165263a1e5deSTom Zanussi 	if (n_str > SYNTH_FIELDS_MAX) {
165363a1e5deSTom Zanussi 		hist_elt_data_free(elt_data);
165463a1e5deSTom Zanussi 		return -EINVAL;
165563a1e5deSTom Zanussi 	}
165602205a67STom Zanussi 
16574a4a56b4STom Zanussi 	BUILD_BUG_ON(STR_VAR_LEN_MAX & (sizeof(u64) - 1));
16584a4a56b4STom Zanussi 
165902205a67STom Zanussi 	size = STR_VAR_LEN_MAX;
166002205a67STom Zanussi 
1661c910db94STom Zanussi 	elt_data->field_var_str = kcalloc(n_str, sizeof(char *), GFP_KERNEL);
1662c910db94STom Zanussi 	if (!elt_data->field_var_str) {
1663c910db94STom Zanussi 		hist_elt_data_free(elt_data);
1664c910db94STom Zanussi 		return -EINVAL;
1665c910db94STom Zanussi 	}
1666c910db94STom Zanussi 	elt_data->n_field_var_str = n_str;
1667c910db94STom Zanussi 
166802205a67STom Zanussi 	for (i = 0; i < n_str; i++) {
166902205a67STom Zanussi 		elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
167002205a67STom Zanussi 		if (!elt_data->field_var_str[i]) {
167102205a67STom Zanussi 			hist_elt_data_free(elt_data);
167202205a67STom Zanussi 			return -ENOMEM;
167302205a67STom Zanussi 		}
167402205a67STom Zanussi 	}
167502205a67STom Zanussi 
1676af6a29bcSTom Zanussi 	elt->private_data = elt_data;
1677af6a29bcSTom Zanussi 
16786b4827adSTom Zanussi 	return 0;
16796b4827adSTom Zanussi }
16806b4827adSTom Zanussi 
hist_trigger_elt_data_init(struct tracing_map_elt * elt)1681af6a29bcSTom Zanussi static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
16826b4827adSTom Zanussi {
1683af6a29bcSTom Zanussi 	struct hist_elt_data *elt_data = elt->private_data;
16846b4827adSTom Zanussi 
1685af6a29bcSTom Zanussi 	if (elt_data->comm)
1686af6a29bcSTom Zanussi 		save_comm(elt_data->comm, current);
16876b4827adSTom Zanussi }
16886b4827adSTom Zanussi 
1689af6a29bcSTom Zanussi static const struct tracing_map_ops hist_trigger_elt_data_ops = {
1690af6a29bcSTom Zanussi 	.elt_alloc	= hist_trigger_elt_data_alloc,
1691af6a29bcSTom Zanussi 	.elt_free	= hist_trigger_elt_data_free,
1692af6a29bcSTom Zanussi 	.elt_init	= hist_trigger_elt_data_init,
16936b4827adSTom Zanussi };
16946b4827adSTom Zanussi 
get_hist_field_flags(struct hist_field * hist_field)16952ece94fbSTom Zanussi static const char *get_hist_field_flags(struct hist_field *hist_field)
16962ece94fbSTom Zanussi {
16972ece94fbSTom Zanussi 	const char *flags_str = NULL;
16982ece94fbSTom Zanussi 
16992ece94fbSTom Zanussi 	if (hist_field->flags & HIST_FIELD_FL_HEX)
17002ece94fbSTom Zanussi 		flags_str = "hex";
17012ece94fbSTom Zanussi 	else if (hist_field->flags & HIST_FIELD_FL_SYM)
17022ece94fbSTom Zanussi 		flags_str = "sym";
17032ece94fbSTom Zanussi 	else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
17042ece94fbSTom Zanussi 		flags_str = "sym-offset";
17052ece94fbSTom Zanussi 	else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
17062ece94fbSTom Zanussi 		flags_str = "execname";
17072ece94fbSTom Zanussi 	else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
17082ece94fbSTom Zanussi 		flags_str = "syscall";
17092ece94fbSTom Zanussi 	else if (hist_field->flags & HIST_FIELD_FL_LOG2)
17102ece94fbSTom Zanussi 		flags_str = "log2";
1711de9a48a3SSteven Rostedt (VMware) 	else if (hist_field->flags & HIST_FIELD_FL_BUCKET)
1712de9a48a3SSteven Rostedt (VMware) 		flags_str = "buckets";
17132ece94fbSTom Zanussi 	else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
17142ece94fbSTom Zanussi 		flags_str = "usecs";
1715abaa5258SMasami Hiramatsu (Google) 	else if (hist_field->flags & HIST_FIELD_FL_PERCENT)
1716abaa5258SMasami Hiramatsu (Google) 		flags_str = "percent";
1717a2c54256SMasami Hiramatsu (Google) 	else if (hist_field->flags & HIST_FIELD_FL_GRAPH)
1718a2c54256SMasami Hiramatsu (Google) 		flags_str = "graph";
1719cc5fc8bfSSteven Rostedt (Google) 	else if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
1720cc5fc8bfSSteven Rostedt (Google) 		flags_str = "stacktrace";
17212ece94fbSTom Zanussi 
17222ece94fbSTom Zanussi 	return flags_str;
17232ece94fbSTom Zanussi }
17242ece94fbSTom Zanussi 
expr_field_str(struct hist_field * field,char * expr)1725100719dcSTom Zanussi static void expr_field_str(struct hist_field *field, char *expr)
1726100719dcSTom Zanussi {
1727067fe038STom Zanussi 	if (field->flags & HIST_FIELD_FL_VAR_REF)
1728067fe038STom Zanussi 		strcat(expr, "$");
172952cfb373SKalesh Singh 	else if (field->flags & HIST_FIELD_FL_CONST) {
173052cfb373SKalesh Singh 		char str[HIST_CONST_DIGITS_MAX];
173152cfb373SKalesh Singh 
173252cfb373SKalesh Singh 		snprintf(str, HIST_CONST_DIGITS_MAX, "%llu", field->constant);
173352cfb373SKalesh Singh 		strcat(expr, str);
173452cfb373SKalesh Singh 	}
1735067fe038STom Zanussi 
1736100719dcSTom Zanussi 	strcat(expr, hist_field_name(field, 0));
1737100719dcSTom Zanussi 
173876690945STom Zanussi 	if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
1739100719dcSTom Zanussi 		const char *flags_str = get_hist_field_flags(field);
1740100719dcSTom Zanussi 
1741100719dcSTom Zanussi 		if (flags_str) {
1742100719dcSTom Zanussi 			strcat(expr, ".");
1743100719dcSTom Zanussi 			strcat(expr, flags_str);
1744100719dcSTom Zanussi 		}
1745100719dcSTom Zanussi 	}
1746100719dcSTom Zanussi }
1747100719dcSTom Zanussi 
expr_str(struct hist_field * field,unsigned int level)1748100719dcSTom Zanussi static char *expr_str(struct hist_field *field, unsigned int level)
1749100719dcSTom Zanussi {
1750100719dcSTom Zanussi 	char *expr;
1751100719dcSTom Zanussi 
1752100719dcSTom Zanussi 	if (level > 1)
1753100719dcSTom Zanussi 		return NULL;
1754100719dcSTom Zanussi 
1755100719dcSTom Zanussi 	expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
1756100719dcSTom Zanussi 	if (!expr)
1757100719dcSTom Zanussi 		return NULL;
1758100719dcSTom Zanussi 
1759100719dcSTom Zanussi 	if (!field->operands[0]) {
1760100719dcSTom Zanussi 		expr_field_str(field, expr);
1761100719dcSTom Zanussi 		return expr;
1762100719dcSTom Zanussi 	}
1763100719dcSTom Zanussi 
1764100719dcSTom Zanussi 	if (field->operator == FIELD_OP_UNARY_MINUS) {
1765100719dcSTom Zanussi 		char *subexpr;
1766100719dcSTom Zanussi 
1767100719dcSTom Zanussi 		strcat(expr, "-(");
1768100719dcSTom Zanussi 		subexpr = expr_str(field->operands[0], ++level);
1769100719dcSTom Zanussi 		if (!subexpr) {
1770100719dcSTom Zanussi 			kfree(expr);
1771100719dcSTom Zanussi 			return NULL;
1772100719dcSTom Zanussi 		}
1773100719dcSTom Zanussi 		strcat(expr, subexpr);
1774100719dcSTom Zanussi 		strcat(expr, ")");
1775100719dcSTom Zanussi 
1776100719dcSTom Zanussi 		kfree(subexpr);
1777100719dcSTom Zanussi 
1778100719dcSTom Zanussi 		return expr;
1779100719dcSTom Zanussi 	}
1780100719dcSTom Zanussi 
1781100719dcSTom Zanussi 	expr_field_str(field->operands[0], expr);
1782100719dcSTom Zanussi 
1783100719dcSTom Zanussi 	switch (field->operator) {
1784100719dcSTom Zanussi 	case FIELD_OP_MINUS:
1785100719dcSTom Zanussi 		strcat(expr, "-");
1786100719dcSTom Zanussi 		break;
1787100719dcSTom Zanussi 	case FIELD_OP_PLUS:
1788100719dcSTom Zanussi 		strcat(expr, "+");
1789100719dcSTom Zanussi 		break;
1790bcef0441SKalesh Singh 	case FIELD_OP_DIV:
1791bcef0441SKalesh Singh 		strcat(expr, "/");
1792bcef0441SKalesh Singh 		break;
1793bcef0441SKalesh Singh 	case FIELD_OP_MULT:
1794bcef0441SKalesh Singh 		strcat(expr, "*");
1795bcef0441SKalesh Singh 		break;
1796100719dcSTom Zanussi 	default:
1797100719dcSTom Zanussi 		kfree(expr);
1798100719dcSTom Zanussi 		return NULL;
1799100719dcSTom Zanussi 	}
1800100719dcSTom Zanussi 
1801100719dcSTom Zanussi 	expr_field_str(field->operands[1], expr);
1802100719dcSTom Zanussi 
1803100719dcSTom Zanussi 	return expr;
1804100719dcSTom Zanussi }
1805100719dcSTom Zanussi 
18069710b2f3SKalesh Singh /*
18079710b2f3SKalesh Singh  * If field_op != FIELD_OP_NONE, *sep points to the root operator
18089710b2f3SKalesh Singh  * of the expression tree to be evaluated.
18099710b2f3SKalesh Singh  */
contains_operator(char * str,char ** sep)18109710b2f3SKalesh Singh static int contains_operator(char *str, char **sep)
1811100719dcSTom Zanussi {
1812100719dcSTom Zanussi 	enum field_op_id field_op = FIELD_OP_NONE;
18139710b2f3SKalesh Singh 	char *minus_op, *plus_op, *div_op, *mult_op;
1814100719dcSTom Zanussi 
1815100719dcSTom Zanussi 
181626c56373SSteven Rostedt (VMware) 	/*
18179710b2f3SKalesh Singh 	 * Report the last occurrence of the operators first, so that the
18189710b2f3SKalesh Singh 	 * expression is evaluated left to right. This is important since
18199710b2f3SKalesh Singh 	 * subtraction and division are not associative.
18209710b2f3SKalesh Singh 	 *
18219710b2f3SKalesh Singh 	 *	e.g
18229710b2f3SKalesh Singh 	 *		64/8/4/2 is 1, i.e 64/8/4/2 = ((64/8)/4)/2
18239710b2f3SKalesh Singh 	 *		14-7-5-2 is 0, i.e 14-7-5-2 = ((14-7)-5)-2
182426c56373SSteven Rostedt (VMware) 	 */
182526c56373SSteven Rostedt (VMware) 
18269710b2f3SKalesh Singh 	/*
18279710b2f3SKalesh Singh 	 * First, find lower precedence addition and subtraction
18289710b2f3SKalesh Singh 	 * since the expression will be evaluated recursively.
18299710b2f3SKalesh Singh 	 */
18309710b2f3SKalesh Singh 	minus_op = strrchr(str, '-');
18319710b2f3SKalesh Singh 	if (minus_op) {
18329710b2f3SKalesh Singh 		/*
18339710b2f3SKalesh Singh 		 * Unary minus is not supported in sub-expressions. If
18349710b2f3SKalesh Singh 		 * present, it is always the next root operator.
18359710b2f3SKalesh Singh 		 */
18369710b2f3SKalesh Singh 		if (minus_op == str) {
1837100719dcSTom Zanussi 			field_op = FIELD_OP_UNARY_MINUS;
18389710b2f3SKalesh Singh 			goto out;
18399710b2f3SKalesh Singh 		}
18409710b2f3SKalesh Singh 
1841100719dcSTom Zanussi 		field_op = FIELD_OP_MINUS;
18429710b2f3SKalesh Singh 	}
18439710b2f3SKalesh Singh 
18449710b2f3SKalesh Singh 	plus_op = strrchr(str, '+');
18459710b2f3SKalesh Singh 	if (plus_op || minus_op) {
18469710b2f3SKalesh Singh 		/*
18479710b2f3SKalesh Singh 		 * For operators of the same precedence use to rightmost as the
18489710b2f3SKalesh Singh 		 * root, so that the expression is evaluated left to right.
18499710b2f3SKalesh Singh 		 */
18509710b2f3SKalesh Singh 		if (plus_op > minus_op)
1851100719dcSTom Zanussi 			field_op = FIELD_OP_PLUS;
18529710b2f3SKalesh Singh 		goto out;
18539710b2f3SKalesh Singh 	}
18549710b2f3SKalesh Singh 
18559710b2f3SKalesh Singh 	/*
18569710b2f3SKalesh Singh 	 * Multiplication and division have higher precedence than addition and
18579710b2f3SKalesh Singh 	 * subtraction.
18589710b2f3SKalesh Singh 	 */
18599710b2f3SKalesh Singh 	div_op = strrchr(str, '/');
18609710b2f3SKalesh Singh 	if (div_op)
1861bcef0441SKalesh Singh 		field_op = FIELD_OP_DIV;
18629710b2f3SKalesh Singh 
18639710b2f3SKalesh Singh 	mult_op = strrchr(str, '*');
18649710b2f3SKalesh Singh 	/*
18659710b2f3SKalesh Singh 	 * For operators of the same precedence use to rightmost as the
18669710b2f3SKalesh Singh 	 * root, so that the expression is evaluated left to right.
18679710b2f3SKalesh Singh 	 */
18689710b2f3SKalesh Singh 	if (mult_op > div_op)
1869bcef0441SKalesh Singh 		field_op = FIELD_OP_MULT;
18709710b2f3SKalesh Singh 
18719710b2f3SKalesh Singh out:
18729710b2f3SKalesh Singh 	if (sep) {
18739710b2f3SKalesh Singh 		switch (field_op) {
18749710b2f3SKalesh Singh 		case FIELD_OP_UNARY_MINUS:
18759710b2f3SKalesh Singh 		case FIELD_OP_MINUS:
18769710b2f3SKalesh Singh 			*sep = minus_op;
1877100719dcSTom Zanussi 			break;
18789710b2f3SKalesh Singh 		case FIELD_OP_PLUS:
18799710b2f3SKalesh Singh 			*sep = plus_op;
18809710b2f3SKalesh Singh 			break;
18819710b2f3SKalesh Singh 		case FIELD_OP_DIV:
18829710b2f3SKalesh Singh 			*sep = div_op;
18839710b2f3SKalesh Singh 			break;
18849710b2f3SKalesh Singh 		case FIELD_OP_MULT:
18859710b2f3SKalesh Singh 			*sep = mult_op;
18869710b2f3SKalesh Singh 			break;
18879710b2f3SKalesh Singh 		case FIELD_OP_NONE:
1888100719dcSTom Zanussi 		default:
18899710b2f3SKalesh Singh 			*sep = NULL;
1890100719dcSTom Zanussi 			break;
1891100719dcSTom Zanussi 		}
18929710b2f3SKalesh Singh 	}
1893100719dcSTom Zanussi 
1894100719dcSTom Zanussi 	return field_op;
1895100719dcSTom Zanussi }
1896100719dcSTom Zanussi 
get_hist_field(struct hist_field * hist_field)18978bcebc77SSteven Rostedt (VMware) static void get_hist_field(struct hist_field *hist_field)
18988bcebc77SSteven Rostedt (VMware) {
18998bcebc77SSteven Rostedt (VMware) 	hist_field->ref++;
19008bcebc77SSteven Rostedt (VMware) }
19018bcebc77SSteven Rostedt (VMware) 
__destroy_hist_field(struct hist_field * hist_field)1902656fe2baSTom Zanussi static void __destroy_hist_field(struct hist_field *hist_field)
1903656fe2baSTom Zanussi {
19048bcebc77SSteven Rostedt (VMware) 	if (--hist_field->ref > 1)
19058bcebc77SSteven Rostedt (VMware) 		return;
19068bcebc77SSteven Rostedt (VMware) 
1907656fe2baSTom Zanussi 	kfree(hist_field->var.name);
1908656fe2baSTom Zanussi 	kfree(hist_field->name);
19093347d80bSSteven Rostedt (VMware) 
19103347d80bSSteven Rostedt (VMware) 	/* Can likely be a const */
19113347d80bSSteven Rostedt (VMware) 	kfree_const(hist_field->type);
1912656fe2baSTom Zanussi 
19139da73974SVamshi K Sthambamkadi 	kfree(hist_field->system);
19149da73974SVamshi K Sthambamkadi 	kfree(hist_field->event_name);
19159da73974SVamshi K Sthambamkadi 
1916656fe2baSTom Zanussi 	kfree(hist_field);
1917656fe2baSTom Zanussi }
1918656fe2baSTom Zanussi 
destroy_hist_field(struct hist_field * hist_field,unsigned int level)19195819eaddSTom Zanussi static void destroy_hist_field(struct hist_field *hist_field,
19205819eaddSTom Zanussi 			       unsigned int level)
19217ef224d1STom Zanussi {
19225819eaddSTom Zanussi 	unsigned int i;
19235819eaddSTom Zanussi 
1924100719dcSTom Zanussi 	if (level > 3)
19255819eaddSTom Zanussi 		return;
19265819eaddSTom Zanussi 
19275819eaddSTom Zanussi 	if (!hist_field)
19285819eaddSTom Zanussi 		return;
19295819eaddSTom Zanussi 
1930656fe2baSTom Zanussi 	if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
1931656fe2baSTom Zanussi 		return; /* var refs will be destroyed separately */
1932656fe2baSTom Zanussi 
19335819eaddSTom Zanussi 	for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
19345819eaddSTom Zanussi 		destroy_hist_field(hist_field->operands[i], level + 1);
19355819eaddSTom Zanussi 
1936656fe2baSTom Zanussi 	__destroy_hist_field(hist_field);
19377ef224d1STom Zanussi }
19387ef224d1STom Zanussi 
create_hist_field(struct hist_trigger_data * hist_data,struct ftrace_event_field * field,unsigned long flags,char * var_name)1939b559d003STom Zanussi static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
1940b559d003STom Zanussi 					    struct ftrace_event_field *field,
194130350d65STom Zanussi 					    unsigned long flags,
194230350d65STom Zanussi 					    char *var_name)
19437ef224d1STom Zanussi {
19447ef224d1STom Zanussi 	struct hist_field *hist_field;
19457ef224d1STom Zanussi 
19467ef224d1STom Zanussi 	if (field && is_function_field(field))
19477ef224d1STom Zanussi 		return NULL;
19487ef224d1STom Zanussi 
19497ef224d1STom Zanussi 	hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
19507ef224d1STom Zanussi 	if (!hist_field)
19517ef224d1STom Zanussi 		return NULL;
19527ef224d1STom Zanussi 
19538bcebc77SSteven Rostedt (VMware) 	hist_field->ref = 1;
19548bcebc77SSteven Rostedt (VMware) 
1955b559d003STom Zanussi 	hist_field->hist_data = hist_data;
1956b559d003STom Zanussi 
19577e8b88a3STom Zanussi 	if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
1958100719dcSTom Zanussi 		goto out; /* caller will populate */
1959100719dcSTom Zanussi 
1960067fe038STom Zanussi 	if (flags & HIST_FIELD_FL_VAR_REF) {
196186087383SSteven Rostedt (Google) 		hist_field->fn_num = HIST_FIELD_FN_VAR_REF;
1962067fe038STom Zanussi 		goto out;
1963067fe038STom Zanussi 	}
1964067fe038STom Zanussi 
19657ef224d1STom Zanussi 	if (flags & HIST_FIELD_FL_HITCOUNT) {
196686087383SSteven Rostedt (Google) 		hist_field->fn_num = HIST_FIELD_FN_COUNTER;
196719a9facdSTom Zanussi 		hist_field->size = sizeof(u64);
19683347d80bSSteven Rostedt (VMware) 		hist_field->type = "u64";
19697ef224d1STom Zanussi 		goto out;
19707ef224d1STom Zanussi 	}
19717ef224d1STom Zanussi 
197252cfb373SKalesh Singh 	if (flags & HIST_FIELD_FL_CONST) {
197386087383SSteven Rostedt (Google) 		hist_field->fn_num = HIST_FIELD_FN_CONST;
197452cfb373SKalesh Singh 		hist_field->size = sizeof(u64);
197552cfb373SKalesh Singh 		hist_field->type = kstrdup("u64", GFP_KERNEL);
197652cfb373SKalesh Singh 		if (!hist_field->type)
197752cfb373SKalesh Singh 			goto free;
197852cfb373SKalesh Singh 		goto out;
197952cfb373SKalesh Singh 	}
198052cfb373SKalesh Singh 
198169a0200cSTom Zanussi 	if (flags & HIST_FIELD_FL_STACKTRACE) {
1982f5914b30STom Zanussi 		if (field)
1983f5914b30STom Zanussi 			hist_field->fn_num = HIST_FIELD_FN_STACK;
1984f5914b30STom Zanussi 		else
198586087383SSteven Rostedt (Google) 			hist_field->fn_num = HIST_FIELD_FN_NOP;
1986288709c9SSteven Rostedt (Google) 		hist_field->size = HIST_STACKTRACE_SIZE;
1987288709c9SSteven Rostedt (Google) 		hist_field->type = kstrdup_const("unsigned long[]", GFP_KERNEL);
1988288709c9SSteven Rostedt (Google) 		if (!hist_field->type)
1989288709c9SSteven Rostedt (Google) 			goto free;
199069a0200cSTom Zanussi 		goto out;
199169a0200cSTom Zanussi 	}
199269a0200cSTom Zanussi 
1993de9a48a3SSteven Rostedt (VMware) 	if (flags & (HIST_FIELD_FL_LOG2 | HIST_FIELD_FL_BUCKET)) {
1994de9a48a3SSteven Rostedt (VMware) 		unsigned long fl = flags & ~(HIST_FIELD_FL_LOG2 | HIST_FIELD_FL_BUCKET);
199586087383SSteven Rostedt (Google) 		hist_field->fn_num = flags & HIST_FIELD_FL_LOG2 ? HIST_FIELD_FN_LOG2 :
199686087383SSteven Rostedt (Google) 			HIST_FIELD_FN_BUCKET;
199730350d65STom Zanussi 		hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
19988b152e91SNatalia Petrova 		if (!hist_field->operands[0])
19998b152e91SNatalia Petrova 			goto free;
20005819eaddSTom Zanussi 		hist_field->size = hist_field->operands[0]->size;
20013347d80bSSteven Rostedt (VMware) 		hist_field->type = kstrdup_const(hist_field->operands[0]->type, GFP_KERNEL);
200219a9facdSTom Zanussi 		if (!hist_field->type)
200319a9facdSTom Zanussi 			goto free;
20044b94f5b7SNamhyung Kim 		goto out;
20054b94f5b7SNamhyung Kim 	}
20064b94f5b7SNamhyung Kim 
2007ad42febeSTom Zanussi 	if (flags & HIST_FIELD_FL_TIMESTAMP) {
200886087383SSteven Rostedt (Google) 		hist_field->fn_num = HIST_FIELD_FN_TIMESTAMP;
2009ad42febeSTom Zanussi 		hist_field->size = sizeof(u64);
20103347d80bSSteven Rostedt (VMware) 		hist_field->type = "u64";
2011ad42febeSTom Zanussi 		goto out;
2012ad42febeSTom Zanussi 	}
2013ad42febeSTom Zanussi 
20148b7622bfSTom Zanussi 	if (flags & HIST_FIELD_FL_CPU) {
201586087383SSteven Rostedt (Google) 		hist_field->fn_num = HIST_FIELD_FN_CPU;
20168b7622bfSTom Zanussi 		hist_field->size = sizeof(int);
20173347d80bSSteven Rostedt (VMware) 		hist_field->type = "unsigned int";
20188b7622bfSTom Zanussi 		goto out;
20198b7622bfSTom Zanussi 	}
20208b7622bfSTom Zanussi 
2021432480c5STom Zanussi 	if (WARN_ON_ONCE(!field))
2022432480c5STom Zanussi 		goto out;
2023432480c5STom Zanussi 
2024704adfb5SSteven Rostedt (VMware) 	/* Pointers to strings are just pointers and dangerous to dereference */
2025704adfb5SSteven Rostedt (VMware) 	if (is_string_field(field) &&
2026704adfb5SSteven Rostedt (VMware) 	    (field->filter_type != FILTER_PTR_STRING)) {
20277ef224d1STom Zanussi 		flags |= HIST_FIELD_FL_STRING;
202879e577cbSNamhyung Kim 
202919a9facdSTom Zanussi 		hist_field->size = MAX_FILTER_STR_VAL;
20303347d80bSSteven Rostedt (VMware) 		hist_field->type = kstrdup_const(field->type, GFP_KERNEL);
203119a9facdSTom Zanussi 		if (!hist_field->type)
203219a9facdSTom Zanussi 			goto free;
203319a9facdSTom Zanussi 
203463f84ae6SMasami Hiramatsu 		if (field->filter_type == FILTER_STATIC_STRING) {
203586087383SSteven Rostedt (Google) 			hist_field->fn_num = HIST_FIELD_FN_STRING;
203663f84ae6SMasami Hiramatsu 			hist_field->size = field->size;
203705770dd0SMasami Hiramatsu 		} else if (field->filter_type == FILTER_DYN_STRING) {
203886087383SSteven Rostedt (Google) 			hist_field->fn_num = HIST_FIELD_FN_DYNSTRING;
203905770dd0SMasami Hiramatsu 		} else if (field->filter_type == FILTER_RDYN_STRING)
204086087383SSteven Rostedt (Google) 			hist_field->fn_num = HIST_FIELD_FN_RELDYNSTRING;
204179e577cbSNamhyung Kim 		else
204286087383SSteven Rostedt (Google) 			hist_field->fn_num = HIST_FIELD_FN_PSTRING;
20437ef224d1STom Zanussi 	} else {
204419a9facdSTom Zanussi 		hist_field->size = field->size;
204519a9facdSTom Zanussi 		hist_field->is_signed = field->is_signed;
20463347d80bSSteven Rostedt (VMware) 		hist_field->type = kstrdup_const(field->type, GFP_KERNEL);
204719a9facdSTom Zanussi 		if (!hist_field->type)
204819a9facdSTom Zanussi 			goto free;
204919a9facdSTom Zanussi 
205086087383SSteven Rostedt (Google) 		hist_field->fn_num = select_value_fn(field->size,
20517ef224d1STom Zanussi 						     field->is_signed);
205286087383SSteven Rostedt (Google) 		if (hist_field->fn_num == HIST_FIELD_FN_NOP) {
20535819eaddSTom Zanussi 			destroy_hist_field(hist_field, 0);
20547ef224d1STom Zanussi 			return NULL;
20557ef224d1STom Zanussi 		}
20567ef224d1STom Zanussi 	}
20577ef224d1STom Zanussi  out:
20587ef224d1STom Zanussi 	hist_field->field = field;
20597ef224d1STom Zanussi 	hist_field->flags = flags;
20607ef224d1STom Zanussi 
206130350d65STom Zanussi 	if (var_name) {
206230350d65STom Zanussi 		hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
206330350d65STom Zanussi 		if (!hist_field->var.name)
206430350d65STom Zanussi 			goto free;
206530350d65STom Zanussi 	}
206630350d65STom Zanussi 
20677ef224d1STom Zanussi 	return hist_field;
206830350d65STom Zanussi  free:
206930350d65STom Zanussi 	destroy_hist_field(hist_field, 0);
207030350d65STom Zanussi 	return NULL;
20717ef224d1STom Zanussi }
20727ef224d1STom Zanussi 
destroy_hist_fields(struct hist_trigger_data * hist_data)20737ef224d1STom Zanussi static void destroy_hist_fields(struct hist_trigger_data *hist_data)
20747ef224d1STom Zanussi {
20757ef224d1STom Zanussi 	unsigned int i;
20767ef224d1STom Zanussi 
207730350d65STom Zanussi 	for (i = 0; i < HIST_FIELDS_MAX; i++) {
20787ef224d1STom Zanussi 		if (hist_data->fields[i]) {
20795819eaddSTom Zanussi 			destroy_hist_field(hist_data->fields[i], 0);
20807ef224d1STom Zanussi 			hist_data->fields[i] = NULL;
20817ef224d1STom Zanussi 		}
20827ef224d1STom Zanussi 	}
2083656fe2baSTom Zanussi 
2084656fe2baSTom Zanussi 	for (i = 0; i < hist_data->n_var_refs; i++) {
2085656fe2baSTom Zanussi 		WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
2086656fe2baSTom Zanussi 		__destroy_hist_field(hist_data->var_refs[i]);
2087656fe2baSTom Zanussi 		hist_data->var_refs[i] = NULL;
2088656fe2baSTom Zanussi 	}
20897ef224d1STom Zanussi }
20907ef224d1STom Zanussi 
init_var_ref(struct hist_field * ref_field,struct hist_field * var_field,char * system,char * event_name)2091067fe038STom Zanussi static int init_var_ref(struct hist_field *ref_field,
2092067fe038STom Zanussi 			struct hist_field *var_field,
2093067fe038STom Zanussi 			char *system, char *event_name)
2094067fe038STom Zanussi {
2095067fe038STom Zanussi 	int err = 0;
2096067fe038STom Zanussi 
2097067fe038STom Zanussi 	ref_field->var.idx = var_field->var.idx;
2098067fe038STom Zanussi 	ref_field->var.hist_data = var_field->hist_data;
2099067fe038STom Zanussi 	ref_field->size = var_field->size;
2100067fe038STom Zanussi 	ref_field->is_signed = var_field->is_signed;
2101067fe038STom Zanussi 	ref_field->flags |= var_field->flags &
2102067fe038STom Zanussi 		(HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2103067fe038STom Zanussi 
2104067fe038STom Zanussi 	if (system) {
2105067fe038STom Zanussi 		ref_field->system = kstrdup(system, GFP_KERNEL);
2106067fe038STom Zanussi 		if (!ref_field->system)
2107067fe038STom Zanussi 			return -ENOMEM;
2108067fe038STom Zanussi 	}
2109067fe038STom Zanussi 
2110067fe038STom Zanussi 	if (event_name) {
2111067fe038STom Zanussi 		ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
2112067fe038STom Zanussi 		if (!ref_field->event_name) {
2113067fe038STom Zanussi 			err = -ENOMEM;
2114067fe038STom Zanussi 			goto free;
2115067fe038STom Zanussi 		}
2116067fe038STom Zanussi 	}
2117067fe038STom Zanussi 
21187e8b88a3STom Zanussi 	if (var_field->var.name) {
2119067fe038STom Zanussi 		ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
2120067fe038STom Zanussi 		if (!ref_field->name) {
2121067fe038STom Zanussi 			err = -ENOMEM;
2122067fe038STom Zanussi 			goto free;
2123067fe038STom Zanussi 		}
21247e8b88a3STom Zanussi 	} else if (var_field->name) {
21257e8b88a3STom Zanussi 		ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
21267e8b88a3STom Zanussi 		if (!ref_field->name) {
21277e8b88a3STom Zanussi 			err = -ENOMEM;
21287e8b88a3STom Zanussi 			goto free;
21297e8b88a3STom Zanussi 		}
21307e8b88a3STom Zanussi 	}
2131067fe038STom Zanussi 
21323347d80bSSteven Rostedt (VMware) 	ref_field->type = kstrdup_const(var_field->type, GFP_KERNEL);
2133067fe038STom Zanussi 	if (!ref_field->type) {
2134067fe038STom Zanussi 		err = -ENOMEM;
2135067fe038STom Zanussi 		goto free;
2136067fe038STom Zanussi 	}
2137067fe038STom Zanussi  out:
2138067fe038STom Zanussi 	return err;
2139067fe038STom Zanussi  free:
2140067fe038STom Zanussi 	kfree(ref_field->system);
214199696a25SKeita Suzuki 	ref_field->system = NULL;
2142067fe038STom Zanussi 	kfree(ref_field->event_name);
214399696a25SKeita Suzuki 	ref_field->event_name = NULL;
2144067fe038STom Zanussi 	kfree(ref_field->name);
214599696a25SKeita Suzuki 	ref_field->name = NULL;
2146067fe038STom Zanussi 
2147067fe038STom Zanussi 	goto out;
2148067fe038STom Zanussi }
2149067fe038STom Zanussi 
find_var_ref_idx(struct hist_trigger_data * hist_data,struct hist_field * var_field)2150d380dcdeSTom Zanussi static int find_var_ref_idx(struct hist_trigger_data *hist_data,
2151d380dcdeSTom Zanussi 			    struct hist_field *var_field)
2152d380dcdeSTom Zanussi {
2153d380dcdeSTom Zanussi 	struct hist_field *ref_field;
2154d380dcdeSTom Zanussi 	int i;
2155d380dcdeSTom Zanussi 
2156d380dcdeSTom Zanussi 	for (i = 0; i < hist_data->n_var_refs; i++) {
2157d380dcdeSTom Zanussi 		ref_field = hist_data->var_refs[i];
2158d380dcdeSTom Zanussi 		if (ref_field->var.idx == var_field->var.idx &&
2159d380dcdeSTom Zanussi 		    ref_field->var.hist_data == var_field->hist_data)
2160d380dcdeSTom Zanussi 			return i;
2161d380dcdeSTom Zanussi 	}
2162d380dcdeSTom Zanussi 
2163d380dcdeSTom Zanussi 	return -ENOENT;
2164d380dcdeSTom Zanussi }
2165d380dcdeSTom Zanussi 
2166de40f033STom Zanussi /**
2167de40f033STom Zanussi  * create_var_ref - Create a variable reference and attach it to trigger
2168de40f033STom Zanussi  * @hist_data: The trigger that will be referencing the variable
2169de40f033STom Zanussi  * @var_field: The VAR field to create a reference to
2170de40f033STom Zanussi  * @system: The optional system string
2171de40f033STom Zanussi  * @event_name: The optional event_name string
2172de40f033STom Zanussi  *
2173de40f033STom Zanussi  * Given a variable hist_field, create a VAR_REF hist_field that
2174de40f033STom Zanussi  * represents a reference to it.
2175de40f033STom Zanussi  *
2176de40f033STom Zanussi  * This function also adds the reference to the trigger that
2177de40f033STom Zanussi  * now references the variable.
2178de40f033STom Zanussi  *
2179de40f033STom Zanussi  * Return: The VAR_REF field if successful, NULL if not
2180de40f033STom Zanussi  */
create_var_ref(struct hist_trigger_data * hist_data,struct hist_field * var_field,char * system,char * event_name)2181de40f033STom Zanussi static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
2182de40f033STom Zanussi 					 struct hist_field *var_field,
2183067fe038STom Zanussi 					 char *system, char *event_name)
2184067fe038STom Zanussi {
2185067fe038STom Zanussi 	unsigned long flags = HIST_FIELD_FL_VAR_REF;
2186067fe038STom Zanussi 	struct hist_field *ref_field;
21878bcebc77SSteven Rostedt (VMware) 	int i;
21888bcebc77SSteven Rostedt (VMware) 
21898bcebc77SSteven Rostedt (VMware) 	/* Check if the variable already exists */
21908bcebc77SSteven Rostedt (VMware) 	for (i = 0; i < hist_data->n_var_refs; i++) {
21918bcebc77SSteven Rostedt (VMware) 		ref_field = hist_data->var_refs[i];
21928bcebc77SSteven Rostedt (VMware) 		if (ref_field->var.idx == var_field->var.idx &&
21938bcebc77SSteven Rostedt (VMware) 		    ref_field->var.hist_data == var_field->hist_data) {
21948bcebc77SSteven Rostedt (VMware) 			get_hist_field(ref_field);
21958bcebc77SSteven Rostedt (VMware) 			return ref_field;
21968bcebc77SSteven Rostedt (VMware) 		}
21978bcebc77SSteven Rostedt (VMware) 	}
219882470f7dSZheng Yejian 	/* Sanity check to avoid out-of-bound write on 'hist_data->var_refs' */
219982470f7dSZheng Yejian 	if (hist_data->n_var_refs >= TRACING_MAP_VARS_MAX)
220082470f7dSZheng Yejian 		return NULL;
2201067fe038STom Zanussi 	ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
2202067fe038STom Zanussi 	if (ref_field) {
2203067fe038STom Zanussi 		if (init_var_ref(ref_field, var_field, system, event_name)) {
2204067fe038STom Zanussi 			destroy_hist_field(ref_field, 0);
2205067fe038STom Zanussi 			return NULL;
2206067fe038STom Zanussi 		}
2207de40f033STom Zanussi 
2208de40f033STom Zanussi 		hist_data->var_refs[hist_data->n_var_refs] = ref_field;
2209de40f033STom Zanussi 		ref_field->var_ref_idx = hist_data->n_var_refs++;
2210067fe038STom Zanussi 	}
2211067fe038STom Zanussi 
2212067fe038STom Zanussi 	return ref_field;
2213067fe038STom Zanussi }
2214067fe038STom Zanussi 
is_var_ref(char * var_name)2215067fe038STom Zanussi static bool is_var_ref(char *var_name)
2216067fe038STom Zanussi {
2217067fe038STom Zanussi 	if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
2218067fe038STom Zanussi 		return false;
2219067fe038STom Zanussi 
2220067fe038STom Zanussi 	return true;
2221067fe038STom Zanussi }
2222067fe038STom Zanussi 
field_name_from_var(struct hist_trigger_data * hist_data,char * var_name)2223067fe038STom Zanussi static char *field_name_from_var(struct hist_trigger_data *hist_data,
2224067fe038STom Zanussi 				 char *var_name)
2225067fe038STom Zanussi {
2226067fe038STom Zanussi 	char *name, *field;
2227067fe038STom Zanussi 	unsigned int i;
2228067fe038STom Zanussi 
2229067fe038STom Zanussi 	for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
2230067fe038STom Zanussi 		name = hist_data->attrs->var_defs.name[i];
2231067fe038STom Zanussi 
2232067fe038STom Zanussi 		if (strcmp(var_name, name) == 0) {
2233067fe038STom Zanussi 			field = hist_data->attrs->var_defs.expr[i];
22349710b2f3SKalesh Singh 			if (contains_operator(field, NULL) || is_var_ref(field))
2235067fe038STom Zanussi 				continue;
2236067fe038STom Zanussi 			return field;
2237067fe038STom Zanussi 		}
2238067fe038STom Zanussi 	}
2239067fe038STom Zanussi 
2240067fe038STom Zanussi 	return NULL;
2241067fe038STom Zanussi }
2242067fe038STom Zanussi 
local_field_var_ref(struct hist_trigger_data * hist_data,char * system,char * event_name,char * var_name)2243067fe038STom Zanussi static char *local_field_var_ref(struct hist_trigger_data *hist_data,
2244067fe038STom Zanussi 				 char *system, char *event_name,
2245067fe038STom Zanussi 				 char *var_name)
2246067fe038STom Zanussi {
2247067fe038STom Zanussi 	struct trace_event_call *call;
2248067fe038STom Zanussi 
2249067fe038STom Zanussi 	if (system && event_name) {
2250067fe038STom Zanussi 		call = hist_data->event_file->event_call;
2251067fe038STom Zanussi 
2252067fe038STom Zanussi 		if (strcmp(system, call->class->system) != 0)
2253067fe038STom Zanussi 			return NULL;
2254067fe038STom Zanussi 
2255067fe038STom Zanussi 		if (strcmp(event_name, trace_event_name(call)) != 0)
2256067fe038STom Zanussi 			return NULL;
2257067fe038STom Zanussi 	}
2258067fe038STom Zanussi 
2259067fe038STom Zanussi 	if (!!system != !!event_name)
2260067fe038STom Zanussi 		return NULL;
2261067fe038STom Zanussi 
2262067fe038STom Zanussi 	if (!is_var_ref(var_name))
2263067fe038STom Zanussi 		return NULL;
2264067fe038STom Zanussi 
2265067fe038STom Zanussi 	var_name++;
2266067fe038STom Zanussi 
2267067fe038STom Zanussi 	return field_name_from_var(hist_data, var_name);
2268067fe038STom Zanussi }
2269067fe038STom Zanussi 
parse_var_ref(struct hist_trigger_data * hist_data,char * system,char * event_name,char * var_name)2270067fe038STom Zanussi static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
2271067fe038STom Zanussi 					char *system, char *event_name,
2272067fe038STom Zanussi 					char *var_name)
2273067fe038STom Zanussi {
2274067fe038STom Zanussi 	struct hist_field *var_field = NULL, *ref_field = NULL;
2275d0cd871bSSteven Rostedt (VMware) 	struct trace_array *tr = hist_data->event_file->tr;
2276067fe038STom Zanussi 
2277067fe038STom Zanussi 	if (!is_var_ref(var_name))
2278067fe038STom Zanussi 		return NULL;
2279067fe038STom Zanussi 
2280067fe038STom Zanussi 	var_name++;
2281067fe038STom Zanussi 
2282067fe038STom Zanussi 	var_field = find_event_var(hist_data, system, event_name, var_name);
2283067fe038STom Zanussi 	if (var_field)
2284de40f033STom Zanussi 		ref_field = create_var_ref(hist_data, var_field,
2285de40f033STom Zanussi 					   system, event_name);
2286067fe038STom Zanussi 
2287f404da6eSTom Zanussi 	if (!ref_field)
2288d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name));
2289f404da6eSTom Zanussi 
2290067fe038STom Zanussi 	return ref_field;
2291067fe038STom Zanussi }
2292067fe038STom Zanussi 
2293100719dcSTom Zanussi static struct ftrace_event_field *
parse_field(struct hist_trigger_data * hist_data,struct trace_event_file * file,char * field_str,unsigned long * flags,unsigned long * buckets)2294100719dcSTom Zanussi parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
2295de9a48a3SSteven Rostedt (VMware) 	    char *field_str, unsigned long *flags, unsigned long *buckets)
2296100719dcSTom Zanussi {
2297100719dcSTom Zanussi 	struct ftrace_event_field *field = NULL;
2298100719dcSTom Zanussi 	char *field_name, *modifier, *str;
2299d0cd871bSSteven Rostedt (VMware) 	struct trace_array *tr = file->tr;
2300100719dcSTom Zanussi 
2301100719dcSTom Zanussi 	modifier = str = kstrdup(field_str, GFP_KERNEL);
2302100719dcSTom Zanussi 	if (!modifier)
2303100719dcSTom Zanussi 		return ERR_PTR(-ENOMEM);
2304100719dcSTom Zanussi 
2305100719dcSTom Zanussi 	field_name = strsep(&modifier, ".");
2306100719dcSTom Zanussi 	if (modifier) {
2307100719dcSTom Zanussi 		if (strcmp(modifier, "hex") == 0)
2308100719dcSTom Zanussi 			*flags |= HIST_FIELD_FL_HEX;
2309100719dcSTom Zanussi 		else if (strcmp(modifier, "sym") == 0)
2310100719dcSTom Zanussi 			*flags |= HIST_FIELD_FL_SYM;
2311c5eac6eeSKalesh Singh 		/*
2312c5eac6eeSKalesh Singh 		 * 'sym-offset' occurrences in the trigger string are modified
2313c5eac6eeSKalesh Singh 		 * to 'symXoffset' to simplify arithmetic expression parsing.
2314c5eac6eeSKalesh Singh 		 */
2315c5eac6eeSKalesh Singh 		else if (strcmp(modifier, "symXoffset") == 0)
2316100719dcSTom Zanussi 			*flags |= HIST_FIELD_FL_SYM_OFFSET;
2317100719dcSTom Zanussi 		else if ((strcmp(modifier, "execname") == 0) &&
2318100719dcSTom Zanussi 			 (strcmp(field_name, "common_pid") == 0))
2319100719dcSTom Zanussi 			*flags |= HIST_FIELD_FL_EXECNAME;
2320100719dcSTom Zanussi 		else if (strcmp(modifier, "syscall") == 0)
2321100719dcSTom Zanussi 			*flags |= HIST_FIELD_FL_SYSCALL;
2322cc5fc8bfSSteven Rostedt (Google) 		else if (strcmp(modifier, "stacktrace") == 0)
2323cc5fc8bfSSteven Rostedt (Google) 			*flags |= HIST_FIELD_FL_STACKTRACE;
2324100719dcSTom Zanussi 		else if (strcmp(modifier, "log2") == 0)
2325100719dcSTom Zanussi 			*flags |= HIST_FIELD_FL_LOG2;
2326100719dcSTom Zanussi 		else if (strcmp(modifier, "usecs") == 0)
2327100719dcSTom Zanussi 			*flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
2328de9a48a3SSteven Rostedt (VMware) 		else if (strncmp(modifier, "bucket", 6) == 0) {
2329de9a48a3SSteven Rostedt (VMware) 			int ret;
2330de9a48a3SSteven Rostedt (VMware) 
2331de9a48a3SSteven Rostedt (VMware) 			modifier += 6;
2332de9a48a3SSteven Rostedt (VMware) 
2333de9a48a3SSteven Rostedt (VMware) 			if (*modifier == 's')
2334de9a48a3SSteven Rostedt (VMware) 				modifier++;
2335de9a48a3SSteven Rostedt (VMware) 			if (*modifier != '=')
2336de9a48a3SSteven Rostedt (VMware) 				goto error;
2337de9a48a3SSteven Rostedt (VMware) 			modifier++;
2338de9a48a3SSteven Rostedt (VMware) 			ret = kstrtoul(modifier, 0, buckets);
2339de9a48a3SSteven Rostedt (VMware) 			if (ret || !(*buckets))
2340de9a48a3SSteven Rostedt (VMware) 				goto error;
2341de9a48a3SSteven Rostedt (VMware) 			*flags |= HIST_FIELD_FL_BUCKET;
2342abaa5258SMasami Hiramatsu (Google) 		} else if (strncmp(modifier, "percent", 7) == 0) {
2343abaa5258SMasami Hiramatsu (Google) 			if (*flags & (HIST_FIELD_FL_VAR | HIST_FIELD_FL_KEY))
2344abaa5258SMasami Hiramatsu (Google) 				goto error;
2345abaa5258SMasami Hiramatsu (Google) 			*flags |= HIST_FIELD_FL_PERCENT;
2346a2c54256SMasami Hiramatsu (Google) 		} else if (strncmp(modifier, "graph", 5) == 0) {
2347a2c54256SMasami Hiramatsu (Google) 			if (*flags & (HIST_FIELD_FL_VAR | HIST_FIELD_FL_KEY))
2348a2c54256SMasami Hiramatsu (Google) 				goto error;
2349a2c54256SMasami Hiramatsu (Google) 			*flags |= HIST_FIELD_FL_GRAPH;
2350de9a48a3SSteven Rostedt (VMware) 		} else {
2351de9a48a3SSteven Rostedt (VMware)  error:
2352d0cd871bSSteven Rostedt (VMware) 			hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier));
2353100719dcSTom Zanussi 			field = ERR_PTR(-EINVAL);
2354100719dcSTom Zanussi 			goto out;
2355100719dcSTom Zanussi 		}
2356100719dcSTom Zanussi 	}
2357100719dcSTom Zanussi 
2358100719dcSTom Zanussi 	if (strcmp(field_name, "common_timestamp") == 0) {
2359100719dcSTom Zanussi 		*flags |= HIST_FIELD_FL_TIMESTAMP;
2360100719dcSTom Zanussi 		hist_data->enable_timestamps = true;
2361100719dcSTom Zanussi 		if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2362100719dcSTom Zanussi 			hist_data->attrs->ts_in_usecs = true;
23634b512860SSteven Rostedt (Google) 	} else if (strcmp(field_name, "common_stacktrace") == 0) {
2364288709c9SSteven Rostedt (Google) 		*flags |= HIST_FIELD_FL_STACKTRACE;
23651e3bac71SSteven Rostedt (VMware) 	} else if (strcmp(field_name, "common_cpu") == 0)
23668b7622bfSTom Zanussi 		*flags |= HIST_FIELD_FL_CPU;
23675f2e094eSTom Zanussi 	else if (strcmp(field_name, "hitcount") == 0)
23685f2e094eSTom Zanussi 		*flags |= HIST_FIELD_FL_HITCOUNT;
23698b7622bfSTom Zanussi 	else {
2370100719dcSTom Zanussi 		field = trace_find_event_field(file->event_call, field_name);
2371100719dcSTom Zanussi 		if (!field || !field->size) {
23721e3bac71SSteven Rostedt (VMware) 			/*
23731e3bac71SSteven Rostedt (VMware) 			 * For backward compatibility, if field_name
23744b512860SSteven Rostedt (Google) 			 * was "cpu" or "stacktrace", then we treat this
23754b512860SSteven Rostedt (Google) 			 * the same as common_cpu and common_stacktrace
23764b512860SSteven Rostedt (Google) 			 * respectively. This also works for "CPU", and
23774b512860SSteven Rostedt (Google) 			 * "STACKTRACE".
23781e3bac71SSteven Rostedt (VMware) 			 */
23791d1898f6SSteven Rostedt (Google) 			if (field && field->filter_type == FILTER_CPU) {
23801e3bac71SSteven Rostedt (VMware) 				*flags |= HIST_FIELD_FL_CPU;
23814b512860SSteven Rostedt (Google) 			} else if (field && field->filter_type == FILTER_STACKTRACE) {
23824b512860SSteven Rostedt (Google) 				*flags |= HIST_FIELD_FL_STACKTRACE;
23831e3bac71SSteven Rostedt (VMware) 			} else {
23841e3bac71SSteven Rostedt (VMware) 				hist_err(tr, HIST_ERR_FIELD_NOT_FOUND,
23851e3bac71SSteven Rostedt (VMware) 					 errpos(field_name));
2386100719dcSTom Zanussi 				field = ERR_PTR(-EINVAL);
2387100719dcSTom Zanussi 				goto out;
2388100719dcSTom Zanussi 			}
2389100719dcSTom Zanussi 		}
23901e3bac71SSteven Rostedt (VMware) 	}
2391100719dcSTom Zanussi  out:
2392100719dcSTom Zanussi 	kfree(str);
2393100719dcSTom Zanussi 
2394100719dcSTom Zanussi 	return field;
2395100719dcSTom Zanussi }
2396100719dcSTom Zanussi 
create_alias(struct hist_trigger_data * hist_data,struct hist_field * var_ref,char * var_name)23977e8b88a3STom Zanussi static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
23987e8b88a3STom Zanussi 				       struct hist_field *var_ref,
23997e8b88a3STom Zanussi 				       char *var_name)
24007e8b88a3STom Zanussi {
24017e8b88a3STom Zanussi 	struct hist_field *alias = NULL;
24027e8b88a3STom Zanussi 	unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
24037e8b88a3STom Zanussi 
24047e8b88a3STom Zanussi 	alias = create_hist_field(hist_data, NULL, flags, var_name);
24057e8b88a3STom Zanussi 	if (!alias)
24067e8b88a3STom Zanussi 		return NULL;
24077e8b88a3STom Zanussi 
240886087383SSteven Rostedt (Google) 	alias->fn_num = var_ref->fn_num;
24097e8b88a3STom Zanussi 	alias->operands[0] = var_ref;
24107e8b88a3STom Zanussi 
24117e8b88a3STom Zanussi 	if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
24127e8b88a3STom Zanussi 		destroy_hist_field(alias, 0);
24137e8b88a3STom Zanussi 		return NULL;
24147e8b88a3STom Zanussi 	}
24157e8b88a3STom Zanussi 
241617f8607aSTom Zanussi 	alias->var_ref_idx = var_ref->var_ref_idx;
241717f8607aSTom Zanussi 
24187e8b88a3STom Zanussi 	return alias;
24197e8b88a3STom Zanussi }
24207e8b88a3STom Zanussi 
parse_const(struct hist_trigger_data * hist_data,char * str,char * var_name,unsigned long * flags)242152cfb373SKalesh Singh static struct hist_field *parse_const(struct hist_trigger_data *hist_data,
242252cfb373SKalesh Singh 				      char *str, char *var_name,
242352cfb373SKalesh Singh 				      unsigned long *flags)
242452cfb373SKalesh Singh {
242552cfb373SKalesh Singh 	struct trace_array *tr = hist_data->event_file->tr;
242652cfb373SKalesh Singh 	struct hist_field *field = NULL;
242752cfb373SKalesh Singh 	u64 constant;
242852cfb373SKalesh Singh 
242952cfb373SKalesh Singh 	if (kstrtoull(str, 0, &constant)) {
243052cfb373SKalesh Singh 		hist_err(tr, HIST_ERR_EXPECT_NUMBER, errpos(str));
243152cfb373SKalesh Singh 		return NULL;
243252cfb373SKalesh Singh 	}
243352cfb373SKalesh Singh 
243452cfb373SKalesh Singh 	*flags |= HIST_FIELD_FL_CONST;
243552cfb373SKalesh Singh 	field = create_hist_field(hist_data, NULL, *flags, var_name);
243652cfb373SKalesh Singh 	if (!field)
243752cfb373SKalesh Singh 		return NULL;
243852cfb373SKalesh Singh 
243952cfb373SKalesh Singh 	field->constant = constant;
244052cfb373SKalesh Singh 
244152cfb373SKalesh Singh 	return field;
244252cfb373SKalesh Singh }
244352cfb373SKalesh Singh 
parse_atom(struct hist_trigger_data * hist_data,struct trace_event_file * file,char * str,unsigned long * flags,char * var_name)2444100719dcSTom Zanussi static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2445100719dcSTom Zanussi 				     struct trace_event_file *file, char *str,
2446100719dcSTom Zanussi 				     unsigned long *flags, char *var_name)
2447100719dcSTom Zanussi {
2448067fe038STom Zanussi 	char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
2449100719dcSTom Zanussi 	struct ftrace_event_field *field = NULL;
2450100719dcSTom Zanussi 	struct hist_field *hist_field = NULL;
2451de9a48a3SSteven Rostedt (VMware) 	unsigned long buckets = 0;
2452100719dcSTom Zanussi 	int ret = 0;
2453100719dcSTom Zanussi 
245452cfb373SKalesh Singh 	if (isdigit(str[0])) {
245552cfb373SKalesh Singh 		hist_field = parse_const(hist_data, str, var_name, flags);
245652cfb373SKalesh Singh 		if (!hist_field) {
245752cfb373SKalesh Singh 			ret = -EINVAL;
245852cfb373SKalesh Singh 			goto out;
245952cfb373SKalesh Singh 		}
246052cfb373SKalesh Singh 		return hist_field;
246152cfb373SKalesh Singh 	}
246252cfb373SKalesh Singh 
2463067fe038STom Zanussi 	s = strchr(str, '.');
2464067fe038STom Zanussi 	if (s) {
2465067fe038STom Zanussi 		s = strchr(++s, '.');
2466067fe038STom Zanussi 		if (s) {
2467067fe038STom Zanussi 			ref_system = strsep(&str, ".");
2468067fe038STom Zanussi 			if (!str) {
2469067fe038STom Zanussi 				ret = -EINVAL;
2470067fe038STom Zanussi 				goto out;
2471067fe038STom Zanussi 			}
2472067fe038STom Zanussi 			ref_event = strsep(&str, ".");
2473067fe038STom Zanussi 			if (!str) {
2474067fe038STom Zanussi 				ret = -EINVAL;
2475067fe038STom Zanussi 				goto out;
2476067fe038STom Zanussi 			}
2477067fe038STom Zanussi 			ref_var = str;
2478067fe038STom Zanussi 		}
2479067fe038STom Zanussi 	}
2480067fe038STom Zanussi 
2481067fe038STom Zanussi 	s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2482067fe038STom Zanussi 	if (!s) {
2483d0cd871bSSteven Rostedt (VMware) 		hist_field = parse_var_ref(hist_data, ref_system,
2484d0cd871bSSteven Rostedt (VMware) 					   ref_event, ref_var);
2485067fe038STom Zanussi 		if (hist_field) {
24867e8b88a3STom Zanussi 			if (var_name) {
24877e8b88a3STom Zanussi 				hist_field = create_alias(hist_data, hist_field, var_name);
24887e8b88a3STom Zanussi 				if (!hist_field) {
24897e8b88a3STom Zanussi 					ret = -ENOMEM;
24907e8b88a3STom Zanussi 					goto out;
24917e8b88a3STom Zanussi 				}
24927e8b88a3STom Zanussi 			}
2493067fe038STom Zanussi 			return hist_field;
2494067fe038STom Zanussi 		}
2495067fe038STom Zanussi 	} else
2496067fe038STom Zanussi 		str = s;
2497067fe038STom Zanussi 
2498de9a48a3SSteven Rostedt (VMware) 	field = parse_field(hist_data, file, str, flags, &buckets);
2499100719dcSTom Zanussi 	if (IS_ERR(field)) {
2500100719dcSTom Zanussi 		ret = PTR_ERR(field);
2501100719dcSTom Zanussi 		goto out;
2502100719dcSTom Zanussi 	}
2503100719dcSTom Zanussi 
2504100719dcSTom Zanussi 	hist_field = create_hist_field(hist_data, field, *flags, var_name);
2505100719dcSTom Zanussi 	if (!hist_field) {
2506100719dcSTom Zanussi 		ret = -ENOMEM;
2507100719dcSTom Zanussi 		goto out;
2508100719dcSTom Zanussi 	}
2509de9a48a3SSteven Rostedt (VMware) 	hist_field->buckets = buckets;
2510100719dcSTom Zanussi 
2511100719dcSTom Zanussi 	return hist_field;
2512100719dcSTom Zanussi  out:
2513100719dcSTom Zanussi 	return ERR_PTR(ret);
2514100719dcSTom Zanussi }
2515100719dcSTom Zanussi 
2516100719dcSTom Zanussi static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2517100719dcSTom Zanussi 				     struct trace_event_file *file,
2518100719dcSTom Zanussi 				     char *str, unsigned long flags,
25199710b2f3SKalesh Singh 				     char *var_name, unsigned int *n_subexprs);
2520100719dcSTom Zanussi 
parse_unary(struct hist_trigger_data * hist_data,struct trace_event_file * file,char * str,unsigned long flags,char * var_name,unsigned int * n_subexprs)2521100719dcSTom Zanussi static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2522100719dcSTom Zanussi 				      struct trace_event_file *file,
2523100719dcSTom Zanussi 				      char *str, unsigned long flags,
25249710b2f3SKalesh Singh 				      char *var_name, unsigned int *n_subexprs)
2525100719dcSTom Zanussi {
2526100719dcSTom Zanussi 	struct hist_field *operand1, *expr = NULL;
2527100719dcSTom Zanussi 	unsigned long operand_flags;
2528100719dcSTom Zanussi 	int ret = 0;
2529100719dcSTom Zanussi 	char *s;
2530100719dcSTom Zanussi 
25319710b2f3SKalesh Singh 	/* Unary minus operator, increment n_subexprs */
25329710b2f3SKalesh Singh 	++*n_subexprs;
25339710b2f3SKalesh Singh 
2534100719dcSTom Zanussi 	/* we support only -(xxx) i.e. explicit parens required */
2535100719dcSTom Zanussi 
25369710b2f3SKalesh Singh 	if (*n_subexprs > 3) {
2537d0cd871bSSteven Rostedt (VMware) 		hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2538100719dcSTom Zanussi 		ret = -EINVAL;
2539100719dcSTom Zanussi 		goto free;
2540100719dcSTom Zanussi 	}
2541100719dcSTom Zanussi 
2542100719dcSTom Zanussi 	str++; /* skip leading '-' */
2543100719dcSTom Zanussi 
2544100719dcSTom Zanussi 	s = strchr(str, '(');
2545100719dcSTom Zanussi 	if (s)
2546100719dcSTom Zanussi 		str++;
2547100719dcSTom Zanussi 	else {
2548100719dcSTom Zanussi 		ret = -EINVAL;
2549100719dcSTom Zanussi 		goto free;
2550100719dcSTom Zanussi 	}
2551100719dcSTom Zanussi 
2552100719dcSTom Zanussi 	s = strrchr(str, ')');
25539710b2f3SKalesh Singh 	if (s) {
25549710b2f3SKalesh Singh 		 /* unary minus not supported in sub-expressions */
25559710b2f3SKalesh Singh 		if (*(s+1) != '\0') {
25569710b2f3SKalesh Singh 			hist_err(file->tr, HIST_ERR_UNARY_MINUS_SUBEXPR,
25579710b2f3SKalesh Singh 				 errpos(str));
25589710b2f3SKalesh Singh 			ret = -EINVAL;
25599710b2f3SKalesh Singh 			goto free;
25609710b2f3SKalesh Singh 		}
2561100719dcSTom Zanussi 		*s = '\0';
25629710b2f3SKalesh Singh 	}
2563100719dcSTom Zanussi 	else {
2564100719dcSTom Zanussi 		ret = -EINVAL; /* no closing ')' */
2565100719dcSTom Zanussi 		goto free;
2566100719dcSTom Zanussi 	}
2567100719dcSTom Zanussi 
2568100719dcSTom Zanussi 	flags |= HIST_FIELD_FL_EXPR;
2569100719dcSTom Zanussi 	expr = create_hist_field(hist_data, NULL, flags, var_name);
2570100719dcSTom Zanussi 	if (!expr) {
2571100719dcSTom Zanussi 		ret = -ENOMEM;
2572100719dcSTom Zanussi 		goto free;
2573100719dcSTom Zanussi 	}
2574100719dcSTom Zanussi 
2575100719dcSTom Zanussi 	operand_flags = 0;
25769710b2f3SKalesh Singh 	operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, n_subexprs);
2577100719dcSTom Zanussi 	if (IS_ERR(operand1)) {
2578100719dcSTom Zanussi 		ret = PTR_ERR(operand1);
2579100719dcSTom Zanussi 		goto free;
2580100719dcSTom Zanussi 	}
2581a9d10ca4SMasami Hiramatsu 	if (operand1->flags & HIST_FIELD_FL_STRING) {
2582a9d10ca4SMasami Hiramatsu 		/* String type can not be the operand of unary operator. */
2583a9d10ca4SMasami Hiramatsu 		hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
2584a9d10ca4SMasami Hiramatsu 		destroy_hist_field(operand1, 0);
2585a9d10ca4SMasami Hiramatsu 		ret = -EINVAL;
2586a9d10ca4SMasami Hiramatsu 		goto free;
2587a9d10ca4SMasami Hiramatsu 	}
2588100719dcSTom Zanussi 
2589100719dcSTom Zanussi 	expr->flags |= operand1->flags &
2590100719dcSTom Zanussi 		(HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
259186087383SSteven Rostedt (Google) 	expr->fn_num = HIST_FIELD_FN_UMINUS;
2592100719dcSTom Zanussi 	expr->operands[0] = operand1;
2593097f1eefSTom Zanussi 	expr->size = operand1->size;
2594097f1eefSTom Zanussi 	expr->is_signed = operand1->is_signed;
2595100719dcSTom Zanussi 	expr->operator = FIELD_OP_UNARY_MINUS;
2596100719dcSTom Zanussi 	expr->name = expr_str(expr, 0);
25973347d80bSSteven Rostedt (VMware) 	expr->type = kstrdup_const(operand1->type, GFP_KERNEL);
259819a9facdSTom Zanussi 	if (!expr->type) {
259919a9facdSTom Zanussi 		ret = -ENOMEM;
260019a9facdSTom Zanussi 		goto free;
260119a9facdSTom Zanussi 	}
2602100719dcSTom Zanussi 
2603100719dcSTom Zanussi 	return expr;
2604100719dcSTom Zanussi  free:
2605100719dcSTom Zanussi 	destroy_hist_field(expr, 0);
2606100719dcSTom Zanussi 	return ERR_PTR(ret);
2607100719dcSTom Zanussi }
2608100719dcSTom Zanussi 
2609f47716b7SKalesh Singh /*
2610f47716b7SKalesh Singh  * If the operands are var refs, return pointers the
2611f47716b7SKalesh Singh  * variable(s) referenced in var1 and var2, else NULL.
2612f47716b7SKalesh Singh  */
check_expr_operands(struct trace_array * tr,struct hist_field * operand1,struct hist_field * operand2,struct hist_field ** var1,struct hist_field ** var2)2613d0cd871bSSteven Rostedt (VMware) static int check_expr_operands(struct trace_array *tr,
2614d0cd871bSSteven Rostedt (VMware) 			       struct hist_field *operand1,
2615f47716b7SKalesh Singh 			       struct hist_field *operand2,
2616f47716b7SKalesh Singh 			       struct hist_field **var1,
2617f47716b7SKalesh Singh 			       struct hist_field **var2)
2618100719dcSTom Zanussi {
2619100719dcSTom Zanussi 	unsigned long operand1_flags = operand1->flags;
2620100719dcSTom Zanussi 	unsigned long operand2_flags = operand2->flags;
2621100719dcSTom Zanussi 
26227e8b88a3STom Zanussi 	if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
26237e8b88a3STom Zanussi 	    (operand1_flags & HIST_FIELD_FL_ALIAS)) {
26247e8b88a3STom Zanussi 		struct hist_field *var;
26257e8b88a3STom Zanussi 
26267e8b88a3STom Zanussi 		var = find_var_field(operand1->var.hist_data, operand1->name);
26277e8b88a3STom Zanussi 		if (!var)
26287e8b88a3STom Zanussi 			return -EINVAL;
26297e8b88a3STom Zanussi 		operand1_flags = var->flags;
2630f47716b7SKalesh Singh 		*var1 = var;
26317e8b88a3STom Zanussi 	}
26327e8b88a3STom Zanussi 
26337e8b88a3STom Zanussi 	if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
26347e8b88a3STom Zanussi 	    (operand2_flags & HIST_FIELD_FL_ALIAS)) {
26357e8b88a3STom Zanussi 		struct hist_field *var;
26367e8b88a3STom Zanussi 
26377e8b88a3STom Zanussi 		var = find_var_field(operand2->var.hist_data, operand2->name);
26387e8b88a3STom Zanussi 		if (!var)
26397e8b88a3STom Zanussi 			return -EINVAL;
26407e8b88a3STom Zanussi 		operand2_flags = var->flags;
2641f47716b7SKalesh Singh 		*var2 = var;
26427e8b88a3STom Zanussi 	}
26437e8b88a3STom Zanussi 
2644100719dcSTom Zanussi 	if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
2645f404da6eSTom Zanussi 	    (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
2646d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0);
2647100719dcSTom Zanussi 		return -EINVAL;
2648f404da6eSTom Zanussi 	}
2649100719dcSTom Zanussi 
2650100719dcSTom Zanussi 	return 0;
2651100719dcSTom Zanussi }
2652100719dcSTom Zanussi 
parse_expr(struct hist_trigger_data * hist_data,struct trace_event_file * file,char * str,unsigned long flags,char * var_name,unsigned int * n_subexprs)2653100719dcSTom Zanussi static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2654100719dcSTom Zanussi 				     struct trace_event_file *file,
2655100719dcSTom Zanussi 				     char *str, unsigned long flags,
26569710b2f3SKalesh Singh 				     char *var_name, unsigned int *n_subexprs)
2657100719dcSTom Zanussi {
2658100719dcSTom Zanussi 	struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2659f47716b7SKalesh Singh 	struct hist_field *var1 = NULL, *var2 = NULL;
2660f47716b7SKalesh Singh 	unsigned long operand_flags, operand2_flags;
2661100719dcSTom Zanussi 	int field_op, ret = -EINVAL;
2662100719dcSTom Zanussi 	char *sep, *operand1_str;
266386087383SSteven Rostedt (Google) 	enum hist_field_fn op_fn;
2664f47716b7SKalesh Singh 	bool combine_consts;
2665100719dcSTom Zanussi 
26669710b2f3SKalesh Singh 	if (*n_subexprs > 3) {
2667d0cd871bSSteven Rostedt (VMware) 		hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2668100719dcSTom Zanussi 		return ERR_PTR(-EINVAL);
2669f404da6eSTom Zanussi 	}
2670100719dcSTom Zanussi 
26719710b2f3SKalesh Singh 	field_op = contains_operator(str, &sep);
2672100719dcSTom Zanussi 
2673100719dcSTom Zanussi 	if (field_op == FIELD_OP_NONE)
2674100719dcSTom Zanussi 		return parse_atom(hist_data, file, str, &flags, var_name);
2675100719dcSTom Zanussi 
2676100719dcSTom Zanussi 	if (field_op == FIELD_OP_UNARY_MINUS)
26779710b2f3SKalesh Singh 		return parse_unary(hist_data, file, str, flags, var_name, n_subexprs);
2678100719dcSTom Zanussi 
26799710b2f3SKalesh Singh 	/* Binary operator found, increment n_subexprs */
26809710b2f3SKalesh Singh 	++*n_subexprs;
26819710b2f3SKalesh Singh 
26829710b2f3SKalesh Singh 	/* Split the expression string at the root operator */
26839710b2f3SKalesh Singh 	if (!sep)
2684f86b0aaaSKalesh Singh 		return ERR_PTR(-EINVAL);
2685f86b0aaaSKalesh Singh 
26869710b2f3SKalesh Singh 	*sep = '\0';
26879710b2f3SKalesh Singh 	operand1_str = str;
26889710b2f3SKalesh Singh 	str = sep+1;
2689100719dcSTom Zanussi 
26901cab6bceSKalesh Singh 	/* Binary operator requires both operands */
26911cab6bceSKalesh Singh 	if (*operand1_str == '\0' || *str == '\0')
2692f86b0aaaSKalesh Singh 		return ERR_PTR(-EINVAL);
2693100719dcSTom Zanussi 
2694100719dcSTom Zanussi 	operand_flags = 0;
26959710b2f3SKalesh Singh 
26969710b2f3SKalesh Singh 	/* LHS of string is an expression e.g. a+b in a+b+c */
26979710b2f3SKalesh Singh 	operand1 = parse_expr(hist_data, file, operand1_str, operand_flags, NULL, n_subexprs);
2698f86b0aaaSKalesh Singh 	if (IS_ERR(operand1))
2699f86b0aaaSKalesh Singh 		return ERR_CAST(operand1);
2700f86b0aaaSKalesh Singh 
2701a9d10ca4SMasami Hiramatsu 	if (operand1->flags & HIST_FIELD_FL_STRING) {
2702a9d10ca4SMasami Hiramatsu 		hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(operand1_str));
2703a9d10ca4SMasami Hiramatsu 		ret = -EINVAL;
2704f86b0aaaSKalesh Singh 		goto free_op1;
2705a9d10ca4SMasami Hiramatsu 	}
2706100719dcSTom Zanussi 
27079710b2f3SKalesh Singh 	/* RHS of string is another expression e.g. c in a+b+c */
2708100719dcSTom Zanussi 	operand_flags = 0;
27099710b2f3SKalesh Singh 	operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, n_subexprs);
2710100719dcSTom Zanussi 	if (IS_ERR(operand2)) {
2711100719dcSTom Zanussi 		ret = PTR_ERR(operand2);
2712f86b0aaaSKalesh Singh 		goto free_op1;
2713100719dcSTom Zanussi 	}
2714a9d10ca4SMasami Hiramatsu 	if (operand2->flags & HIST_FIELD_FL_STRING) {
2715a9d10ca4SMasami Hiramatsu 		hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
2716a9d10ca4SMasami Hiramatsu 		ret = -EINVAL;
2717f86b0aaaSKalesh Singh 		goto free_operands;
2718a9d10ca4SMasami Hiramatsu 	}
2719100719dcSTom Zanussi 
2720f47716b7SKalesh Singh 	switch (field_op) {
2721f47716b7SKalesh Singh 	case FIELD_OP_MINUS:
272286087383SSteven Rostedt (Google) 		op_fn = HIST_FIELD_FN_MINUS;
2723f47716b7SKalesh Singh 		break;
2724f47716b7SKalesh Singh 	case FIELD_OP_PLUS:
272586087383SSteven Rostedt (Google) 		op_fn = HIST_FIELD_FN_PLUS;
2726f47716b7SKalesh Singh 		break;
2727f47716b7SKalesh Singh 	case FIELD_OP_DIV:
272886087383SSteven Rostedt (Google) 		op_fn = HIST_FIELD_FN_DIV;
2729f47716b7SKalesh Singh 		break;
2730f47716b7SKalesh Singh 	case FIELD_OP_MULT:
273186087383SSteven Rostedt (Google) 		op_fn = HIST_FIELD_FN_MULT;
2732f47716b7SKalesh Singh 		break;
2733f47716b7SKalesh Singh 	default:
2734f47716b7SKalesh Singh 		ret = -EINVAL;
2735f86b0aaaSKalesh Singh 		goto free_operands;
2736f47716b7SKalesh Singh 	}
2737f47716b7SKalesh Singh 
2738f47716b7SKalesh Singh 	ret = check_expr_operands(file->tr, operand1, operand2, &var1, &var2);
2739100719dcSTom Zanussi 	if (ret)
2740f86b0aaaSKalesh Singh 		goto free_operands;
2741100719dcSTom Zanussi 
2742f47716b7SKalesh Singh 	operand_flags = var1 ? var1->flags : operand1->flags;
2743f47716b7SKalesh Singh 	operand2_flags = var2 ? var2->flags : operand2->flags;
2744f47716b7SKalesh Singh 
2745f47716b7SKalesh Singh 	/*
2746f47716b7SKalesh Singh 	 * If both operands are constant, the expression can be
2747f47716b7SKalesh Singh 	 * collapsed to a single constant.
2748f47716b7SKalesh Singh 	 */
2749f47716b7SKalesh Singh 	combine_consts = operand_flags & operand2_flags & HIST_FIELD_FL_CONST;
2750f47716b7SKalesh Singh 
2751f47716b7SKalesh Singh 	flags |= combine_consts ? HIST_FIELD_FL_CONST : HIST_FIELD_FL_EXPR;
2752100719dcSTom Zanussi 
2753100719dcSTom Zanussi 	flags |= operand1->flags &
2754100719dcSTom Zanussi 		(HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2755100719dcSTom Zanussi 
2756100719dcSTom Zanussi 	expr = create_hist_field(hist_data, NULL, flags, var_name);
2757100719dcSTom Zanussi 	if (!expr) {
2758100719dcSTom Zanussi 		ret = -ENOMEM;
2759f86b0aaaSKalesh Singh 		goto free_operands;
2760100719dcSTom Zanussi 	}
2761100719dcSTom Zanussi 
2762067fe038STom Zanussi 	operand1->read_once = true;
2763067fe038STom Zanussi 	operand2->read_once = true;
2764067fe038STom Zanussi 
2765f86b0aaaSKalesh Singh 	/* The operands are now owned and free'd by 'expr' */
2766100719dcSTom Zanussi 	expr->operands[0] = operand1;
2767100719dcSTom Zanussi 	expr->operands[1] = operand2;
27682c05caa7SSteven Rostedt (VMware) 
27698b5d46fdSKalesh Singh 	if (field_op == FIELD_OP_DIV &&
27708b5d46fdSKalesh Singh 			operand2_flags & HIST_FIELD_FL_CONST) {
27718b5d46fdSKalesh Singh 		u64 divisor = var2 ? var2->constant : operand2->constant;
27728b5d46fdSKalesh Singh 
27738b5d46fdSKalesh Singh 		if (!divisor) {
27748b5d46fdSKalesh Singh 			hist_err(file->tr, HIST_ERR_DIVISION_BY_ZERO, errpos(str));
27758b5d46fdSKalesh Singh 			ret = -EDOM;
2776f86b0aaaSKalesh Singh 			goto free_expr;
27778b5d46fdSKalesh Singh 		}
27788b5d46fdSKalesh Singh 
27798b5d46fdSKalesh Singh 		/*
27808b5d46fdSKalesh Singh 		 * Copy the divisor here so we don't have to look it up
27818b5d46fdSKalesh Singh 		 * later if this is a var ref
27828b5d46fdSKalesh Singh 		 */
27838b5d46fdSKalesh Singh 		operand2->constant = divisor;
27848b5d46fdSKalesh Singh 		op_fn = hist_field_get_div_fn(operand2);
27858b5d46fdSKalesh Singh 	}
27868b5d46fdSKalesh Singh 
278786087383SSteven Rostedt (Google) 	expr->fn_num = op_fn;
278886087383SSteven Rostedt (Google) 
2789f47716b7SKalesh Singh 	if (combine_consts) {
2790f47716b7SKalesh Singh 		if (var1)
2791f47716b7SKalesh Singh 			expr->operands[0] = var1;
2792f47716b7SKalesh Singh 		if (var2)
2793f47716b7SKalesh Singh 			expr->operands[1] = var2;
2794f47716b7SKalesh Singh 
279586087383SSteven Rostedt (Google) 		expr->constant = hist_fn_call(expr, NULL, NULL, NULL, NULL);
279686087383SSteven Rostedt (Google) 		expr->fn_num = HIST_FIELD_FN_CONST;
2797f47716b7SKalesh Singh 
2798f47716b7SKalesh Singh 		expr->operands[0] = NULL;
2799f47716b7SKalesh Singh 		expr->operands[1] = NULL;
2800f47716b7SKalesh Singh 
2801f47716b7SKalesh Singh 		/*
2802f47716b7SKalesh Singh 		 * var refs won't be destroyed immediately
2803f47716b7SKalesh Singh 		 * See: destroy_hist_field()
2804f47716b7SKalesh Singh 		 */
2805f47716b7SKalesh Singh 		destroy_hist_field(operand2, 0);
2806f47716b7SKalesh Singh 		destroy_hist_field(operand1, 0);
2807f47716b7SKalesh Singh 
2808f47716b7SKalesh Singh 		expr->name = expr_str(expr, 0);
2809f47716b7SKalesh Singh 	} else {
28102c05caa7SSteven Rostedt (VMware) 		/* The operand sizes should be the same, so just pick one */
28112c05caa7SSteven Rostedt (VMware) 		expr->size = operand1->size;
2812097f1eefSTom Zanussi 		expr->is_signed = operand1->is_signed;
28132c05caa7SSteven Rostedt (VMware) 
2814100719dcSTom Zanussi 		expr->operator = field_op;
28153347d80bSSteven Rostedt (VMware) 		expr->type = kstrdup_const(operand1->type, GFP_KERNEL);
281619a9facdSTom Zanussi 		if (!expr->type) {
281719a9facdSTom Zanussi 			ret = -ENOMEM;
2818f86b0aaaSKalesh Singh 			goto free_expr;
281919a9facdSTom Zanussi 		}
2820100719dcSTom Zanussi 
2821f47716b7SKalesh Singh 		expr->name = expr_str(expr, 0);
2822100719dcSTom Zanussi 	}
2823100719dcSTom Zanussi 
2824100719dcSTom Zanussi 	return expr;
2825100719dcSTom Zanussi 
2826f86b0aaaSKalesh Singh free_operands:
2827f86b0aaaSKalesh Singh 	destroy_hist_field(operand2, 0);
2828f86b0aaaSKalesh Singh free_op1:
2829f86b0aaaSKalesh Singh 	destroy_hist_field(operand1, 0);
2830f86b0aaaSKalesh Singh 	return ERR_PTR(ret);
2831f86b0aaaSKalesh Singh 
2832f86b0aaaSKalesh Singh free_expr:
2833f86b0aaaSKalesh Singh 	destroy_hist_field(expr, 0);
2834100719dcSTom Zanussi 	return ERR_PTR(ret);
2835100719dcSTom Zanussi }
2836100719dcSTom Zanussi 
find_trigger_filter(struct hist_trigger_data * hist_data,struct trace_event_file * file)283702205a67STom Zanussi static char *find_trigger_filter(struct hist_trigger_data *hist_data,
283802205a67STom Zanussi 				 struct trace_event_file *file)
283902205a67STom Zanussi {
284002205a67STom Zanussi 	struct event_trigger_data *test;
284102205a67STom Zanussi 
2842aeed8aa3SMasami Hiramatsu 	lockdep_assert_held(&event_mutex);
2843aeed8aa3SMasami Hiramatsu 
2844aeed8aa3SMasami Hiramatsu 	list_for_each_entry(test, &file->triggers, list) {
284502205a67STom Zanussi 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
284602205a67STom Zanussi 			if (test->private_data == hist_data)
284702205a67STom Zanussi 				return test->filter_str;
284802205a67STom Zanussi 		}
284902205a67STom Zanussi 	}
285002205a67STom Zanussi 
285102205a67STom Zanussi 	return NULL;
285202205a67STom Zanussi }
285302205a67STom Zanussi 
285402205a67STom Zanussi static struct event_command trigger_hist_cmd;
28559ec5a7d1STom Zanussi static int event_hist_trigger_parse(struct event_command *cmd_ops,
285602205a67STom Zanussi 				    struct trace_event_file *file,
2857e1f187d0STom Zanussi 				    char *glob, char *cmd,
2858e1f187d0STom Zanussi 				    char *param_and_filter);
285902205a67STom Zanussi 
compatible_keys(struct hist_trigger_data * target_hist_data,struct hist_trigger_data * hist_data,unsigned int n_keys)286002205a67STom Zanussi static bool compatible_keys(struct hist_trigger_data *target_hist_data,
286102205a67STom Zanussi 			    struct hist_trigger_data *hist_data,
286202205a67STom Zanussi 			    unsigned int n_keys)
286302205a67STom Zanussi {
286402205a67STom Zanussi 	struct hist_field *target_hist_field, *hist_field;
286502205a67STom Zanussi 	unsigned int n, i, j;
286602205a67STom Zanussi 
286702205a67STom Zanussi 	if (hist_data->n_fields - hist_data->n_vals != n_keys)
286802205a67STom Zanussi 		return false;
286902205a67STom Zanussi 
287002205a67STom Zanussi 	i = hist_data->n_vals;
287102205a67STom Zanussi 	j = target_hist_data->n_vals;
287202205a67STom Zanussi 
287302205a67STom Zanussi 	for (n = 0; n < n_keys; n++) {
287402205a67STom Zanussi 		hist_field = hist_data->fields[i + n];
287502205a67STom Zanussi 		target_hist_field = target_hist_data->fields[j + n];
287602205a67STom Zanussi 
287702205a67STom Zanussi 		if (strcmp(hist_field->type, target_hist_field->type) != 0)
287802205a67STom Zanussi 			return false;
287902205a67STom Zanussi 		if (hist_field->size != target_hist_field->size)
288002205a67STom Zanussi 			return false;
288102205a67STom Zanussi 		if (hist_field->is_signed != target_hist_field->is_signed)
288202205a67STom Zanussi 			return false;
288302205a67STom Zanussi 	}
288402205a67STom Zanussi 
288502205a67STom Zanussi 	return true;
288602205a67STom Zanussi }
288702205a67STom Zanussi 
288802205a67STom Zanussi static struct hist_trigger_data *
find_compatible_hist(struct hist_trigger_data * target_hist_data,struct trace_event_file * file)288902205a67STom Zanussi find_compatible_hist(struct hist_trigger_data *target_hist_data,
289002205a67STom Zanussi 		     struct trace_event_file *file)
289102205a67STom Zanussi {
289202205a67STom Zanussi 	struct hist_trigger_data *hist_data;
289302205a67STom Zanussi 	struct event_trigger_data *test;
289402205a67STom Zanussi 	unsigned int n_keys;
289502205a67STom Zanussi 
2896aeed8aa3SMasami Hiramatsu 	lockdep_assert_held(&event_mutex);
2897aeed8aa3SMasami Hiramatsu 
289802205a67STom Zanussi 	n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
289902205a67STom Zanussi 
2900aeed8aa3SMasami Hiramatsu 	list_for_each_entry(test, &file->triggers, list) {
290102205a67STom Zanussi 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
290202205a67STom Zanussi 			hist_data = test->private_data;
290302205a67STom Zanussi 
290402205a67STom Zanussi 			if (compatible_keys(target_hist_data, hist_data, n_keys))
290502205a67STom Zanussi 				return hist_data;
290602205a67STom Zanussi 		}
290702205a67STom Zanussi 	}
290802205a67STom Zanussi 
290902205a67STom Zanussi 	return NULL;
291002205a67STom Zanussi }
291102205a67STom Zanussi 
event_file(struct trace_array * tr,char * system,char * event_name)291202205a67STom Zanussi static struct trace_event_file *event_file(struct trace_array *tr,
291302205a67STom Zanussi 					   char *system, char *event_name)
291402205a67STom Zanussi {
291502205a67STom Zanussi 	struct trace_event_file *file;
291602205a67STom Zanussi 
29173be4c1e5SSteven Rostedt (VMware) 	file = __find_event_file(tr, system, event_name);
291802205a67STom Zanussi 	if (!file)
291902205a67STom Zanussi 		return ERR_PTR(-EINVAL);
292002205a67STom Zanussi 
292102205a67STom Zanussi 	return file;
292202205a67STom Zanussi }
292302205a67STom Zanussi 
292402205a67STom Zanussi static struct hist_field *
find_synthetic_field_var(struct hist_trigger_data * target_hist_data,char * system,char * event_name,char * field_name)292502205a67STom Zanussi find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
292602205a67STom Zanussi 			 char *system, char *event_name, char *field_name)
292702205a67STom Zanussi {
292802205a67STom Zanussi 	struct hist_field *event_var;
292902205a67STom Zanussi 	char *synthetic_name;
293002205a67STom Zanussi 
293102205a67STom Zanussi 	synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
293202205a67STom Zanussi 	if (!synthetic_name)
293302205a67STom Zanussi 		return ERR_PTR(-ENOMEM);
293402205a67STom Zanussi 
293502205a67STom Zanussi 	strcpy(synthetic_name, "synthetic_");
293602205a67STom Zanussi 	strcat(synthetic_name, field_name);
293702205a67STom Zanussi 
293802205a67STom Zanussi 	event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
293902205a67STom Zanussi 
294002205a67STom Zanussi 	kfree(synthetic_name);
294102205a67STom Zanussi 
294202205a67STom Zanussi 	return event_var;
294302205a67STom Zanussi }
294402205a67STom Zanussi 
294502205a67STom Zanussi /**
294602205a67STom Zanussi  * create_field_var_hist - Automatically create a histogram and var for a field
294702205a67STom Zanussi  * @target_hist_data: The target hist trigger
294802205a67STom Zanussi  * @subsys_name: Optional subsystem name
294902205a67STom Zanussi  * @event_name: Optional event name
295002205a67STom Zanussi  * @field_name: The name of the field (and the resulting variable)
295102205a67STom Zanussi  *
295202205a67STom Zanussi  * Hist trigger actions fetch data from variables, not directly from
295302205a67STom Zanussi  * events.  However, for convenience, users are allowed to directly
295402205a67STom Zanussi  * specify an event field in an action, which will be automatically
295502205a67STom Zanussi  * converted into a variable on their behalf.
2956b26503b1SColin Ian King  *
295702205a67STom Zanussi  * If a user specifies a field on an event that isn't the event the
295802205a67STom Zanussi  * histogram currently being defined (the target event histogram), the
295902205a67STom Zanussi  * only way that can be accomplished is if a new hist trigger is
296002205a67STom Zanussi  * created and the field variable defined on that.
296102205a67STom Zanussi  *
296202205a67STom Zanussi  * This function creates a new histogram compatible with the target
296302205a67STom Zanussi  * event (meaning a histogram with the same key as the target
296402205a67STom Zanussi  * histogram), and creates a variable for the specified field, but
296502205a67STom Zanussi  * with 'synthetic_' prepended to the variable name in order to avoid
296602205a67STom Zanussi  * collision with normal field variables.
296702205a67STom Zanussi  *
296802205a67STom Zanussi  * Return: The variable created for the field.
296902205a67STom Zanussi  */
2970c282a386STom Zanussi static struct hist_field *
create_field_var_hist(struct hist_trigger_data * target_hist_data,char * subsys_name,char * event_name,char * field_name)297102205a67STom Zanussi create_field_var_hist(struct hist_trigger_data *target_hist_data,
297202205a67STom Zanussi 		      char *subsys_name, char *event_name, char *field_name)
297302205a67STom Zanussi {
297402205a67STom Zanussi 	struct trace_array *tr = target_hist_data->event_file->tr;
297502205a67STom Zanussi 	struct hist_trigger_data *hist_data;
297602205a67STom Zanussi 	unsigned int i, n, first = true;
297702205a67STom Zanussi 	struct field_var_hist *var_hist;
297802205a67STom Zanussi 	struct trace_event_file *file;
297902205a67STom Zanussi 	struct hist_field *key_field;
2980614db49cSJiapeng Chong 	struct hist_field *event_var;
298102205a67STom Zanussi 	char *saved_filter;
298202205a67STom Zanussi 	char *cmd;
298302205a67STom Zanussi 	int ret;
298402205a67STom Zanussi 
2985f404da6eSTom Zanussi 	if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
2986d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
298702205a67STom Zanussi 		return ERR_PTR(-EINVAL);
2988f404da6eSTom Zanussi 	}
298902205a67STom Zanussi 
299002205a67STom Zanussi 	file = event_file(tr, subsys_name, event_name);
299102205a67STom Zanussi 
299202205a67STom Zanussi 	if (IS_ERR(file)) {
2993d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name));
299402205a67STom Zanussi 		ret = PTR_ERR(file);
299502205a67STom Zanussi 		return ERR_PTR(ret);
299602205a67STom Zanussi 	}
299702205a67STom Zanussi 
299802205a67STom Zanussi 	/*
299902205a67STom Zanussi 	 * Look for a histogram compatible with target.  We'll use the
300002205a67STom Zanussi 	 * found histogram specification to create a new matching
300102205a67STom Zanussi 	 * histogram with our variable on it.  target_hist_data is not
300202205a67STom Zanussi 	 * yet a registered histogram so we can't use that.
300302205a67STom Zanussi 	 */
300402205a67STom Zanussi 	hist_data = find_compatible_hist(target_hist_data, file);
3005f404da6eSTom Zanussi 	if (!hist_data) {
3006d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name));
300702205a67STom Zanussi 		return ERR_PTR(-EINVAL);
3008f404da6eSTom Zanussi 	}
300902205a67STom Zanussi 
301002205a67STom Zanussi 	/* See if a synthetic field variable has already been created */
301102205a67STom Zanussi 	event_var = find_synthetic_field_var(target_hist_data, subsys_name,
301202205a67STom Zanussi 					     event_name, field_name);
301302205a67STom Zanussi 	if (!IS_ERR_OR_NULL(event_var))
301402205a67STom Zanussi 		return event_var;
301502205a67STom Zanussi 
301602205a67STom Zanussi 	var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
301702205a67STom Zanussi 	if (!var_hist)
301802205a67STom Zanussi 		return ERR_PTR(-ENOMEM);
301902205a67STom Zanussi 
302002205a67STom Zanussi 	cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
302102205a67STom Zanussi 	if (!cmd) {
302202205a67STom Zanussi 		kfree(var_hist);
302302205a67STom Zanussi 		return ERR_PTR(-ENOMEM);
302402205a67STom Zanussi 	}
302502205a67STom Zanussi 
302602205a67STom Zanussi 	/* Use the same keys as the compatible histogram */
302702205a67STom Zanussi 	strcat(cmd, "keys=");
302802205a67STom Zanussi 
302902205a67STom Zanussi 	for_each_hist_key_field(i, hist_data) {
303002205a67STom Zanussi 		key_field = hist_data->fields[i];
303102205a67STom Zanussi 		if (!first)
303202205a67STom Zanussi 			strcat(cmd, ",");
303302205a67STom Zanussi 		strcat(cmd, key_field->field->name);
303402205a67STom Zanussi 		first = false;
303502205a67STom Zanussi 	}
303602205a67STom Zanussi 
303702205a67STom Zanussi 	/* Create the synthetic field variable specification */
303802205a67STom Zanussi 	strcat(cmd, ":synthetic_");
303902205a67STom Zanussi 	strcat(cmd, field_name);
304002205a67STom Zanussi 	strcat(cmd, "=");
304102205a67STom Zanussi 	strcat(cmd, field_name);
304202205a67STom Zanussi 
304302205a67STom Zanussi 	/* Use the same filter as the compatible histogram */
304402205a67STom Zanussi 	saved_filter = find_trigger_filter(hist_data, file);
304502205a67STom Zanussi 	if (saved_filter) {
304602205a67STom Zanussi 		strcat(cmd, " if ");
304702205a67STom Zanussi 		strcat(cmd, saved_filter);
304802205a67STom Zanussi 	}
304902205a67STom Zanussi 
305002205a67STom Zanussi 	var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
305102205a67STom Zanussi 	if (!var_hist->cmd) {
305202205a67STom Zanussi 		kfree(cmd);
305302205a67STom Zanussi 		kfree(var_hist);
305402205a67STom Zanussi 		return ERR_PTR(-ENOMEM);
305502205a67STom Zanussi 	}
305602205a67STom Zanussi 
305702205a67STom Zanussi 	/* Save the compatible histogram information */
305802205a67STom Zanussi 	var_hist->hist_data = hist_data;
305902205a67STom Zanussi 
306002205a67STom Zanussi 	/* Create the new histogram with our variable */
30619ec5a7d1STom Zanussi 	ret = event_hist_trigger_parse(&trigger_hist_cmd, file,
306202205a67STom Zanussi 				       "", "hist", cmd);
306302205a67STom Zanussi 	if (ret) {
306402205a67STom Zanussi 		kfree(cmd);
306502205a67STom Zanussi 		kfree(var_hist->cmd);
306602205a67STom Zanussi 		kfree(var_hist);
3067d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name));
306802205a67STom Zanussi 		return ERR_PTR(ret);
306902205a67STom Zanussi 	}
307002205a67STom Zanussi 
307102205a67STom Zanussi 	kfree(cmd);
307202205a67STom Zanussi 
307302205a67STom Zanussi 	/* If we can't find the variable, something went wrong */
307402205a67STom Zanussi 	event_var = find_synthetic_field_var(target_hist_data, subsys_name,
307502205a67STom Zanussi 					     event_name, field_name);
307602205a67STom Zanussi 	if (IS_ERR_OR_NULL(event_var)) {
307702205a67STom Zanussi 		kfree(var_hist->cmd);
307802205a67STom Zanussi 		kfree(var_hist);
3079d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
308002205a67STom Zanussi 		return ERR_PTR(-EINVAL);
308102205a67STom Zanussi 	}
308202205a67STom Zanussi 
308302205a67STom Zanussi 	n = target_hist_data->n_field_var_hists;
308402205a67STom Zanussi 	target_hist_data->field_var_hists[n] = var_hist;
308502205a67STom Zanussi 	target_hist_data->n_field_var_hists++;
308602205a67STom Zanussi 
308702205a67STom Zanussi 	return event_var;
308802205a67STom Zanussi }
308902205a67STom Zanussi 
3090c282a386STom Zanussi static struct hist_field *
find_target_event_var(struct hist_trigger_data * hist_data,char * subsys_name,char * event_name,char * var_name)309102205a67STom Zanussi find_target_event_var(struct hist_trigger_data *hist_data,
309202205a67STom Zanussi 		      char *subsys_name, char *event_name, char *var_name)
309302205a67STom Zanussi {
309402205a67STom Zanussi 	struct trace_event_file *file = hist_data->event_file;
309502205a67STom Zanussi 	struct hist_field *hist_field = NULL;
309602205a67STom Zanussi 
309702205a67STom Zanussi 	if (subsys_name) {
309802205a67STom Zanussi 		struct trace_event_call *call;
309902205a67STom Zanussi 
310002205a67STom Zanussi 		if (!event_name)
310102205a67STom Zanussi 			return NULL;
310202205a67STom Zanussi 
310302205a67STom Zanussi 		call = file->event_call;
310402205a67STom Zanussi 
310502205a67STom Zanussi 		if (strcmp(subsys_name, call->class->system) != 0)
310602205a67STom Zanussi 			return NULL;
310702205a67STom Zanussi 
310802205a67STom Zanussi 		if (strcmp(event_name, trace_event_name(call)) != 0)
310902205a67STom Zanussi 			return NULL;
311002205a67STom Zanussi 	}
311102205a67STom Zanussi 
311202205a67STom Zanussi 	hist_field = find_var_field(hist_data, var_name);
311302205a67STom Zanussi 
311402205a67STom Zanussi 	return hist_field;
311502205a67STom Zanussi }
311602205a67STom Zanussi 
__update_field_vars(struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * rec,struct field_var ** field_vars,unsigned int n_field_vars,unsigned int field_var_str_start)311702205a67STom Zanussi static inline void __update_field_vars(struct tracing_map_elt *elt,
3118b47e3302SSteven Rostedt (VMware) 				       struct trace_buffer *buffer,
311902205a67STom Zanussi 				       struct ring_buffer_event *rbe,
312002205a67STom Zanussi 				       void *rec,
312102205a67STom Zanussi 				       struct field_var **field_vars,
312202205a67STom Zanussi 				       unsigned int n_field_vars,
312302205a67STom Zanussi 				       unsigned int field_var_str_start)
312402205a67STom Zanussi {
312502205a67STom Zanussi 	struct hist_elt_data *elt_data = elt->private_data;
312602205a67STom Zanussi 	unsigned int i, j, var_idx;
312702205a67STom Zanussi 	u64 var_val;
312802205a67STom Zanussi 
31298261ef2eSSteven Rostedt (Google) 	/* Make sure stacktrace can fit in the string variable length */
31308261ef2eSSteven Rostedt (Google) 	BUILD_BUG_ON((HIST_STACKTRACE_DEPTH + 1) * sizeof(long) >= STR_VAR_LEN_MAX);
31318261ef2eSSteven Rostedt (Google) 
313202205a67STom Zanussi 	for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
313302205a67STom Zanussi 		struct field_var *field_var = field_vars[i];
313402205a67STom Zanussi 		struct hist_field *var = field_var->var;
313502205a67STom Zanussi 		struct hist_field *val = field_var->val;
313602205a67STom Zanussi 
313786087383SSteven Rostedt (Google) 		var_val = hist_fn_call(val, elt, buffer, rbe, rec);
313802205a67STom Zanussi 		var_idx = var->var.idx;
313902205a67STom Zanussi 
3140288709c9SSteven Rostedt (Google) 		if (val->flags & (HIST_FIELD_FL_STRING |
3141288709c9SSteven Rostedt (Google) 				  HIST_FIELD_FL_STACKTRACE)) {
314202205a67STom Zanussi 			char *str = elt_data->field_var_str[j++];
314302205a67STom Zanussi 			char *val_str = (char *)(uintptr_t)var_val;
3144938aa33fSSteven Rostedt (VMware) 			unsigned int size;
314502205a67STom Zanussi 
3146288709c9SSteven Rostedt (Google) 			if (val->flags & HIST_FIELD_FL_STRING) {
3147938aa33fSSteven Rostedt (VMware) 				size = min(val->size, STR_VAR_LEN_MAX);
3148938aa33fSSteven Rostedt (VMware) 				strscpy(str, val_str, size);
3149288709c9SSteven Rostedt (Google) 			} else {
3150fc1a9dc1STom Zanussi 				char *stack_start = str + sizeof(unsigned long);
3151288709c9SSteven Rostedt (Google) 				int e;
3152288709c9SSteven Rostedt (Google) 
3153fc1a9dc1STom Zanussi 				e = stack_trace_save((void *)stack_start,
3154288709c9SSteven Rostedt (Google) 						     HIST_STACKTRACE_DEPTH,
3155288709c9SSteven Rostedt (Google) 						     HIST_STACKTRACE_SKIP);
3156288709c9SSteven Rostedt (Google) 				if (e < HIST_STACKTRACE_DEPTH - 1)
3157fc1a9dc1STom Zanussi 					((unsigned long *)stack_start)[e] = 0;
3158fc1a9dc1STom Zanussi 				*((unsigned long *)str) = e;
3159288709c9SSteven Rostedt (Google) 			}
316002205a67STom Zanussi 			var_val = (u64)(uintptr_t)str;
316102205a67STom Zanussi 		}
316202205a67STom Zanussi 		tracing_map_set_var(elt, var_idx, var_val);
316302205a67STom Zanussi 	}
316402205a67STom Zanussi }
316502205a67STom Zanussi 
update_field_vars(struct hist_trigger_data * hist_data,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * rec)316602205a67STom Zanussi static void update_field_vars(struct hist_trigger_data *hist_data,
316702205a67STom Zanussi 			      struct tracing_map_elt *elt,
3168b47e3302SSteven Rostedt (VMware) 			      struct trace_buffer *buffer,
316902205a67STom Zanussi 			      struct ring_buffer_event *rbe,
317002205a67STom Zanussi 			      void *rec)
317102205a67STom Zanussi {
3172b47e3302SSteven Rostedt (VMware) 	__update_field_vars(elt, buffer, rbe, rec, hist_data->field_vars,
317302205a67STom Zanussi 			    hist_data->n_field_vars, 0);
317402205a67STom Zanussi }
317502205a67STom Zanussi 
save_track_data_vars(struct hist_trigger_data * hist_data,struct tracing_map_elt * elt,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * rbe,void * key,struct action_data * data,u64 * var_ref_vals)3176466f4528STom Zanussi static void save_track_data_vars(struct hist_trigger_data *hist_data,
3177b47e3302SSteven Rostedt (VMware) 				 struct tracing_map_elt *elt,
3178b47e3302SSteven Rostedt (VMware) 				 struct trace_buffer *buffer,  void *rec,
3179466f4528STom Zanussi 				 struct ring_buffer_event *rbe, void *key,
3180466f4528STom Zanussi 				 struct action_data *data, u64 *var_ref_vals)
318150450603STom Zanussi {
3182b47e3302SSteven Rostedt (VMware) 	__update_field_vars(elt, buffer, rbe, rec, hist_data->save_vars,
31837d18a10cSTom Zanussi 			    hist_data->n_save_vars, hist_data->n_field_var_str);
318450450603STom Zanussi }
318550450603STom Zanussi 
create_var(struct hist_trigger_data * hist_data,struct trace_event_file * file,char * name,int size,const char * type)318602205a67STom Zanussi static struct hist_field *create_var(struct hist_trigger_data *hist_data,
318702205a67STom Zanussi 				     struct trace_event_file *file,
318802205a67STom Zanussi 				     char *name, int size, const char *type)
318902205a67STom Zanussi {
319002205a67STom Zanussi 	struct hist_field *var;
319102205a67STom Zanussi 	int idx;
319202205a67STom Zanussi 
319302205a67STom Zanussi 	if (find_var(hist_data, file, name) && !hist_data->remove) {
319402205a67STom Zanussi 		var = ERR_PTR(-EINVAL);
319502205a67STom Zanussi 		goto out;
319602205a67STom Zanussi 	}
319702205a67STom Zanussi 
319802205a67STom Zanussi 	var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
319902205a67STom Zanussi 	if (!var) {
320002205a67STom Zanussi 		var = ERR_PTR(-ENOMEM);
320102205a67STom Zanussi 		goto out;
320202205a67STom Zanussi 	}
320302205a67STom Zanussi 
320402205a67STom Zanussi 	idx = tracing_map_add_var(hist_data->map);
320502205a67STom Zanussi 	if (idx < 0) {
320602205a67STom Zanussi 		kfree(var);
320702205a67STom Zanussi 		var = ERR_PTR(-EINVAL);
320802205a67STom Zanussi 		goto out;
320902205a67STom Zanussi 	}
321002205a67STom Zanussi 
32119da73974SVamshi K Sthambamkadi 	var->ref = 1;
321202205a67STom Zanussi 	var->flags = HIST_FIELD_FL_VAR;
321302205a67STom Zanussi 	var->var.idx = idx;
321402205a67STom Zanussi 	var->var.hist_data = var->hist_data = hist_data;
321502205a67STom Zanussi 	var->size = size;
321602205a67STom Zanussi 	var->var.name = kstrdup(name, GFP_KERNEL);
32173347d80bSSteven Rostedt (VMware) 	var->type = kstrdup_const(type, GFP_KERNEL);
321802205a67STom Zanussi 	if (!var->var.name || !var->type) {
32193347d80bSSteven Rostedt (VMware) 		kfree_const(var->type);
322002205a67STom Zanussi 		kfree(var->var.name);
322102205a67STom Zanussi 		kfree(var);
322202205a67STom Zanussi 		var = ERR_PTR(-ENOMEM);
322302205a67STom Zanussi 	}
322402205a67STom Zanussi  out:
322502205a67STom Zanussi 	return var;
322602205a67STom Zanussi }
322702205a67STom Zanussi 
create_field_var(struct hist_trigger_data * hist_data,struct trace_event_file * file,char * field_name)322802205a67STom Zanussi static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
322902205a67STom Zanussi 					  struct trace_event_file *file,
323002205a67STom Zanussi 					  char *field_name)
323102205a67STom Zanussi {
323202205a67STom Zanussi 	struct hist_field *val = NULL, *var = NULL;
323302205a67STom Zanussi 	unsigned long flags = HIST_FIELD_FL_VAR;
3234d0cd871bSSteven Rostedt (VMware) 	struct trace_array *tr = file->tr;
323502205a67STom Zanussi 	struct field_var *field_var;
323602205a67STom Zanussi 	int ret = 0;
323702205a67STom Zanussi 
323802205a67STom Zanussi 	if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
3239d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
324002205a67STom Zanussi 		ret = -EINVAL;
324102205a67STom Zanussi 		goto err;
324202205a67STom Zanussi 	}
324302205a67STom Zanussi 
324402205a67STom Zanussi 	val = parse_atom(hist_data, file, field_name, &flags, NULL);
324502205a67STom Zanussi 	if (IS_ERR(val)) {
3246d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name));
324702205a67STom Zanussi 		ret = PTR_ERR(val);
324802205a67STom Zanussi 		goto err;
324902205a67STom Zanussi 	}
325002205a67STom Zanussi 
325102205a67STom Zanussi 	var = create_var(hist_data, file, field_name, val->size, val->type);
325202205a67STom Zanussi 	if (IS_ERR(var)) {
3253d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name));
325402205a67STom Zanussi 		kfree(val);
325502205a67STom Zanussi 		ret = PTR_ERR(var);
325602205a67STom Zanussi 		goto err;
325702205a67STom Zanussi 	}
325802205a67STom Zanussi 
325902205a67STom Zanussi 	field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
326002205a67STom Zanussi 	if (!field_var) {
326102205a67STom Zanussi 		kfree(val);
326202205a67STom Zanussi 		kfree(var);
326302205a67STom Zanussi 		ret =  -ENOMEM;
326402205a67STom Zanussi 		goto err;
326502205a67STom Zanussi 	}
326602205a67STom Zanussi 
326702205a67STom Zanussi 	field_var->var = var;
326802205a67STom Zanussi 	field_var->val = val;
326902205a67STom Zanussi  out:
327002205a67STom Zanussi 	return field_var;
327102205a67STom Zanussi  err:
327202205a67STom Zanussi 	field_var = ERR_PTR(ret);
327302205a67STom Zanussi 	goto out;
327402205a67STom Zanussi }
327502205a67STom Zanussi 
327602205a67STom Zanussi /**
327702205a67STom Zanussi  * create_target_field_var - Automatically create a variable for a field
327802205a67STom Zanussi  * @target_hist_data: The target hist trigger
327902205a67STom Zanussi  * @subsys_name: Optional subsystem name
328002205a67STom Zanussi  * @event_name: Optional event name
328102205a67STom Zanussi  * @var_name: The name of the field (and the resulting variable)
328202205a67STom Zanussi  *
328302205a67STom Zanussi  * Hist trigger actions fetch data from variables, not directly from
328402205a67STom Zanussi  * events.  However, for convenience, users are allowed to directly
328502205a67STom Zanussi  * specify an event field in an action, which will be automatically
328602205a67STom Zanussi  * converted into a variable on their behalf.
32870a068f4aSColin Ian King  *
328802205a67STom Zanussi  * This function creates a field variable with the name var_name on
328902205a67STom Zanussi  * the hist trigger currently being defined on the target event.  If
329002205a67STom Zanussi  * subsys_name and event_name are specified, this function simply
329102205a67STom Zanussi  * verifies that they do in fact match the target event subsystem and
329202205a67STom Zanussi  * event name.
329302205a67STom Zanussi  *
329402205a67STom Zanussi  * Return: The variable created for the field.
329502205a67STom Zanussi  */
3296c282a386STom Zanussi static struct field_var *
create_target_field_var(struct hist_trigger_data * target_hist_data,char * subsys_name,char * event_name,char * var_name)329702205a67STom Zanussi create_target_field_var(struct hist_trigger_data *target_hist_data,
329802205a67STom Zanussi 			char *subsys_name, char *event_name, char *var_name)
329902205a67STom Zanussi {
330002205a67STom Zanussi 	struct trace_event_file *file = target_hist_data->event_file;
330102205a67STom Zanussi 
330202205a67STom Zanussi 	if (subsys_name) {
330302205a67STom Zanussi 		struct trace_event_call *call;
330402205a67STom Zanussi 
330502205a67STom Zanussi 		if (!event_name)
330602205a67STom Zanussi 			return NULL;
330702205a67STom Zanussi 
330802205a67STom Zanussi 		call = file->event_call;
330902205a67STom Zanussi 
331002205a67STom Zanussi 		if (strcmp(subsys_name, call->class->system) != 0)
331102205a67STom Zanussi 			return NULL;
331202205a67STom Zanussi 
331302205a67STom Zanussi 		if (strcmp(event_name, trace_event_name(call)) != 0)
331402205a67STom Zanussi 			return NULL;
331502205a67STom Zanussi 	}
331602205a67STom Zanussi 
331702205a67STom Zanussi 	return create_field_var(target_hist_data, file, var_name);
331802205a67STom Zanussi }
331902205a67STom Zanussi 
check_track_val_max(u64 track_val,u64 var_val)3320466f4528STom Zanussi static bool check_track_val_max(u64 track_val, u64 var_val)
3321466f4528STom Zanussi {
3322466f4528STom Zanussi 	if (var_val <= track_val)
3323466f4528STom Zanussi 		return false;
3324466f4528STom Zanussi 
3325466f4528STom Zanussi 	return true;
3326466f4528STom Zanussi }
3327466f4528STom Zanussi 
check_track_val_changed(u64 track_val,u64 var_val)3328dff81f55STom Zanussi static bool check_track_val_changed(u64 track_val, u64 var_val)
3329dff81f55STom Zanussi {
3330dff81f55STom Zanussi 	if (var_val == track_val)
3331dff81f55STom Zanussi 		return false;
3332dff81f55STom Zanussi 
3333dff81f55STom Zanussi 	return true;
3334dff81f55STom Zanussi }
3335dff81f55STom Zanussi 
get_track_val(struct hist_trigger_data * hist_data,struct tracing_map_elt * elt,struct action_data * data)3336466f4528STom Zanussi static u64 get_track_val(struct hist_trigger_data *hist_data,
3337466f4528STom Zanussi 			 struct tracing_map_elt *elt,
3338466f4528STom Zanussi 			 struct action_data *data)
3339466f4528STom Zanussi {
3340466f4528STom Zanussi 	unsigned int track_var_idx = data->track_data.track_var->var.idx;
3341466f4528STom Zanussi 	u64 track_val;
3342466f4528STom Zanussi 
3343466f4528STom Zanussi 	track_val = tracing_map_read_var(elt, track_var_idx);
3344466f4528STom Zanussi 
3345466f4528STom Zanussi 	return track_val;
3346466f4528STom Zanussi }
3347466f4528STom Zanussi 
save_track_val(struct hist_trigger_data * hist_data,struct tracing_map_elt * elt,struct action_data * data,u64 var_val)3348466f4528STom Zanussi static void save_track_val(struct hist_trigger_data *hist_data,
3349466f4528STom Zanussi 			   struct tracing_map_elt *elt,
3350466f4528STom Zanussi 			   struct action_data *data, u64 var_val)
3351466f4528STom Zanussi {
3352466f4528STom Zanussi 	unsigned int track_var_idx = data->track_data.track_var->var.idx;
3353466f4528STom Zanussi 
3354466f4528STom Zanussi 	tracing_map_set_var(elt, track_var_idx, var_val);
3355466f4528STom Zanussi }
3356466f4528STom Zanussi 
save_track_data(struct hist_trigger_data * hist_data,struct tracing_map_elt * elt,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * rbe,void * key,struct action_data * data,u64 * var_ref_vals)3357466f4528STom Zanussi static void save_track_data(struct hist_trigger_data *hist_data,
3358b47e3302SSteven Rostedt (VMware) 			    struct tracing_map_elt *elt,
3359b47e3302SSteven Rostedt (VMware) 			    struct trace_buffer *buffer, void *rec,
3360466f4528STom Zanussi 			    struct ring_buffer_event *rbe, void *key,
3361466f4528STom Zanussi 			    struct action_data *data, u64 *var_ref_vals)
3362466f4528STom Zanussi {
3363466f4528STom Zanussi 	if (data->track_data.save_data)
3364b47e3302SSteven Rostedt (VMware) 		data->track_data.save_data(hist_data, elt, buffer, rec, rbe,
3365b47e3302SSteven Rostedt (VMware) 					   key, data, var_ref_vals);
3366466f4528STom Zanussi }
3367466f4528STom Zanussi 
check_track_val(struct tracing_map_elt * elt,struct action_data * data,u64 var_val)3368466f4528STom Zanussi static bool check_track_val(struct tracing_map_elt *elt,
3369466f4528STom Zanussi 			    struct action_data *data,
3370466f4528STom Zanussi 			    u64 var_val)
3371466f4528STom Zanussi {
3372466f4528STom Zanussi 	struct hist_trigger_data *hist_data;
3373466f4528STom Zanussi 	u64 track_val;
3374466f4528STom Zanussi 
3375466f4528STom Zanussi 	hist_data = data->track_data.track_var->hist_data;
3376466f4528STom Zanussi 	track_val = get_track_val(hist_data, elt, data);
3377466f4528STom Zanussi 
3378466f4528STom Zanussi 	return data->track_data.check_val(track_val, var_val);
3379466f4528STom Zanussi }
3380466f4528STom Zanussi 
3381a3785b7eSTom Zanussi #ifdef CONFIG_TRACER_SNAPSHOT
cond_snapshot_update(struct trace_array * tr,void * cond_data)3382a3785b7eSTom Zanussi static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3383a3785b7eSTom Zanussi {
3384a3785b7eSTom Zanussi 	/* called with tr->max_lock held */
3385a3785b7eSTom Zanussi 	struct track_data *track_data = tr->cond_snapshot->cond_data;
3386a3785b7eSTom Zanussi 	struct hist_elt_data *elt_data, *track_elt_data;
3387a3785b7eSTom Zanussi 	struct snapshot_context *context = cond_data;
33889b2ca371STom Zanussi 	struct action_data *action;
3389a3785b7eSTom Zanussi 	u64 track_val;
3390a3785b7eSTom Zanussi 
3391a3785b7eSTom Zanussi 	if (!track_data)
3392a3785b7eSTom Zanussi 		return false;
3393a3785b7eSTom Zanussi 
33949b2ca371STom Zanussi 	action = track_data->action_data;
33959b2ca371STom Zanussi 
3396a3785b7eSTom Zanussi 	track_val = get_track_val(track_data->hist_data, context->elt,
3397a3785b7eSTom Zanussi 				  track_data->action_data);
3398a3785b7eSTom Zanussi 
33999b2ca371STom Zanussi 	if (!action->track_data.check_val(track_data->track_val, track_val))
34009b2ca371STom Zanussi 		return false;
34019b2ca371STom Zanussi 
3402a3785b7eSTom Zanussi 	track_data->track_val = track_val;
3403a3785b7eSTom Zanussi 	memcpy(track_data->key, context->key, track_data->key_len);
3404a3785b7eSTom Zanussi 
3405a3785b7eSTom Zanussi 	elt_data = context->elt->private_data;
3406a3785b7eSTom Zanussi 	track_elt_data = track_data->elt.private_data;
3407a3785b7eSTom Zanussi 	if (elt_data->comm)
340827242c62STom Zanussi 		strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
3409a3785b7eSTom Zanussi 
3410a3785b7eSTom Zanussi 	track_data->updated = true;
3411a3785b7eSTom Zanussi 
3412a3785b7eSTom Zanussi 	return true;
3413a3785b7eSTom Zanussi }
3414a3785b7eSTom Zanussi 
save_track_data_snapshot(struct hist_trigger_data * hist_data,struct tracing_map_elt * elt,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * rbe,void * key,struct action_data * data,u64 * var_ref_vals)3415a3785b7eSTom Zanussi static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3416b47e3302SSteven Rostedt (VMware) 				     struct tracing_map_elt *elt,
3417b47e3302SSteven Rostedt (VMware) 				     struct trace_buffer *buffer, void *rec,
3418a3785b7eSTom Zanussi 				     struct ring_buffer_event *rbe, void *key,
3419a3785b7eSTom Zanussi 				     struct action_data *data,
3420a3785b7eSTom Zanussi 				     u64 *var_ref_vals)
3421a3785b7eSTom Zanussi {
3422a3785b7eSTom Zanussi 	struct trace_event_file *file = hist_data->event_file;
3423a3785b7eSTom Zanussi 	struct snapshot_context context;
3424a3785b7eSTom Zanussi 
3425a3785b7eSTom Zanussi 	context.elt = elt;
3426a3785b7eSTom Zanussi 	context.key = key;
3427a3785b7eSTom Zanussi 
3428a3785b7eSTom Zanussi 	tracing_snapshot_cond(file->tr, &context);
3429a3785b7eSTom Zanussi }
3430a3785b7eSTom Zanussi 
3431a3785b7eSTom Zanussi static void hist_trigger_print_key(struct seq_file *m,
3432a3785b7eSTom Zanussi 				   struct hist_trigger_data *hist_data,
3433a3785b7eSTom Zanussi 				   void *key,
3434a3785b7eSTom Zanussi 				   struct tracing_map_elt *elt);
3435a3785b7eSTom Zanussi 
snapshot_action(struct hist_trigger_data * hist_data)3436a3785b7eSTom Zanussi static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
3437a3785b7eSTom Zanussi {
3438a3785b7eSTom Zanussi 	unsigned int i;
3439a3785b7eSTom Zanussi 
3440a3785b7eSTom Zanussi 	if (!hist_data->n_actions)
3441a3785b7eSTom Zanussi 		return NULL;
3442a3785b7eSTom Zanussi 
3443a3785b7eSTom Zanussi 	for (i = 0; i < hist_data->n_actions; i++) {
3444a3785b7eSTom Zanussi 		struct action_data *data = hist_data->actions[i];
3445a3785b7eSTom Zanussi 
3446a3785b7eSTom Zanussi 		if (data->action == ACTION_SNAPSHOT)
3447a3785b7eSTom Zanussi 			return data;
3448a3785b7eSTom Zanussi 	}
3449a3785b7eSTom Zanussi 
3450a3785b7eSTom Zanussi 	return NULL;
3451a3785b7eSTom Zanussi }
3452a3785b7eSTom Zanussi 
track_data_snapshot_print(struct seq_file * m,struct hist_trigger_data * hist_data)3453a3785b7eSTom Zanussi static void track_data_snapshot_print(struct seq_file *m,
3454a3785b7eSTom Zanussi 				      struct hist_trigger_data *hist_data)
3455a3785b7eSTom Zanussi {
3456a3785b7eSTom Zanussi 	struct trace_event_file *file = hist_data->event_file;
3457a3785b7eSTom Zanussi 	struct track_data *track_data;
3458a3785b7eSTom Zanussi 	struct action_data *action;
3459a3785b7eSTom Zanussi 
3460a3785b7eSTom Zanussi 	track_data = tracing_cond_snapshot_data(file->tr);
3461a3785b7eSTom Zanussi 	if (!track_data)
3462a3785b7eSTom Zanussi 		return;
3463a3785b7eSTom Zanussi 
3464a3785b7eSTom Zanussi 	if (!track_data->updated)
3465a3785b7eSTom Zanussi 		return;
3466a3785b7eSTom Zanussi 
3467a3785b7eSTom Zanussi 	action = snapshot_action(hist_data);
3468a3785b7eSTom Zanussi 	if (!action)
3469a3785b7eSTom Zanussi 		return;
3470a3785b7eSTom Zanussi 
3471a3785b7eSTom Zanussi 	seq_puts(m, "\nSnapshot taken (see tracing/snapshot).  Details:\n");
3472a3785b7eSTom Zanussi 	seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
3473a3785b7eSTom Zanussi 		   action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
3474a3785b7eSTom Zanussi 		   action->track_data.var_str, track_data->track_val);
3475a3785b7eSTom Zanussi 
3476a3785b7eSTom Zanussi 	seq_puts(m, "\ttriggered by event with key: ");
3477a3785b7eSTom Zanussi 	hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
3478a3785b7eSTom Zanussi 	seq_putc(m, '\n');
3479a3785b7eSTom Zanussi }
3480a3785b7eSTom Zanussi #else
cond_snapshot_update(struct trace_array * tr,void * cond_data)3481a3785b7eSTom Zanussi static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3482a3785b7eSTom Zanussi {
3483a3785b7eSTom Zanussi 	return false;
3484a3785b7eSTom Zanussi }
save_track_data_snapshot(struct hist_trigger_data * hist_data,struct tracing_map_elt * elt,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * rbe,void * key,struct action_data * data,u64 * var_ref_vals)3485a3785b7eSTom Zanussi static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3486b47e3302SSteven Rostedt (VMware) 				     struct tracing_map_elt *elt,
3487b47e3302SSteven Rostedt (VMware) 				     struct trace_buffer *buffer, void *rec,
3488a3785b7eSTom Zanussi 				     struct ring_buffer_event *rbe, void *key,
3489a3785b7eSTom Zanussi 				     struct action_data *data,
3490a3785b7eSTom Zanussi 				     u64 *var_ref_vals) {}
track_data_snapshot_print(struct seq_file * m,struct hist_trigger_data * hist_data)3491a3785b7eSTom Zanussi static void track_data_snapshot_print(struct seq_file *m,
3492a3785b7eSTom Zanussi 				      struct hist_trigger_data *hist_data) {}
3493a3785b7eSTom Zanussi #endif /* CONFIG_TRACER_SNAPSHOT */
3494a3785b7eSTom Zanussi 
track_data_print(struct seq_file * m,struct hist_trigger_data * hist_data,struct tracing_map_elt * elt,struct action_data * data)3495466f4528STom Zanussi static void track_data_print(struct seq_file *m,
349650450603STom Zanussi 			     struct hist_trigger_data *hist_data,
349750450603STom Zanussi 			     struct tracing_map_elt *elt,
349850450603STom Zanussi 			     struct action_data *data)
349950450603STom Zanussi {
3500466f4528STom Zanussi 	u64 track_val = get_track_val(hist_data, elt, data);
3501466f4528STom Zanussi 	unsigned int i, save_var_idx;
350250450603STom Zanussi 
3503466f4528STom Zanussi 	if (data->handler == HANDLER_ONMAX)
3504466f4528STom Zanussi 		seq_printf(m, "\n\tmax: %10llu", track_val);
3505dff81f55STom Zanussi 	else if (data->handler == HANDLER_ONCHANGE)
3506dff81f55STom Zanussi 		seq_printf(m, "\n\tchanged: %10llu", track_val);
350750450603STom Zanussi 
3508a3785b7eSTom Zanussi 	if (data->action == ACTION_SNAPSHOT)
3509a3785b7eSTom Zanussi 		return;
3510a3785b7eSTom Zanussi 
35117d18a10cSTom Zanussi 	for (i = 0; i < hist_data->n_save_vars; i++) {
35127d18a10cSTom Zanussi 		struct hist_field *save_val = hist_data->save_vars[i]->val;
35137d18a10cSTom Zanussi 		struct hist_field *save_var = hist_data->save_vars[i]->var;
351450450603STom Zanussi 		u64 val;
351550450603STom Zanussi 
351650450603STom Zanussi 		save_var_idx = save_var->var.idx;
351750450603STom Zanussi 
351850450603STom Zanussi 		val = tracing_map_read_var(elt, save_var_idx);
351950450603STom Zanussi 
352050450603STom Zanussi 		if (save_val->flags & HIST_FIELD_FL_STRING) {
352150450603STom Zanussi 			seq_printf(m, "  %s: %-32s", save_var->var.name,
352250450603STom Zanussi 				   (char *)(uintptr_t)(val));
352350450603STom Zanussi 		} else
352450450603STom Zanussi 			seq_printf(m, "  %s: %10llu", save_var->var.name, val);
352550450603STom Zanussi 	}
352650450603STom Zanussi }
352750450603STom Zanussi 
ontrack_action(struct hist_trigger_data * hist_data,struct tracing_map_elt * elt,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * rbe,void * key,struct action_data * data,u64 * var_ref_vals)3528466f4528STom Zanussi static void ontrack_action(struct hist_trigger_data *hist_data,
3529b47e3302SSteven Rostedt (VMware) 			   struct tracing_map_elt *elt,
3530b47e3302SSteven Rostedt (VMware) 			   struct trace_buffer *buffer, void *rec,
35317d18a10cSTom Zanussi 			   struct ring_buffer_event *rbe, void *key,
353250450603STom Zanussi 			   struct action_data *data, u64 *var_ref_vals)
353350450603STom Zanussi {
3534466f4528STom Zanussi 	u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
353550450603STom Zanussi 
3536466f4528STom Zanussi 	if (check_track_val(elt, data, var_val)) {
3537466f4528STom Zanussi 		save_track_val(hist_data, elt, data, var_val);
3538b47e3302SSteven Rostedt (VMware) 		save_track_data(hist_data, elt, buffer, rec, rbe,
3539b47e3302SSteven Rostedt (VMware) 				key, data, var_ref_vals);
3540466f4528STom Zanussi 	}
354150450603STom Zanussi }
354250450603STom Zanussi 
action_data_destroy(struct action_data * data)3543c3e49506STom Zanussi static void action_data_destroy(struct action_data *data)
354450450603STom Zanussi {
354550450603STom Zanussi 	unsigned int i;
354650450603STom Zanussi 
3547c3e49506STom Zanussi 	lockdep_assert_held(&event_mutex);
354850450603STom Zanussi 
35497d18a10cSTom Zanussi 	kfree(data->action_name);
355050450603STom Zanussi 
355150450603STom Zanussi 	for (i = 0; i < data->n_params; i++)
355250450603STom Zanussi 		kfree(data->params[i]);
355350450603STom Zanussi 
3554c3e49506STom Zanussi 	if (data->synth_event)
3555c3e49506STom Zanussi 		data->synth_event->ref--;
3556c3e49506STom Zanussi 
3557e91eefd7STom Zanussi 	kfree(data->synth_event_name);
3558e91eefd7STom Zanussi 
355950450603STom Zanussi 	kfree(data);
356050450603STom Zanussi }
356150450603STom Zanussi 
track_data_destroy(struct hist_trigger_data * hist_data,struct action_data * data)3562466f4528STom Zanussi static void track_data_destroy(struct hist_trigger_data *hist_data,
3563466f4528STom Zanussi 			       struct action_data *data)
3564c3e49506STom Zanussi {
3565a3785b7eSTom Zanussi 	struct trace_event_file *file = hist_data->event_file;
3566a3785b7eSTom Zanussi 
3567466f4528STom Zanussi 	destroy_hist_field(data->track_data.track_var, 0);
3568c3e49506STom Zanussi 
3569a3785b7eSTom Zanussi 	if (data->action == ACTION_SNAPSHOT) {
3570a3785b7eSTom Zanussi 		struct track_data *track_data;
3571a3785b7eSTom Zanussi 
3572a3785b7eSTom Zanussi 		track_data = tracing_cond_snapshot_data(file->tr);
3573a3785b7eSTom Zanussi 		if (track_data && track_data->hist_data == hist_data) {
3574a3785b7eSTom Zanussi 			tracing_snapshot_cond_disable(file->tr);
3575a3785b7eSTom Zanussi 			track_data_free(track_data);
3576a3785b7eSTom Zanussi 		}
3577a3785b7eSTom Zanussi 	}
3578a3785b7eSTom Zanussi 
3579466f4528STom Zanussi 	kfree(data->track_data.var_str);
3580c3e49506STom Zanussi 
3581c3e49506STom Zanussi 	action_data_destroy(data);
3582c3e49506STom Zanussi }
3583c3e49506STom Zanussi 
35847d18a10cSTom Zanussi static int action_create(struct hist_trigger_data *hist_data,
35857d18a10cSTom Zanussi 			 struct action_data *data);
35867d18a10cSTom Zanussi 
track_data_create(struct hist_trigger_data * hist_data,struct action_data * data)3587466f4528STom Zanussi static int track_data_create(struct hist_trigger_data *hist_data,
358850450603STom Zanussi 			     struct action_data *data)
358950450603STom Zanussi {
3590466f4528STom Zanussi 	struct hist_field *var_field, *ref_field, *track_var = NULL;
359150450603STom Zanussi 	struct trace_event_file *file = hist_data->event_file;
3592d0cd871bSSteven Rostedt (VMware) 	struct trace_array *tr = file->tr;
3593466f4528STom Zanussi 	char *track_data_var_str;
359450450603STom Zanussi 	int ret = 0;
359550450603STom Zanussi 
3596466f4528STom Zanussi 	track_data_var_str = data->track_data.var_str;
3597466f4528STom Zanussi 	if (track_data_var_str[0] != '$') {
3598d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str));
359950450603STom Zanussi 		return -EINVAL;
3600f404da6eSTom Zanussi 	}
3601466f4528STom Zanussi 	track_data_var_str++;
360250450603STom Zanussi 
3603466f4528STom Zanussi 	var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
3604f404da6eSTom Zanussi 	if (!var_field) {
3605d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str));
360650450603STom Zanussi 		return -EINVAL;
3607f404da6eSTom Zanussi 	}
360850450603STom Zanussi 
3609de40f033STom Zanussi 	ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
361050450603STom Zanussi 	if (!ref_field)
361150450603STom Zanussi 		return -ENOMEM;
361250450603STom Zanussi 
3613466f4528STom Zanussi 	data->track_data.var_ref = ref_field;
361450450603STom Zanussi 
3615466f4528STom Zanussi 	if (data->handler == HANDLER_ONMAX)
3616466f4528STom Zanussi 		track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
3617466f4528STom Zanussi 	if (IS_ERR(track_var)) {
3618d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3619466f4528STom Zanussi 		ret = PTR_ERR(track_var);
362050450603STom Zanussi 		goto out;
362150450603STom Zanussi 	}
3622dff81f55STom Zanussi 
3623dff81f55STom Zanussi 	if (data->handler == HANDLER_ONCHANGE)
3624dff81f55STom Zanussi 		track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
3625dff81f55STom Zanussi 	if (IS_ERR(track_var)) {
3626d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3627dff81f55STom Zanussi 		ret = PTR_ERR(track_var);
3628dff81f55STom Zanussi 		goto out;
3629dff81f55STom Zanussi 	}
3630466f4528STom Zanussi 	data->track_data.track_var = track_var;
363150450603STom Zanussi 
36327d18a10cSTom Zanussi 	ret = action_create(hist_data, data);
363350450603STom Zanussi  out:
363450450603STom Zanussi 	return ret;
363550450603STom Zanussi }
363650450603STom Zanussi 
parse_action_params(struct trace_array * tr,char * params,struct action_data * data)3637d0cd871bSSteven Rostedt (VMware) static int parse_action_params(struct trace_array *tr, char *params,
3638d0cd871bSSteven Rostedt (VMware) 			       struct action_data *data)
363950450603STom Zanussi {
364050450603STom Zanussi 	char *param, *saved_param;
3641e91eefd7STom Zanussi 	bool first_param = true;
364250450603STom Zanussi 	int ret = 0;
364350450603STom Zanussi 
364450450603STom Zanussi 	while (params) {
36457d18a10cSTom Zanussi 		if (data->n_params >= SYNTH_FIELDS_MAX) {
3646d0cd871bSSteven Rostedt (VMware) 			hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0);
36472cc6a528SZheng Yejian 			ret = -EINVAL;
364850450603STom Zanussi 			goto out;
36497d18a10cSTom Zanussi 		}
365050450603STom Zanussi 
365150450603STom Zanussi 		param = strsep(&params, ",");
365250450603STom Zanussi 		if (!param) {
3653d0cd871bSSteven Rostedt (VMware) 			hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0);
365450450603STom Zanussi 			ret = -EINVAL;
365550450603STom Zanussi 			goto out;
365650450603STom Zanussi 		}
365750450603STom Zanussi 
365850450603STom Zanussi 		param = strstrip(param);
365950450603STom Zanussi 		if (strlen(param) < 2) {
3660d0cd871bSSteven Rostedt (VMware) 			hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param));
366150450603STom Zanussi 			ret = -EINVAL;
366250450603STom Zanussi 			goto out;
366350450603STom Zanussi 		}
366450450603STom Zanussi 
366550450603STom Zanussi 		saved_param = kstrdup(param, GFP_KERNEL);
366650450603STom Zanussi 		if (!saved_param) {
366750450603STom Zanussi 			ret = -ENOMEM;
366850450603STom Zanussi 			goto out;
366950450603STom Zanussi 		}
367050450603STom Zanussi 
3671e91eefd7STom Zanussi 		if (first_param && data->use_trace_keyword) {
3672e91eefd7STom Zanussi 			data->synth_event_name = saved_param;
3673e91eefd7STom Zanussi 			first_param = false;
3674e91eefd7STom Zanussi 			continue;
3675e91eefd7STom Zanussi 		}
3676e91eefd7STom Zanussi 		first_param = false;
3677e91eefd7STom Zanussi 
367850450603STom Zanussi 		data->params[data->n_params++] = saved_param;
367950450603STom Zanussi 	}
368050450603STom Zanussi  out:
368150450603STom Zanussi 	return ret;
368250450603STom Zanussi }
368350450603STom Zanussi 
action_parse(struct trace_array * tr,char * str,struct action_data * data,enum handler_id handler)3684d0cd871bSSteven Rostedt (VMware) static int action_parse(struct trace_array *tr, char *str, struct action_data *data,
36857d18a10cSTom Zanussi 			enum handler_id handler)
368650450603STom Zanussi {
36877d18a10cSTom Zanussi 	char *action_name;
36887d18a10cSTom Zanussi 	int ret = 0;
36897d18a10cSTom Zanussi 
36907d18a10cSTom Zanussi 	strsep(&str, ".");
36917d18a10cSTom Zanussi 	if (!str) {
3692d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
36937d18a10cSTom Zanussi 		ret = -EINVAL;
36947d18a10cSTom Zanussi 		goto out;
36957d18a10cSTom Zanussi 	}
36967d18a10cSTom Zanussi 
36977d18a10cSTom Zanussi 	action_name = strsep(&str, "(");
36987d18a10cSTom Zanussi 	if (!action_name || !str) {
3699d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
37007d18a10cSTom Zanussi 		ret = -EINVAL;
37017d18a10cSTom Zanussi 		goto out;
37027d18a10cSTom Zanussi 	}
37037d18a10cSTom Zanussi 
37047d18a10cSTom Zanussi 	if (str_has_prefix(action_name, "save")) {
37057d18a10cSTom Zanussi 		char *params = strsep(&str, ")");
37067d18a10cSTom Zanussi 
37077d18a10cSTom Zanussi 		if (!params) {
3708d0cd871bSSteven Rostedt (VMware) 			hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0);
37097d18a10cSTom Zanussi 			ret = -EINVAL;
37107d18a10cSTom Zanussi 			goto out;
37117d18a10cSTom Zanussi 		}
37127d18a10cSTom Zanussi 
3713d0cd871bSSteven Rostedt (VMware) 		ret = parse_action_params(tr, params, data);
37147d18a10cSTom Zanussi 		if (ret)
37157d18a10cSTom Zanussi 			goto out;
37167d18a10cSTom Zanussi 
37177d18a10cSTom Zanussi 		if (handler == HANDLER_ONMAX)
3718466f4528STom Zanussi 			data->track_data.check_val = check_track_val_max;
3719dff81f55STom Zanussi 		else if (handler == HANDLER_ONCHANGE)
3720dff81f55STom Zanussi 			data->track_data.check_val = check_track_val_changed;
3721466f4528STom Zanussi 		else {
3722d0cd871bSSteven Rostedt (VMware) 			hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3723466f4528STom Zanussi 			ret = -EINVAL;
3724466f4528STom Zanussi 			goto out;
3725466f4528STom Zanussi 		}
37267d18a10cSTom Zanussi 
3727466f4528STom Zanussi 		data->track_data.save_data = save_track_data_vars;
3728466f4528STom Zanussi 		data->fn = ontrack_action;
37297d18a10cSTom Zanussi 		data->action = ACTION_SAVE;
3730a3785b7eSTom Zanussi 	} else if (str_has_prefix(action_name, "snapshot")) {
3731a3785b7eSTom Zanussi 		char *params = strsep(&str, ")");
3732a3785b7eSTom Zanussi 
3733a3785b7eSTom Zanussi 		if (!str) {
3734d0cd871bSSteven Rostedt (VMware) 			hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params));
3735a3785b7eSTom Zanussi 			ret = -EINVAL;
3736a3785b7eSTom Zanussi 			goto out;
3737a3785b7eSTom Zanussi 		}
3738a3785b7eSTom Zanussi 
3739a3785b7eSTom Zanussi 		if (handler == HANDLER_ONMAX)
3740a3785b7eSTom Zanussi 			data->track_data.check_val = check_track_val_max;
3741dff81f55STom Zanussi 		else if (handler == HANDLER_ONCHANGE)
3742dff81f55STom Zanussi 			data->track_data.check_val = check_track_val_changed;
3743a3785b7eSTom Zanussi 		else {
3744d0cd871bSSteven Rostedt (VMware) 			hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3745a3785b7eSTom Zanussi 			ret = -EINVAL;
3746a3785b7eSTom Zanussi 			goto out;
3747a3785b7eSTom Zanussi 		}
3748a3785b7eSTom Zanussi 
3749a3785b7eSTom Zanussi 		data->track_data.save_data = save_track_data_snapshot;
3750a3785b7eSTom Zanussi 		data->fn = ontrack_action;
3751a3785b7eSTom Zanussi 		data->action = ACTION_SNAPSHOT;
37527d18a10cSTom Zanussi 	} else {
37537d18a10cSTom Zanussi 		char *params = strsep(&str, ")");
37547d18a10cSTom Zanussi 
3755e91eefd7STom Zanussi 		if (str_has_prefix(action_name, "trace"))
3756e91eefd7STom Zanussi 			data->use_trace_keyword = true;
3757e91eefd7STom Zanussi 
37587d18a10cSTom Zanussi 		if (params) {
3759d0cd871bSSteven Rostedt (VMware) 			ret = parse_action_params(tr, params, data);
37607d18a10cSTom Zanussi 			if (ret)
37617d18a10cSTom Zanussi 				goto out;
37627d18a10cSTom Zanussi 		}
37637d18a10cSTom Zanussi 
3764466f4528STom Zanussi 		if (handler == HANDLER_ONMAX)
3765466f4528STom Zanussi 			data->track_data.check_val = check_track_val_max;
3766dff81f55STom Zanussi 		else if (handler == HANDLER_ONCHANGE)
3767dff81f55STom Zanussi 			data->track_data.check_val = check_track_val_changed;
3768466f4528STom Zanussi 
3769466f4528STom Zanussi 		if (handler != HANDLER_ONMATCH) {
3770466f4528STom Zanussi 			data->track_data.save_data = action_trace;
3771466f4528STom Zanussi 			data->fn = ontrack_action;
3772466f4528STom Zanussi 		} else
37737d18a10cSTom Zanussi 			data->fn = action_trace;
3774466f4528STom Zanussi 
37757d18a10cSTom Zanussi 		data->action = ACTION_TRACE;
37767d18a10cSTom Zanussi 	}
37777d18a10cSTom Zanussi 
37787d18a10cSTom Zanussi 	data->action_name = kstrdup(action_name, GFP_KERNEL);
37797d18a10cSTom Zanussi 	if (!data->action_name) {
37807d18a10cSTom Zanussi 		ret = -ENOMEM;
37817d18a10cSTom Zanussi 		goto out;
37827d18a10cSTom Zanussi 	}
37837d18a10cSTom Zanussi 
37847d18a10cSTom Zanussi 	data->handler = handler;
37857d18a10cSTom Zanussi  out:
37867d18a10cSTom Zanussi 	return ret;
37877d18a10cSTom Zanussi }
37887d18a10cSTom Zanussi 
track_data_parse(struct hist_trigger_data * hist_data,char * str,enum handler_id handler)3789466f4528STom Zanussi static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
3790466f4528STom Zanussi 					    char *str, enum handler_id handler)
37917d18a10cSTom Zanussi {
379250450603STom Zanussi 	struct action_data *data;
379350450603STom Zanussi 	int ret = -EINVAL;
3794466f4528STom Zanussi 	char *var_str;
379550450603STom Zanussi 
379650450603STom Zanussi 	data = kzalloc(sizeof(*data), GFP_KERNEL);
379750450603STom Zanussi 	if (!data)
379850450603STom Zanussi 		return ERR_PTR(-ENOMEM);
379950450603STom Zanussi 
3800466f4528STom Zanussi 	var_str = strsep(&str, ")");
3801466f4528STom Zanussi 	if (!var_str || !str) {
380250450603STom Zanussi 		ret = -EINVAL;
380350450603STom Zanussi 		goto free;
380450450603STom Zanussi 	}
380550450603STom Zanussi 
3806466f4528STom Zanussi 	data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
3807466f4528STom Zanussi 	if (!data->track_data.var_str) {
380850450603STom Zanussi 		ret = -ENOMEM;
380950450603STom Zanussi 		goto free;
381050450603STom Zanussi 	}
381150450603STom Zanussi 
3812d0cd871bSSteven Rostedt (VMware) 	ret = action_parse(hist_data->event_file->tr, str, data, handler);
381350450603STom Zanussi 	if (ret)
381450450603STom Zanussi 		goto free;
381550450603STom Zanussi  out:
381650450603STom Zanussi 	return data;
381750450603STom Zanussi  free:
3818466f4528STom Zanussi 	track_data_destroy(hist_data, data);
381950450603STom Zanussi 	data = ERR_PTR(ret);
382050450603STom Zanussi 	goto out;
382150450603STom Zanussi }
382250450603STom Zanussi 
onmatch_destroy(struct action_data * data)3823c282a386STom Zanussi static void onmatch_destroy(struct action_data *data)
3824c282a386STom Zanussi {
3825c3e49506STom Zanussi 	kfree(data->match_data.event);
3826c3e49506STom Zanussi 	kfree(data->match_data.event_system);
3827c282a386STom Zanussi 
3828c3e49506STom Zanussi 	action_data_destroy(data);
3829c282a386STom Zanussi }
3830c282a386STom Zanussi 
destroy_field_var(struct field_var * field_var)383102205a67STom Zanussi static void destroy_field_var(struct field_var *field_var)
383202205a67STom Zanussi {
383302205a67STom Zanussi 	if (!field_var)
383402205a67STom Zanussi 		return;
383502205a67STom Zanussi 
383602205a67STom Zanussi 	destroy_hist_field(field_var->var, 0);
383702205a67STom Zanussi 	destroy_hist_field(field_var->val, 0);
383802205a67STom Zanussi 
383902205a67STom Zanussi 	kfree(field_var);
384002205a67STom Zanussi }
384102205a67STom Zanussi 
destroy_field_vars(struct hist_trigger_data * hist_data)384202205a67STom Zanussi static void destroy_field_vars(struct hist_trigger_data *hist_data)
384302205a67STom Zanussi {
384402205a67STom Zanussi 	unsigned int i;
384502205a67STom Zanussi 
384602205a67STom Zanussi 	for (i = 0; i < hist_data->n_field_vars; i++)
384702205a67STom Zanussi 		destroy_field_var(hist_data->field_vars[i]);
38489da73974SVamshi K Sthambamkadi 
38499da73974SVamshi K Sthambamkadi 	for (i = 0; i < hist_data->n_save_vars; i++)
38509da73974SVamshi K Sthambamkadi 		destroy_field_var(hist_data->save_vars[i]);
385102205a67STom Zanussi }
385202205a67STom Zanussi 
save_field_var(struct hist_trigger_data * hist_data,struct field_var * field_var)3853c282a386STom Zanussi static void save_field_var(struct hist_trigger_data *hist_data,
385402205a67STom Zanussi 			   struct field_var *field_var)
385502205a67STom Zanussi {
385602205a67STom Zanussi 	hist_data->field_vars[hist_data->n_field_vars++] = field_var;
385702205a67STom Zanussi 
3858288709c9SSteven Rostedt (Google) 	/* Stack traces are saved in the string storage too */
3859288709c9SSteven Rostedt (Google) 	if (field_var->val->flags & (HIST_FIELD_FL_STRING | HIST_FIELD_FL_STACKTRACE))
386002205a67STom Zanussi 		hist_data->n_field_var_str++;
386102205a67STom Zanussi }
386202205a67STom Zanussi 
3863c282a386STom Zanussi 
check_synth_field(struct synth_event * event,struct hist_field * hist_field,unsigned int field_pos)3864c282a386STom Zanussi static int check_synth_field(struct synth_event *event,
3865c282a386STom Zanussi 			     struct hist_field *hist_field,
3866c282a386STom Zanussi 			     unsigned int field_pos)
3867c282a386STom Zanussi {
3868c282a386STom Zanussi 	struct synth_field *field;
3869c282a386STom Zanussi 
3870c282a386STom Zanussi 	if (field_pos >= event->n_fields)
3871c282a386STom Zanussi 		return -EINVAL;
3872c282a386STom Zanussi 
3873c282a386STom Zanussi 	field = event->fields[field_pos];
3874c282a386STom Zanussi 
3875bd82631dSTom Zanussi 	/*
3876bd82631dSTom Zanussi 	 * A dynamic string synth field can accept static or
3877bd82631dSTom Zanussi 	 * dynamic. A static string synth field can only accept a
3878bd82631dSTom Zanussi 	 * same-sized static string, which is checked for later.
3879bd82631dSTom Zanussi 	 */
3880bd82631dSTom Zanussi 	if (strstr(hist_field->type, "char[") && field->is_string
3881bd82631dSTom Zanussi 	    && field->is_dynamic)
3882bd82631dSTom Zanussi 		return 0;
3883bd82631dSTom Zanussi 
388400cf3d67SSteven Rostedt (Google) 	if (strstr(hist_field->type, "long[") && field->is_stack)
388500cf3d67SSteven Rostedt (Google) 		return 0;
388600cf3d67SSteven Rostedt (Google) 
3887b05e89aeSMasami Hiramatsu 	if (strcmp(field->type, hist_field->type) != 0) {
3888b05e89aeSMasami Hiramatsu 		if (field->size != hist_field->size ||
3889450fec13SSteven Rostedt (VMware) 		    (!field->is_string && field->is_signed != hist_field->is_signed))
3890c282a386STom Zanussi 			return -EINVAL;
3891b05e89aeSMasami Hiramatsu 	}
3892c282a386STom Zanussi 
3893c282a386STom Zanussi 	return 0;
3894c282a386STom Zanussi }
3895c282a386STom Zanussi 
3896c282a386STom Zanussi static struct hist_field *
trace_action_find_var(struct hist_trigger_data * hist_data,struct action_data * data,char * system,char * event,char * var)38977d18a10cSTom Zanussi trace_action_find_var(struct hist_trigger_data *hist_data,
38987d18a10cSTom Zanussi 		      struct action_data *data,
3899c282a386STom Zanussi 		      char *system, char *event, char *var)
3900c282a386STom Zanussi {
3901d0cd871bSSteven Rostedt (VMware) 	struct trace_array *tr = hist_data->event_file->tr;
3902c282a386STom Zanussi 	struct hist_field *hist_field;
3903c282a386STom Zanussi 
3904c282a386STom Zanussi 	var++; /* skip '$' */
3905c282a386STom Zanussi 
3906c282a386STom Zanussi 	hist_field = find_target_event_var(hist_data, system, event, var);
3907c282a386STom Zanussi 	if (!hist_field) {
39087d18a10cSTom Zanussi 		if (!system && data->handler == HANDLER_ONMATCH) {
3909c3e49506STom Zanussi 			system = data->match_data.event_system;
3910c3e49506STom Zanussi 			event = data->match_data.event;
3911c282a386STom Zanussi 		}
3912c282a386STom Zanussi 
3913c282a386STom Zanussi 		hist_field = find_event_var(hist_data, system, event, var);
3914c282a386STom Zanussi 	}
3915c282a386STom Zanussi 
3916f404da6eSTom Zanussi 	if (!hist_field)
3917d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var));
3918f404da6eSTom Zanussi 
3919c282a386STom Zanussi 	return hist_field;
3920c282a386STom Zanussi }
3921c282a386STom Zanussi 
3922c282a386STom Zanussi static struct hist_field *
trace_action_create_field_var(struct hist_trigger_data * hist_data,struct action_data * data,char * system,char * event,char * var)39237d18a10cSTom Zanussi trace_action_create_field_var(struct hist_trigger_data *hist_data,
3924c282a386STom Zanussi 			      struct action_data *data, char *system,
3925c282a386STom Zanussi 			      char *event, char *var)
3926c282a386STom Zanussi {
3927c282a386STom Zanussi 	struct hist_field *hist_field = NULL;
3928c282a386STom Zanussi 	struct field_var *field_var;
3929c282a386STom Zanussi 
3930c282a386STom Zanussi 	/*
3931c282a386STom Zanussi 	 * First try to create a field var on the target event (the
3932c282a386STom Zanussi 	 * currently being defined).  This will create a variable for
3933c282a386STom Zanussi 	 * unqualified fields on the target event, or if qualified,
3934c282a386STom Zanussi 	 * target fields that have qualified names matching the target.
3935c282a386STom Zanussi 	 */
3936c282a386STom Zanussi 	field_var = create_target_field_var(hist_data, system, event, var);
3937c282a386STom Zanussi 
3938c282a386STom Zanussi 	if (field_var && !IS_ERR(field_var)) {
3939c282a386STom Zanussi 		save_field_var(hist_data, field_var);
3940c282a386STom Zanussi 		hist_field = field_var->var;
3941c282a386STom Zanussi 	} else {
3942c282a386STom Zanussi 		field_var = NULL;
3943c282a386STom Zanussi 		/*
39442b5894ccSQiujun Huang 		 * If no explicit system.event is specified, default to
3945c282a386STom Zanussi 		 * looking for fields on the onmatch(system.event.xxx)
3946c282a386STom Zanussi 		 * event.
3947c282a386STom Zanussi 		 */
39487d18a10cSTom Zanussi 		if (!system && data->handler == HANDLER_ONMATCH) {
3949c3e49506STom Zanussi 			system = data->match_data.event_system;
3950c3e49506STom Zanussi 			event = data->match_data.event;
3951c282a386STom Zanussi 		}
3952c282a386STom Zanussi 
39535acce0bfSSteven Rostedt (VMware) 		if (!event)
39545acce0bfSSteven Rostedt (VMware) 			goto free;
3955c282a386STom Zanussi 		/*
3956c282a386STom Zanussi 		 * At this point, we're looking at a field on another
3957c282a386STom Zanussi 		 * event.  Because we can't modify a hist trigger on
3958c282a386STom Zanussi 		 * another event to add a variable for a field, we need
3959c282a386STom Zanussi 		 * to create a new trigger on that event and create the
3960c282a386STom Zanussi 		 * variable at the same time.
3961c282a386STom Zanussi 		 */
3962c282a386STom Zanussi 		hist_field = create_field_var_hist(hist_data, system, event, var);
3963c282a386STom Zanussi 		if (IS_ERR(hist_field))
3964c282a386STom Zanussi 			goto free;
3965c282a386STom Zanussi 	}
3966c282a386STom Zanussi  out:
3967c282a386STom Zanussi 	return hist_field;
3968c282a386STom Zanussi  free:
3969c282a386STom Zanussi 	destroy_field_var(field_var);
3970c282a386STom Zanussi 	hist_field = NULL;
3971c282a386STom Zanussi 	goto out;
3972c282a386STom Zanussi }
3973c282a386STom Zanussi 
trace_action_create(struct hist_trigger_data * hist_data,struct action_data * data)39747d18a10cSTom Zanussi static int trace_action_create(struct hist_trigger_data *hist_data,
3975c282a386STom Zanussi 			       struct action_data *data)
3976c282a386STom Zanussi {
3977d0cd871bSSteven Rostedt (VMware) 	struct trace_array *tr = hist_data->event_file->tr;
3978c282a386STom Zanussi 	char *event_name, *param, *system = NULL;
3979c282a386STom Zanussi 	struct hist_field *hist_field, *var_ref;
3980d380dcdeSTom Zanussi 	unsigned int i;
3981c282a386STom Zanussi 	unsigned int field_pos = 0;
3982c282a386STom Zanussi 	struct synth_event *event;
3983e91eefd7STom Zanussi 	char *synth_event_name;
3984d380dcdeSTom Zanussi 	int var_ref_idx, ret = 0;
3985c282a386STom Zanussi 
39860e2b81f7SMasami Hiramatsu 	lockdep_assert_held(&event_mutex);
39870e2b81f7SMasami Hiramatsu 
398882470f7dSZheng Yejian 	/* Sanity check to avoid out-of-bound write on 'data->var_ref_idx' */
398982470f7dSZheng Yejian 	if (data->n_params > SYNTH_FIELDS_MAX)
399082470f7dSZheng Yejian 		return -EINVAL;
399182470f7dSZheng Yejian 
3992e91eefd7STom Zanussi 	if (data->use_trace_keyword)
3993e91eefd7STom Zanussi 		synth_event_name = data->synth_event_name;
3994e91eefd7STom Zanussi 	else
3995e91eefd7STom Zanussi 		synth_event_name = data->action_name;
3996e91eefd7STom Zanussi 
3997e91eefd7STom Zanussi 	event = find_synth_event(synth_event_name);
3998c282a386STom Zanussi 	if (!event) {
3999d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name));
4000c282a386STom Zanussi 		return -EINVAL;
4001c282a386STom Zanussi 	}
40027d18a10cSTom Zanussi 
4003c282a386STom Zanussi 	event->ref++;
4004c282a386STom Zanussi 
4005c282a386STom Zanussi 	for (i = 0; i < data->n_params; i++) {
4006c282a386STom Zanussi 		char *p;
4007c282a386STom Zanussi 
4008c282a386STom Zanussi 		p = param = kstrdup(data->params[i], GFP_KERNEL);
4009c282a386STom Zanussi 		if (!param) {
4010c282a386STom Zanussi 			ret = -ENOMEM;
4011c282a386STom Zanussi 			goto err;
4012c282a386STom Zanussi 		}
4013c282a386STom Zanussi 
4014c282a386STom Zanussi 		system = strsep(&param, ".");
4015c282a386STom Zanussi 		if (!param) {
4016c282a386STom Zanussi 			param = (char *)system;
4017c282a386STom Zanussi 			system = event_name = NULL;
4018c282a386STom Zanussi 		} else {
4019c282a386STom Zanussi 			event_name = strsep(&param, ".");
4020c282a386STom Zanussi 			if (!param) {
4021c282a386STom Zanussi 				kfree(p);
4022c282a386STom Zanussi 				ret = -EINVAL;
4023c282a386STom Zanussi 				goto err;
4024c282a386STom Zanussi 			}
4025c282a386STom Zanussi 		}
4026c282a386STom Zanussi 
4027c282a386STom Zanussi 		if (param[0] == '$')
40287d18a10cSTom Zanussi 			hist_field = trace_action_find_var(hist_data, data,
40297d18a10cSTom Zanussi 							   system, event_name,
40307d18a10cSTom Zanussi 							   param);
4031c282a386STom Zanussi 		else
40327d18a10cSTom Zanussi 			hist_field = trace_action_create_field_var(hist_data,
40337d18a10cSTom Zanussi 								   data,
4034c282a386STom Zanussi 								   system,
4035c282a386STom Zanussi 								   event_name,
4036c282a386STom Zanussi 								   param);
4037c282a386STom Zanussi 
4038c282a386STom Zanussi 		if (!hist_field) {
4039c282a386STom Zanussi 			kfree(p);
4040c282a386STom Zanussi 			ret = -EINVAL;
4041c282a386STom Zanussi 			goto err;
4042c282a386STom Zanussi 		}
4043c282a386STom Zanussi 
4044c282a386STom Zanussi 		if (check_synth_field(event, hist_field, field_pos) == 0) {
4045de40f033STom Zanussi 			var_ref = create_var_ref(hist_data, hist_field,
4046de40f033STom Zanussi 						 system, event_name);
4047c282a386STom Zanussi 			if (!var_ref) {
4048c282a386STom Zanussi 				kfree(p);
4049c282a386STom Zanussi 				ret = -ENOMEM;
4050c282a386STom Zanussi 				goto err;
4051c282a386STom Zanussi 			}
4052c282a386STom Zanussi 
4053d380dcdeSTom Zanussi 			var_ref_idx = find_var_ref_idx(hist_data, var_ref);
4054d380dcdeSTom Zanussi 			if (WARN_ON(var_ref_idx < 0)) {
4055e629e7b5SXiaoke Wang 				kfree(p);
4056d380dcdeSTom Zanussi 				ret = var_ref_idx;
4057d380dcdeSTom Zanussi 				goto err;
4058d380dcdeSTom Zanussi 			}
4059d380dcdeSTom Zanussi 
4060d380dcdeSTom Zanussi 			data->var_ref_idx[i] = var_ref_idx;
4061d380dcdeSTom Zanussi 
4062c282a386STom Zanussi 			field_pos++;
4063c282a386STom Zanussi 			kfree(p);
4064c282a386STom Zanussi 			continue;
4065c282a386STom Zanussi 		}
4066c282a386STom Zanussi 
4067d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param));
4068c282a386STom Zanussi 		kfree(p);
4069c282a386STom Zanussi 		ret = -EINVAL;
4070c282a386STom Zanussi 		goto err;
4071c282a386STom Zanussi 	}
4072c282a386STom Zanussi 
4073c282a386STom Zanussi 	if (field_pos != event->n_fields) {
4074d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name));
4075c282a386STom Zanussi 		ret = -EINVAL;
4076c282a386STom Zanussi 		goto err;
4077c282a386STom Zanussi 	}
4078c282a386STom Zanussi 
4079c3e49506STom Zanussi 	data->synth_event = event;
4080c282a386STom Zanussi  out:
4081c282a386STom Zanussi 	return ret;
4082c282a386STom Zanussi  err:
4083c282a386STom Zanussi 	event->ref--;
4084c282a386STom Zanussi 
4085c282a386STom Zanussi 	goto out;
4086c282a386STom Zanussi }
4087c282a386STom Zanussi 
action_create(struct hist_trigger_data * hist_data,struct action_data * data)40887d18a10cSTom Zanussi static int action_create(struct hist_trigger_data *hist_data,
40897d18a10cSTom Zanussi 			 struct action_data *data)
40907d18a10cSTom Zanussi {
4091a3785b7eSTom Zanussi 	struct trace_event_file *file = hist_data->event_file;
4092d0cd871bSSteven Rostedt (VMware) 	struct trace_array *tr = file->tr;
4093a3785b7eSTom Zanussi 	struct track_data *track_data;
40947d18a10cSTom Zanussi 	struct field_var *field_var;
40957d18a10cSTom Zanussi 	unsigned int i;
40967d18a10cSTom Zanussi 	char *param;
40977d18a10cSTom Zanussi 	int ret = 0;
40987d18a10cSTom Zanussi 
40997d18a10cSTom Zanussi 	if (data->action == ACTION_TRACE)
41007d18a10cSTom Zanussi 		return trace_action_create(hist_data, data);
41017d18a10cSTom Zanussi 
4102a3785b7eSTom Zanussi 	if (data->action == ACTION_SNAPSHOT) {
4103a3785b7eSTom Zanussi 		track_data = track_data_alloc(hist_data->key_size, data, hist_data);
4104a3785b7eSTom Zanussi 		if (IS_ERR(track_data)) {
4105a3785b7eSTom Zanussi 			ret = PTR_ERR(track_data);
4106a3785b7eSTom Zanussi 			goto out;
4107a3785b7eSTom Zanussi 		}
4108a3785b7eSTom Zanussi 
4109a3785b7eSTom Zanussi 		ret = tracing_snapshot_cond_enable(file->tr, track_data,
4110a3785b7eSTom Zanussi 						   cond_snapshot_update);
4111a3785b7eSTom Zanussi 		if (ret)
4112a3785b7eSTom Zanussi 			track_data_free(track_data);
4113a3785b7eSTom Zanussi 
4114a3785b7eSTom Zanussi 		goto out;
4115a3785b7eSTom Zanussi 	}
4116a3785b7eSTom Zanussi 
41177d18a10cSTom Zanussi 	if (data->action == ACTION_SAVE) {
41187d18a10cSTom Zanussi 		if (hist_data->n_save_vars) {
41197d18a10cSTom Zanussi 			ret = -EEXIST;
4120d0cd871bSSteven Rostedt (VMware) 			hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0);
41217d18a10cSTom Zanussi 			goto out;
41227d18a10cSTom Zanussi 		}
41237d18a10cSTom Zanussi 
41247d18a10cSTom Zanussi 		for (i = 0; i < data->n_params; i++) {
41257d18a10cSTom Zanussi 			param = kstrdup(data->params[i], GFP_KERNEL);
41267d18a10cSTom Zanussi 			if (!param) {
41277d18a10cSTom Zanussi 				ret = -ENOMEM;
41287d18a10cSTom Zanussi 				goto out;
41297d18a10cSTom Zanussi 			}
41307d18a10cSTom Zanussi 
41317d18a10cSTom Zanussi 			field_var = create_target_field_var(hist_data, NULL, NULL, param);
41327d18a10cSTom Zanussi 			if (IS_ERR(field_var)) {
4133d0cd871bSSteven Rostedt (VMware) 				hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL,
4134d0cd871bSSteven Rostedt (VMware) 					 errpos(param));
41357d18a10cSTom Zanussi 				ret = PTR_ERR(field_var);
41367d18a10cSTom Zanussi 				kfree(param);
41377d18a10cSTom Zanussi 				goto out;
41387d18a10cSTom Zanussi 			}
41397d18a10cSTom Zanussi 
41407d18a10cSTom Zanussi 			hist_data->save_vars[hist_data->n_save_vars++] = field_var;
4141288709c9SSteven Rostedt (Google) 			if (field_var->val->flags &
4142288709c9SSteven Rostedt (Google) 			    (HIST_FIELD_FL_STRING | HIST_FIELD_FL_STACKTRACE))
41437d18a10cSTom Zanussi 				hist_data->n_save_var_str++;
41447d18a10cSTom Zanussi 			kfree(param);
41457d18a10cSTom Zanussi 		}
41467d18a10cSTom Zanussi 	}
41477d18a10cSTom Zanussi  out:
41487d18a10cSTom Zanussi 	return ret;
41497d18a10cSTom Zanussi }
41507d18a10cSTom Zanussi 
onmatch_create(struct hist_trigger_data * hist_data,struct action_data * data)41517d18a10cSTom Zanussi static int onmatch_create(struct hist_trigger_data *hist_data,
41527d18a10cSTom Zanussi 			  struct action_data *data)
41537d18a10cSTom Zanussi {
41547d18a10cSTom Zanussi 	return action_create(hist_data, data);
41557d18a10cSTom Zanussi }
41567d18a10cSTom Zanussi 
onmatch_parse(struct trace_array * tr,char * str)4157c282a386STom Zanussi static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
4158c282a386STom Zanussi {
4159c282a386STom Zanussi 	char *match_event, *match_event_system;
4160c282a386STom Zanussi 	struct action_data *data;
4161c282a386STom Zanussi 	int ret = -EINVAL;
4162c282a386STom Zanussi 
4163c282a386STom Zanussi 	data = kzalloc(sizeof(*data), GFP_KERNEL);
4164c282a386STom Zanussi 	if (!data)
4165c282a386STom Zanussi 		return ERR_PTR(-ENOMEM);
4166c282a386STom Zanussi 
4167c282a386STom Zanussi 	match_event = strsep(&str, ")");
4168f404da6eSTom Zanussi 	if (!match_event || !str) {
4169d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event));
4170c282a386STom Zanussi 		goto free;
4171f404da6eSTom Zanussi 	}
4172c282a386STom Zanussi 
4173c282a386STom Zanussi 	match_event_system = strsep(&match_event, ".");
4174f404da6eSTom Zanussi 	if (!match_event) {
4175d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system));
4176c282a386STom Zanussi 		goto free;
4177f404da6eSTom Zanussi 	}
4178c282a386STom Zanussi 
4179f404da6eSTom Zanussi 	if (IS_ERR(event_file(tr, match_event_system, match_event))) {
4180d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event));
4181c282a386STom Zanussi 		goto free;
4182f404da6eSTom Zanussi 	}
4183c282a386STom Zanussi 
4184c3e49506STom Zanussi 	data->match_data.event = kstrdup(match_event, GFP_KERNEL);
4185c3e49506STom Zanussi 	if (!data->match_data.event) {
4186c282a386STom Zanussi 		ret = -ENOMEM;
4187c282a386STom Zanussi 		goto free;
4188c282a386STom Zanussi 	}
4189c282a386STom Zanussi 
4190c3e49506STom Zanussi 	data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
4191c3e49506STom Zanussi 	if (!data->match_data.event_system) {
4192c282a386STom Zanussi 		ret = -ENOMEM;
4193c282a386STom Zanussi 		goto free;
4194c282a386STom Zanussi 	}
4195c282a386STom Zanussi 
4196d0cd871bSSteven Rostedt (VMware) 	ret = action_parse(tr, str, data, HANDLER_ONMATCH);
4197c282a386STom Zanussi 	if (ret)
4198c282a386STom Zanussi 		goto free;
4199c282a386STom Zanussi  out:
4200c282a386STom Zanussi 	return data;
4201c282a386STom Zanussi  free:
4202c282a386STom Zanussi 	onmatch_destroy(data);
4203c282a386STom Zanussi 	data = ERR_PTR(ret);
4204c282a386STom Zanussi 	goto out;
4205c282a386STom Zanussi }
4206c282a386STom Zanussi 
create_hitcount_val(struct hist_trigger_data * hist_data)42077ef224d1STom Zanussi static int create_hitcount_val(struct hist_trigger_data *hist_data)
42087ef224d1STom Zanussi {
42097ef224d1STom Zanussi 	hist_data->fields[HITCOUNT_IDX] =
421030350d65STom Zanussi 		create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
42117ef224d1STom Zanussi 	if (!hist_data->fields[HITCOUNT_IDX])
42127ef224d1STom Zanussi 		return -ENOMEM;
42137ef224d1STom Zanussi 
42147ef224d1STom Zanussi 	hist_data->n_vals++;
421530350d65STom Zanussi 	hist_data->n_fields++;
42167ef224d1STom Zanussi 
42177ef224d1STom Zanussi 	if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
42187ef224d1STom Zanussi 		return -EINVAL;
42197ef224d1STom Zanussi 
42207ef224d1STom Zanussi 	return 0;
42217ef224d1STom Zanussi }
42227ef224d1STom Zanussi 
__create_val_field(struct hist_trigger_data * hist_data,unsigned int val_idx,struct trace_event_file * file,char * var_name,char * field_str,unsigned long flags)422330350d65STom Zanussi static int __create_val_field(struct hist_trigger_data *hist_data,
4224f2606835STom Zanussi 			      unsigned int val_idx,
4225f2606835STom Zanussi 			      struct trace_event_file *file,
422630350d65STom Zanussi 			      char *var_name, char *field_str,
422730350d65STom Zanussi 			      unsigned long flags)
4228f2606835STom Zanussi {
4229100719dcSTom Zanussi 	struct hist_field *hist_field;
42309710b2f3SKalesh Singh 	int ret = 0, n_subexprs = 0;
4231f2606835STom Zanussi 
42329710b2f3SKalesh Singh 	hist_field = parse_expr(hist_data, file, field_str, flags, var_name, &n_subexprs);
4233100719dcSTom Zanussi 	if (IS_ERR(hist_field)) {
4234100719dcSTom Zanussi 		ret = PTR_ERR(hist_field);
42350c4a6b46STom Zanussi 		goto out;
42360c4a6b46STom Zanussi 	}
42370c4a6b46STom Zanussi 
4238e30fbc61SSteven Rostedt (Google) 	/* values and variables should not have some modifiers */
4239e30fbc61SSteven Rostedt (Google) 	if (hist_field->flags & HIST_FIELD_FL_VAR) {
4240e30fbc61SSteven Rostedt (Google) 		/* Variable */
4241e30fbc61SSteven Rostedt (Google) 		if (hist_field->flags & (HIST_FIELD_FL_GRAPH | HIST_FIELD_FL_PERCENT |
4242e30fbc61SSteven Rostedt (Google) 					 HIST_FIELD_FL_BUCKET | HIST_FIELD_FL_LOG2))
4243e30fbc61SSteven Rostedt (Google) 			goto err;
4244e30fbc61SSteven Rostedt (Google) 	} else {
4245e30fbc61SSteven Rostedt (Google) 		/* Value */
4246e0213434SSteven Rostedt (Google) 		if (hist_field->flags & (HIST_FIELD_FL_GRAPH | HIST_FIELD_FL_PERCENT |
4247e0213434SSteven Rostedt (Google) 					 HIST_FIELD_FL_BUCKET | HIST_FIELD_FL_LOG2 |
4248e0213434SSteven Rostedt (Google) 					 HIST_FIELD_FL_SYM | HIST_FIELD_FL_SYM_OFFSET |
4249e30fbc61SSteven Rostedt (Google) 					 HIST_FIELD_FL_SYSCALL | HIST_FIELD_FL_STACKTRACE))
4250e30fbc61SSteven Rostedt (Google) 			goto err;
4251e0213434SSteven Rostedt (Google) 	}
4252e0213434SSteven Rostedt (Google) 
4253100719dcSTom Zanussi 	hist_data->fields[val_idx] = hist_field;
4254f2606835STom Zanussi 
4255f2606835STom Zanussi 	++hist_data->n_vals;
425630350d65STom Zanussi 	++hist_data->n_fields;
4257f2606835STom Zanussi 
425830350d65STom Zanussi 	if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4259f2606835STom Zanussi 		ret = -EINVAL;
4260f2606835STom Zanussi  out:
4261f2606835STom Zanussi 	return ret;
4262e30fbc61SSteven Rostedt (Google)  err:
4263e30fbc61SSteven Rostedt (Google) 	hist_err(file->tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(field_str));
4264e30fbc61SSteven Rostedt (Google) 	return -EINVAL;
4265f2606835STom Zanussi }
4266f2606835STom Zanussi 
create_val_field(struct hist_trigger_data * hist_data,unsigned int val_idx,struct trace_event_file * file,char * field_str)426730350d65STom Zanussi static int create_val_field(struct hist_trigger_data *hist_data,
426830350d65STom Zanussi 			    unsigned int val_idx,
426930350d65STom Zanussi 			    struct trace_event_file *file,
427030350d65STom Zanussi 			    char *field_str)
427130350d65STom Zanussi {
427230350d65STom Zanussi 	if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
427330350d65STom Zanussi 		return -EINVAL;
427430350d65STom Zanussi 
427530350d65STom Zanussi 	return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
427630350d65STom Zanussi }
427730350d65STom Zanussi 
42782d601b98Sliqiong static const char no_comm[] = "(no comm)";
4279ed2cf907SSteven Rostedt (VMware) 
hist_field_execname(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)4280ed2cf907SSteven Rostedt (VMware) static u64 hist_field_execname(struct hist_field *hist_field,
4281ed2cf907SSteven Rostedt (VMware) 			       struct tracing_map_elt *elt,
4282ed2cf907SSteven Rostedt (VMware) 			       struct trace_buffer *buffer,
4283ed2cf907SSteven Rostedt (VMware) 			       struct ring_buffer_event *rbe,
4284ed2cf907SSteven Rostedt (VMware) 			       void *event)
4285ed2cf907SSteven Rostedt (VMware) {
4286ed2cf907SSteven Rostedt (VMware) 	struct hist_elt_data *elt_data;
4287ed2cf907SSteven Rostedt (VMware) 
4288ed2cf907SSteven Rostedt (VMware) 	if (WARN_ON_ONCE(!elt))
4289ed2cf907SSteven Rostedt (VMware) 		return (u64)(unsigned long)no_comm;
4290ed2cf907SSteven Rostedt (VMware) 
4291ed2cf907SSteven Rostedt (VMware) 	elt_data = elt->private_data;
4292ed2cf907SSteven Rostedt (VMware) 
4293ed2cf907SSteven Rostedt (VMware) 	if (WARN_ON_ONCE(!elt_data->comm))
4294ed2cf907SSteven Rostedt (VMware) 		return (u64)(unsigned long)no_comm;
4295ed2cf907SSteven Rostedt (VMware) 
4296ed2cf907SSteven Rostedt (VMware) 	return (u64)(unsigned long)(elt_data->comm);
4297ed2cf907SSteven Rostedt (VMware) }
4298ed2cf907SSteven Rostedt (VMware) 
hist_field_stack(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)4299f5914b30STom Zanussi static u64 hist_field_stack(struct hist_field *hist_field,
4300f5914b30STom Zanussi 			    struct tracing_map_elt *elt,
4301f5914b30STom Zanussi 			    struct trace_buffer *buffer,
4302f5914b30STom Zanussi 			    struct ring_buffer_event *rbe,
4303f5914b30STom Zanussi 			    void *event)
4304f5914b30STom Zanussi {
4305f5914b30STom Zanussi 	u32 str_item = *(u32 *)(event + hist_field->field->offset);
4306f5914b30STom Zanussi 	int str_loc = str_item & 0xffff;
4307f5914b30STom Zanussi 	char *addr = (char *)(event + str_loc);
4308f5914b30STom Zanussi 
4309f5914b30STom Zanussi 	return (u64)(unsigned long)addr;
4310f5914b30STom Zanussi }
4311f5914b30STom Zanussi 
hist_fn_call(struct hist_field * hist_field,struct tracing_map_elt * elt,struct trace_buffer * buffer,struct ring_buffer_event * rbe,void * event)431286087383SSteven Rostedt (Google) static u64 hist_fn_call(struct hist_field *hist_field,
431386087383SSteven Rostedt (Google) 			struct tracing_map_elt *elt,
431486087383SSteven Rostedt (Google) 			struct trace_buffer *buffer,
431586087383SSteven Rostedt (Google) 			struct ring_buffer_event *rbe,
431686087383SSteven Rostedt (Google) 			void *event)
431786087383SSteven Rostedt (Google) {
431886087383SSteven Rostedt (Google) 	switch (hist_field->fn_num) {
431986087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_VAR_REF:
432086087383SSteven Rostedt (Google) 		return hist_field_var_ref(hist_field, elt, buffer, rbe, event);
432186087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_COUNTER:
432286087383SSteven Rostedt (Google) 		return hist_field_counter(hist_field, elt, buffer, rbe, event);
432386087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_CONST:
432486087383SSteven Rostedt (Google) 		return hist_field_const(hist_field, elt, buffer, rbe, event);
432586087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_LOG2:
432686087383SSteven Rostedt (Google) 		return hist_field_log2(hist_field, elt, buffer, rbe, event);
432786087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_BUCKET:
432886087383SSteven Rostedt (Google) 		return hist_field_bucket(hist_field, elt, buffer, rbe, event);
432986087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_TIMESTAMP:
433086087383SSteven Rostedt (Google) 		return hist_field_timestamp(hist_field, elt, buffer, rbe, event);
433186087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_CPU:
433286087383SSteven Rostedt (Google) 		return hist_field_cpu(hist_field, elt, buffer, rbe, event);
433386087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_STRING:
433486087383SSteven Rostedt (Google) 		return hist_field_string(hist_field, elt, buffer, rbe, event);
433586087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_DYNSTRING:
433686087383SSteven Rostedt (Google) 		return hist_field_dynstring(hist_field, elt, buffer, rbe, event);
433786087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_RELDYNSTRING:
433886087383SSteven Rostedt (Google) 		return hist_field_reldynstring(hist_field, elt, buffer, rbe, event);
433986087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_PSTRING:
434086087383SSteven Rostedt (Google) 		return hist_field_pstring(hist_field, elt, buffer, rbe, event);
434186087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_S64:
434286087383SSteven Rostedt (Google) 		return hist_field_s64(hist_field, elt, buffer, rbe, event);
434386087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_U64:
434486087383SSteven Rostedt (Google) 		return hist_field_u64(hist_field, elt, buffer, rbe, event);
434586087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_S32:
434686087383SSteven Rostedt (Google) 		return hist_field_s32(hist_field, elt, buffer, rbe, event);
434786087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_U32:
434886087383SSteven Rostedt (Google) 		return hist_field_u32(hist_field, elt, buffer, rbe, event);
434986087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_S16:
435086087383SSteven Rostedt (Google) 		return hist_field_s16(hist_field, elt, buffer, rbe, event);
435186087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_U16:
435286087383SSteven Rostedt (Google) 		return hist_field_u16(hist_field, elt, buffer, rbe, event);
435386087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_S8:
435486087383SSteven Rostedt (Google) 		return hist_field_s8(hist_field, elt, buffer, rbe, event);
435586087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_U8:
435686087383SSteven Rostedt (Google) 		return hist_field_u8(hist_field, elt, buffer, rbe, event);
435786087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_UMINUS:
435886087383SSteven Rostedt (Google) 		return hist_field_unary_minus(hist_field, elt, buffer, rbe, event);
435986087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_MINUS:
436086087383SSteven Rostedt (Google) 		return hist_field_minus(hist_field, elt, buffer, rbe, event);
436186087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_PLUS:
436286087383SSteven Rostedt (Google) 		return hist_field_plus(hist_field, elt, buffer, rbe, event);
436386087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_DIV:
436486087383SSteven Rostedt (Google) 		return hist_field_div(hist_field, elt, buffer, rbe, event);
436586087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_MULT:
436686087383SSteven Rostedt (Google) 		return hist_field_mult(hist_field, elt, buffer, rbe, event);
436786087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_DIV_POWER2:
436886087383SSteven Rostedt (Google) 		return div_by_power_of_two(hist_field, elt, buffer, rbe, event);
436986087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_DIV_NOT_POWER2:
437086087383SSteven Rostedt (Google) 		return div_by_not_power_of_two(hist_field, elt, buffer, rbe, event);
437186087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_DIV_MULT_SHIFT:
437286087383SSteven Rostedt (Google) 		return div_by_mult_and_shift(hist_field, elt, buffer, rbe, event);
437386087383SSteven Rostedt (Google) 	case HIST_FIELD_FN_EXECNAME:
437486087383SSteven Rostedt (Google) 		return hist_field_execname(hist_field, elt, buffer, rbe, event);
4375f5914b30STom Zanussi 	case HIST_FIELD_FN_STACK:
4376f5914b30STom Zanussi 		return hist_field_stack(hist_field, elt, buffer, rbe, event);
437786087383SSteven Rostedt (Google) 	default:
437886087383SSteven Rostedt (Google) 		return 0;
437986087383SSteven Rostedt (Google) 	}
438086087383SSteven Rostedt (Google) }
438186087383SSteven Rostedt (Google) 
4382ed2cf907SSteven Rostedt (VMware) /* Convert a var that points to common_pid.execname to a string */
update_var_execname(struct hist_field * hist_field)4383ed2cf907SSteven Rostedt (VMware) static void update_var_execname(struct hist_field *hist_field)
4384ed2cf907SSteven Rostedt (VMware) {
4385ed2cf907SSteven Rostedt (VMware) 	hist_field->flags = HIST_FIELD_FL_STRING | HIST_FIELD_FL_VAR |
4386ed2cf907SSteven Rostedt (VMware) 		HIST_FIELD_FL_EXECNAME;
4387ed2cf907SSteven Rostedt (VMware) 	hist_field->size = MAX_FILTER_STR_VAL;
4388ed2cf907SSteven Rostedt (VMware) 	hist_field->is_signed = 0;
4389ed2cf907SSteven Rostedt (VMware) 
4390ed2cf907SSteven Rostedt (VMware) 	kfree_const(hist_field->type);
4391ed2cf907SSteven Rostedt (VMware) 	hist_field->type = "char[]";
4392ed2cf907SSteven Rostedt (VMware) 
439386087383SSteven Rostedt (Google) 	hist_field->fn_num = HIST_FIELD_FN_EXECNAME;
4394ed2cf907SSteven Rostedt (VMware) }
4395ed2cf907SSteven Rostedt (VMware) 
create_var_field(struct hist_trigger_data * hist_data,unsigned int val_idx,struct trace_event_file * file,char * var_name,char * expr_str)439630350d65STom Zanussi static int create_var_field(struct hist_trigger_data *hist_data,
439730350d65STom Zanussi 			    unsigned int val_idx,
439830350d65STom Zanussi 			    struct trace_event_file *file,
439930350d65STom Zanussi 			    char *var_name, char *expr_str)
440030350d65STom Zanussi {
4401d0cd871bSSteven Rostedt (VMware) 	struct trace_array *tr = hist_data->event_file->tr;
440230350d65STom Zanussi 	unsigned long flags = 0;
440363a1e5deSTom Zanussi 	int ret;
440430350d65STom Zanussi 
440530350d65STom Zanussi 	if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
440630350d65STom Zanussi 		return -EINVAL;
4407f404da6eSTom Zanussi 
440830350d65STom Zanussi 	if (find_var(hist_data, file, var_name) && !hist_data->remove) {
4409d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name));
441030350d65STom Zanussi 		return -EINVAL;
441130350d65STom Zanussi 	}
441230350d65STom Zanussi 
441330350d65STom Zanussi 	flags |= HIST_FIELD_FL_VAR;
441430350d65STom Zanussi 	hist_data->n_vars++;
441530350d65STom Zanussi 	if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
441630350d65STom Zanussi 		return -EINVAL;
441730350d65STom Zanussi 
441863a1e5deSTom Zanussi 	ret = __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
441963a1e5deSTom Zanussi 
4420ed2cf907SSteven Rostedt (VMware) 	if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_EXECNAME)
4421ed2cf907SSteven Rostedt (VMware) 		update_var_execname(hist_data->fields[val_idx]);
4422ed2cf907SSteven Rostedt (VMware) 
4423288709c9SSteven Rostedt (Google) 	if (!ret && hist_data->fields[val_idx]->flags &
4424288709c9SSteven Rostedt (Google) 	    (HIST_FIELD_FL_STRING | HIST_FIELD_FL_STACKTRACE))
442563a1e5deSTom Zanussi 		hist_data->fields[val_idx]->var_str_idx = hist_data->n_var_str++;
442663a1e5deSTom Zanussi 
442763a1e5deSTom Zanussi 	return ret;
442830350d65STom Zanussi }
442930350d65STom Zanussi 
create_val_fields(struct hist_trigger_data * hist_data,struct trace_event_file * file)44307ef224d1STom Zanussi static int create_val_fields(struct hist_trigger_data *hist_data,
44317ef224d1STom Zanussi 			     struct trace_event_file *file)
44327ef224d1STom Zanussi {
44335f2e094eSTom Zanussi 	unsigned int i, j = 1, n_hitcount = 0;
4434f2606835STom Zanussi 	char *fields_str, *field_str;
44357ef224d1STom Zanussi 	int ret;
44367ef224d1STom Zanussi 
44377ef224d1STom Zanussi 	ret = create_hitcount_val(hist_data);
4438f2606835STom Zanussi 	if (ret)
4439f2606835STom Zanussi 		goto out;
44407ef224d1STom Zanussi 
4441f2606835STom Zanussi 	fields_str = hist_data->attrs->vals_str;
4442f2606835STom Zanussi 	if (!fields_str)
4443f2606835STom Zanussi 		goto out;
4444f2606835STom Zanussi 
4445f2606835STom Zanussi 	for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
4446f2606835STom Zanussi 		     j < TRACING_MAP_VALS_MAX; i++) {
4447f2606835STom Zanussi 		field_str = strsep(&fields_str, ",");
4448f2606835STom Zanussi 		if (!field_str)
4449f2606835STom Zanussi 			break;
445030350d65STom Zanussi 
44515f2e094eSTom Zanussi 		if (strcmp(field_str, "hitcount") == 0) {
44525f2e094eSTom Zanussi 			if (!n_hitcount++)
4453f2606835STom Zanussi 				continue;
44545f2e094eSTom Zanussi 		}
445530350d65STom Zanussi 
4456f2606835STom Zanussi 		ret = create_val_field(hist_data, j++, file, field_str);
4457f2606835STom Zanussi 		if (ret)
4458f2606835STom Zanussi 			goto out;
4459f2606835STom Zanussi 	}
446030350d65STom Zanussi 
4461f2606835STom Zanussi 	if (fields_str && (strcmp(fields_str, "hitcount") != 0))
4462f2606835STom Zanussi 		ret = -EINVAL;
4463f2606835STom Zanussi  out:
4464ccf47f5cSMasami Hiramatsu (Google) 	/* There is only raw hitcount but nohitcount suppresses it. */
4465ccf47f5cSMasami Hiramatsu (Google) 	if (j == 1 && hist_data->attrs->no_hitcount) {
4466ccf47f5cSMasami Hiramatsu (Google) 		hist_err(hist_data->event_file->tr, HIST_ERR_NEED_NOHC_VAL, 0);
4467ccf47f5cSMasami Hiramatsu (Google) 		ret = -ENOENT;
4468ccf47f5cSMasami Hiramatsu (Google) 	}
4469ccf47f5cSMasami Hiramatsu (Google) 
44707ef224d1STom Zanussi 	return ret;
44717ef224d1STom Zanussi }
44727ef224d1STom Zanussi 
create_key_field(struct hist_trigger_data * hist_data,unsigned int key_idx,unsigned int key_offset,struct trace_event_file * file,char * field_str)44737ef224d1STom Zanussi static int create_key_field(struct hist_trigger_data *hist_data,
44747ef224d1STom Zanussi 			    unsigned int key_idx,
447576a3b0c8STom Zanussi 			    unsigned int key_offset,
44767ef224d1STom Zanussi 			    struct trace_event_file *file,
44777ef224d1STom Zanussi 			    char *field_str)
44787ef224d1STom Zanussi {
4479d0cd871bSSteven Rostedt (VMware) 	struct trace_array *tr = hist_data->event_file->tr;
448030350d65STom Zanussi 	struct hist_field *hist_field = NULL;
44817ef224d1STom Zanussi 	unsigned long flags = 0;
44827ef224d1STom Zanussi 	unsigned int key_size;
44839710b2f3SKalesh Singh 	int ret = 0, n_subexprs = 0;
44847ef224d1STom Zanussi 
448530350d65STom Zanussi 	if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
44867ef224d1STom Zanussi 		return -EINVAL;
44877ef224d1STom Zanussi 
44887ef224d1STom Zanussi 	flags |= HIST_FIELD_FL_KEY;
44897ef224d1STom Zanussi 
449069a0200cSTom Zanussi 	if (strcmp(field_str, "stacktrace") == 0) {
449169a0200cSTom Zanussi 		flags |= HIST_FIELD_FL_STACKTRACE;
449269a0200cSTom Zanussi 		key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
449330350d65STom Zanussi 		hist_field = create_hist_field(hist_data, NULL, flags, NULL);
449469a0200cSTom Zanussi 	} else {
4495100719dcSTom Zanussi 		hist_field = parse_expr(hist_data, file, field_str, flags,
44969710b2f3SKalesh Singh 					NULL, &n_subexprs);
4497100719dcSTom Zanussi 		if (IS_ERR(hist_field)) {
4498100719dcSTom Zanussi 			ret = PTR_ERR(hist_field);
44997ef224d1STom Zanussi 			goto out;
45007ef224d1STom Zanussi 		}
45017ef224d1STom Zanussi 
4502c8d94a18STom Zanussi 		if (field_has_hist_vars(hist_field, 0))	{
4503d0cd871bSSteven Rostedt (VMware) 			hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str));
4504067fe038STom Zanussi 			destroy_hist_field(hist_field, 0);
4505067fe038STom Zanussi 			ret = -EINVAL;
4506067fe038STom Zanussi 			goto out;
4507067fe038STom Zanussi 		}
4508067fe038STom Zanussi 
4509100719dcSTom Zanussi 		key_size = hist_field->size;
4510ad42febeSTom Zanussi 	}
45117ef224d1STom Zanussi 
4512100719dcSTom Zanussi 	hist_data->fields[key_idx] = hist_field;
45137ef224d1STom Zanussi 
45147ef224d1STom Zanussi 	key_size = ALIGN(key_size, sizeof(u64));
45157ef224d1STom Zanussi 	hist_data->fields[key_idx]->size = key_size;
451676a3b0c8STom Zanussi 	hist_data->fields[key_idx]->offset = key_offset;
4517100719dcSTom Zanussi 
451876a3b0c8STom Zanussi 	hist_data->key_size += key_size;
4519100719dcSTom Zanussi 
45207ef224d1STom Zanussi 	if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
45217ef224d1STom Zanussi 		ret = -EINVAL;
45227ef224d1STom Zanussi 		goto out;
45237ef224d1STom Zanussi 	}
45247ef224d1STom Zanussi 
45257ef224d1STom Zanussi 	hist_data->n_keys++;
452630350d65STom Zanussi 	hist_data->n_fields++;
45277ef224d1STom Zanussi 
45287ef224d1STom Zanussi 	if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
45297ef224d1STom Zanussi 		return -EINVAL;
45307ef224d1STom Zanussi 
45317ef224d1STom Zanussi 	ret = key_size;
45327ef224d1STom Zanussi  out:
45337ef224d1STom Zanussi 	return ret;
45347ef224d1STom Zanussi }
45357ef224d1STom Zanussi 
create_key_fields(struct hist_trigger_data * hist_data,struct trace_event_file * file)45367ef224d1STom Zanussi static int create_key_fields(struct hist_trigger_data *hist_data,
45377ef224d1STom Zanussi 			     struct trace_event_file *file)
45387ef224d1STom Zanussi {
453976a3b0c8STom Zanussi 	unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
45407ef224d1STom Zanussi 	char *fields_str, *field_str;
45417ef224d1STom Zanussi 	int ret = -EINVAL;
45427ef224d1STom Zanussi 
45437ef224d1STom Zanussi 	fields_str = hist_data->attrs->keys_str;
45447ef224d1STom Zanussi 	if (!fields_str)
45457ef224d1STom Zanussi 		goto out;
45467ef224d1STom Zanussi 
454776a3b0c8STom Zanussi 	for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
45487ef224d1STom Zanussi 		field_str = strsep(&fields_str, ",");
45497ef224d1STom Zanussi 		if (!field_str)
45507ef224d1STom Zanussi 			break;
455176a3b0c8STom Zanussi 		ret = create_key_field(hist_data, i, key_offset,
455276a3b0c8STom Zanussi 				       file, field_str);
45537ef224d1STom Zanussi 		if (ret < 0)
45547ef224d1STom Zanussi 			goto out;
455576a3b0c8STom Zanussi 		key_offset += ret;
45567ef224d1STom Zanussi 	}
45577ef224d1STom Zanussi 	if (fields_str) {
45587ef224d1STom Zanussi 		ret = -EINVAL;
45597ef224d1STom Zanussi 		goto out;
45607ef224d1STom Zanussi 	}
45617ef224d1STom Zanussi 	ret = 0;
45627ef224d1STom Zanussi  out:
45637ef224d1STom Zanussi 	return ret;
45647ef224d1STom Zanussi }
45657ef224d1STom Zanussi 
create_var_fields(struct hist_trigger_data * hist_data,struct trace_event_file * file)456630350d65STom Zanussi static int create_var_fields(struct hist_trigger_data *hist_data,
456730350d65STom Zanussi 			     struct trace_event_file *file)
456830350d65STom Zanussi {
456930350d65STom Zanussi 	unsigned int i, j = hist_data->n_vals;
457030350d65STom Zanussi 	int ret = 0;
457130350d65STom Zanussi 
457230350d65STom Zanussi 	unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
457330350d65STom Zanussi 
457430350d65STom Zanussi 	for (i = 0; i < n_vars; i++) {
457530350d65STom Zanussi 		char *var_name = hist_data->attrs->var_defs.name[i];
457630350d65STom Zanussi 		char *expr = hist_data->attrs->var_defs.expr[i];
457730350d65STom Zanussi 
457830350d65STom Zanussi 		ret = create_var_field(hist_data, j++, file, var_name, expr);
457930350d65STom Zanussi 		if (ret)
458030350d65STom Zanussi 			goto out;
458130350d65STom Zanussi 	}
458230350d65STom Zanussi  out:
458330350d65STom Zanussi 	return ret;
458430350d65STom Zanussi }
458530350d65STom Zanussi 
free_var_defs(struct hist_trigger_data * hist_data)458630350d65STom Zanussi static void free_var_defs(struct hist_trigger_data *hist_data)
458730350d65STom Zanussi {
458830350d65STom Zanussi 	unsigned int i;
458930350d65STom Zanussi 
459030350d65STom Zanussi 	for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
459130350d65STom Zanussi 		kfree(hist_data->attrs->var_defs.name[i]);
459230350d65STom Zanussi 		kfree(hist_data->attrs->var_defs.expr[i]);
459330350d65STom Zanussi 	}
459430350d65STom Zanussi 
459530350d65STom Zanussi 	hist_data->attrs->var_defs.n_vars = 0;
459630350d65STom Zanussi }
459730350d65STom Zanussi 
parse_var_defs(struct hist_trigger_data * hist_data)459830350d65STom Zanussi static int parse_var_defs(struct hist_trigger_data *hist_data)
459930350d65STom Zanussi {
4600d0cd871bSSteven Rostedt (VMware) 	struct trace_array *tr = hist_data->event_file->tr;
460130350d65STom Zanussi 	char *s, *str, *var_name, *field_str;
460230350d65STom Zanussi 	unsigned int i, j, n_vars = 0;
460330350d65STom Zanussi 	int ret = 0;
460430350d65STom Zanussi 
460530350d65STom Zanussi 	for (i = 0; i < hist_data->attrs->n_assignments; i++) {
460630350d65STom Zanussi 		str = hist_data->attrs->assignment_str[i];
460730350d65STom Zanussi 		for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
460830350d65STom Zanussi 			field_str = strsep(&str, ",");
460930350d65STom Zanussi 			if (!field_str)
461030350d65STom Zanussi 				break;
461130350d65STom Zanussi 
461230350d65STom Zanussi 			var_name = strsep(&field_str, "=");
461330350d65STom Zanussi 			if (!var_name || !field_str) {
4614d0cd871bSSteven Rostedt (VMware) 				hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT,
4615d0cd871bSSteven Rostedt (VMware) 					 errpos(var_name));
461630350d65STom Zanussi 				ret = -EINVAL;
461730350d65STom Zanussi 				goto free;
461830350d65STom Zanussi 			}
461930350d65STom Zanussi 
462030350d65STom Zanussi 			if (n_vars == TRACING_MAP_VARS_MAX) {
4621d0cd871bSSteven Rostedt (VMware) 				hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name));
462230350d65STom Zanussi 				ret = -EINVAL;
462330350d65STom Zanussi 				goto free;
462430350d65STom Zanussi 			}
462530350d65STom Zanussi 
462630350d65STom Zanussi 			s = kstrdup(var_name, GFP_KERNEL);
462730350d65STom Zanussi 			if (!s) {
462830350d65STom Zanussi 				ret = -ENOMEM;
462930350d65STom Zanussi 				goto free;
463030350d65STom Zanussi 			}
463130350d65STom Zanussi 			hist_data->attrs->var_defs.name[n_vars] = s;
463230350d65STom Zanussi 
463330350d65STom Zanussi 			s = kstrdup(field_str, GFP_KERNEL);
463430350d65STom Zanussi 			if (!s) {
46357edc3945SZheng Yejian 				kfree(hist_data->attrs->var_defs.name[n_vars]);
46367edc3945SZheng Yejian 				hist_data->attrs->var_defs.name[n_vars] = NULL;
463730350d65STom Zanussi 				ret = -ENOMEM;
463830350d65STom Zanussi 				goto free;
463930350d65STom Zanussi 			}
464030350d65STom Zanussi 			hist_data->attrs->var_defs.expr[n_vars++] = s;
464130350d65STom Zanussi 
464230350d65STom Zanussi 			hist_data->attrs->var_defs.n_vars = n_vars;
464330350d65STom Zanussi 		}
464430350d65STom Zanussi 	}
464530350d65STom Zanussi 
464630350d65STom Zanussi 	return ret;
464730350d65STom Zanussi  free:
464830350d65STom Zanussi 	free_var_defs(hist_data);
464930350d65STom Zanussi 
465030350d65STom Zanussi 	return ret;
465130350d65STom Zanussi }
465230350d65STom Zanussi 
create_hist_fields(struct hist_trigger_data * hist_data,struct trace_event_file * file)46537ef224d1STom Zanussi static int create_hist_fields(struct hist_trigger_data *hist_data,
46547ef224d1STom Zanussi 			      struct trace_event_file *file)
46557ef224d1STom Zanussi {
46567ef224d1STom Zanussi 	int ret;
46577ef224d1STom Zanussi 
465830350d65STom Zanussi 	ret = parse_var_defs(hist_data);
465930350d65STom Zanussi 	if (ret)
4660fb991f19SZheng Yejian 		return ret;
466130350d65STom Zanussi 
46627ef224d1STom Zanussi 	ret = create_val_fields(hist_data, file);
46637ef224d1STom Zanussi 	if (ret)
46647ef224d1STom Zanussi 		goto out;
46657ef224d1STom Zanussi 
466630350d65STom Zanussi 	ret = create_var_fields(hist_data, file);
466730350d65STom Zanussi 	if (ret)
466830350d65STom Zanussi 		goto out;
466930350d65STom Zanussi 
46707ef224d1STom Zanussi 	ret = create_key_fields(hist_data, file);
4671fb991f19SZheng Yejian 
46727ef224d1STom Zanussi  out:
467330350d65STom Zanussi 	free_var_defs(hist_data);
467430350d65STom Zanussi 
46757ef224d1STom Zanussi 	return ret;
46767ef224d1STom Zanussi }
46777ef224d1STom Zanussi 
is_descending(struct trace_array * tr,const char * str)46784de26c8cSTom Zanussi static int is_descending(struct trace_array *tr, const char *str)
4679e62347d2STom Zanussi {
4680e62347d2STom Zanussi 	if (!str)
4681e62347d2STom Zanussi 		return 0;
4682e62347d2STom Zanussi 
4683e62347d2STom Zanussi 	if (strcmp(str, "descending") == 0)
4684e62347d2STom Zanussi 		return 1;
4685e62347d2STom Zanussi 
4686e62347d2STom Zanussi 	if (strcmp(str, "ascending") == 0)
4687e62347d2STom Zanussi 		return 0;
4688e62347d2STom Zanussi 
46894de26c8cSTom Zanussi 	hist_err(tr, HIST_ERR_INVALID_SORT_MODIFIER, errpos((char *)str));
46904de26c8cSTom Zanussi 
4691e62347d2STom Zanussi 	return -EINVAL;
4692e62347d2STom Zanussi }
4693e62347d2STom Zanussi 
create_sort_keys(struct hist_trigger_data * hist_data)46947ef224d1STom Zanussi static int create_sort_keys(struct hist_trigger_data *hist_data)
46957ef224d1STom Zanussi {
46964de26c8cSTom Zanussi 	struct trace_array *tr = hist_data->event_file->tr;
4697e62347d2STom Zanussi 	char *fields_str = hist_data->attrs->sort_key_str;
4698e62347d2STom Zanussi 	struct tracing_map_sort_key *sort_key;
4699e62347d2STom Zanussi 	int descending, ret = 0;
470030350d65STom Zanussi 	unsigned int i, j, k;
47017ef224d1STom Zanussi 
4702e62347d2STom Zanussi 	hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
47037ef224d1STom Zanussi 
4704e62347d2STom Zanussi 	if (!fields_str)
4705e62347d2STom Zanussi 		goto out;
4706e62347d2STom Zanussi 
4707e62347d2STom Zanussi 	for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
470885013256STom Zanussi 		struct hist_field *hist_field;
4709e62347d2STom Zanussi 		char *field_str, *field_name;
471085013256STom Zanussi 		const char *test_name;
4711e62347d2STom Zanussi 
4712e62347d2STom Zanussi 		sort_key = &hist_data->sort_keys[i];
4713e62347d2STom Zanussi 
4714e62347d2STom Zanussi 		field_str = strsep(&fields_str, ",");
4715b527b638STom Zanussi 		if (!field_str)
4716b527b638STom Zanussi 			break;
4717b527b638STom Zanussi 
4718b527b638STom Zanussi 		if (!*field_str) {
4719e62347d2STom Zanussi 			ret = -EINVAL;
47204de26c8cSTom Zanussi 			hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4721e62347d2STom Zanussi 			break;
4722e62347d2STom Zanussi 		}
4723e62347d2STom Zanussi 
4724e62347d2STom Zanussi 		if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
47254de26c8cSTom Zanussi 			hist_err(tr, HIST_ERR_TOO_MANY_SORT_FIELDS, errpos("sort="));
4726e62347d2STom Zanussi 			ret = -EINVAL;
4727e62347d2STom Zanussi 			break;
4728e62347d2STom Zanussi 		}
4729e62347d2STom Zanussi 
4730e62347d2STom Zanussi 		field_name = strsep(&field_str, ".");
4731b527b638STom Zanussi 		if (!field_name || !*field_name) {
4732e62347d2STom Zanussi 			ret = -EINVAL;
47334de26c8cSTom Zanussi 			hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4734e62347d2STom Zanussi 			break;
4735e62347d2STom Zanussi 		}
4736e62347d2STom Zanussi 
4737e62347d2STom Zanussi 		if (strcmp(field_name, "hitcount") == 0) {
47384de26c8cSTom Zanussi 			descending = is_descending(tr, field_str);
4739e62347d2STom Zanussi 			if (descending < 0) {
4740e62347d2STom Zanussi 				ret = descending;
4741e62347d2STom Zanussi 				break;
4742e62347d2STom Zanussi 			}
4743e62347d2STom Zanussi 			sort_key->descending = descending;
4744e62347d2STom Zanussi 			continue;
4745e62347d2STom Zanussi 		}
4746e62347d2STom Zanussi 
474730350d65STom Zanussi 		for (j = 1, k = 1; j < hist_data->n_fields; j++) {
474830350d65STom Zanussi 			unsigned int idx;
474930350d65STom Zanussi 
475085013256STom Zanussi 			hist_field = hist_data->fields[j];
475130350d65STom Zanussi 			if (hist_field->flags & HIST_FIELD_FL_VAR)
475230350d65STom Zanussi 				continue;
475330350d65STom Zanussi 
475430350d65STom Zanussi 			idx = k++;
475530350d65STom Zanussi 
475685013256STom Zanussi 			test_name = hist_field_name(hist_field, 0);
475785013256STom Zanussi 
475885013256STom Zanussi 			if (strcmp(field_name, test_name) == 0) {
475930350d65STom Zanussi 				sort_key->field_idx = idx;
47604de26c8cSTom Zanussi 				descending = is_descending(tr, field_str);
4761e62347d2STom Zanussi 				if (descending < 0) {
4762e62347d2STom Zanussi 					ret = descending;
4763e62347d2STom Zanussi 					goto out;
4764e62347d2STom Zanussi 				}
4765e62347d2STom Zanussi 				sort_key->descending = descending;
4766e62347d2STom Zanussi 				break;
4767e62347d2STom Zanussi 			}
4768e62347d2STom Zanussi 		}
4769e62347d2STom Zanussi 		if (j == hist_data->n_fields) {
4770e62347d2STom Zanussi 			ret = -EINVAL;
47714de26c8cSTom Zanussi 			hist_err(tr, HIST_ERR_INVALID_SORT_FIELD, errpos(field_name));
4772e62347d2STom Zanussi 			break;
4773e62347d2STom Zanussi 		}
4774e62347d2STom Zanussi 	}
477530350d65STom Zanussi 
4776e62347d2STom Zanussi 	hist_data->n_sort_keys = i;
4777e62347d2STom Zanussi  out:
47787ef224d1STom Zanussi 	return ret;
47797ef224d1STom Zanussi }
47807ef224d1STom Zanussi 
destroy_actions(struct hist_trigger_data * hist_data)47810212e2aaSTom Zanussi static void destroy_actions(struct hist_trigger_data *hist_data)
47820212e2aaSTom Zanussi {
47830212e2aaSTom Zanussi 	unsigned int i;
47840212e2aaSTom Zanussi 
47850212e2aaSTom Zanussi 	for (i = 0; i < hist_data->n_actions; i++) {
47860212e2aaSTom Zanussi 		struct action_data *data = hist_data->actions[i];
47870212e2aaSTom Zanussi 
47887d18a10cSTom Zanussi 		if (data->handler == HANDLER_ONMATCH)
4789c282a386STom Zanussi 			onmatch_destroy(data);
4790dff81f55STom Zanussi 		else if (data->handler == HANDLER_ONMAX ||
4791dff81f55STom Zanussi 			 data->handler == HANDLER_ONCHANGE)
4792466f4528STom Zanussi 			track_data_destroy(hist_data, data);
4793c282a386STom Zanussi 		else
47940212e2aaSTom Zanussi 			kfree(data);
47950212e2aaSTom Zanussi 	}
47960212e2aaSTom Zanussi }
47970212e2aaSTom Zanussi 
parse_actions(struct hist_trigger_data * hist_data)47980212e2aaSTom Zanussi static int parse_actions(struct hist_trigger_data *hist_data)
47990212e2aaSTom Zanussi {
4800c282a386STom Zanussi 	struct trace_array *tr = hist_data->event_file->tr;
4801c282a386STom Zanussi 	struct action_data *data;
48020212e2aaSTom Zanussi 	unsigned int i;
48030212e2aaSTom Zanussi 	int ret = 0;
48040212e2aaSTom Zanussi 	char *str;
4805036876faSSteven Rostedt (VMware) 	int len;
48060212e2aaSTom Zanussi 
48070212e2aaSTom Zanussi 	for (i = 0; i < hist_data->attrs->n_actions; i++) {
48084f1991a9SSteven Rostedt (Google) 		enum handler_id hid = 0;
48094f1991a9SSteven Rostedt (Google) 		char *action_str;
48104f1991a9SSteven Rostedt (Google) 
48110212e2aaSTom Zanussi 		str = hist_data->attrs->action_str[i];
4812c282a386STom Zanussi 
48134f1991a9SSteven Rostedt (Google) 		if ((len = str_has_prefix(str, "onmatch(")))
48144f1991a9SSteven Rostedt (Google) 			hid = HANDLER_ONMATCH;
48154f1991a9SSteven Rostedt (Google) 		else if ((len = str_has_prefix(str, "onmax(")))
48164f1991a9SSteven Rostedt (Google) 			hid = HANDLER_ONMAX;
48174f1991a9SSteven Rostedt (Google) 		else if ((len = str_has_prefix(str, "onchange(")))
48184f1991a9SSteven Rostedt (Google) 			hid = HANDLER_ONCHANGE;
4819c282a386STom Zanussi 
48204f1991a9SSteven Rostedt (Google) 		action_str = str + len;
48214f1991a9SSteven Rostedt (Google) 
48224f1991a9SSteven Rostedt (Google) 		switch (hid) {
48234f1991a9SSteven Rostedt (Google) 		case HANDLER_ONMATCH:
4824c282a386STom Zanussi 			data = onmatch_parse(tr, action_str);
48254f1991a9SSteven Rostedt (Google) 			break;
48264f1991a9SSteven Rostedt (Google) 		case HANDLER_ONMAX:
48274f1991a9SSteven Rostedt (Google) 		case HANDLER_ONCHANGE:
48284f1991a9SSteven Rostedt (Google) 			data = track_data_parse(hist_data, action_str, hid);
48294f1991a9SSteven Rostedt (Google) 			break;
48304f1991a9SSteven Rostedt (Google) 		default:
48314f1991a9SSteven Rostedt (Google) 			data = ERR_PTR(-EINVAL);
4832c282a386STom Zanussi 			break;
4833c282a386STom Zanussi 		}
483450450603STom Zanussi 
483550450603STom Zanussi 		if (IS_ERR(data)) {
483650450603STom Zanussi 			ret = PTR_ERR(data);
483750450603STom Zanussi 			break;
483850450603STom Zanussi 		}
4839c282a386STom Zanussi 
4840c282a386STom Zanussi 		hist_data->actions[hist_data->n_actions++] = data;
48410212e2aaSTom Zanussi 	}
48420212e2aaSTom Zanussi 
48430212e2aaSTom Zanussi 	return ret;
48440212e2aaSTom Zanussi }
48450212e2aaSTom Zanussi 
create_actions(struct hist_trigger_data * hist_data)48467d18a10cSTom Zanussi static int create_actions(struct hist_trigger_data *hist_data)
48470212e2aaSTom Zanussi {
48480212e2aaSTom Zanussi 	struct action_data *data;
48490212e2aaSTom Zanussi 	unsigned int i;
48500212e2aaSTom Zanussi 	int ret = 0;
48510212e2aaSTom Zanussi 
48520212e2aaSTom Zanussi 	for (i = 0; i < hist_data->attrs->n_actions; i++) {
48530212e2aaSTom Zanussi 		data = hist_data->actions[i];
4854c282a386STom Zanussi 
48557d18a10cSTom Zanussi 		if (data->handler == HANDLER_ONMATCH) {
48567d18a10cSTom Zanussi 			ret = onmatch_create(hist_data, data);
4857c282a386STom Zanussi 			if (ret)
48587d18a10cSTom Zanussi 				break;
4859dff81f55STom Zanussi 		} else if (data->handler == HANDLER_ONMAX ||
4860dff81f55STom Zanussi 			   data->handler == HANDLER_ONCHANGE) {
4861466f4528STom Zanussi 			ret = track_data_create(hist_data, data);
486250450603STom Zanussi 			if (ret)
48637d18a10cSTom Zanussi 				break;
48647d18a10cSTom Zanussi 		} else {
48657d18a10cSTom Zanussi 			ret = -EINVAL;
48667d18a10cSTom Zanussi 			break;
4867c282a386STom Zanussi 		}
48680212e2aaSTom Zanussi 	}
48690212e2aaSTom Zanussi 
48700212e2aaSTom Zanussi 	return ret;
48710212e2aaSTom Zanussi }
48720212e2aaSTom Zanussi 
print_actions(struct seq_file * m,struct hist_trigger_data * hist_data,struct tracing_map_elt * elt)487350450603STom Zanussi static void print_actions(struct seq_file *m,
487450450603STom Zanussi 			  struct hist_trigger_data *hist_data,
487550450603STom Zanussi 			  struct tracing_map_elt *elt)
487650450603STom Zanussi {
487750450603STom Zanussi 	unsigned int i;
487850450603STom Zanussi 
487950450603STom Zanussi 	for (i = 0; i < hist_data->n_actions; i++) {
488050450603STom Zanussi 		struct action_data *data = hist_data->actions[i];
488150450603STom Zanussi 
4882a3785b7eSTom Zanussi 		if (data->action == ACTION_SNAPSHOT)
4883a3785b7eSTom Zanussi 			continue;
4884a3785b7eSTom Zanussi 
4885dff81f55STom Zanussi 		if (data->handler == HANDLER_ONMAX ||
4886dff81f55STom Zanussi 		    data->handler == HANDLER_ONCHANGE)
4887466f4528STom Zanussi 			track_data_print(m, hist_data, elt, data);
488850450603STom Zanussi 	}
488950450603STom Zanussi }
489050450603STom Zanussi 
print_action_spec(struct seq_file * m,struct hist_trigger_data * hist_data,struct action_data * data)48917d18a10cSTom Zanussi static void print_action_spec(struct seq_file *m,
48927d18a10cSTom Zanussi 			      struct hist_trigger_data *hist_data,
48937d18a10cSTom Zanussi 			      struct action_data *data)
48947d18a10cSTom Zanussi {
48957d18a10cSTom Zanussi 	unsigned int i;
48967d18a10cSTom Zanussi 
48977d18a10cSTom Zanussi 	if (data->action == ACTION_SAVE) {
48987d18a10cSTom Zanussi 		for (i = 0; i < hist_data->n_save_vars; i++) {
48997d18a10cSTom Zanussi 			seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
49007d18a10cSTom Zanussi 			if (i < hist_data->n_save_vars - 1)
49017d18a10cSTom Zanussi 				seq_puts(m, ",");
49027d18a10cSTom Zanussi 		}
49037d18a10cSTom Zanussi 	} else if (data->action == ACTION_TRACE) {
4904e91eefd7STom Zanussi 		if (data->use_trace_keyword)
4905e91eefd7STom Zanussi 			seq_printf(m, "%s", data->synth_event_name);
49067d18a10cSTom Zanussi 		for (i = 0; i < data->n_params; i++) {
4907e91eefd7STom Zanussi 			if (i || data->use_trace_keyword)
49087d18a10cSTom Zanussi 				seq_puts(m, ",");
49097d18a10cSTom Zanussi 			seq_printf(m, "%s", data->params[i]);
49107d18a10cSTom Zanussi 		}
49117d18a10cSTom Zanussi 	}
49127d18a10cSTom Zanussi }
49137d18a10cSTom Zanussi 
print_track_data_spec(struct seq_file * m,struct hist_trigger_data * hist_data,struct action_data * data)4914466f4528STom Zanussi static void print_track_data_spec(struct seq_file *m,
491550450603STom Zanussi 				  struct hist_trigger_data *hist_data,
491650450603STom Zanussi 				  struct action_data *data)
491750450603STom Zanussi {
4918466f4528STom Zanussi 	if (data->handler == HANDLER_ONMAX)
491950450603STom Zanussi 		seq_puts(m, ":onmax(");
4920dff81f55STom Zanussi 	else if (data->handler == HANDLER_ONCHANGE)
4921dff81f55STom Zanussi 		seq_puts(m, ":onchange(");
4922466f4528STom Zanussi 	seq_printf(m, "%s", data->track_data.var_str);
49237d18a10cSTom Zanussi 	seq_printf(m, ").%s(", data->action_name);
492450450603STom Zanussi 
49257d18a10cSTom Zanussi 	print_action_spec(m, hist_data, data);
49267d18a10cSTom Zanussi 
492750450603STom Zanussi 	seq_puts(m, ")");
492850450603STom Zanussi }
492950450603STom Zanussi 
print_onmatch_spec(struct seq_file * m,struct hist_trigger_data * hist_data,struct action_data * data)4930c282a386STom Zanussi static void print_onmatch_spec(struct seq_file *m,
4931c282a386STom Zanussi 			       struct hist_trigger_data *hist_data,
4932c282a386STom Zanussi 			       struct action_data *data)
4933c282a386STom Zanussi {
4934c3e49506STom Zanussi 	seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
4935c3e49506STom Zanussi 		   data->match_data.event);
4936c282a386STom Zanussi 
49377d18a10cSTom Zanussi 	seq_printf(m, "%s(", data->action_name);
4938c282a386STom Zanussi 
49397d18a10cSTom Zanussi 	print_action_spec(m, hist_data, data);
4940c282a386STom Zanussi 
4941c282a386STom Zanussi 	seq_puts(m, ")");
4942c282a386STom Zanussi }
4943c282a386STom Zanussi 
actions_match(struct hist_trigger_data * hist_data,struct hist_trigger_data * hist_data_test)494448f79473STom Zanussi static bool actions_match(struct hist_trigger_data *hist_data,
494548f79473STom Zanussi 			  struct hist_trigger_data *hist_data_test)
494648f79473STom Zanussi {
494748f79473STom Zanussi 	unsigned int i, j;
494848f79473STom Zanussi 
494948f79473STom Zanussi 	if (hist_data->n_actions != hist_data_test->n_actions)
495048f79473STom Zanussi 		return false;
495148f79473STom Zanussi 
495248f79473STom Zanussi 	for (i = 0; i < hist_data->n_actions; i++) {
495348f79473STom Zanussi 		struct action_data *data = hist_data->actions[i];
495448f79473STom Zanussi 		struct action_data *data_test = hist_data_test->actions[i];
4955e91eefd7STom Zanussi 		char *action_name, *action_name_test;
495648f79473STom Zanussi 
49577d18a10cSTom Zanussi 		if (data->handler != data_test->handler)
49587d18a10cSTom Zanussi 			return false;
49597d18a10cSTom Zanussi 		if (data->action != data_test->action)
496048f79473STom Zanussi 			return false;
496148f79473STom Zanussi 
496248f79473STom Zanussi 		if (data->n_params != data_test->n_params)
496348f79473STom Zanussi 			return false;
496448f79473STom Zanussi 
496548f79473STom Zanussi 		for (j = 0; j < data->n_params; j++) {
496648f79473STom Zanussi 			if (strcmp(data->params[j], data_test->params[j]) != 0)
496748f79473STom Zanussi 				return false;
496848f79473STom Zanussi 		}
496948f79473STom Zanussi 
4970e91eefd7STom Zanussi 		if (data->use_trace_keyword)
4971e91eefd7STom Zanussi 			action_name = data->synth_event_name;
4972e91eefd7STom Zanussi 		else
4973e91eefd7STom Zanussi 			action_name = data->action_name;
4974e91eefd7STom Zanussi 
4975e91eefd7STom Zanussi 		if (data_test->use_trace_keyword)
4976e91eefd7STom Zanussi 			action_name_test = data_test->synth_event_name;
4977e91eefd7STom Zanussi 		else
4978e91eefd7STom Zanussi 			action_name_test = data_test->action_name;
4979e91eefd7STom Zanussi 
4980e91eefd7STom Zanussi 		if (strcmp(action_name, action_name_test) != 0)
498148f79473STom Zanussi 			return false;
49827d18a10cSTom Zanussi 
49837d18a10cSTom Zanussi 		if (data->handler == HANDLER_ONMATCH) {
4984c3e49506STom Zanussi 			if (strcmp(data->match_data.event_system,
4985c3e49506STom Zanussi 				   data_test->match_data.event_system) != 0)
498648f79473STom Zanussi 				return false;
4987c3e49506STom Zanussi 			if (strcmp(data->match_data.event,
4988c3e49506STom Zanussi 				   data_test->match_data.event) != 0)
498948f79473STom Zanussi 				return false;
4990dff81f55STom Zanussi 		} else if (data->handler == HANDLER_ONMAX ||
4991dff81f55STom Zanussi 			   data->handler == HANDLER_ONCHANGE) {
4992466f4528STom Zanussi 			if (strcmp(data->track_data.var_str,
4993466f4528STom Zanussi 				   data_test->track_data.var_str) != 0)
499448f79473STom Zanussi 				return false;
499548f79473STom Zanussi 		}
499648f79473STom Zanussi 	}
499748f79473STom Zanussi 
499848f79473STom Zanussi 	return true;
499948f79473STom Zanussi }
500048f79473STom Zanussi 
500148f79473STom Zanussi 
print_actions_spec(struct seq_file * m,struct hist_trigger_data * hist_data)5002c282a386STom Zanussi static void print_actions_spec(struct seq_file *m,
5003c282a386STom Zanussi 			       struct hist_trigger_data *hist_data)
5004c282a386STom Zanussi {
5005c282a386STom Zanussi 	unsigned int i;
5006c282a386STom Zanussi 
5007c282a386STom Zanussi 	for (i = 0; i < hist_data->n_actions; i++) {
5008c282a386STom Zanussi 		struct action_data *data = hist_data->actions[i];
5009c282a386STom Zanussi 
50107d18a10cSTom Zanussi 		if (data->handler == HANDLER_ONMATCH)
5011c282a386STom Zanussi 			print_onmatch_spec(m, hist_data, data);
5012dff81f55STom Zanussi 		else if (data->handler == HANDLER_ONMAX ||
5013dff81f55STom Zanussi 			 data->handler == HANDLER_ONCHANGE)
5014466f4528STom Zanussi 			print_track_data_spec(m, hist_data, data);
5015c282a386STom Zanussi 	}
5016c282a386STom Zanussi }
5017c282a386STom Zanussi 
destroy_field_var_hists(struct hist_trigger_data * hist_data)501802205a67STom Zanussi static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
501902205a67STom Zanussi {
502002205a67STom Zanussi 	unsigned int i;
502102205a67STom Zanussi 
502202205a67STom Zanussi 	for (i = 0; i < hist_data->n_field_var_hists; i++) {
502302205a67STom Zanussi 		kfree(hist_data->field_var_hists[i]->cmd);
502402205a67STom Zanussi 		kfree(hist_data->field_var_hists[i]);
502502205a67STom Zanussi 	}
502602205a67STom Zanussi }
502702205a67STom Zanussi 
destroy_hist_data(struct hist_trigger_data * hist_data)50287ef224d1STom Zanussi static void destroy_hist_data(struct hist_trigger_data *hist_data)
50297ef224d1STom Zanussi {
50300212e2aaSTom Zanussi 	if (!hist_data)
50310212e2aaSTom Zanussi 		return;
50320212e2aaSTom Zanussi 
50337ef224d1STom Zanussi 	destroy_hist_trigger_attrs(hist_data->attrs);
50347ef224d1STom Zanussi 	destroy_hist_fields(hist_data);
50357ef224d1STom Zanussi 	tracing_map_destroy(hist_data->map);
50360212e2aaSTom Zanussi 
50370212e2aaSTom Zanussi 	destroy_actions(hist_data);
503802205a67STom Zanussi 	destroy_field_vars(hist_data);
503902205a67STom Zanussi 	destroy_field_var_hists(hist_data);
50400212e2aaSTom Zanussi 
50417ef224d1STom Zanussi 	kfree(hist_data);
50427ef224d1STom Zanussi }
50437ef224d1STom Zanussi 
create_tracing_map_fields(struct hist_trigger_data * hist_data)50447ef224d1STom Zanussi static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
50457ef224d1STom Zanussi {
50467ef224d1STom Zanussi 	struct tracing_map *map = hist_data->map;
50477ef224d1STom Zanussi 	struct ftrace_event_field *field;
50487ef224d1STom Zanussi 	struct hist_field *hist_field;
5049b28d7b2dSDan Carpenter 	int i, idx = 0;
50507ef224d1STom Zanussi 
50517ef224d1STom Zanussi 	for_each_hist_field(i, hist_data) {
50527ef224d1STom Zanussi 		hist_field = hist_data->fields[i];
50537ef224d1STom Zanussi 		if (hist_field->flags & HIST_FIELD_FL_KEY) {
50547ef224d1STom Zanussi 			tracing_map_cmp_fn_t cmp_fn;
50557ef224d1STom Zanussi 
50567ef224d1STom Zanussi 			field = hist_field->field;
50577ef224d1STom Zanussi 
505869a0200cSTom Zanussi 			if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
505969a0200cSTom Zanussi 				cmp_fn = tracing_map_cmp_none;
50601d1898f6SSteven Rostedt (Google) 			else if (!field || hist_field->flags & HIST_FIELD_FL_CPU)
5061ad42febeSTom Zanussi 				cmp_fn = tracing_map_cmp_num(hist_field->size,
5062ad42febeSTom Zanussi 							     hist_field->is_signed);
506369a0200cSTom Zanussi 			else if (is_string_field(field))
50647ef224d1STom Zanussi 				cmp_fn = tracing_map_cmp_string;
50657ef224d1STom Zanussi 			else
50667ef224d1STom Zanussi 				cmp_fn = tracing_map_cmp_num(field->size,
50677ef224d1STom Zanussi 							     field->is_signed);
506876a3b0c8STom Zanussi 			idx = tracing_map_add_key_field(map,
506976a3b0c8STom Zanussi 							hist_field->offset,
507076a3b0c8STom Zanussi 							cmp_fn);
507130350d65STom Zanussi 		} else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
50727ef224d1STom Zanussi 			idx = tracing_map_add_sum_field(map);
50737ef224d1STom Zanussi 
50747ef224d1STom Zanussi 		if (idx < 0)
50757ef224d1STom Zanussi 			return idx;
507630350d65STom Zanussi 
507730350d65STom Zanussi 		if (hist_field->flags & HIST_FIELD_FL_VAR) {
507830350d65STom Zanussi 			idx = tracing_map_add_var(map);
507930350d65STom Zanussi 			if (idx < 0)
508030350d65STom Zanussi 				return idx;
508130350d65STom Zanussi 			hist_field->var.idx = idx;
508230350d65STom Zanussi 			hist_field->var.hist_data = hist_data;
508330350d65STom Zanussi 		}
50847ef224d1STom Zanussi 	}
50857ef224d1STom Zanussi 
50867ef224d1STom Zanussi 	return 0;
50877ef224d1STom Zanussi }
50887ef224d1STom Zanussi 
50897ef224d1STom Zanussi static struct hist_trigger_data *
create_hist_data(unsigned int map_bits,struct hist_trigger_attrs * attrs,struct trace_event_file * file,bool remove)50907ef224d1STom Zanussi create_hist_data(unsigned int map_bits,
50917ef224d1STom Zanussi 		 struct hist_trigger_attrs *attrs,
509230350d65STom Zanussi 		 struct trace_event_file *file,
509330350d65STom Zanussi 		 bool remove)
50947ef224d1STom Zanussi {
50956b4827adSTom Zanussi 	const struct tracing_map_ops *map_ops = NULL;
50967ef224d1STom Zanussi 	struct hist_trigger_data *hist_data;
50977ef224d1STom Zanussi 	int ret = 0;
50987ef224d1STom Zanussi 
50997ef224d1STom Zanussi 	hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
51007ef224d1STom Zanussi 	if (!hist_data)
51017ef224d1STom Zanussi 		return ERR_PTR(-ENOMEM);
51027ef224d1STom Zanussi 
51037ef224d1STom Zanussi 	hist_data->attrs = attrs;
510430350d65STom Zanussi 	hist_data->remove = remove;
5105067fe038STom Zanussi 	hist_data->event_file = file;
51067ef224d1STom Zanussi 
51070212e2aaSTom Zanussi 	ret = parse_actions(hist_data);
51080212e2aaSTom Zanussi 	if (ret)
51090212e2aaSTom Zanussi 		goto free;
51100212e2aaSTom Zanussi 
51117ef224d1STom Zanussi 	ret = create_hist_fields(hist_data, file);
51127ef224d1STom Zanussi 	if (ret)
51137ef224d1STom Zanussi 		goto free;
51147ef224d1STom Zanussi 
51157ef224d1STom Zanussi 	ret = create_sort_keys(hist_data);
51167ef224d1STom Zanussi 	if (ret)
51177ef224d1STom Zanussi 		goto free;
51187ef224d1STom Zanussi 
5119af6a29bcSTom Zanussi 	map_ops = &hist_trigger_elt_data_ops;
51206b4827adSTom Zanussi 
51217ef224d1STom Zanussi 	hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
51226b4827adSTom Zanussi 					    map_ops, hist_data);
51237ef224d1STom Zanussi 	if (IS_ERR(hist_data->map)) {
51247ef224d1STom Zanussi 		ret = PTR_ERR(hist_data->map);
51257ef224d1STom Zanussi 		hist_data->map = NULL;
51267ef224d1STom Zanussi 		goto free;
51277ef224d1STom Zanussi 	}
51287ef224d1STom Zanussi 
51297ef224d1STom Zanussi 	ret = create_tracing_map_fields(hist_data);
51307ef224d1STom Zanussi 	if (ret)
51317ef224d1STom Zanussi 		goto free;
51327ef224d1STom Zanussi  out:
51337ef224d1STom Zanussi 	return hist_data;
51347ef224d1STom Zanussi  free:
51357ef224d1STom Zanussi 	hist_data->attrs = NULL;
51367ef224d1STom Zanussi 
51377ef224d1STom Zanussi 	destroy_hist_data(hist_data);
51387ef224d1STom Zanussi 
51397ef224d1STom Zanussi 	hist_data = ERR_PTR(ret);
51407ef224d1STom Zanussi 
51417ef224d1STom Zanussi 	goto out;
51427ef224d1STom Zanussi }
51437ef224d1STom Zanussi 
hist_trigger_elt_update(struct hist_trigger_data * hist_data,struct tracing_map_elt * elt,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * rbe,u64 * var_ref_vals)51447ef224d1STom Zanussi static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
5145b47e3302SSteven Rostedt (VMware) 				    struct tracing_map_elt *elt,
5146b47e3302SSteven Rostedt (VMware) 				    struct trace_buffer *buffer, void *rec,
5147067fe038STom Zanussi 				    struct ring_buffer_event *rbe,
5148067fe038STom Zanussi 				    u64 *var_ref_vals)
51497ef224d1STom Zanussi {
5150067fe038STom Zanussi 	struct hist_elt_data *elt_data;
51517ef224d1STom Zanussi 	struct hist_field *hist_field;
515230350d65STom Zanussi 	unsigned int i, var_idx;
51537ef224d1STom Zanussi 	u64 hist_val;
51547ef224d1STom Zanussi 
5155067fe038STom Zanussi 	elt_data = elt->private_data;
5156067fe038STom Zanussi 	elt_data->var_ref_vals = var_ref_vals;
5157067fe038STom Zanussi 
51587ef224d1STom Zanussi 	for_each_hist_val_field(i, hist_data) {
51597ef224d1STom Zanussi 		hist_field = hist_data->fields[i];
516086087383SSteven Rostedt (Google) 		hist_val = hist_fn_call(hist_field, elt, buffer, rbe, rec);
516130350d65STom Zanussi 		if (hist_field->flags & HIST_FIELD_FL_VAR) {
516230350d65STom Zanussi 			var_idx = hist_field->var.idx;
516363a1e5deSTom Zanussi 
5164288709c9SSteven Rostedt (Google) 			if (hist_field->flags &
5165288709c9SSteven Rostedt (Google) 			    (HIST_FIELD_FL_STRING | HIST_FIELD_FL_STACKTRACE)) {
516663a1e5deSTom Zanussi 				unsigned int str_start, var_str_idx, idx;
516763a1e5deSTom Zanussi 				char *str, *val_str;
5168938aa33fSSteven Rostedt (VMware) 				unsigned int size;
516963a1e5deSTom Zanussi 
517063a1e5deSTom Zanussi 				str_start = hist_data->n_field_var_str +
517163a1e5deSTom Zanussi 					hist_data->n_save_var_str;
517263a1e5deSTom Zanussi 				var_str_idx = hist_field->var_str_idx;
517363a1e5deSTom Zanussi 				idx = str_start + var_str_idx;
517463a1e5deSTom Zanussi 
517563a1e5deSTom Zanussi 				str = elt_data->field_var_str[idx];
517663a1e5deSTom Zanussi 				val_str = (char *)(uintptr_t)hist_val;
5177938aa33fSSteven Rostedt (VMware) 
5178288709c9SSteven Rostedt (Google) 				if (hist_field->flags & HIST_FIELD_FL_STRING) {
5179938aa33fSSteven Rostedt (VMware) 					size = min(hist_field->size, STR_VAR_LEN_MAX);
5180938aa33fSSteven Rostedt (VMware) 					strscpy(str, val_str, size);
5181288709c9SSteven Rostedt (Google) 				} else {
5182fc1a9dc1STom Zanussi 					char *stack_start = str + sizeof(unsigned long);
5183288709c9SSteven Rostedt (Google) 					int e;
518463a1e5deSTom Zanussi 
5185fc1a9dc1STom Zanussi 					e = stack_trace_save((void *)stack_start,
5186288709c9SSteven Rostedt (Google) 							     HIST_STACKTRACE_DEPTH,
5187288709c9SSteven Rostedt (Google) 							     HIST_STACKTRACE_SKIP);
5188288709c9SSteven Rostedt (Google) 					if (e < HIST_STACKTRACE_DEPTH - 1)
5189fc1a9dc1STom Zanussi 						((unsigned long *)stack_start)[e] = 0;
5190fc1a9dc1STom Zanussi 					*((unsigned long *)str) = e;
5191288709c9SSteven Rostedt (Google) 				}
519263a1e5deSTom Zanussi 				hist_val = (u64)(uintptr_t)str;
519363a1e5deSTom Zanussi 			}
519430350d65STom Zanussi 			tracing_map_set_var(elt, var_idx, hist_val);
519530350d65STom Zanussi 			continue;
519630350d65STom Zanussi 		}
51977ef224d1STom Zanussi 		tracing_map_update_sum(elt, i, hist_val);
51987ef224d1STom Zanussi 	}
519930350d65STom Zanussi 
520030350d65STom Zanussi 	for_each_hist_key_field(i, hist_data) {
520130350d65STom Zanussi 		hist_field = hist_data->fields[i];
520230350d65STom Zanussi 		if (hist_field->flags & HIST_FIELD_FL_VAR) {
520386087383SSteven Rostedt (Google) 			hist_val = hist_fn_call(hist_field, elt, buffer, rbe, rec);
520430350d65STom Zanussi 			var_idx = hist_field->var.idx;
520530350d65STom Zanussi 			tracing_map_set_var(elt, var_idx, hist_val);
520630350d65STom Zanussi 		}
520730350d65STom Zanussi 	}
520802205a67STom Zanussi 
5209b47e3302SSteven Rostedt (VMware) 	update_field_vars(hist_data, elt, buffer, rbe, rec);
52107ef224d1STom Zanussi }
52117ef224d1STom Zanussi 
add_to_key(char * compound_key,void * key,struct hist_field * key_field,void * rec)52126a475cb1STom Zanussi static inline void add_to_key(char *compound_key, void *key,
52136a475cb1STom Zanussi 			      struct hist_field *key_field, void *rec)
52146a475cb1STom Zanussi {
52156a475cb1STom Zanussi 	size_t size = key_field->size;
52166a475cb1STom Zanussi 
52176a475cb1STom Zanussi 	if (key_field->flags & HIST_FIELD_FL_STRING) {
52186a475cb1STom Zanussi 		struct ftrace_event_field *field;
52196a475cb1STom Zanussi 
52206a475cb1STom Zanussi 		field = key_field->field;
522105770dd0SMasami Hiramatsu 		if (field->filter_type == FILTER_DYN_STRING ||
522205770dd0SMasami Hiramatsu 		    field->filter_type == FILTER_RDYN_STRING)
52236a475cb1STom Zanussi 			size = *(u32 *)(rec + field->offset) >> 16;
52246a475cb1STom Zanussi 		else if (field->filter_type == FILTER_STATIC_STRING)
52256a475cb1STom Zanussi 			size = field->size;
52266a475cb1STom Zanussi 
52276a475cb1STom Zanussi 		/* ensure NULL-termination */
52286a475cb1STom Zanussi 		if (size > key_field->size - 1)
52296a475cb1STom Zanussi 			size = key_field->size - 1;
52306a475cb1STom Zanussi 
52319f0bbf31STom Zanussi 		strncpy(compound_key + key_field->offset, (char *)key, size);
52329f0bbf31STom Zanussi 	} else
52336a475cb1STom Zanussi 		memcpy(compound_key + key_field->offset, key, size);
52346a475cb1STom Zanussi }
52356a475cb1STom Zanussi 
52360212e2aaSTom Zanussi static void
hist_trigger_actions(struct hist_trigger_data * hist_data,struct tracing_map_elt * elt,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * rbe,void * key,u64 * var_ref_vals)52370212e2aaSTom Zanussi hist_trigger_actions(struct hist_trigger_data *hist_data,
5238b47e3302SSteven Rostedt (VMware) 		     struct tracing_map_elt *elt,
5239b47e3302SSteven Rostedt (VMware) 		     struct trace_buffer *buffer, void *rec,
52407d18a10cSTom Zanussi 		     struct ring_buffer_event *rbe, void *key,
52417d18a10cSTom Zanussi 		     u64 *var_ref_vals)
52420212e2aaSTom Zanussi {
52430212e2aaSTom Zanussi 	struct action_data *data;
52440212e2aaSTom Zanussi 	unsigned int i;
52450212e2aaSTom Zanussi 
52460212e2aaSTom Zanussi 	for (i = 0; i < hist_data->n_actions; i++) {
52470212e2aaSTom Zanussi 		data = hist_data->actions[i];
5248b47e3302SSteven Rostedt (VMware) 		data->fn(hist_data, elt, buffer, rec, rbe, key, data, var_ref_vals);
52490212e2aaSTom Zanussi 	}
52500212e2aaSTom Zanussi }
52510212e2aaSTom Zanussi 
event_hist_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * rbe)5252b47e3302SSteven Rostedt (VMware) static void event_hist_trigger(struct event_trigger_data *data,
5253b47e3302SSteven Rostedt (VMware) 			       struct trace_buffer *buffer, void *rec,
5254fbd302cbSTom Zanussi 			       struct ring_buffer_event *rbe)
52557ef224d1STom Zanussi {
52567ef224d1STom Zanussi 	struct hist_trigger_data *hist_data = data->private_data;
52576a475cb1STom Zanussi 	bool use_compound_key = (hist_data->n_keys > 1);
525869a0200cSTom Zanussi 	unsigned long entries[HIST_STACKTRACE_DEPTH];
5259067fe038STom Zanussi 	u64 var_ref_vals[TRACING_MAP_VARS_MAX];
526076a3b0c8STom Zanussi 	char compound_key[HIST_KEY_SIZE_MAX];
5261df35d93bSTom Zanussi 	struct tracing_map_elt *elt = NULL;
52627ef224d1STom Zanussi 	struct hist_field *key_field;
52637ef224d1STom Zanussi 	u64 field_contents;
52647ef224d1STom Zanussi 	void *key = NULL;
52657ef224d1STom Zanussi 	unsigned int i;
52667ef224d1STom Zanussi 
5267ef38c79aSSteven Rostedt (Google) 	if (unlikely(!rbe))
5268ef38c79aSSteven Rostedt (Google) 		return;
5269ef38c79aSSteven Rostedt (Google) 
527076a3b0c8STom Zanussi 	memset(compound_key, 0, hist_data->key_size);
527176a3b0c8STom Zanussi 
52727ef224d1STom Zanussi 	for_each_hist_key_field(i, hist_data) {
52737ef224d1STom Zanussi 		key_field = hist_data->fields[i];
52747ef224d1STom Zanussi 
527569a0200cSTom Zanussi 		if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5276e7d91663SThomas Gleixner 			memset(entries, 0, HIST_STACKTRACE_SIZE);
5277f5914b30STom Zanussi 			if (key_field->field) {
5278f5914b30STom Zanussi 				unsigned long *stack, n_entries;
5279f5914b30STom Zanussi 
5280f5914b30STom Zanussi 				field_contents = hist_fn_call(key_field, elt, buffer, rbe, rec);
5281f5914b30STom Zanussi 				stack = (unsigned long *)(long)field_contents;
5282f5914b30STom Zanussi 				n_entries = *stack;
5283f5914b30STom Zanussi 				memcpy(entries, ++stack, n_entries * sizeof(unsigned long));
5284f5914b30STom Zanussi 			} else {
5285e7d91663SThomas Gleixner 				stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
5286e7d91663SThomas Gleixner 						 HIST_STACKTRACE_SKIP);
5287f5914b30STom Zanussi 			}
528869a0200cSTom Zanussi 			key = entries;
528969a0200cSTom Zanussi 		} else {
529086087383SSteven Rostedt (Google) 			field_contents = hist_fn_call(key_field, elt, buffer, rbe, rec);
529179e577cbSNamhyung Kim 			if (key_field->flags & HIST_FIELD_FL_STRING) {
52926a475cb1STom Zanussi 				key = (void *)(unsigned long)field_contents;
52936a475cb1STom Zanussi 				use_compound_key = true;
52946a475cb1STom Zanussi 			} else
52956a475cb1STom Zanussi 				key = (void *)&field_contents;
529679e577cbSNamhyung Kim 		}
529779e577cbSNamhyung Kim 
52986a475cb1STom Zanussi 		if (use_compound_key)
52996a475cb1STom Zanussi 			add_to_key(compound_key, key, key_field, rec);
530069a0200cSTom Zanussi 	}
530176a3b0c8STom Zanussi 
53026a475cb1STom Zanussi 	if (use_compound_key)
530376a3b0c8STom Zanussi 		key = compound_key;
53047ef224d1STom Zanussi 
5305067fe038STom Zanussi 	if (hist_data->n_var_refs &&
5306067fe038STom Zanussi 	    !resolve_var_refs(hist_data, key, var_ref_vals, false))
5307067fe038STom Zanussi 		return;
5308067fe038STom Zanussi 
53097ef224d1STom Zanussi 	elt = tracing_map_insert(hist_data->map, key);
5310067fe038STom Zanussi 	if (!elt)
5311067fe038STom Zanussi 		return;
5312067fe038STom Zanussi 
5313b47e3302SSteven Rostedt (VMware) 	hist_trigger_elt_update(hist_data, elt, buffer, rec, rbe, var_ref_vals);
53140212e2aaSTom Zanussi 
53150212e2aaSTom Zanussi 	if (resolve_var_refs(hist_data, key, var_ref_vals, true))
5316b47e3302SSteven Rostedt (VMware) 		hist_trigger_actions(hist_data, elt, buffer, rec, rbe, key, var_ref_vals);
53177ef224d1STom Zanussi }
53187ef224d1STom Zanussi 
hist_trigger_stacktrace_print(struct seq_file * m,unsigned long * stacktrace_entries,unsigned int max_entries)531969a0200cSTom Zanussi static void hist_trigger_stacktrace_print(struct seq_file *m,
532069a0200cSTom Zanussi 					  unsigned long *stacktrace_entries,
532169a0200cSTom Zanussi 					  unsigned int max_entries)
532269a0200cSTom Zanussi {
532369a0200cSTom Zanussi 	unsigned int spaces = 8;
532469a0200cSTom Zanussi 	unsigned int i;
532569a0200cSTom Zanussi 
532669a0200cSTom Zanussi 	for (i = 0; i < max_entries; i++) {
53274285f2fcSThomas Gleixner 		if (!stacktrace_entries[i])
532869a0200cSTom Zanussi 			return;
532969a0200cSTom Zanussi 
533069a0200cSTom Zanussi 		seq_printf(m, "%*c", 1 + spaces, ' ');
53318720aeecSArnd Bergmann 		seq_printf(m, "%pS\n", (void*)stacktrace_entries[i]);
533269a0200cSTom Zanussi 	}
533369a0200cSTom Zanussi }
533469a0200cSTom Zanussi 
hist_trigger_print_key(struct seq_file * m,struct hist_trigger_data * hist_data,void * key,struct tracing_map_elt * elt)5335a3785b7eSTom Zanussi static void hist_trigger_print_key(struct seq_file *m,
5336a3785b7eSTom Zanussi 				   struct hist_trigger_data *hist_data,
5337a3785b7eSTom Zanussi 				   void *key,
53387ef224d1STom Zanussi 				   struct tracing_map_elt *elt)
53397ef224d1STom Zanussi {
53407ef224d1STom Zanussi 	struct hist_field *key_field;
534169a0200cSTom Zanussi 	bool multiline = false;
534285013256STom Zanussi 	const char *field_name;
53437ef224d1STom Zanussi 	unsigned int i;
53447ef224d1STom Zanussi 	u64 uval;
53457ef224d1STom Zanussi 
53467ef224d1STom Zanussi 	seq_puts(m, "{ ");
53477ef224d1STom Zanussi 
53487ef224d1STom Zanussi 	for_each_hist_key_field(i, hist_data) {
53497ef224d1STom Zanussi 		key_field = hist_data->fields[i];
53507ef224d1STom Zanussi 
53517ef224d1STom Zanussi 		if (i > hist_data->n_vals)
53527ef224d1STom Zanussi 			seq_puts(m, ", ");
53537ef224d1STom Zanussi 
535485013256STom Zanussi 		field_name = hist_field_name(key_field, 0);
535585013256STom Zanussi 
53560c4a6b46STom Zanussi 		if (key_field->flags & HIST_FIELD_FL_HEX) {
53570c4a6b46STom Zanussi 			uval = *(u64 *)(key + key_field->offset);
535885013256STom Zanussi 			seq_printf(m, "%s: %llx", field_name, uval);
5359c6afad49STom Zanussi 		} else if (key_field->flags & HIST_FIELD_FL_SYM) {
5360c6afad49STom Zanussi 			uval = *(u64 *)(key + key_field->offset);
53618720aeecSArnd Bergmann 			seq_printf(m, "%s: [%llx] %-45ps", field_name,
53628720aeecSArnd Bergmann 				   uval, (void *)(uintptr_t)uval);
5363c6afad49STom Zanussi 		} else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
5364c6afad49STom Zanussi 			uval = *(u64 *)(key + key_field->offset);
53658720aeecSArnd Bergmann 			seq_printf(m, "%s: [%llx] %-55pS", field_name,
53668720aeecSArnd Bergmann 				   uval, (void *)(uintptr_t)uval);
53676b4827adSTom Zanussi 		} else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
5368af6a29bcSTom Zanussi 			struct hist_elt_data *elt_data = elt->private_data;
5369af6a29bcSTom Zanussi 			char *comm;
5370af6a29bcSTom Zanussi 
5371af6a29bcSTom Zanussi 			if (WARN_ON_ONCE(!elt_data))
5372af6a29bcSTom Zanussi 				return;
5373af6a29bcSTom Zanussi 
5374af6a29bcSTom Zanussi 			comm = elt_data->comm;
53756b4827adSTom Zanussi 
53766b4827adSTom Zanussi 			uval = *(u64 *)(key + key_field->offset);
537785013256STom Zanussi 			seq_printf(m, "%s: %-16s[%10llu]", field_name,
537885013256STom Zanussi 				   comm, uval);
537931696198STom Zanussi 		} else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
538031696198STom Zanussi 			const char *syscall_name;
538131696198STom Zanussi 
538231696198STom Zanussi 			uval = *(u64 *)(key + key_field->offset);
538331696198STom Zanussi 			syscall_name = get_syscall_name(uval);
538431696198STom Zanussi 			if (!syscall_name)
538531696198STom Zanussi 				syscall_name = "unknown_syscall";
538631696198STom Zanussi 
538785013256STom Zanussi 			seq_printf(m, "%s: %-30s[%3llu]", field_name,
538885013256STom Zanussi 				   syscall_name, uval);
538969a0200cSTom Zanussi 		} else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
53902bacfd9fSTom Zanussi 			if (key_field->field)
53912bacfd9fSTom Zanussi 				seq_printf(m, "%s.stacktrace", key_field->field->name);
53922bacfd9fSTom Zanussi 			else
53934b512860SSteven Rostedt (Google) 				seq_puts(m, "common_stacktrace:\n");
539469a0200cSTom Zanussi 			hist_trigger_stacktrace_print(m,
539569a0200cSTom Zanussi 						      key + key_field->offset,
539669a0200cSTom Zanussi 						      HIST_STACKTRACE_DEPTH);
539769a0200cSTom Zanussi 			multiline = true;
53984b94f5b7SNamhyung Kim 		} else if (key_field->flags & HIST_FIELD_FL_LOG2) {
539985013256STom Zanussi 			seq_printf(m, "%s: ~ 2^%-2llu", field_name,
54004b94f5b7SNamhyung Kim 				   *(u64 *)(key + key_field->offset));
5401de9a48a3SSteven Rostedt (VMware) 		} else if (key_field->flags & HIST_FIELD_FL_BUCKET) {
5402de9a48a3SSteven Rostedt (VMware) 			unsigned long buckets = key_field->buckets;
5403de9a48a3SSteven Rostedt (VMware) 			uval = *(u64 *)(key + key_field->offset);
5404de9a48a3SSteven Rostedt (VMware) 			seq_printf(m, "%s: ~ %llu-%llu", field_name,
5405de9a48a3SSteven Rostedt (VMware) 				   uval, uval + buckets -1);
54060c4a6b46STom Zanussi 		} else if (key_field->flags & HIST_FIELD_FL_STRING) {
540785013256STom Zanussi 			seq_printf(m, "%s: %-50s", field_name,
540876a3b0c8STom Zanussi 				   (char *)(key + key_field->offset));
54097ef224d1STom Zanussi 		} else {
541076a3b0c8STom Zanussi 			uval = *(u64 *)(key + key_field->offset);
541185013256STom Zanussi 			seq_printf(m, "%s: %10llu", field_name, uval);
54127ef224d1STom Zanussi 		}
54137ef224d1STom Zanussi 	}
54147ef224d1STom Zanussi 
541569a0200cSTom Zanussi 	if (!multiline)
541669a0200cSTom Zanussi 		seq_puts(m, " ");
541769a0200cSTom Zanussi 
54187ef224d1STom Zanussi 	seq_puts(m, "}");
5419a3785b7eSTom Zanussi }
5420a3785b7eSTom Zanussi 
5421abaa5258SMasami Hiramatsu (Google) /* Get the 100 times of the percentage of @val in @total */
__get_percentage(u64 val,u64 total)5422abaa5258SMasami Hiramatsu (Google) static inline unsigned int __get_percentage(u64 val, u64 total)
5423abaa5258SMasami Hiramatsu (Google) {
5424abaa5258SMasami Hiramatsu (Google) 	if (!total)
5425abaa5258SMasami Hiramatsu (Google) 		goto div0;
5426abaa5258SMasami Hiramatsu (Google) 
5427abaa5258SMasami Hiramatsu (Google) 	if (val < (U64_MAX / 10000))
5428abaa5258SMasami Hiramatsu (Google) 		return (unsigned int)div64_ul(val * 10000, total);
5429abaa5258SMasami Hiramatsu (Google) 
5430abaa5258SMasami Hiramatsu (Google) 	total = div64_u64(total, 10000);
5431abaa5258SMasami Hiramatsu (Google) 	if (!total)
5432abaa5258SMasami Hiramatsu (Google) 		goto div0;
5433abaa5258SMasami Hiramatsu (Google) 
5434abaa5258SMasami Hiramatsu (Google) 	return (unsigned int)div64_ul(val, total);
5435abaa5258SMasami Hiramatsu (Google) div0:
5436abaa5258SMasami Hiramatsu (Google) 	return val ? UINT_MAX : 0;
5437abaa5258SMasami Hiramatsu (Google) }
5438abaa5258SMasami Hiramatsu (Google) 
5439a2c54256SMasami Hiramatsu (Google) #define BAR_CHAR '#'
5440a2c54256SMasami Hiramatsu (Google) 
__fill_bar_str(char * buf,int size,u64 val,u64 max)5441a2c54256SMasami Hiramatsu (Google) static inline const char *__fill_bar_str(char *buf, int size, u64 val, u64 max)
5442a2c54256SMasami Hiramatsu (Google) {
5443a2c54256SMasami Hiramatsu (Google) 	unsigned int len = __get_percentage(val, max);
5444a2c54256SMasami Hiramatsu (Google) 	int i;
5445a2c54256SMasami Hiramatsu (Google) 
5446a2c54256SMasami Hiramatsu (Google) 	if (len == UINT_MAX) {
5447a2c54256SMasami Hiramatsu (Google) 		snprintf(buf, size, "[ERROR]");
5448a2c54256SMasami Hiramatsu (Google) 		return buf;
5449a2c54256SMasami Hiramatsu (Google) 	}
5450a2c54256SMasami Hiramatsu (Google) 
5451a2c54256SMasami Hiramatsu (Google) 	len = len * size / 10000;
5452a2c54256SMasami Hiramatsu (Google) 	for (i = 0; i < len && i < size; i++)
5453a2c54256SMasami Hiramatsu (Google) 		buf[i] = BAR_CHAR;
5454a2c54256SMasami Hiramatsu (Google) 	while (i < size)
5455a2c54256SMasami Hiramatsu (Google) 		buf[i++] = ' ';
5456a2c54256SMasami Hiramatsu (Google) 	buf[size] = '\0';
5457a2c54256SMasami Hiramatsu (Google) 
5458a2c54256SMasami Hiramatsu (Google) 	return buf;
5459a2c54256SMasami Hiramatsu (Google) }
5460a2c54256SMasami Hiramatsu (Google) 
5461a2c54256SMasami Hiramatsu (Google) struct hist_val_stat {
5462a2c54256SMasami Hiramatsu (Google) 	u64 max;
5463a2c54256SMasami Hiramatsu (Google) 	u64 total;
5464a2c54256SMasami Hiramatsu (Google) };
5465a2c54256SMasami Hiramatsu (Google) 
hist_trigger_print_val(struct seq_file * m,unsigned int idx,const char * field_name,unsigned long flags,struct hist_val_stat * stats,struct tracing_map_elt * elt)5466abaa5258SMasami Hiramatsu (Google) static void hist_trigger_print_val(struct seq_file *m, unsigned int idx,
5467abaa5258SMasami Hiramatsu (Google) 				   const char *field_name, unsigned long flags,
5468a2c54256SMasami Hiramatsu (Google) 				   struct hist_val_stat *stats,
5469a2c54256SMasami Hiramatsu (Google) 				   struct tracing_map_elt *elt)
5470abaa5258SMasami Hiramatsu (Google) {
5471abaa5258SMasami Hiramatsu (Google) 	u64 val = tracing_map_read_sum(elt, idx);
5472abaa5258SMasami Hiramatsu (Google) 	unsigned int pc;
5473a2c54256SMasami Hiramatsu (Google) 	char bar[21];
5474abaa5258SMasami Hiramatsu (Google) 
5475abaa5258SMasami Hiramatsu (Google) 	if (flags & HIST_FIELD_FL_PERCENT) {
5476a2c54256SMasami Hiramatsu (Google) 		pc = __get_percentage(val, stats[idx].total);
5477abaa5258SMasami Hiramatsu (Google) 		if (pc == UINT_MAX)
5478abaa5258SMasami Hiramatsu (Google) 			seq_printf(m, " %s (%%):[ERROR]", field_name);
5479abaa5258SMasami Hiramatsu (Google) 		else
5480abaa5258SMasami Hiramatsu (Google) 			seq_printf(m, " %s (%%): %3u.%02u", field_name,
5481abaa5258SMasami Hiramatsu (Google) 					pc / 100, pc % 100);
5482a2c54256SMasami Hiramatsu (Google) 	} else if (flags & HIST_FIELD_FL_GRAPH) {
5483a2c54256SMasami Hiramatsu (Google) 		seq_printf(m, " %s: %20s", field_name,
5484a2c54256SMasami Hiramatsu (Google) 			   __fill_bar_str(bar, 20, val, stats[idx].max));
5485abaa5258SMasami Hiramatsu (Google) 	} else if (flags & HIST_FIELD_FL_HEX) {
5486abaa5258SMasami Hiramatsu (Google) 		seq_printf(m, " %s: %10llx", field_name, val);
5487abaa5258SMasami Hiramatsu (Google) 	} else {
5488abaa5258SMasami Hiramatsu (Google) 		seq_printf(m, " %s: %10llu", field_name, val);
5489abaa5258SMasami Hiramatsu (Google) 	}
5490abaa5258SMasami Hiramatsu (Google) }
5491abaa5258SMasami Hiramatsu (Google) 
hist_trigger_entry_print(struct seq_file * m,struct hist_trigger_data * hist_data,struct hist_val_stat * stats,void * key,struct tracing_map_elt * elt)5492a3785b7eSTom Zanussi static void hist_trigger_entry_print(struct seq_file *m,
5493a3785b7eSTom Zanussi 				     struct hist_trigger_data *hist_data,
5494a2c54256SMasami Hiramatsu (Google) 				     struct hist_val_stat *stats,
5495a3785b7eSTom Zanussi 				     void *key,
5496a3785b7eSTom Zanussi 				     struct tracing_map_elt *elt)
5497a3785b7eSTom Zanussi {
5498a3785b7eSTom Zanussi 	const char *field_name;
5499abaa5258SMasami Hiramatsu (Google) 	unsigned int i = HITCOUNT_IDX;
5500abaa5258SMasami Hiramatsu (Google) 	unsigned long flags;
5501a3785b7eSTom Zanussi 
5502a3785b7eSTom Zanussi 	hist_trigger_print_key(m, hist_data, key, elt);
55037ef224d1STom Zanussi 
5504ccf47f5cSMasami Hiramatsu (Google) 	/* At first, show the raw hitcount if !nohitcount */
5505ccf47f5cSMasami Hiramatsu (Google) 	if (!hist_data->attrs->no_hitcount)
5506a2c54256SMasami Hiramatsu (Google) 		hist_trigger_print_val(m, i, "hitcount", 0, stats, elt);
55077ef224d1STom Zanussi 
5508f2606835STom Zanussi 	for (i = 1; i < hist_data->n_vals; i++) {
550985013256STom Zanussi 		field_name = hist_field_name(hist_data->fields[i], 0);
5510abaa5258SMasami Hiramatsu (Google) 		flags = hist_data->fields[i]->flags;
5511abaa5258SMasami Hiramatsu (Google) 		if (flags & HIST_FIELD_FL_VAR || flags & HIST_FIELD_FL_EXPR)
551230350d65STom Zanussi 			continue;
551330350d65STom Zanussi 
5514abaa5258SMasami Hiramatsu (Google) 		seq_puts(m, " ");
5515a2c54256SMasami Hiramatsu (Google) 		hist_trigger_print_val(m, i, field_name, flags, stats, elt);
55160c4a6b46STom Zanussi 	}
5517f2606835STom Zanussi 
551850450603STom Zanussi 	print_actions(m, hist_data, elt);
551950450603STom Zanussi 
55207ef224d1STom Zanussi 	seq_puts(m, "\n");
55217ef224d1STom Zanussi }
55227ef224d1STom Zanussi 
print_entries(struct seq_file * m,struct hist_trigger_data * hist_data)55237ef224d1STom Zanussi static int print_entries(struct seq_file *m,
55247ef224d1STom Zanussi 			 struct hist_trigger_data *hist_data)
55257ef224d1STom Zanussi {
55267ef224d1STom Zanussi 	struct tracing_map_sort_entry **sort_entries = NULL;
55277ef224d1STom Zanussi 	struct tracing_map *map = hist_data->map;
5528abaa5258SMasami Hiramatsu (Google) 	int i, j, n_entries;
5529a2c54256SMasami Hiramatsu (Google) 	struct hist_val_stat *stats = NULL;
5530a2c54256SMasami Hiramatsu (Google) 	u64 val;
55317ef224d1STom Zanussi 
55327ef224d1STom Zanussi 	n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
55337ef224d1STom Zanussi 					     hist_data->n_sort_keys,
55347ef224d1STom Zanussi 					     &sort_entries);
55357ef224d1STom Zanussi 	if (n_entries < 0)
55367ef224d1STom Zanussi 		return n_entries;
55377ef224d1STom Zanussi 
5538a2c54256SMasami Hiramatsu (Google) 	/* Calculate the max and the total for each field if needed. */
5539abaa5258SMasami Hiramatsu (Google) 	for (j = 0; j < hist_data->n_vals; j++) {
5540a2c54256SMasami Hiramatsu (Google) 		if (!(hist_data->fields[j]->flags &
5541a2c54256SMasami Hiramatsu (Google) 			(HIST_FIELD_FL_PERCENT | HIST_FIELD_FL_GRAPH)))
5542abaa5258SMasami Hiramatsu (Google) 			continue;
5543a2c54256SMasami Hiramatsu (Google) 		if (!stats) {
5544a2c54256SMasami Hiramatsu (Google) 			stats = kcalloc(hist_data->n_vals, sizeof(*stats),
5545abaa5258SMasami Hiramatsu (Google) 				       GFP_KERNEL);
5546a2c54256SMasami Hiramatsu (Google) 			if (!stats) {
5547abaa5258SMasami Hiramatsu (Google) 				n_entries = -ENOMEM;
5548abaa5258SMasami Hiramatsu (Google) 				goto out;
5549abaa5258SMasami Hiramatsu (Google) 			}
5550abaa5258SMasami Hiramatsu (Google) 		}
5551a2c54256SMasami Hiramatsu (Google) 		for (i = 0; i < n_entries; i++) {
5552a2c54256SMasami Hiramatsu (Google) 			val = tracing_map_read_sum(sort_entries[i]->elt, j);
5553a2c54256SMasami Hiramatsu (Google) 			stats[j].total += val;
5554a2c54256SMasami Hiramatsu (Google) 			if (stats[j].max < val)
5555a2c54256SMasami Hiramatsu (Google) 				stats[j].max = val;
5556a2c54256SMasami Hiramatsu (Google) 		}
5557abaa5258SMasami Hiramatsu (Google) 	}
5558abaa5258SMasami Hiramatsu (Google) 
5559abaa5258SMasami Hiramatsu (Google) 	for (i = 0; i < n_entries; i++)
5560a2c54256SMasami Hiramatsu (Google) 		hist_trigger_entry_print(m, hist_data, stats,
55617ef224d1STom Zanussi 					 sort_entries[i]->key,
55627ef224d1STom Zanussi 					 sort_entries[i]->elt);
55637ef224d1STom Zanussi 
5564a2c54256SMasami Hiramatsu (Google) 	kfree(stats);
5565abaa5258SMasami Hiramatsu (Google) out:
55667ef224d1STom Zanussi 	tracing_map_destroy_sort_entries(sort_entries, n_entries);
55677ef224d1STom Zanussi 
55687ef224d1STom Zanussi 	return n_entries;
55697ef224d1STom Zanussi }
55707ef224d1STom Zanussi 
hist_trigger_show(struct seq_file * m,struct event_trigger_data * data,int n)557152a7f16dSTom Zanussi static void hist_trigger_show(struct seq_file *m,
557252a7f16dSTom Zanussi 			      struct event_trigger_data *data, int n)
55737ef224d1STom Zanussi {
55747ef224d1STom Zanussi 	struct hist_trigger_data *hist_data;
55756e7a2398SColin Ian King 	int n_entries;
55767ef224d1STom Zanussi 
557752a7f16dSTom Zanussi 	if (n > 0)
557852a7f16dSTom Zanussi 		seq_puts(m, "\n\n");
55797ef224d1STom Zanussi 
55807ef224d1STom Zanussi 	seq_puts(m, "# event histogram\n#\n# trigger info: ");
558147670541STom Zanussi 	data->ops->print(m, data);
558252a7f16dSTom Zanussi 	seq_puts(m, "#\n\n");
55837ef224d1STom Zanussi 
55847ef224d1STom Zanussi 	hist_data = data->private_data;
55857ef224d1STom Zanussi 	n_entries = print_entries(m, hist_data);
55866e7a2398SColin Ian King 	if (n_entries < 0)
55877ef224d1STom Zanussi 		n_entries = 0;
55887ef224d1STom Zanussi 
5589a3785b7eSTom Zanussi 	track_data_snapshot_print(m, hist_data);
5590a3785b7eSTom Zanussi 
55917ef224d1STom Zanussi 	seq_printf(m, "\nTotals:\n    Hits: %llu\n    Entries: %u\n    Dropped: %llu\n",
55927ef224d1STom Zanussi 		   (u64)atomic64_read(&hist_data->map->hits),
55937ef224d1STom Zanussi 		   n_entries, (u64)atomic64_read(&hist_data->map->drops));
559452a7f16dSTom Zanussi }
559552a7f16dSTom Zanussi 
hist_show(struct seq_file * m,void * v)559652a7f16dSTom Zanussi static int hist_show(struct seq_file *m, void *v)
559752a7f16dSTom Zanussi {
559852a7f16dSTom Zanussi 	struct event_trigger_data *data;
559952a7f16dSTom Zanussi 	struct trace_event_file *event_file;
560052a7f16dSTom Zanussi 	int n = 0, ret = 0;
560152a7f16dSTom Zanussi 
560252a7f16dSTom Zanussi 	mutex_lock(&event_mutex);
560352a7f16dSTom Zanussi 
5604*b1560408SSteven Rostedt 	event_file = event_file_file(m->private);
560552a7f16dSTom Zanussi 	if (unlikely(!event_file)) {
560652a7f16dSTom Zanussi 		ret = -ENODEV;
560752a7f16dSTom Zanussi 		goto out_unlock;
560852a7f16dSTom Zanussi 	}
560952a7f16dSTom Zanussi 
5610aeed8aa3SMasami Hiramatsu 	list_for_each_entry(data, &event_file->triggers, list) {
561152a7f16dSTom Zanussi 		if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
561252a7f16dSTom Zanussi 			hist_trigger_show(m, data, n++);
561352a7f16dSTom Zanussi 	}
561452a7f16dSTom Zanussi 
56157ef224d1STom Zanussi  out_unlock:
56167ef224d1STom Zanussi 	mutex_unlock(&event_mutex);
56177ef224d1STom Zanussi 
56187ef224d1STom Zanussi 	return ret;
56197ef224d1STom Zanussi }
56207ef224d1STom Zanussi 
event_hist_open(struct inode * inode,struct file * file)56217ef224d1STom Zanussi static int event_hist_open(struct inode *inode, struct file *file)
56227ef224d1STom Zanussi {
562317911ff3SSteven Rostedt (VMware) 	int ret;
562417911ff3SSteven Rostedt (VMware) 
56251cc111b9SZheng Yejian 	ret = tracing_open_file_tr(inode, file);
562617911ff3SSteven Rostedt (VMware) 	if (ret)
562717911ff3SSteven Rostedt (VMware) 		return ret;
562817911ff3SSteven Rostedt (VMware) 
56291cc111b9SZheng Yejian 	/* Clear private_data to avoid warning in single_open() */
56301cc111b9SZheng Yejian 	file->private_data = NULL;
56317ef224d1STom Zanussi 	return single_open(file, hist_show, file);
56327ef224d1STom Zanussi }
56337ef224d1STom Zanussi 
56347ef224d1STom Zanussi const struct file_operations event_hist_fops = {
56357ef224d1STom Zanussi 	.open = event_hist_open,
56367ef224d1STom Zanussi 	.read = seq_read,
56377ef224d1STom Zanussi 	.llseek = seq_lseek,
56381cc111b9SZheng Yejian 	.release = tracing_single_release_file_tr,
56397ef224d1STom Zanussi };
56407ef224d1STom Zanussi 
56412d19bd79STom Zanussi #ifdef CONFIG_HIST_TRIGGERS_DEBUG
hist_field_debug_show_flags(struct seq_file * m,unsigned long flags)56422d19bd79STom Zanussi static void hist_field_debug_show_flags(struct seq_file *m,
56432d19bd79STom Zanussi 					unsigned long flags)
56442d19bd79STom Zanussi {
56452d19bd79STom Zanussi 	seq_puts(m, "      flags:\n");
56462d19bd79STom Zanussi 
56472d19bd79STom Zanussi 	if (flags & HIST_FIELD_FL_KEY)
56482d19bd79STom Zanussi 		seq_puts(m, "        HIST_FIELD_FL_KEY\n");
56492d19bd79STom Zanussi 	else if (flags & HIST_FIELD_FL_HITCOUNT)
56502d19bd79STom Zanussi 		seq_puts(m, "        VAL: HIST_FIELD_FL_HITCOUNT\n");
56512d19bd79STom Zanussi 	else if (flags & HIST_FIELD_FL_VAR)
56522d19bd79STom Zanussi 		seq_puts(m, "        HIST_FIELD_FL_VAR\n");
56532d19bd79STom Zanussi 	else if (flags & HIST_FIELD_FL_VAR_REF)
56542d19bd79STom Zanussi 		seq_puts(m, "        HIST_FIELD_FL_VAR_REF\n");
56552d19bd79STom Zanussi 	else
56562d19bd79STom Zanussi 		seq_puts(m, "        VAL: normal u64 value\n");
56572d19bd79STom Zanussi 
56582d19bd79STom Zanussi 	if (flags & HIST_FIELD_FL_ALIAS)
56592d19bd79STom Zanussi 		seq_puts(m, "        HIST_FIELD_FL_ALIAS\n");
566052cfb373SKalesh Singh 	else if (flags & HIST_FIELD_FL_CONST)
566152cfb373SKalesh Singh 		seq_puts(m, "        HIST_FIELD_FL_CONST\n");
56622d19bd79STom Zanussi }
56632d19bd79STom Zanussi 
hist_field_debug_show(struct seq_file * m,struct hist_field * field,unsigned long flags)56642d19bd79STom Zanussi static int hist_field_debug_show(struct seq_file *m,
56652d19bd79STom Zanussi 				 struct hist_field *field, unsigned long flags)
56662d19bd79STom Zanussi {
56672d19bd79STom Zanussi 	if ((field->flags & flags) != flags) {
56682d19bd79STom Zanussi 		seq_printf(m, "ERROR: bad flags - %lx\n", flags);
56692d19bd79STom Zanussi 		return -EINVAL;
56702d19bd79STom Zanussi 	}
56712d19bd79STom Zanussi 
56722d19bd79STom Zanussi 	hist_field_debug_show_flags(m, field->flags);
56732d19bd79STom Zanussi 	if (field->field)
56742d19bd79STom Zanussi 		seq_printf(m, "      ftrace_event_field name: %s\n",
56752d19bd79STom Zanussi 			   field->field->name);
56762d19bd79STom Zanussi 
56772d19bd79STom Zanussi 	if (field->flags & HIST_FIELD_FL_VAR) {
56782d19bd79STom Zanussi 		seq_printf(m, "      var.name: %s\n", field->var.name);
56792d19bd79STom Zanussi 		seq_printf(m, "      var.idx (into tracing_map_elt.vars[]): %u\n",
56802d19bd79STom Zanussi 			   field->var.idx);
56812d19bd79STom Zanussi 	}
56822d19bd79STom Zanussi 
568352cfb373SKalesh Singh 	if (field->flags & HIST_FIELD_FL_CONST)
568452cfb373SKalesh Singh 		seq_printf(m, "      constant: %llu\n", field->constant);
568552cfb373SKalesh Singh 
56862d19bd79STom Zanussi 	if (field->flags & HIST_FIELD_FL_ALIAS)
56872d19bd79STom Zanussi 		seq_printf(m, "      var_ref_idx (into hist_data->var_refs[]): %u\n",
56882d19bd79STom Zanussi 			   field->var_ref_idx);
56892d19bd79STom Zanussi 
56902d19bd79STom Zanussi 	if (field->flags & HIST_FIELD_FL_VAR_REF) {
56912d19bd79STom Zanussi 		seq_printf(m, "      name: %s\n", field->name);
56922d19bd79STom Zanussi 		seq_printf(m, "      var.idx (into tracing_map_elt.vars[]): %u\n",
56932d19bd79STom Zanussi 			   field->var.idx);
56942d19bd79STom Zanussi 		seq_printf(m, "      var.hist_data: %p\n", field->var.hist_data);
56952d19bd79STom Zanussi 		seq_printf(m, "      var_ref_idx (into hist_data->var_refs[]): %u\n",
56962d19bd79STom Zanussi 			   field->var_ref_idx);
56972d19bd79STom Zanussi 		if (field->system)
56982d19bd79STom Zanussi 			seq_printf(m, "      system: %s\n", field->system);
56992d19bd79STom Zanussi 		if (field->event_name)
57002d19bd79STom Zanussi 			seq_printf(m, "      event_name: %s\n", field->event_name);
57012d19bd79STom Zanussi 	}
57022d19bd79STom Zanussi 
57032d19bd79STom Zanussi 	seq_printf(m, "      type: %s\n", field->type);
57042d19bd79STom Zanussi 	seq_printf(m, "      size: %u\n", field->size);
57052d19bd79STom Zanussi 	seq_printf(m, "      is_signed: %u\n", field->is_signed);
57062d19bd79STom Zanussi 
57072d19bd79STom Zanussi 	return 0;
57082d19bd79STom Zanussi }
57092d19bd79STom Zanussi 
field_var_debug_show(struct seq_file * m,struct field_var * field_var,unsigned int i,bool save_vars)57102d19bd79STom Zanussi static int field_var_debug_show(struct seq_file *m,
57112d19bd79STom Zanussi 				struct field_var *field_var, unsigned int i,
57122d19bd79STom Zanussi 				bool save_vars)
57132d19bd79STom Zanussi {
57142d19bd79STom Zanussi 	const char *vars_name = save_vars ? "save_vars" : "field_vars";
57152d19bd79STom Zanussi 	struct hist_field *field;
57162d19bd79STom Zanussi 	int ret = 0;
57172d19bd79STom Zanussi 
57182d19bd79STom Zanussi 	seq_printf(m, "\n    hist_data->%s[%d]:\n", vars_name, i);
57192d19bd79STom Zanussi 
57202d19bd79STom Zanussi 	field = field_var->var;
57212d19bd79STom Zanussi 
57222d19bd79STom Zanussi 	seq_printf(m, "\n      %s[%d].var:\n", vars_name, i);
57232d19bd79STom Zanussi 
57242d19bd79STom Zanussi 	hist_field_debug_show_flags(m, field->flags);
57252d19bd79STom Zanussi 	seq_printf(m, "      var.name: %s\n", field->var.name);
57262d19bd79STom Zanussi 	seq_printf(m, "      var.idx (into tracing_map_elt.vars[]): %u\n",
57272d19bd79STom Zanussi 		   field->var.idx);
57282d19bd79STom Zanussi 
57292d19bd79STom Zanussi 	field = field_var->val;
57302d19bd79STom Zanussi 
57312d19bd79STom Zanussi 	seq_printf(m, "\n      %s[%d].val:\n", vars_name, i);
57322d19bd79STom Zanussi 	if (field->field)
57332d19bd79STom Zanussi 		seq_printf(m, "      ftrace_event_field name: %s\n",
57342d19bd79STom Zanussi 			   field->field->name);
57352d19bd79STom Zanussi 	else {
57362d19bd79STom Zanussi 		ret = -EINVAL;
57372d19bd79STom Zanussi 		goto out;
57382d19bd79STom Zanussi 	}
57392d19bd79STom Zanussi 
57402d19bd79STom Zanussi 	seq_printf(m, "      type: %s\n", field->type);
57412d19bd79STom Zanussi 	seq_printf(m, "      size: %u\n", field->size);
57422d19bd79STom Zanussi 	seq_printf(m, "      is_signed: %u\n", field->is_signed);
57432d19bd79STom Zanussi out:
57442d19bd79STom Zanussi 	return ret;
57452d19bd79STom Zanussi }
57462d19bd79STom Zanussi 
hist_action_debug_show(struct seq_file * m,struct action_data * data,int i)57472d19bd79STom Zanussi static int hist_action_debug_show(struct seq_file *m,
57482d19bd79STom Zanussi 				  struct action_data *data, int i)
57492d19bd79STom Zanussi {
57502d19bd79STom Zanussi 	int ret = 0;
57512d19bd79STom Zanussi 
57522d19bd79STom Zanussi 	if (data->handler == HANDLER_ONMAX ||
57532d19bd79STom Zanussi 	    data->handler == HANDLER_ONCHANGE) {
57542d19bd79STom Zanussi 		seq_printf(m, "\n    hist_data->actions[%d].track_data.var_ref:\n", i);
57552d19bd79STom Zanussi 		ret = hist_field_debug_show(m, data->track_data.var_ref,
57562d19bd79STom Zanussi 					    HIST_FIELD_FL_VAR_REF);
57572d19bd79STom Zanussi 		if (ret)
57582d19bd79STom Zanussi 			goto out;
57592d19bd79STom Zanussi 
57602d19bd79STom Zanussi 		seq_printf(m, "\n    hist_data->actions[%d].track_data.track_var:\n", i);
57612d19bd79STom Zanussi 		ret = hist_field_debug_show(m, data->track_data.track_var,
57622d19bd79STom Zanussi 					    HIST_FIELD_FL_VAR);
57632d19bd79STom Zanussi 		if (ret)
57642d19bd79STom Zanussi 			goto out;
57652d19bd79STom Zanussi 	}
57662d19bd79STom Zanussi 
57672d19bd79STom Zanussi 	if (data->handler == HANDLER_ONMATCH) {
57682d19bd79STom Zanussi 		seq_printf(m, "\n    hist_data->actions[%d].match_data.event_system: %s\n",
57692d19bd79STom Zanussi 			   i, data->match_data.event_system);
57702d19bd79STom Zanussi 		seq_printf(m, "    hist_data->actions[%d].match_data.event: %s\n",
57712d19bd79STom Zanussi 			   i, data->match_data.event);
57722d19bd79STom Zanussi 	}
57732d19bd79STom Zanussi out:
57742d19bd79STom Zanussi 	return ret;
57752d19bd79STom Zanussi }
57762d19bd79STom Zanussi 
hist_actions_debug_show(struct seq_file * m,struct hist_trigger_data * hist_data)57772d19bd79STom Zanussi static int hist_actions_debug_show(struct seq_file *m,
57782d19bd79STom Zanussi 				   struct hist_trigger_data *hist_data)
57792d19bd79STom Zanussi {
57802d19bd79STom Zanussi 	int i, ret = 0;
57812d19bd79STom Zanussi 
57822d19bd79STom Zanussi 	if (hist_data->n_actions)
57832d19bd79STom Zanussi 		seq_puts(m, "\n  action tracking variables (for onmax()/onchange()/onmatch()):\n");
57842d19bd79STom Zanussi 
57852d19bd79STom Zanussi 	for (i = 0; i < hist_data->n_actions; i++) {
57862d19bd79STom Zanussi 		struct action_data *action = hist_data->actions[i];
57872d19bd79STom Zanussi 
57882d19bd79STom Zanussi 		ret = hist_action_debug_show(m, action, i);
57892d19bd79STom Zanussi 		if (ret)
57902d19bd79STom Zanussi 			goto out;
57912d19bd79STom Zanussi 	}
57922d19bd79STom Zanussi 
57932d19bd79STom Zanussi 	if (hist_data->n_save_vars)
57942d19bd79STom Zanussi 		seq_puts(m, "\n  save action variables (save() params):\n");
57952d19bd79STom Zanussi 
57962d19bd79STom Zanussi 	for (i = 0; i < hist_data->n_save_vars; i++) {
57972d19bd79STom Zanussi 		ret = field_var_debug_show(m, hist_data->save_vars[i], i, true);
57982d19bd79STom Zanussi 		if (ret)
57992d19bd79STom Zanussi 			goto out;
58002d19bd79STom Zanussi 	}
58012d19bd79STom Zanussi out:
58022d19bd79STom Zanussi 	return ret;
58032d19bd79STom Zanussi }
58042d19bd79STom Zanussi 
hist_trigger_debug_show(struct seq_file * m,struct event_trigger_data * data,int n)58052d19bd79STom Zanussi static void hist_trigger_debug_show(struct seq_file *m,
58062d19bd79STom Zanussi 				    struct event_trigger_data *data, int n)
58072d19bd79STom Zanussi {
58082d19bd79STom Zanussi 	struct hist_trigger_data *hist_data;
58092d19bd79STom Zanussi 	int i, ret;
58102d19bd79STom Zanussi 
58112d19bd79STom Zanussi 	if (n > 0)
58122d19bd79STom Zanussi 		seq_puts(m, "\n\n");
58132d19bd79STom Zanussi 
58142d19bd79STom Zanussi 	seq_puts(m, "# event histogram\n#\n# trigger info: ");
581547670541STom Zanussi 	data->ops->print(m, data);
58162d19bd79STom Zanussi 	seq_puts(m, "#\n\n");
58172d19bd79STom Zanussi 
58182d19bd79STom Zanussi 	hist_data = data->private_data;
58192d19bd79STom Zanussi 
58202d19bd79STom Zanussi 	seq_printf(m, "hist_data: %p\n\n", hist_data);
58212d19bd79STom Zanussi 	seq_printf(m, "  n_vals: %u\n", hist_data->n_vals);
58222d19bd79STom Zanussi 	seq_printf(m, "  n_keys: %u\n", hist_data->n_keys);
58232d19bd79STom Zanussi 	seq_printf(m, "  n_fields: %u\n", hist_data->n_fields);
58242d19bd79STom Zanussi 
58252d19bd79STom Zanussi 	seq_puts(m, "\n  val fields:\n\n");
58262d19bd79STom Zanussi 
58272d19bd79STom Zanussi 	seq_puts(m, "    hist_data->fields[0]:\n");
58282d19bd79STom Zanussi 	ret = hist_field_debug_show(m, hist_data->fields[0],
58292d19bd79STom Zanussi 				    HIST_FIELD_FL_HITCOUNT);
58302d19bd79STom Zanussi 	if (ret)
58312d19bd79STom Zanussi 		return;
58322d19bd79STom Zanussi 
58332d19bd79STom Zanussi 	for (i = 1; i < hist_data->n_vals; i++) {
58342d19bd79STom Zanussi 		seq_printf(m, "\n    hist_data->fields[%d]:\n", i);
58352d19bd79STom Zanussi 		ret = hist_field_debug_show(m, hist_data->fields[i], 0);
58362d19bd79STom Zanussi 		if (ret)
58372d19bd79STom Zanussi 			return;
58382d19bd79STom Zanussi 	}
58392d19bd79STom Zanussi 
58402d19bd79STom Zanussi 	seq_puts(m, "\n  key fields:\n");
58412d19bd79STom Zanussi 
58422d19bd79STom Zanussi 	for (i = hist_data->n_vals; i < hist_data->n_fields; i++) {
58432d19bd79STom Zanussi 		seq_printf(m, "\n    hist_data->fields[%d]:\n", i);
58442d19bd79STom Zanussi 		ret = hist_field_debug_show(m, hist_data->fields[i],
58452d19bd79STom Zanussi 					    HIST_FIELD_FL_KEY);
58462d19bd79STom Zanussi 		if (ret)
58472d19bd79STom Zanussi 			return;
58482d19bd79STom Zanussi 	}
58492d19bd79STom Zanussi 
58502d19bd79STom Zanussi 	if (hist_data->n_var_refs)
58512d19bd79STom Zanussi 		seq_puts(m, "\n  variable reference fields:\n");
58522d19bd79STom Zanussi 
58532d19bd79STom Zanussi 	for (i = 0; i < hist_data->n_var_refs; i++) {
58542d19bd79STom Zanussi 		seq_printf(m, "\n    hist_data->var_refs[%d]:\n", i);
58552d19bd79STom Zanussi 		ret = hist_field_debug_show(m, hist_data->var_refs[i],
58562d19bd79STom Zanussi 					    HIST_FIELD_FL_VAR_REF);
58572d19bd79STom Zanussi 		if (ret)
58582d19bd79STom Zanussi 			return;
58592d19bd79STom Zanussi 	}
58602d19bd79STom Zanussi 
58612d19bd79STom Zanussi 	if (hist_data->n_field_vars)
58622d19bd79STom Zanussi 		seq_puts(m, "\n  field variables:\n");
58632d19bd79STom Zanussi 
58642d19bd79STom Zanussi 	for (i = 0; i < hist_data->n_field_vars; i++) {
58652d19bd79STom Zanussi 		ret = field_var_debug_show(m, hist_data->field_vars[i], i, false);
58662d19bd79STom Zanussi 		if (ret)
58672d19bd79STom Zanussi 			return;
58682d19bd79STom Zanussi 	}
58692d19bd79STom Zanussi 
58702d19bd79STom Zanussi 	ret = hist_actions_debug_show(m, hist_data);
58712d19bd79STom Zanussi 	if (ret)
58722d19bd79STom Zanussi 		return;
58732d19bd79STom Zanussi }
58742d19bd79STom Zanussi 
hist_debug_show(struct seq_file * m,void * v)58752d19bd79STom Zanussi static int hist_debug_show(struct seq_file *m, void *v)
58762d19bd79STom Zanussi {
58772d19bd79STom Zanussi 	struct event_trigger_data *data;
58782d19bd79STom Zanussi 	struct trace_event_file *event_file;
58792d19bd79STom Zanussi 	int n = 0, ret = 0;
58802d19bd79STom Zanussi 
58812d19bd79STom Zanussi 	mutex_lock(&event_mutex);
58822d19bd79STom Zanussi 
5883*b1560408SSteven Rostedt 	event_file = event_file_file(m->private);
58842d19bd79STom Zanussi 	if (unlikely(!event_file)) {
58852d19bd79STom Zanussi 		ret = -ENODEV;
58862d19bd79STom Zanussi 		goto out_unlock;
58872d19bd79STom Zanussi 	}
58882d19bd79STom Zanussi 
58892d19bd79STom Zanussi 	list_for_each_entry(data, &event_file->triggers, list) {
58902d19bd79STom Zanussi 		if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
58912d19bd79STom Zanussi 			hist_trigger_debug_show(m, data, n++);
58922d19bd79STom Zanussi 	}
58932d19bd79STom Zanussi 
58942d19bd79STom Zanussi  out_unlock:
58952d19bd79STom Zanussi 	mutex_unlock(&event_mutex);
58962d19bd79STom Zanussi 
58972d19bd79STom Zanussi 	return ret;
58982d19bd79STom Zanussi }
58992d19bd79STom Zanussi 
event_hist_debug_open(struct inode * inode,struct file * file)59002d19bd79STom Zanussi static int event_hist_debug_open(struct inode *inode, struct file *file)
59012d19bd79STom Zanussi {
59022d19bd79STom Zanussi 	int ret;
59032d19bd79STom Zanussi 
59041cc111b9SZheng Yejian 	ret = tracing_open_file_tr(inode, file);
59052d19bd79STom Zanussi 	if (ret)
59062d19bd79STom Zanussi 		return ret;
59072d19bd79STom Zanussi 
59081cc111b9SZheng Yejian 	/* Clear private_data to avoid warning in single_open() */
59091cc111b9SZheng Yejian 	file->private_data = NULL;
59102d19bd79STom Zanussi 	return single_open(file, hist_debug_show, file);
59112d19bd79STom Zanussi }
59122d19bd79STom Zanussi 
59132d19bd79STom Zanussi const struct file_operations event_hist_debug_fops = {
59142d19bd79STom Zanussi 	.open = event_hist_debug_open,
59152d19bd79STom Zanussi 	.read = seq_read,
59162d19bd79STom Zanussi 	.llseek = seq_lseek,
59171cc111b9SZheng Yejian 	.release = tracing_single_release_file_tr,
59182d19bd79STom Zanussi };
59192d19bd79STom Zanussi #endif
59202d19bd79STom Zanussi 
hist_field_print(struct seq_file * m,struct hist_field * hist_field)59217ef224d1STom Zanussi static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
59227ef224d1STom Zanussi {
592385013256STom Zanussi 	const char *field_name = hist_field_name(hist_field, 0);
592485013256STom Zanussi 
592530350d65STom Zanussi 	if (hist_field->var.name)
592630350d65STom Zanussi 		seq_printf(m, "%s=", hist_field->var.name);
592730350d65STom Zanussi 
59280ae7961eSTom Zanussi 	if (hist_field->flags & HIST_FIELD_FL_CPU)
59291e3bac71SSteven Rostedt (VMware) 		seq_puts(m, "common_cpu");
593052cfb373SKalesh Singh 	else if (hist_field->flags & HIST_FIELD_FL_CONST)
593152cfb373SKalesh Singh 		seq_printf(m, "%llu", hist_field->constant);
5932067fe038STom Zanussi 	else if (field_name) {
59337e8b88a3STom Zanussi 		if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
59347e8b88a3STom Zanussi 		    hist_field->flags & HIST_FIELD_FL_ALIAS)
5935067fe038STom Zanussi 			seq_putc(m, '$');
593685013256STom Zanussi 		seq_printf(m, "%s", field_name);
59370ae7961eSTom Zanussi 	} else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
59380ae7961eSTom Zanussi 		seq_puts(m, "common_timestamp");
5939608940daSTom Zanussi 
5940608940daSTom Zanussi 	if (hist_field->flags) {
5941608940daSTom Zanussi 		if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
59422bacfd9fSTom Zanussi 		    !(hist_field->flags & HIST_FIELD_FL_EXPR) &&
59432bacfd9fSTom Zanussi 		    !(hist_field->flags & HIST_FIELD_FL_STACKTRACE)) {
5944608940daSTom Zanussi 			const char *flags = get_hist_field_flags(hist_field);
5945608940daSTom Zanussi 
5946608940daSTom Zanussi 			if (flags)
5947608940daSTom Zanussi 				seq_printf(m, ".%s", flags);
5948608940daSTom Zanussi 		}
5949608940daSTom Zanussi 	}
5950de9a48a3SSteven Rostedt (VMware) 	if (hist_field->buckets)
5951de9a48a3SSteven Rostedt (VMware) 		seq_printf(m, "=%ld", hist_field->buckets);
59527ef224d1STom Zanussi }
59537ef224d1STom Zanussi 
event_hist_trigger_print(struct seq_file * m,struct event_trigger_data * data)59547ef224d1STom Zanussi static int event_hist_trigger_print(struct seq_file *m,
59557ef224d1STom Zanussi 				    struct event_trigger_data *data)
59567ef224d1STom Zanussi {
59577ef224d1STom Zanussi 	struct hist_trigger_data *hist_data = data->private_data;
595830350d65STom Zanussi 	struct hist_field *field;
595930350d65STom Zanussi 	bool have_var = false;
5960ccf47f5cSMasami Hiramatsu (Google) 	bool show_val = false;
59617ef224d1STom Zanussi 	unsigned int i;
59627ef224d1STom Zanussi 
5963edfeed31STom Zanussi 	seq_puts(m, HIST_PREFIX);
59645463bfdaSTom Zanussi 
59655463bfdaSTom Zanussi 	if (data->name)
59665463bfdaSTom Zanussi 		seq_printf(m, "%s:", data->name);
59675463bfdaSTom Zanussi 
59685463bfdaSTom Zanussi 	seq_puts(m, "keys=");
59697ef224d1STom Zanussi 
59707ef224d1STom Zanussi 	for_each_hist_key_field(i, hist_data) {
597130350d65STom Zanussi 		field = hist_data->fields[i];
59727ef224d1STom Zanussi 
59737ef224d1STom Zanussi 		if (i > hist_data->n_vals)
59747ef224d1STom Zanussi 			seq_puts(m, ",");
59757ef224d1STom Zanussi 
59762bacfd9fSTom Zanussi 		if (field->flags & HIST_FIELD_FL_STACKTRACE) {
59772bacfd9fSTom Zanussi 			if (field->field)
59782bacfd9fSTom Zanussi 				seq_printf(m, "%s.stacktrace", field->field->name);
597969a0200cSTom Zanussi 			else
59804b512860SSteven Rostedt (Google) 				seq_puts(m, "common_stacktrace");
59812bacfd9fSTom Zanussi 		} else
598230350d65STom Zanussi 			hist_field_print(m, field);
59837ef224d1STom Zanussi 	}
59847ef224d1STom Zanussi 
59857ef224d1STom Zanussi 	seq_puts(m, ":vals=");
5986f2606835STom Zanussi 
5987f2606835STom Zanussi 	for_each_hist_val_field(i, hist_data) {
598830350d65STom Zanussi 		field = hist_data->fields[i];
598930350d65STom Zanussi 		if (field->flags & HIST_FIELD_FL_VAR) {
599030350d65STom Zanussi 			have_var = true;
599130350d65STom Zanussi 			continue;
599230350d65STom Zanussi 		}
599330350d65STom Zanussi 
5994ccf47f5cSMasami Hiramatsu (Google) 		if (i == HITCOUNT_IDX) {
5995ccf47f5cSMasami Hiramatsu (Google) 			if (hist_data->attrs->no_hitcount)
5996ccf47f5cSMasami Hiramatsu (Google) 				continue;
59977ef224d1STom Zanussi 			seq_puts(m, "hitcount");
5998ccf47f5cSMasami Hiramatsu (Google) 		} else {
5999ccf47f5cSMasami Hiramatsu (Google) 			if (show_val)
6000f2606835STom Zanussi 				seq_puts(m, ",");
600130350d65STom Zanussi 			hist_field_print(m, field);
600230350d65STom Zanussi 		}
6003ccf47f5cSMasami Hiramatsu (Google) 		show_val = true;
600430350d65STom Zanussi 	}
600530350d65STom Zanussi 
600630350d65STom Zanussi 	if (have_var) {
600730350d65STom Zanussi 		unsigned int n = 0;
600830350d65STom Zanussi 
600930350d65STom Zanussi 		seq_puts(m, ":");
601030350d65STom Zanussi 
601130350d65STom Zanussi 		for_each_hist_val_field(i, hist_data) {
601230350d65STom Zanussi 			field = hist_data->fields[i];
601330350d65STom Zanussi 
601430350d65STom Zanussi 			if (field->flags & HIST_FIELD_FL_VAR) {
601530350d65STom Zanussi 				if (n++)
601630350d65STom Zanussi 					seq_puts(m, ",");
601730350d65STom Zanussi 				hist_field_print(m, field);
601830350d65STom Zanussi 			}
6019f2606835STom Zanussi 		}
6020f2606835STom Zanussi 	}
60217ef224d1STom Zanussi 
60227ef224d1STom Zanussi 	seq_puts(m, ":sort=");
6023e62347d2STom Zanussi 
6024e62347d2STom Zanussi 	for (i = 0; i < hist_data->n_sort_keys; i++) {
6025e62347d2STom Zanussi 		struct tracing_map_sort_key *sort_key;
602630350d65STom Zanussi 		unsigned int idx, first_key_idx;
602730350d65STom Zanussi 
602830350d65STom Zanussi 		/* skip VAR vals */
602930350d65STom Zanussi 		first_key_idx = hist_data->n_vals - hist_data->n_vars;
6030e62347d2STom Zanussi 
6031e62347d2STom Zanussi 		sort_key = &hist_data->sort_keys[i];
6032ad42febeSTom Zanussi 		idx = sort_key->field_idx;
6033e62347d2STom Zanussi 
60341a361dfcSTom Zanussi 		if (WARN_ON(idx >= HIST_FIELDS_MAX))
6035e62347d2STom Zanussi 			return -EINVAL;
6036e62347d2STom Zanussi 
6037ad42febeSTom Zanussi 		if (i > 0)
6038ad42febeSTom Zanussi 			seq_puts(m, ",");
6039ad42febeSTom Zanussi 
6040ad42febeSTom Zanussi 		if (idx == HITCOUNT_IDX)
6041ad42febeSTom Zanussi 			seq_puts(m, "hitcount");
604230350d65STom Zanussi 		else {
604330350d65STom Zanussi 			if (idx >= first_key_idx)
604430350d65STom Zanussi 				idx += hist_data->n_vars;
6045e62347d2STom Zanussi 			hist_field_print(m, hist_data->fields[idx]);
604630350d65STom Zanussi 		}
6047e62347d2STom Zanussi 
6048e62347d2STom Zanussi 		if (sort_key->descending)
6049e62347d2STom Zanussi 			seq_puts(m, ".descending");
6050e62347d2STom Zanussi 	}
60517ef224d1STom Zanussi 	seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
6052a4072fe8STom Zanussi 	if (hist_data->enable_timestamps)
6053a4072fe8STom Zanussi 		seq_printf(m, ":clock=%s", hist_data->attrs->clock);
6054ccf47f5cSMasami Hiramatsu (Google) 	if (hist_data->attrs->no_hitcount)
6055ccf47f5cSMasami Hiramatsu (Google) 		seq_puts(m, ":nohitcount");
60567ef224d1STom Zanussi 
6057c282a386STom Zanussi 	print_actions_spec(m, hist_data);
6058c282a386STom Zanussi 
60597ef224d1STom Zanussi 	if (data->filter_str)
60607ef224d1STom Zanussi 		seq_printf(m, " if %s", data->filter_str);
60617ef224d1STom Zanussi 
606283e99914STom Zanussi 	if (data->paused)
606383e99914STom Zanussi 		seq_puts(m, " [paused]");
606483e99914STom Zanussi 	else
60657ef224d1STom Zanussi 		seq_puts(m, " [active]");
60667ef224d1STom Zanussi 
60677ef224d1STom Zanussi 	seq_putc(m, '\n');
60687ef224d1STom Zanussi 
60697ef224d1STom Zanussi 	return 0;
60707ef224d1STom Zanussi }
60717ef224d1STom Zanussi 
event_hist_trigger_init(struct event_trigger_data * data)607247670541STom Zanussi static int event_hist_trigger_init(struct event_trigger_data *data)
60735463bfdaSTom Zanussi {
60745463bfdaSTom Zanussi 	struct hist_trigger_data *hist_data = data->private_data;
60755463bfdaSTom Zanussi 
60765463bfdaSTom Zanussi 	if (!data->ref && hist_data->attrs->name)
60775463bfdaSTom Zanussi 		save_named_trigger(hist_data->attrs->name, data);
60785463bfdaSTom Zanussi 
60795463bfdaSTom Zanussi 	data->ref++;
60805463bfdaSTom Zanussi 
60815463bfdaSTom Zanussi 	return 0;
60825463bfdaSTom Zanussi }
60835463bfdaSTom Zanussi 
unregister_field_var_hists(struct hist_trigger_data * hist_data)608402205a67STom Zanussi static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
608502205a67STom Zanussi {
608602205a67STom Zanussi 	struct trace_event_file *file;
608702205a67STom Zanussi 	unsigned int i;
608802205a67STom Zanussi 	char *cmd;
608902205a67STom Zanussi 	int ret;
609002205a67STom Zanussi 
609102205a67STom Zanussi 	for (i = 0; i < hist_data->n_field_var_hists; i++) {
609202205a67STom Zanussi 		file = hist_data->field_var_hists[i]->hist_data->event_file;
609302205a67STom Zanussi 		cmd = hist_data->field_var_hists[i]->cmd;
60949ec5a7d1STom Zanussi 		ret = event_hist_trigger_parse(&trigger_hist_cmd, file,
609502205a67STom Zanussi 					       "!hist", "hist", cmd);
60966c610dbaSHyeonggon Yoo 		WARN_ON_ONCE(ret < 0);
609702205a67STom Zanussi 	}
609802205a67STom Zanussi }
609902205a67STom Zanussi 
event_hist_trigger_free(struct event_trigger_data * data)610047670541STom Zanussi static void event_hist_trigger_free(struct event_trigger_data *data)
61017ef224d1STom Zanussi {
61027ef224d1STom Zanussi 	struct hist_trigger_data *hist_data = data->private_data;
61037ef224d1STom Zanussi 
61047ef224d1STom Zanussi 	if (WARN_ON_ONCE(data->ref <= 0))
61057ef224d1STom Zanussi 		return;
61067ef224d1STom Zanussi 
61077ef224d1STom Zanussi 	data->ref--;
61087ef224d1STom Zanussi 	if (!data->ref) {
61095463bfdaSTom Zanussi 		if (data->name)
61105463bfdaSTom Zanussi 			del_named_trigger(data);
6111067fe038STom Zanussi 
61127ef224d1STom Zanussi 		trigger_data_free(data);
6113067fe038STom Zanussi 
6114067fe038STom Zanussi 		remove_hist_vars(hist_data);
6115067fe038STom Zanussi 
611602205a67STom Zanussi 		unregister_field_var_hists(hist_data);
611702205a67STom Zanussi 
61187ef224d1STom Zanussi 		destroy_hist_data(hist_data);
61197ef224d1STom Zanussi 	}
61207ef224d1STom Zanussi }
61217ef224d1STom Zanussi 
61227ef224d1STom Zanussi static struct event_trigger_ops event_hist_trigger_ops = {
6123fb339e53STom Zanussi 	.trigger		= event_hist_trigger,
61247ef224d1STom Zanussi 	.print			= event_hist_trigger_print,
61255463bfdaSTom Zanussi 	.init			= event_hist_trigger_init,
61267ef224d1STom Zanussi 	.free			= event_hist_trigger_free,
61277ef224d1STom Zanussi };
61287ef224d1STom Zanussi 
event_hist_trigger_named_init(struct event_trigger_data * data)612947670541STom Zanussi static int event_hist_trigger_named_init(struct event_trigger_data *data)
61305463bfdaSTom Zanussi {
61315463bfdaSTom Zanussi 	data->ref++;
61325463bfdaSTom Zanussi 
61335463bfdaSTom Zanussi 	save_named_trigger(data->named_data->name, data);
61345463bfdaSTom Zanussi 
613547670541STom Zanussi 	event_hist_trigger_init(data->named_data);
61365463bfdaSTom Zanussi 
61375463bfdaSTom Zanussi 	return 0;
61385463bfdaSTom Zanussi }
61395463bfdaSTom Zanussi 
event_hist_trigger_named_free(struct event_trigger_data * data)614047670541STom Zanussi static void event_hist_trigger_named_free(struct event_trigger_data *data)
61415463bfdaSTom Zanussi {
61425463bfdaSTom Zanussi 	if (WARN_ON_ONCE(data->ref <= 0))
61435463bfdaSTom Zanussi 		return;
61445463bfdaSTom Zanussi 
614547670541STom Zanussi 	event_hist_trigger_free(data->named_data);
61465463bfdaSTom Zanussi 
61475463bfdaSTom Zanussi 	data->ref--;
61485463bfdaSTom Zanussi 	if (!data->ref) {
61495463bfdaSTom Zanussi 		del_named_trigger(data);
61505463bfdaSTom Zanussi 		trigger_data_free(data);
61515463bfdaSTom Zanussi 	}
61525463bfdaSTom Zanussi }
61535463bfdaSTom Zanussi 
61545463bfdaSTom Zanussi static struct event_trigger_ops event_hist_trigger_named_ops = {
6155fb339e53STom Zanussi 	.trigger		= event_hist_trigger,
61565463bfdaSTom Zanussi 	.print			= event_hist_trigger_print,
61575463bfdaSTom Zanussi 	.init			= event_hist_trigger_named_init,
61585463bfdaSTom Zanussi 	.free			= event_hist_trigger_named_free,
61595463bfdaSTom Zanussi };
61605463bfdaSTom Zanussi 
event_hist_get_trigger_ops(char * cmd,char * param)61617ef224d1STom Zanussi static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
61627ef224d1STom Zanussi 							    char *param)
61637ef224d1STom Zanussi {
61647ef224d1STom Zanussi 	return &event_hist_trigger_ops;
61657ef224d1STom Zanussi }
61667ef224d1STom Zanussi 
hist_clear(struct event_trigger_data * data)6167e86ae9baSTom Zanussi static void hist_clear(struct event_trigger_data *data)
6168e86ae9baSTom Zanussi {
6169e86ae9baSTom Zanussi 	struct hist_trigger_data *hist_data = data->private_data;
6170e86ae9baSTom Zanussi 
61715463bfdaSTom Zanussi 	if (data->name)
61725463bfdaSTom Zanussi 		pause_named_trigger(data);
6173e86ae9baSTom Zanussi 
6174e0a568dcSSteven Rostedt (VMware) 	tracepoint_synchronize_unregister();
6175e86ae9baSTom Zanussi 
6176e86ae9baSTom Zanussi 	tracing_map_clear(hist_data->map);
6177e86ae9baSTom Zanussi 
61785463bfdaSTom Zanussi 	if (data->name)
61795463bfdaSTom Zanussi 		unpause_named_trigger(data);
61805463bfdaSTom Zanussi }
61815463bfdaSTom Zanussi 
compatible_field(struct ftrace_event_field * field,struct ftrace_event_field * test_field)61825463bfdaSTom Zanussi static bool compatible_field(struct ftrace_event_field *field,
61835463bfdaSTom Zanussi 			     struct ftrace_event_field *test_field)
61845463bfdaSTom Zanussi {
61855463bfdaSTom Zanussi 	if (field == test_field)
61865463bfdaSTom Zanussi 		return true;
61875463bfdaSTom Zanussi 	if (field == NULL || test_field == NULL)
61885463bfdaSTom Zanussi 		return false;
61895463bfdaSTom Zanussi 	if (strcmp(field->name, test_field->name) != 0)
61905463bfdaSTom Zanussi 		return false;
61915463bfdaSTom Zanussi 	if (strcmp(field->type, test_field->type) != 0)
61925463bfdaSTom Zanussi 		return false;
61935463bfdaSTom Zanussi 	if (field->size != test_field->size)
61945463bfdaSTom Zanussi 		return false;
61955463bfdaSTom Zanussi 	if (field->is_signed != test_field->is_signed)
61965463bfdaSTom Zanussi 		return false;
61975463bfdaSTom Zanussi 
61985463bfdaSTom Zanussi 	return true;
6199e86ae9baSTom Zanussi }
6200e86ae9baSTom Zanussi 
hist_trigger_match(struct event_trigger_data * data,struct event_trigger_data * data_test,struct event_trigger_data * named_data,bool ignore_filter)620152a7f16dSTom Zanussi static bool hist_trigger_match(struct event_trigger_data *data,
62025463bfdaSTom Zanussi 			       struct event_trigger_data *data_test,
62035463bfdaSTom Zanussi 			       struct event_trigger_data *named_data,
62045463bfdaSTom Zanussi 			       bool ignore_filter)
620552a7f16dSTom Zanussi {
620652a7f16dSTom Zanussi 	struct tracing_map_sort_key *sort_key, *sort_key_test;
620752a7f16dSTom Zanussi 	struct hist_trigger_data *hist_data, *hist_data_test;
620852a7f16dSTom Zanussi 	struct hist_field *key_field, *key_field_test;
620952a7f16dSTom Zanussi 	unsigned int i;
621052a7f16dSTom Zanussi 
62115463bfdaSTom Zanussi 	if (named_data && (named_data != data_test) &&
62125463bfdaSTom Zanussi 	    (named_data != data_test->named_data))
62135463bfdaSTom Zanussi 		return false;
62145463bfdaSTom Zanussi 
62155463bfdaSTom Zanussi 	if (!named_data && is_named_trigger(data_test))
62165463bfdaSTom Zanussi 		return false;
62175463bfdaSTom Zanussi 
621852a7f16dSTom Zanussi 	hist_data = data->private_data;
621952a7f16dSTom Zanussi 	hist_data_test = data_test->private_data;
622052a7f16dSTom Zanussi 
622152a7f16dSTom Zanussi 	if (hist_data->n_vals != hist_data_test->n_vals ||
622252a7f16dSTom Zanussi 	    hist_data->n_fields != hist_data_test->n_fields ||
622352a7f16dSTom Zanussi 	    hist_data->n_sort_keys != hist_data_test->n_sort_keys)
622452a7f16dSTom Zanussi 		return false;
622552a7f16dSTom Zanussi 
62265463bfdaSTom Zanussi 	if (!ignore_filter) {
622752a7f16dSTom Zanussi 		if ((data->filter_str && !data_test->filter_str) ||
622852a7f16dSTom Zanussi 		   (!data->filter_str && data_test->filter_str))
622952a7f16dSTom Zanussi 			return false;
62305463bfdaSTom Zanussi 	}
623152a7f16dSTom Zanussi 
623252a7f16dSTom Zanussi 	for_each_hist_field(i, hist_data) {
623352a7f16dSTom Zanussi 		key_field = hist_data->fields[i];
623452a7f16dSTom Zanussi 		key_field_test = hist_data_test->fields[i];
623552a7f16dSTom Zanussi 
623652a7f16dSTom Zanussi 		if (key_field->flags != key_field_test->flags)
623752a7f16dSTom Zanussi 			return false;
62385463bfdaSTom Zanussi 		if (!compatible_field(key_field->field, key_field_test->field))
623952a7f16dSTom Zanussi 			return false;
624052a7f16dSTom Zanussi 		if (key_field->offset != key_field_test->offset)
624152a7f16dSTom Zanussi 			return false;
6242ad42febeSTom Zanussi 		if (key_field->size != key_field_test->size)
6243ad42febeSTom Zanussi 			return false;
6244ad42febeSTom Zanussi 		if (key_field->is_signed != key_field_test->is_signed)
6245ad42febeSTom Zanussi 			return false;
62461a361dfcSTom Zanussi 		if (!!key_field->var.name != !!key_field_test->var.name)
62471a361dfcSTom Zanussi 			return false;
62481a361dfcSTom Zanussi 		if (key_field->var.name &&
62491a361dfcSTom Zanussi 		    strcmp(key_field->var.name, key_field_test->var.name) != 0)
62501a361dfcSTom Zanussi 			return false;
625152a7f16dSTom Zanussi 	}
625252a7f16dSTom Zanussi 
625352a7f16dSTom Zanussi 	for (i = 0; i < hist_data->n_sort_keys; i++) {
625452a7f16dSTom Zanussi 		sort_key = &hist_data->sort_keys[i];
625552a7f16dSTom Zanussi 		sort_key_test = &hist_data_test->sort_keys[i];
625652a7f16dSTom Zanussi 
625752a7f16dSTom Zanussi 		if (sort_key->field_idx != sort_key_test->field_idx ||
625852a7f16dSTom Zanussi 		    sort_key->descending != sort_key_test->descending)
625952a7f16dSTom Zanussi 			return false;
626052a7f16dSTom Zanussi 	}
626152a7f16dSTom Zanussi 
62625463bfdaSTom Zanussi 	if (!ignore_filter && data->filter_str &&
626352a7f16dSTom Zanussi 	    (strcmp(data->filter_str, data_test->filter_str) != 0))
626452a7f16dSTom Zanussi 		return false;
626552a7f16dSTom Zanussi 
626648f79473STom Zanussi 	if (!actions_match(hist_data, hist_data_test))
626748f79473STom Zanussi 		return false;
626848f79473STom Zanussi 
626952a7f16dSTom Zanussi 	return true;
627052a7f16dSTom Zanussi }
627152a7f16dSTom Zanussi 
existing_hist_update_only(char * glob,struct event_trigger_data * data,struct trace_event_file * file)6272a7e6b7dcSTom Zanussi static bool existing_hist_update_only(char *glob,
6273a7e6b7dcSTom Zanussi 				      struct event_trigger_data *data,
6274a7e6b7dcSTom Zanussi 				      struct trace_event_file *file)
6275a7e6b7dcSTom Zanussi {
6276a7e6b7dcSTom Zanussi 	struct hist_trigger_data *hist_data = data->private_data;
6277a7e6b7dcSTom Zanussi 	struct event_trigger_data *test, *named_data = NULL;
6278a7e6b7dcSTom Zanussi 	bool updated = false;
6279a7e6b7dcSTom Zanussi 
6280a7e6b7dcSTom Zanussi 	if (!hist_data->attrs->pause && !hist_data->attrs->cont &&
6281a7e6b7dcSTom Zanussi 	    !hist_data->attrs->clear)
6282a7e6b7dcSTom Zanussi 		goto out;
6283a7e6b7dcSTom Zanussi 
6284a7e6b7dcSTom Zanussi 	if (hist_data->attrs->name) {
6285a7e6b7dcSTom Zanussi 		named_data = find_named_trigger(hist_data->attrs->name);
6286a7e6b7dcSTom Zanussi 		if (named_data) {
6287a7e6b7dcSTom Zanussi 			if (!hist_trigger_match(data, named_data, named_data,
6288a7e6b7dcSTom Zanussi 						true))
6289a7e6b7dcSTom Zanussi 				goto out;
6290a7e6b7dcSTom Zanussi 		}
6291a7e6b7dcSTom Zanussi 	}
6292a7e6b7dcSTom Zanussi 
6293a7e6b7dcSTom Zanussi 	if (hist_data->attrs->name && !named_data)
6294a7e6b7dcSTom Zanussi 		goto out;
6295a7e6b7dcSTom Zanussi 
6296a7e6b7dcSTom Zanussi 	list_for_each_entry(test, &file->triggers, list) {
6297a7e6b7dcSTom Zanussi 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6298a7e6b7dcSTom Zanussi 			if (!hist_trigger_match(data, test, named_data, false))
6299a7e6b7dcSTom Zanussi 				continue;
6300a7e6b7dcSTom Zanussi 			if (hist_data->attrs->pause)
6301a7e6b7dcSTom Zanussi 				test->paused = true;
6302a7e6b7dcSTom Zanussi 			else if (hist_data->attrs->cont)
6303a7e6b7dcSTom Zanussi 				test->paused = false;
6304a7e6b7dcSTom Zanussi 			else if (hist_data->attrs->clear)
6305a7e6b7dcSTom Zanussi 				hist_clear(test);
6306a7e6b7dcSTom Zanussi 			updated = true;
6307a7e6b7dcSTom Zanussi 			goto out;
6308a7e6b7dcSTom Zanussi 		}
6309a7e6b7dcSTom Zanussi 	}
6310a7e6b7dcSTom Zanussi  out:
6311a7e6b7dcSTom Zanussi 	return updated;
6312a7e6b7dcSTom Zanussi }
6313a7e6b7dcSTom Zanussi 
hist_register_trigger(char * glob,struct event_trigger_data * data,struct trace_event_file * file)63142378a2d6STom Zanussi static int hist_register_trigger(char *glob,
63157ef224d1STom Zanussi 				 struct event_trigger_data *data,
63167ef224d1STom Zanussi 				 struct trace_event_file *file)
63177ef224d1STom Zanussi {
631883e99914STom Zanussi 	struct hist_trigger_data *hist_data = data->private_data;
63195463bfdaSTom Zanussi 	struct event_trigger_data *test, *named_data = NULL;
6320d0cd871bSSteven Rostedt (VMware) 	struct trace_array *tr = file->tr;
63217ef224d1STom Zanussi 	int ret = 0;
63227ef224d1STom Zanussi 
63235463bfdaSTom Zanussi 	if (hist_data->attrs->name) {
63245463bfdaSTom Zanussi 		named_data = find_named_trigger(hist_data->attrs->name);
63255463bfdaSTom Zanussi 		if (named_data) {
63265463bfdaSTom Zanussi 			if (!hist_trigger_match(data, named_data, named_data,
63275463bfdaSTom Zanussi 						true)) {
6328d0cd871bSSteven Rostedt (VMware) 				hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name));
63295463bfdaSTom Zanussi 				ret = -EINVAL;
63305463bfdaSTom Zanussi 				goto out;
63315463bfdaSTom Zanussi 			}
63325463bfdaSTom Zanussi 		}
63335463bfdaSTom Zanussi 	}
63345463bfdaSTom Zanussi 
63355463bfdaSTom Zanussi 	if (hist_data->attrs->name && !named_data)
63365463bfdaSTom Zanussi 		goto new;
63375463bfdaSTom Zanussi 
6338aeed8aa3SMasami Hiramatsu 	lockdep_assert_held(&event_mutex);
6339aeed8aa3SMasami Hiramatsu 
6340aeed8aa3SMasami Hiramatsu 	list_for_each_entry(test, &file->triggers, list) {
63417ef224d1STom Zanussi 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6342a7e6b7dcSTom Zanussi 			if (hist_trigger_match(data, test, named_data, false)) {
6343d0cd871bSSteven Rostedt (VMware) 				hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
63447ef224d1STom Zanussi 				ret = -EEXIST;
63457ef224d1STom Zanussi 				goto out;
63467ef224d1STom Zanussi 			}
63477ef224d1STom Zanussi 		}
6348a7e6b7dcSTom Zanussi 	}
63495463bfdaSTom Zanussi  new:
6350e86ae9baSTom Zanussi 	if (hist_data->attrs->cont || hist_data->attrs->clear) {
6351d0cd871bSSteven Rostedt (VMware) 		hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0);
635283e99914STom Zanussi 		ret = -ENOENT;
635383e99914STom Zanussi 		goto out;
635483e99914STom Zanussi 	}
635583e99914STom Zanussi 
63567522c03aSTom Zanussi 	if (hist_data->attrs->pause)
63577522c03aSTom Zanussi 		data->paused = true;
63587522c03aSTom Zanussi 
63595463bfdaSTom Zanussi 	if (named_data) {
63605463bfdaSTom Zanussi 		data->private_data = named_data->private_data;
63615463bfdaSTom Zanussi 		set_named_trigger_data(data, named_data);
63625463bfdaSTom Zanussi 		data->ops = &event_hist_trigger_named_ops;
63635463bfdaSTom Zanussi 	}
63645463bfdaSTom Zanussi 
63657ef224d1STom Zanussi 	if (data->ops->init) {
636647670541STom Zanussi 		ret = data->ops->init(data);
63677ef224d1STom Zanussi 		if (ret < 0)
63687ef224d1STom Zanussi 			goto out;
63697ef224d1STom Zanussi 	}
63707ef224d1STom Zanussi 
6371a4072fe8STom Zanussi 	if (hist_data->enable_timestamps) {
6372a4072fe8STom Zanussi 		char *clock = hist_data->attrs->clock;
63737ef224d1STom Zanussi 
6374a4072fe8STom Zanussi 		ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
6375a4072fe8STom Zanussi 		if (ret) {
6376d0cd871bSSteven Rostedt (VMware) 			hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock));
6377a4072fe8STom Zanussi 			goto out;
6378a4072fe8STom Zanussi 		}
6379a4072fe8STom Zanussi 
6380b94bc80dSSteven Rostedt (VMware) 		tracing_set_filter_buffering(file->tr, true);
6381a4072fe8STom Zanussi 	}
6382a4072fe8STom Zanussi 
6383a4072fe8STom Zanussi 	if (named_data)
6384a4072fe8STom Zanussi 		destroy_hist_data(hist_data);
6385067fe038STom Zanussi  out:
6386067fe038STom Zanussi 	return ret;
6387067fe038STom Zanussi }
6388067fe038STom Zanussi 
hist_trigger_enable(struct event_trigger_data * data,struct trace_event_file * file)6389067fe038STom Zanussi static int hist_trigger_enable(struct event_trigger_data *data,
6390067fe038STom Zanussi 			       struct trace_event_file *file)
6391067fe038STom Zanussi {
6392067fe038STom Zanussi 	int ret = 0;
6393067fe038STom Zanussi 
6394067fe038STom Zanussi 	list_add_tail_rcu(&data->list, &file->triggers);
6395067fe038STom Zanussi 
6396067fe038STom Zanussi 	update_cond_flag(file);
6397ad42febeSTom Zanussi 
63987ef224d1STom Zanussi 	if (trace_event_trigger_enable_disable(file, 1) < 0) {
63997ef224d1STom Zanussi 		list_del_rcu(&data->list);
64007ef224d1STom Zanussi 		update_cond_flag(file);
64017ef224d1STom Zanussi 		ret--;
64027ef224d1STom Zanussi 	}
6403067fe038STom Zanussi 
64047ef224d1STom Zanussi 	return ret;
64057ef224d1STom Zanussi }
64067ef224d1STom Zanussi 
have_hist_trigger_match(struct event_trigger_data * data,struct trace_event_file * file)64074b147936STom Zanussi static bool have_hist_trigger_match(struct event_trigger_data *data,
64084b147936STom Zanussi 				    struct trace_event_file *file)
64094b147936STom Zanussi {
64104b147936STom Zanussi 	struct hist_trigger_data *hist_data = data->private_data;
64114b147936STom Zanussi 	struct event_trigger_data *test, *named_data = NULL;
64124b147936STom Zanussi 	bool match = false;
64134b147936STom Zanussi 
6414aeed8aa3SMasami Hiramatsu 	lockdep_assert_held(&event_mutex);
6415aeed8aa3SMasami Hiramatsu 
64164b147936STom Zanussi 	if (hist_data->attrs->name)
64174b147936STom Zanussi 		named_data = find_named_trigger(hist_data->attrs->name);
64184b147936STom Zanussi 
6419aeed8aa3SMasami Hiramatsu 	list_for_each_entry(test, &file->triggers, list) {
64204b147936STom Zanussi 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
64214b147936STom Zanussi 			if (hist_trigger_match(data, test, named_data, false)) {
64224b147936STom Zanussi 				match = true;
64234b147936STom Zanussi 				break;
64244b147936STom Zanussi 			}
64254b147936STom Zanussi 		}
64264b147936STom Zanussi 	}
64274b147936STom Zanussi 
64284b147936STom Zanussi 	return match;
64294b147936STom Zanussi }
64304b147936STom Zanussi 
hist_trigger_check_refs(struct event_trigger_data * data,struct trace_event_file * file)6431067fe038STom Zanussi static bool hist_trigger_check_refs(struct event_trigger_data *data,
6432067fe038STom Zanussi 				    struct trace_event_file *file)
6433067fe038STom Zanussi {
6434067fe038STom Zanussi 	struct hist_trigger_data *hist_data = data->private_data;
6435067fe038STom Zanussi 	struct event_trigger_data *test, *named_data = NULL;
6436067fe038STom Zanussi 
6437aeed8aa3SMasami Hiramatsu 	lockdep_assert_held(&event_mutex);
6438aeed8aa3SMasami Hiramatsu 
6439067fe038STom Zanussi 	if (hist_data->attrs->name)
6440067fe038STom Zanussi 		named_data = find_named_trigger(hist_data->attrs->name);
6441067fe038STom Zanussi 
6442aeed8aa3SMasami Hiramatsu 	list_for_each_entry(test, &file->triggers, list) {
6443067fe038STom Zanussi 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6444067fe038STom Zanussi 			if (!hist_trigger_match(data, test, named_data, false))
6445067fe038STom Zanussi 				continue;
6446067fe038STom Zanussi 			hist_data = test->private_data;
6447067fe038STom Zanussi 			if (check_var_refs(hist_data))
6448067fe038STom Zanussi 				return true;
6449067fe038STom Zanussi 			break;
6450067fe038STom Zanussi 		}
6451067fe038STom Zanussi 	}
6452067fe038STom Zanussi 
6453067fe038STom Zanussi 	return false;
6454067fe038STom Zanussi }
6455067fe038STom Zanussi 
hist_unregister_trigger(char * glob,struct event_trigger_data * data,struct trace_event_file * file)64562378a2d6STom Zanussi static void hist_unregister_trigger(char *glob,
645752a7f16dSTom Zanussi 				    struct event_trigger_data *data,
645852a7f16dSTom Zanussi 				    struct trace_event_file *file)
645952a7f16dSTom Zanussi {
646045e333ceSJakob Koschel 	struct event_trigger_data *test = NULL, *iter, *named_data = NULL;
64615463bfdaSTom Zanussi 	struct hist_trigger_data *hist_data = data->private_data;
646252a7f16dSTom Zanussi 
6463aeed8aa3SMasami Hiramatsu 	lockdep_assert_held(&event_mutex);
6464aeed8aa3SMasami Hiramatsu 
64655463bfdaSTom Zanussi 	if (hist_data->attrs->name)
64665463bfdaSTom Zanussi 		named_data = find_named_trigger(hist_data->attrs->name);
64675463bfdaSTom Zanussi 
646845e333ceSJakob Koschel 	list_for_each_entry(iter, &file->triggers, list) {
646945e333ceSJakob Koschel 		if (iter->cmd_ops->trigger_type == ETT_EVENT_HIST) {
647045e333ceSJakob Koschel 			if (!hist_trigger_match(data, iter, named_data, false))
647152a7f16dSTom Zanussi 				continue;
647245e333ceSJakob Koschel 			test = iter;
647352a7f16dSTom Zanussi 			list_del_rcu(&test->list);
647452a7f16dSTom Zanussi 			trace_event_trigger_enable_disable(file, 0);
647552a7f16dSTom Zanussi 			update_cond_flag(file);
647652a7f16dSTom Zanussi 			break;
647752a7f16dSTom Zanussi 		}
647852a7f16dSTom Zanussi 	}
647952a7f16dSTom Zanussi 
648045e333ceSJakob Koschel 	if (test && test->ops->free)
648147670541STom Zanussi 		test->ops->free(test);
6482ad42febeSTom Zanussi 
6483ad42febeSTom Zanussi 	if (hist_data->enable_timestamps) {
648445e333ceSJakob Koschel 		if (!hist_data->remove || test)
6485b94bc80dSSteven Rostedt (VMware) 			tracing_set_filter_buffering(file->tr, false);
6486ad42febeSTom Zanussi 	}
648752a7f16dSTom Zanussi }
648852a7f16dSTom Zanussi 
hist_file_check_refs(struct trace_event_file * file)6489067fe038STom Zanussi static bool hist_file_check_refs(struct trace_event_file *file)
6490067fe038STom Zanussi {
6491067fe038STom Zanussi 	struct hist_trigger_data *hist_data;
6492067fe038STom Zanussi 	struct event_trigger_data *test;
6493067fe038STom Zanussi 
6494aeed8aa3SMasami Hiramatsu 	lockdep_assert_held(&event_mutex);
6495aeed8aa3SMasami Hiramatsu 
6496aeed8aa3SMasami Hiramatsu 	list_for_each_entry(test, &file->triggers, list) {
6497067fe038STom Zanussi 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6498067fe038STom Zanussi 			hist_data = test->private_data;
6499067fe038STom Zanussi 			if (check_var_refs(hist_data))
6500067fe038STom Zanussi 				return true;
6501067fe038STom Zanussi 		}
6502067fe038STom Zanussi 	}
6503067fe038STom Zanussi 
6504067fe038STom Zanussi 	return false;
6505067fe038STom Zanussi }
6506067fe038STom Zanussi 
hist_unreg_all(struct trace_event_file * file)650752a7f16dSTom Zanussi static void hist_unreg_all(struct trace_event_file *file)
650852a7f16dSTom Zanussi {
650947c18569SSteven Rostedt 	struct event_trigger_data *test, *n;
6510ad42febeSTom Zanussi 	struct hist_trigger_data *hist_data;
65114b147936STom Zanussi 	struct synth_event *se;
65124b147936STom Zanussi 	const char *se_name;
651352a7f16dSTom Zanussi 
65140e2b81f7SMasami Hiramatsu 	lockdep_assert_held(&event_mutex);
65150e2b81f7SMasami Hiramatsu 
6516067fe038STom Zanussi 	if (hist_file_check_refs(file))
6517067fe038STom Zanussi 		return;
6518067fe038STom Zanussi 
651947c18569SSteven Rostedt 	list_for_each_entry_safe(test, n, &file->triggers, list) {
652052a7f16dSTom Zanussi 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6521ad42febeSTom Zanussi 			hist_data = test->private_data;
652252a7f16dSTom Zanussi 			list_del_rcu(&test->list);
652352a7f16dSTom Zanussi 			trace_event_trigger_enable_disable(file, 0);
65244b147936STom Zanussi 
65254b147936STom Zanussi 			se_name = trace_event_name(file->event_call);
65264b147936STom Zanussi 			se = find_synth_event(se_name);
65274b147936STom Zanussi 			if (se)
65284b147936STom Zanussi 				se->ref--;
65294b147936STom Zanussi 
653052a7f16dSTom Zanussi 			update_cond_flag(file);
6531ad42febeSTom Zanussi 			if (hist_data->enable_timestamps)
6532b94bc80dSSteven Rostedt (VMware) 				tracing_set_filter_buffering(file->tr, false);
653352a7f16dSTom Zanussi 			if (test->ops->free)
653447670541STom Zanussi 				test->ops->free(test);
653552a7f16dSTom Zanussi 		}
653652a7f16dSTom Zanussi 	}
653752a7f16dSTom Zanussi }
653852a7f16dSTom Zanussi 
event_hist_trigger_parse(struct event_command * cmd_ops,struct trace_event_file * file,char * glob,char * cmd,char * param_and_filter)65399ec5a7d1STom Zanussi static int event_hist_trigger_parse(struct event_command *cmd_ops,
65407ef224d1STom Zanussi 				    struct trace_event_file *file,
6541e1f187d0STom Zanussi 				    char *glob, char *cmd,
6542e1f187d0STom Zanussi 				    char *param_and_filter)
65437ef224d1STom Zanussi {
65447ef224d1STom Zanussi 	unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
65457ef224d1STom Zanussi 	struct event_trigger_data *trigger_data;
65467ef224d1STom Zanussi 	struct hist_trigger_attrs *attrs;
65477ef224d1STom Zanussi 	struct hist_trigger_data *hist_data;
6548e1f187d0STom Zanussi 	char *param, *filter, *p, *start;
65494b147936STom Zanussi 	struct synth_event *se;
65504b147936STom Zanussi 	const char *se_name;
6551e1f187d0STom Zanussi 	bool remove;
65527ef224d1STom Zanussi 	int ret = 0;
65537ef224d1STom Zanussi 
65540e2b81f7SMasami Hiramatsu 	lockdep_assert_held(&event_mutex);
65550e2b81f7SMasami Hiramatsu 
655697a5d2e5SAmmar Faizi 	if (WARN_ON(!glob))
655797a5d2e5SAmmar Faizi 		return -EINVAL;
6558798a5b6cSTom Zanussi 
655969686fcbSAmmar Faizi 	if (glob[0]) {
6560f404da6eSTom Zanussi 		hist_err_clear();
6561e1f187d0STom Zanussi 		last_cmd_set(file, param_and_filter);
6562f404da6eSTom Zanussi 	}
6563f404da6eSTom Zanussi 
6564e1f187d0STom Zanussi 	remove = event_trigger_check_remove(glob);
65657ef224d1STom Zanussi 
6566e1f187d0STom Zanussi 	if (event_trigger_empty_param(param_and_filter))
6567e1f187d0STom Zanussi 		return -EINVAL;
656830350d65STom Zanussi 
6569ec5ce098STom Zanussi 	/*
6570ec5ce098STom Zanussi 	 * separate the trigger from the filter (k:v [if filter])
6571ec5ce098STom Zanussi 	 * allowing for whitespace in the trigger
6572ec5ce098STom Zanussi 	 */
6573e1f187d0STom Zanussi 	p = param = param_and_filter;
6574ec5ce098STom Zanussi 	do {
6575ec5ce098STom Zanussi 		p = strstr(p, "if");
6576ec5ce098STom Zanussi 		if (!p)
6577ec5ce098STom Zanussi 			break;
6578e1f187d0STom Zanussi 		if (p == param_and_filter)
65797ef224d1STom Zanussi 			return -EINVAL;
6580ec5ce098STom Zanussi 		if (*(p - 1) != ' ' && *(p - 1) != '\t') {
6581ec5ce098STom Zanussi 			p++;
6582ec5ce098STom Zanussi 			continue;
6583ec5ce098STom Zanussi 		}
6584e1f187d0STom Zanussi 		if (p >= param_and_filter + strlen(param_and_filter) - (sizeof("if") - 1) - 1)
6585ec5ce098STom Zanussi 			return -EINVAL;
65862f31ed93STom Zanussi 		if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
6587ec5ce098STom Zanussi 			p++;
6588ec5ce098STom Zanussi 			continue;
6589ec5ce098STom Zanussi 		}
6590ec5ce098STom Zanussi 		break;
6591b59f2f2bSTom Zanussi 	} while (1);
6592ec5ce098STom Zanussi 
6593ec5ce098STom Zanussi 	if (!p)
6594e1f187d0STom Zanussi 		filter = NULL;
6595ec5ce098STom Zanussi 	else {
6596ec5ce098STom Zanussi 		*(p - 1) = '\0';
6597e1f187d0STom Zanussi 		filter = strstrip(p);
6598e1f187d0STom Zanussi 		param = strstrip(param);
6599ec5ce098STom Zanussi 	}
66007ef224d1STom Zanussi 
6601c5eac6eeSKalesh Singh 	/*
6602c5eac6eeSKalesh Singh 	 * To simplify arithmetic expression parsing, replace occurrences of
6603c5eac6eeSKalesh Singh 	 * '.sym-offset' modifier with '.symXoffset'
6604c5eac6eeSKalesh Singh 	 */
6605e1f187d0STom Zanussi 	start = strstr(param, ".sym-offset");
6606c5eac6eeSKalesh Singh 	while (start) {
6607c5eac6eeSKalesh Singh 		*(start + 4) = 'X';
6608c5eac6eeSKalesh Singh 		start = strstr(start + 11, ".sym-offset");
6609feea69ecSkernel test robot 	}
6610c5eac6eeSKalesh Singh 
6611e1f187d0STom Zanussi 	attrs = parse_hist_trigger_attrs(file->tr, param);
66127ef224d1STom Zanussi 	if (IS_ERR(attrs))
66137ef224d1STom Zanussi 		return PTR_ERR(attrs);
66147ef224d1STom Zanussi 
66157ef224d1STom Zanussi 	if (attrs->map_bits)
66167ef224d1STom Zanussi 		hist_trigger_bits = attrs->map_bits;
66177ef224d1STom Zanussi 
661830350d65STom Zanussi 	hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
66197ef224d1STom Zanussi 	if (IS_ERR(hist_data)) {
66207ef224d1STom Zanussi 		destroy_hist_trigger_attrs(attrs);
66217ef224d1STom Zanussi 		return PTR_ERR(hist_data);
66227ef224d1STom Zanussi 	}
66237ef224d1STom Zanussi 
6624e1f187d0STom Zanussi 	trigger_data = event_trigger_alloc(cmd_ops, cmd, param, hist_data);
66254b147936STom Zanussi 	if (!trigger_data) {
66264b147936STom Zanussi 		ret = -ENOMEM;
66277ef224d1STom Zanussi 		goto out_free;
66284b147936STom Zanussi 	}
66297ef224d1STom Zanussi 
6630e1f187d0STom Zanussi 	ret = event_trigger_set_filter(cmd_ops, file, filter, trigger_data);
663152a7f16dSTom Zanussi 	if (ret < 0)
663252a7f16dSTom Zanussi 		goto out_free;
663352a7f16dSTom Zanussi 
663430350d65STom Zanussi 	if (remove) {
66354b147936STom Zanussi 		if (!have_hist_trigger_match(trigger_data, file))
66364b147936STom Zanussi 			goto out_free;
66374b147936STom Zanussi 
6638067fe038STom Zanussi 		if (hist_trigger_check_refs(trigger_data, file)) {
6639067fe038STom Zanussi 			ret = -EBUSY;
6640067fe038STom Zanussi 			goto out_free;
6641067fe038STom Zanussi 		}
6642067fe038STom Zanussi 
6643b8cc44a4STom Zanussi 		event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
66444b147936STom Zanussi 		se_name = trace_event_name(file->event_call);
66454b147936STom Zanussi 		se = find_synth_event(se_name);
66464b147936STom Zanussi 		if (se)
66474b147936STom Zanussi 			se->ref--;
66487ef224d1STom Zanussi 		ret = 0;
66497ef224d1STom Zanussi 		goto out_free;
66507ef224d1STom Zanussi 	}
66517ef224d1STom Zanussi 
6652a7e6b7dcSTom Zanussi 	if (existing_hist_update_only(glob, trigger_data, file))
6653a7e6b7dcSTom Zanussi 		goto out_free;
6654a7e6b7dcSTom Zanussi 
6655b8cc44a4STom Zanussi 	ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
6656a7e6b7dcSTom Zanussi 	if (ret < 0)
6657b8cc44a4STom Zanussi 		goto out_free;
6658067fe038STom Zanussi 
6659067fe038STom Zanussi 	if (get_named_trigger_data(trigger_data))
6660067fe038STom Zanussi 		goto enable;
6661067fe038STom Zanussi 
66627d18a10cSTom Zanussi 	ret = create_actions(hist_data);
66630212e2aaSTom Zanussi 	if (ret)
66640212e2aaSTom Zanussi 		goto out_unreg;
66650212e2aaSTom Zanussi 
66666018b585SMohamed Khalfella 	if (has_hist_vars(hist_data) || hist_data->n_var_refs) {
66674b8b3905SMohamed Khalfella 		ret = save_hist_vars(hist_data);
66684b8b3905SMohamed Khalfella 		if (ret)
66696018b585SMohamed Khalfella 			goto out_unreg;
66706018b585SMohamed Khalfella 	}
66716018b585SMohamed Khalfella 
6672067fe038STom Zanussi 	ret = tracing_map_init(hist_data->map);
6673067fe038STom Zanussi 	if (ret)
6674067fe038STom Zanussi 		goto out_unreg;
6675067fe038STom Zanussi enable:
6676067fe038STom Zanussi 	ret = hist_trigger_enable(trigger_data, file);
6677067fe038STom Zanussi 	if (ret)
6678067fe038STom Zanussi 		goto out_unreg;
6679067fe038STom Zanussi 
66804b147936STom Zanussi 	se_name = trace_event_name(file->event_call);
66814b147936STom Zanussi 	se = find_synth_event(se_name);
66824b147936STom Zanussi 	if (se)
66834b147936STom Zanussi 		se->ref++;
66847ef224d1STom Zanussi  out:
6685608c6ed3SZheng Yejian 	if (ret == 0 && glob[0])
6686f404da6eSTom Zanussi 		hist_err_clear();
6687f404da6eSTom Zanussi 
66887ef224d1STom Zanussi 	return ret;
6689067fe038STom Zanussi  out_unreg:
6690b8cc44a4STom Zanussi 	event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
66917ef224d1STom Zanussi  out_free:
6692e1f187d0STom Zanussi 	event_trigger_reset_filter(cmd_ops, trigger_data);
66937ef224d1STom Zanussi 
6694067fe038STom Zanussi 	remove_hist_vars(hist_data);
6695067fe038STom Zanussi 
66967ef224d1STom Zanussi 	kfree(trigger_data);
66977ef224d1STom Zanussi 
66987ef224d1STom Zanussi 	destroy_hist_data(hist_data);
66997ef224d1STom Zanussi 	goto out;
67007ef224d1STom Zanussi }
67017ef224d1STom Zanussi 
67027ef224d1STom Zanussi static struct event_command trigger_hist_cmd = {
67037ef224d1STom Zanussi 	.name			= "hist",
67047ef224d1STom Zanussi 	.trigger_type		= ETT_EVENT_HIST,
67057ef224d1STom Zanussi 	.flags			= EVENT_CMD_FL_NEEDS_REC,
67069ec5a7d1STom Zanussi 	.parse			= event_hist_trigger_parse,
67077ef224d1STom Zanussi 	.reg			= hist_register_trigger,
670852a7f16dSTom Zanussi 	.unreg			= hist_unregister_trigger,
670952a7f16dSTom Zanussi 	.unreg_all		= hist_unreg_all,
67107ef224d1STom Zanussi 	.get_trigger_ops	= event_hist_get_trigger_ops,
67117ef224d1STom Zanussi 	.set_filter		= set_trigger_filter,
67127ef224d1STom Zanussi };
67137ef224d1STom Zanussi 
register_trigger_hist_cmd(void)67147ef224d1STom Zanussi __init int register_trigger_hist_cmd(void)
67157ef224d1STom Zanussi {
67167ef224d1STom Zanussi 	int ret;
67177ef224d1STom Zanussi 
67187ef224d1STom Zanussi 	ret = register_event_command(&trigger_hist_cmd);
67197ef224d1STom Zanussi 	WARN_ON(ret < 0);
67207ef224d1STom Zanussi 
67217ef224d1STom Zanussi 	return ret;
67227ef224d1STom Zanussi }
6723d0bad49bSTom Zanussi 
6724d0bad49bSTom Zanussi static void
hist_enable_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)6725b47e3302SSteven Rostedt (VMware) hist_enable_trigger(struct event_trigger_data *data,
6726b47e3302SSteven Rostedt (VMware) 		    struct trace_buffer *buffer,  void *rec,
67271ac4f51cSTom Zanussi 		    struct ring_buffer_event *event)
6728d0bad49bSTom Zanussi {
6729d0bad49bSTom Zanussi 	struct enable_trigger_data *enable_data = data->private_data;
6730d0bad49bSTom Zanussi 	struct event_trigger_data *test;
6731d0bad49bSTom Zanussi 
6732aeed8aa3SMasami Hiramatsu 	list_for_each_entry_rcu(test, &enable_data->file->triggers, list,
6733aeed8aa3SMasami Hiramatsu 				lockdep_is_held(&event_mutex)) {
6734d0bad49bSTom Zanussi 		if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6735d0bad49bSTom Zanussi 			if (enable_data->enable)
6736d0bad49bSTom Zanussi 				test->paused = false;
6737d0bad49bSTom Zanussi 			else
6738d0bad49bSTom Zanussi 				test->paused = true;
6739d0bad49bSTom Zanussi 		}
6740d0bad49bSTom Zanussi 	}
6741d0bad49bSTom Zanussi }
6742d0bad49bSTom Zanussi 
6743d0bad49bSTom Zanussi static void
hist_enable_count_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)6744b47e3302SSteven Rostedt (VMware) hist_enable_count_trigger(struct event_trigger_data *data,
6745b47e3302SSteven Rostedt (VMware) 			  struct trace_buffer *buffer,  void *rec,
67461ac4f51cSTom Zanussi 			  struct ring_buffer_event *event)
6747d0bad49bSTom Zanussi {
6748d0bad49bSTom Zanussi 	if (!data->count)
6749d0bad49bSTom Zanussi 		return;
6750d0bad49bSTom Zanussi 
6751d0bad49bSTom Zanussi 	if (data->count != -1)
6752d0bad49bSTom Zanussi 		(data->count)--;
6753d0bad49bSTom Zanussi 
6754b47e3302SSteven Rostedt (VMware) 	hist_enable_trigger(data, buffer, rec, event);
6755d0bad49bSTom Zanussi }
6756d0bad49bSTom Zanussi 
6757d0bad49bSTom Zanussi static struct event_trigger_ops hist_enable_trigger_ops = {
6758fb339e53STom Zanussi 	.trigger		= hist_enable_trigger,
6759d0bad49bSTom Zanussi 	.print			= event_enable_trigger_print,
6760d0bad49bSTom Zanussi 	.init			= event_trigger_init,
6761d0bad49bSTom Zanussi 	.free			= event_enable_trigger_free,
6762d0bad49bSTom Zanussi };
6763d0bad49bSTom Zanussi 
6764d0bad49bSTom Zanussi static struct event_trigger_ops hist_enable_count_trigger_ops = {
6765fb339e53STom Zanussi 	.trigger		= hist_enable_count_trigger,
6766d0bad49bSTom Zanussi 	.print			= event_enable_trigger_print,
6767d0bad49bSTom Zanussi 	.init			= event_trigger_init,
6768d0bad49bSTom Zanussi 	.free			= event_enable_trigger_free,
6769d0bad49bSTom Zanussi };
6770d0bad49bSTom Zanussi 
6771d0bad49bSTom Zanussi static struct event_trigger_ops hist_disable_trigger_ops = {
6772fb339e53STom Zanussi 	.trigger		= hist_enable_trigger,
6773d0bad49bSTom Zanussi 	.print			= event_enable_trigger_print,
6774d0bad49bSTom Zanussi 	.init			= event_trigger_init,
6775d0bad49bSTom Zanussi 	.free			= event_enable_trigger_free,
6776d0bad49bSTom Zanussi };
6777d0bad49bSTom Zanussi 
6778d0bad49bSTom Zanussi static struct event_trigger_ops hist_disable_count_trigger_ops = {
6779fb339e53STom Zanussi 	.trigger		= hist_enable_count_trigger,
6780d0bad49bSTom Zanussi 	.print			= event_enable_trigger_print,
6781d0bad49bSTom Zanussi 	.init			= event_trigger_init,
6782d0bad49bSTom Zanussi 	.free			= event_enable_trigger_free,
6783d0bad49bSTom Zanussi };
6784d0bad49bSTom Zanussi 
6785d0bad49bSTom Zanussi static struct event_trigger_ops *
hist_enable_get_trigger_ops(char * cmd,char * param)6786d0bad49bSTom Zanussi hist_enable_get_trigger_ops(char *cmd, char *param)
6787d0bad49bSTom Zanussi {
6788d0bad49bSTom Zanussi 	struct event_trigger_ops *ops;
6789d0bad49bSTom Zanussi 	bool enable;
6790d0bad49bSTom Zanussi 
6791d0bad49bSTom Zanussi 	enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
6792d0bad49bSTom Zanussi 
6793d0bad49bSTom Zanussi 	if (enable)
6794d0bad49bSTom Zanussi 		ops = param ? &hist_enable_count_trigger_ops :
6795d0bad49bSTom Zanussi 			&hist_enable_trigger_ops;
6796d0bad49bSTom Zanussi 	else
6797d0bad49bSTom Zanussi 		ops = param ? &hist_disable_count_trigger_ops :
6798d0bad49bSTom Zanussi 			&hist_disable_trigger_ops;
6799d0bad49bSTom Zanussi 
6800d0bad49bSTom Zanussi 	return ops;
6801d0bad49bSTom Zanussi }
6802d0bad49bSTom Zanussi 
hist_enable_unreg_all(struct trace_event_file * file)680352a7f16dSTom Zanussi static void hist_enable_unreg_all(struct trace_event_file *file)
680452a7f16dSTom Zanussi {
680547c18569SSteven Rostedt 	struct event_trigger_data *test, *n;
680652a7f16dSTom Zanussi 
680747c18569SSteven Rostedt 	list_for_each_entry_safe(test, n, &file->triggers, list) {
680852a7f16dSTom Zanussi 		if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
680952a7f16dSTom Zanussi 			list_del_rcu(&test->list);
681052a7f16dSTom Zanussi 			update_cond_flag(file);
681152a7f16dSTom Zanussi 			trace_event_trigger_enable_disable(file, 0);
681252a7f16dSTom Zanussi 			if (test->ops->free)
681347670541STom Zanussi 				test->ops->free(test);
681452a7f16dSTom Zanussi 		}
681552a7f16dSTom Zanussi 	}
681652a7f16dSTom Zanussi }
681752a7f16dSTom Zanussi 
6818d0bad49bSTom Zanussi static struct event_command trigger_hist_enable_cmd = {
6819d0bad49bSTom Zanussi 	.name			= ENABLE_HIST_STR,
6820d0bad49bSTom Zanussi 	.trigger_type		= ETT_HIST_ENABLE,
68219ec5a7d1STom Zanussi 	.parse			= event_enable_trigger_parse,
6822d0bad49bSTom Zanussi 	.reg			= event_enable_register_trigger,
6823d0bad49bSTom Zanussi 	.unreg			= event_enable_unregister_trigger,
682452a7f16dSTom Zanussi 	.unreg_all		= hist_enable_unreg_all,
6825d0bad49bSTom Zanussi 	.get_trigger_ops	= hist_enable_get_trigger_ops,
6826d0bad49bSTom Zanussi 	.set_filter		= set_trigger_filter,
6827d0bad49bSTom Zanussi };
6828d0bad49bSTom Zanussi 
6829d0bad49bSTom Zanussi static struct event_command trigger_hist_disable_cmd = {
6830d0bad49bSTom Zanussi 	.name			= DISABLE_HIST_STR,
6831d0bad49bSTom Zanussi 	.trigger_type		= ETT_HIST_ENABLE,
68329ec5a7d1STom Zanussi 	.parse			= event_enable_trigger_parse,
6833d0bad49bSTom Zanussi 	.reg			= event_enable_register_trigger,
6834d0bad49bSTom Zanussi 	.unreg			= event_enable_unregister_trigger,
683552a7f16dSTom Zanussi 	.unreg_all		= hist_enable_unreg_all,
6836d0bad49bSTom Zanussi 	.get_trigger_ops	= hist_enable_get_trigger_ops,
6837d0bad49bSTom Zanussi 	.set_filter		= set_trigger_filter,
6838d0bad49bSTom Zanussi };
6839d0bad49bSTom Zanussi 
unregister_trigger_hist_enable_disable_cmds(void)6840d0bad49bSTom Zanussi static __init void unregister_trigger_hist_enable_disable_cmds(void)
6841d0bad49bSTom Zanussi {
6842d0bad49bSTom Zanussi 	unregister_event_command(&trigger_hist_enable_cmd);
6843d0bad49bSTom Zanussi 	unregister_event_command(&trigger_hist_disable_cmd);
6844d0bad49bSTom Zanussi }
6845d0bad49bSTom Zanussi 
register_trigger_hist_enable_disable_cmds(void)6846d0bad49bSTom Zanussi __init int register_trigger_hist_enable_disable_cmds(void)
6847d0bad49bSTom Zanussi {
6848d0bad49bSTom Zanussi 	int ret;
6849d0bad49bSTom Zanussi 
6850d0bad49bSTom Zanussi 	ret = register_event_command(&trigger_hist_enable_cmd);
6851d0bad49bSTom Zanussi 	if (WARN_ON(ret < 0))
6852d0bad49bSTom Zanussi 		return ret;
6853d0bad49bSTom Zanussi 	ret = register_event_command(&trigger_hist_disable_cmd);
6854d0bad49bSTom Zanussi 	if (WARN_ON(ret < 0))
6855d0bad49bSTom Zanussi 		unregister_trigger_hist_enable_disable_cmds();
6856d0bad49bSTom Zanussi 
6857d0bad49bSTom Zanussi 	return ret;
6858d0bad49bSTom Zanussi }
6859