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