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