1 // SPDX-License-Identifier: GPL-2.0
3 * trace_events_hist - trace event hist triggers
5 * Copyright (C) 2015 Tom Zanussi <tom.zanussi@linux.intel.com>
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>
17 /* for gfp flag names */
18 #include <linux/trace_events.h>
19 #include <trace/events/mmflags.h>
21 #include "tracing_map.h"
22 #include "trace_synth.h"
25 C(NONE, "No error"), \
26 C(DUPLICATE_VAR, "Variable already defined"), \
27 C(VAR_NOT_UNIQUE, "Variable name not unique, need to use fully qualified name (subsys.event.var) for variable"), \
28 C(TOO_MANY_VARS, "Too many variables defined"), \
29 C(MALFORMED_ASSIGNMENT, "Malformed assignment"), \
30 C(NAMED_MISMATCH, "Named hist trigger doesn't match existing named trigger (includes variables)"), \
31 C(TRIGGER_EEXIST, "Hist trigger already exists"), \
32 C(TRIGGER_ENOENT_CLEAR, "Can't clear or continue a nonexistent hist trigger"), \
33 C(SET_CLOCK_FAIL, "Couldn't set trace_clock"), \
34 C(BAD_FIELD_MODIFIER, "Invalid field modifier"), \
35 C(TOO_MANY_SUBEXPR, "Too many subexpressions (3 max)"), \
36 C(TIMESTAMP_MISMATCH, "Timestamp units in expression don't match"), \
37 C(TOO_MANY_FIELD_VARS, "Too many field variables defined"), \
38 C(EVENT_FILE_NOT_FOUND, "Event file not found"), \
39 C(HIST_NOT_FOUND, "Matching event histogram not found"), \
40 C(HIST_CREATE_FAIL, "Couldn't create histogram for field"), \
41 C(SYNTH_VAR_NOT_FOUND, "Couldn't find synthetic variable"), \
42 C(SYNTH_EVENT_NOT_FOUND,"Couldn't find synthetic event"), \
43 C(SYNTH_TYPE_MISMATCH, "Param type doesn't match synthetic event field type"), \
44 C(SYNTH_COUNT_MISMATCH, "Param count doesn't match synthetic event field count"), \
45 C(FIELD_VAR_PARSE_FAIL, "Couldn't parse field variable"), \
46 C(VAR_CREATE_FIND_FAIL, "Couldn't create or find variable"), \
47 C(ONX_NOT_VAR, "For onmax(x) or onchange(x), x must be a variable"), \
48 C(ONX_VAR_NOT_FOUND, "Couldn't find onmax or onchange variable"), \
49 C(ONX_VAR_CREATE_FAIL, "Couldn't create onmax or onchange variable"), \
50 C(FIELD_VAR_CREATE_FAIL,"Couldn't create field variable"), \
51 C(TOO_MANY_PARAMS, "Too many action params"), \
52 C(PARAM_NOT_FOUND, "Couldn't find param"), \
53 C(INVALID_PARAM, "Invalid action param"), \
54 C(ACTION_NOT_FOUND, "No action found"), \
55 C(NO_SAVE_PARAMS, "No params found for save()"), \
56 C(TOO_MANY_SAVE_ACTIONS,"Can't have more than one save() action per hist"), \
57 C(ACTION_MISMATCH, "Handler doesn't support action"), \
58 C(NO_CLOSING_PAREN, "No closing paren found"), \
59 C(SUBSYS_NOT_FOUND, "Missing subsystem"), \
60 C(INVALID_SUBSYS_EVENT, "Invalid subsystem or event name"), \
61 C(INVALID_REF_KEY, "Using variable references in keys not supported"), \
62 C(VAR_NOT_FOUND, "Couldn't find variable"), \
63 C(FIELD_NOT_FOUND, "Couldn't find field"), \
64 C(EMPTY_ASSIGNMENT, "Empty assignment"), \
65 C(INVALID_SORT_MODIFIER,"Invalid sort modifier"), \
66 C(EMPTY_SORT_FIELD, "Empty sort field"), \
67 C(TOO_MANY_SORT_FIELDS, "Too many sort fields (Max = 2)"), \
68 C(INVALID_SORT_FIELD, "Sort field must be a key or a val"), \
69 C(INVALID_STR_OPERAND, "String type can not be an operand in expression"), \
70 C(EXPECT_NUMBER, "Expecting numeric literal"), \
71 C(UNARY_MINUS_SUBEXPR, "Unary minus not supported in sub-expressions"), \
72 C(DIVISION_BY_ZERO, "Division by zero"), \
73 C(NEED_NOHC_VAL, "Non-hitcount value is required for 'nohitcount'"),
76 #define C(a, b) HIST_ERR_##a
83 static const char *err_text[] = { ERRORS };
87 typedef u64 (*hist_field_fn_t) (struct hist_field *field,
88 struct tracing_map_elt *elt,
89 struct trace_buffer *buffer,
90 struct ring_buffer_event *rbe,
93 #define HIST_FIELD_OPERANDS_MAX 2
94 #define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
95 #define HIST_ACTIONS_MAX 8
96 #define HIST_CONST_DIGITS_MAX 21
97 #define HIST_DIV_SHIFT 20 /* For optimizing division by constants */
103 FIELD_OP_UNARY_MINUS,
110 HIST_FIELD_FN_VAR_REF,
111 HIST_FIELD_FN_COUNTER,
114 HIST_FIELD_FN_BUCKET,
115 HIST_FIELD_FN_TIMESTAMP,
117 HIST_FIELD_FN_STRING,
118 HIST_FIELD_FN_DYNSTRING,
119 HIST_FIELD_FN_RELDYNSTRING,
120 HIST_FIELD_FN_PSTRING,
129 HIST_FIELD_FN_UMINUS,
134 HIST_FIELD_FN_DIV_POWER2,
135 HIST_FIELD_FN_DIV_NOT_POWER2,
136 HIST_FIELD_FN_DIV_MULT_SHIFT,
137 HIST_FIELD_FN_EXECNAME,
141 * A hist_var (histogram variable) contains variable information for
142 * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF
143 * flag set. A hist_var has a variable name e.g. ts0, and is
144 * associated with a given histogram trigger, as specified by
145 * hist_data. The hist_var idx is the unique index assigned to the
146 * variable by the hist trigger's tracing_map. The idx is what is
147 * used to set a variable's value and, by a variable reference, to
152 struct hist_trigger_data *hist_data;
157 struct ftrace_event_field *field;
159 unsigned long buckets;
161 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
162 struct hist_trigger_data *hist_data;
163 enum hist_field_fn fn_num;
167 unsigned int is_signed;
170 * Variable fields contain variable-specific info in var.
173 enum field_op_id operator;
178 * The name field is used for EXPR and VAR_REF fields. VAR
179 * fields contain the variable name in var.name.
184 * When a histogram trigger is hit, if it has any references
185 * to variables, the values of those variables are collected
186 * into a var_ref_vals array by resolve_var_refs(). The
187 * current value of each variable is read from the tracing_map
188 * using the hist field's hist_var.idx and entered into the
189 * var_ref_idx entry i.e. var_ref_vals[var_ref_idx].
191 unsigned int var_ref_idx;
194 unsigned int var_str_idx;
196 /* Numeric literals are represented as u64 */
198 /* Used to optimize division by constants */
202 static u64 hist_fn_call(struct hist_field *hist_field,
203 struct tracing_map_elt *elt,
204 struct trace_buffer *buffer,
205 struct ring_buffer_event *rbe,
208 static u64 hist_field_const(struct hist_field *field,
209 struct tracing_map_elt *elt,
210 struct trace_buffer *buffer,
211 struct ring_buffer_event *rbe,
214 return field->constant;
217 static u64 hist_field_counter(struct hist_field *field,
218 struct tracing_map_elt *elt,
219 struct trace_buffer *buffer,
220 struct ring_buffer_event *rbe,
226 static u64 hist_field_string(struct hist_field *hist_field,
227 struct tracing_map_elt *elt,
228 struct trace_buffer *buffer,
229 struct ring_buffer_event *rbe,
232 char *addr = (char *)(event + hist_field->field->offset);
234 return (u64)(unsigned long)addr;
237 static u64 hist_field_dynstring(struct hist_field *hist_field,
238 struct tracing_map_elt *elt,
239 struct trace_buffer *buffer,
240 struct ring_buffer_event *rbe,
243 u32 str_item = *(u32 *)(event + hist_field->field->offset);
244 int str_loc = str_item & 0xffff;
245 char *addr = (char *)(event + str_loc);
247 return (u64)(unsigned long)addr;
250 static u64 hist_field_reldynstring(struct hist_field *hist_field,
251 struct tracing_map_elt *elt,
252 struct trace_buffer *buffer,
253 struct ring_buffer_event *rbe,
256 u32 *item = event + hist_field->field->offset;
257 u32 str_item = *item;
258 int str_loc = str_item & 0xffff;
259 char *addr = (char *)&item[1] + str_loc;
261 return (u64)(unsigned long)addr;
264 static u64 hist_field_pstring(struct hist_field *hist_field,
265 struct tracing_map_elt *elt,
266 struct trace_buffer *buffer,
267 struct ring_buffer_event *rbe,
270 char **addr = (char **)(event + hist_field->field->offset);
272 return (u64)(unsigned long)*addr;
275 static u64 hist_field_log2(struct hist_field *hist_field,
276 struct tracing_map_elt *elt,
277 struct trace_buffer *buffer,
278 struct ring_buffer_event *rbe,
281 struct hist_field *operand = hist_field->operands[0];
283 u64 val = hist_fn_call(operand, elt, buffer, rbe, event);
285 return (u64) ilog2(roundup_pow_of_two(val));
288 static u64 hist_field_bucket(struct hist_field *hist_field,
289 struct tracing_map_elt *elt,
290 struct trace_buffer *buffer,
291 struct ring_buffer_event *rbe,
294 struct hist_field *operand = hist_field->operands[0];
295 unsigned long buckets = hist_field->buckets;
297 u64 val = hist_fn_call(operand, elt, buffer, rbe, event);
299 if (WARN_ON_ONCE(!buckets))
303 val = div64_ul(val, buckets);
305 val = (u64)((unsigned long)val / buckets);
306 return val * buckets;
309 static u64 hist_field_plus(struct hist_field *hist_field,
310 struct tracing_map_elt *elt,
311 struct trace_buffer *buffer,
312 struct ring_buffer_event *rbe,
315 struct hist_field *operand1 = hist_field->operands[0];
316 struct hist_field *operand2 = hist_field->operands[1];
318 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
319 u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
324 static u64 hist_field_minus(struct hist_field *hist_field,
325 struct tracing_map_elt *elt,
326 struct trace_buffer *buffer,
327 struct ring_buffer_event *rbe,
330 struct hist_field *operand1 = hist_field->operands[0];
331 struct hist_field *operand2 = hist_field->operands[1];
333 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
334 u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
339 static u64 hist_field_div(struct hist_field *hist_field,
340 struct tracing_map_elt *elt,
341 struct trace_buffer *buffer,
342 struct ring_buffer_event *rbe,
345 struct hist_field *operand1 = hist_field->operands[0];
346 struct hist_field *operand2 = hist_field->operands[1];
348 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
349 u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
351 /* Return -1 for the undefined case */
355 /* Use shift if the divisor is a power of 2 */
356 if (!(val2 & (val2 - 1)))
357 return val1 >> __ffs64(val2);
359 return div64_u64(val1, val2);
362 static u64 div_by_power_of_two(struct hist_field *hist_field,
363 struct tracing_map_elt *elt,
364 struct trace_buffer *buffer,
365 struct ring_buffer_event *rbe,
368 struct hist_field *operand1 = hist_field->operands[0];
369 struct hist_field *operand2 = hist_field->operands[1];
371 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
373 return val1 >> __ffs64(operand2->constant);
376 static u64 div_by_not_power_of_two(struct hist_field *hist_field,
377 struct tracing_map_elt *elt,
378 struct trace_buffer *buffer,
379 struct ring_buffer_event *rbe,
382 struct hist_field *operand1 = hist_field->operands[0];
383 struct hist_field *operand2 = hist_field->operands[1];
385 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
387 return div64_u64(val1, operand2->constant);
390 static u64 div_by_mult_and_shift(struct hist_field *hist_field,
391 struct tracing_map_elt *elt,
392 struct trace_buffer *buffer,
393 struct ring_buffer_event *rbe,
396 struct hist_field *operand1 = hist_field->operands[0];
397 struct hist_field *operand2 = hist_field->operands[1];
399 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
402 * If the divisor is a constant, do a multiplication and shift instead.
404 * Choose Z = some power of 2. If Y <= Z, then:
405 * X / Y = (X * (Z / Y)) / Z
407 * (Z / Y) is a constant (mult) which is calculated at parse time, so:
408 * X / Y = (X * mult) / Z
410 * The division by Z can be replaced by a shift since Z is a power of 2:
411 * X / Y = (X * mult) >> HIST_DIV_SHIFT
413 * As long, as X < Z the results will not be off by more than 1.
415 if (val1 < (1 << HIST_DIV_SHIFT)) {
416 u64 mult = operand2->div_multiplier;
418 return (val1 * mult + ((1 << HIST_DIV_SHIFT) - 1)) >> HIST_DIV_SHIFT;
421 return div64_u64(val1, operand2->constant);
424 static u64 hist_field_mult(struct hist_field *hist_field,
425 struct tracing_map_elt *elt,
426 struct trace_buffer *buffer,
427 struct ring_buffer_event *rbe,
430 struct hist_field *operand1 = hist_field->operands[0];
431 struct hist_field *operand2 = hist_field->operands[1];
433 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
434 u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
439 static u64 hist_field_unary_minus(struct hist_field *hist_field,
440 struct tracing_map_elt *elt,
441 struct trace_buffer *buffer,
442 struct ring_buffer_event *rbe,
445 struct hist_field *operand = hist_field->operands[0];
447 s64 sval = (s64)hist_fn_call(operand, elt, buffer, rbe, event);
448 u64 val = (u64)-sval;
453 #define DEFINE_HIST_FIELD_FN(type) \
454 static u64 hist_field_##type(struct hist_field *hist_field, \
455 struct tracing_map_elt *elt, \
456 struct trace_buffer *buffer, \
457 struct ring_buffer_event *rbe, \
460 type *addr = (type *)(event + hist_field->field->offset); \
462 return (u64)(unsigned long)*addr; \
465 DEFINE_HIST_FIELD_FN(s64);
466 DEFINE_HIST_FIELD_FN(u64);
467 DEFINE_HIST_FIELD_FN(s32);
468 DEFINE_HIST_FIELD_FN(u32);
469 DEFINE_HIST_FIELD_FN(s16);
470 DEFINE_HIST_FIELD_FN(u16);
471 DEFINE_HIST_FIELD_FN(s8);
472 DEFINE_HIST_FIELD_FN(u8);
474 #define for_each_hist_field(i, hist_data) \
475 for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
477 #define for_each_hist_val_field(i, hist_data) \
478 for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
480 #define for_each_hist_key_field(i, hist_data) \
481 for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
483 #define HITCOUNT_IDX 0
484 #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
486 enum hist_field_flags {
487 HIST_FIELD_FL_HITCOUNT = 1 << 0,
488 HIST_FIELD_FL_KEY = 1 << 1,
489 HIST_FIELD_FL_STRING = 1 << 2,
490 HIST_FIELD_FL_HEX = 1 << 3,
491 HIST_FIELD_FL_SYM = 1 << 4,
492 HIST_FIELD_FL_SYM_OFFSET = 1 << 5,
493 HIST_FIELD_FL_EXECNAME = 1 << 6,
494 HIST_FIELD_FL_SYSCALL = 1 << 7,
495 HIST_FIELD_FL_STACKTRACE = 1 << 8,
496 HIST_FIELD_FL_LOG2 = 1 << 9,
497 HIST_FIELD_FL_TIMESTAMP = 1 << 10,
498 HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11,
499 HIST_FIELD_FL_VAR = 1 << 12,
500 HIST_FIELD_FL_EXPR = 1 << 13,
501 HIST_FIELD_FL_VAR_REF = 1 << 14,
502 HIST_FIELD_FL_CPU = 1 << 15,
503 HIST_FIELD_FL_ALIAS = 1 << 16,
504 HIST_FIELD_FL_BUCKET = 1 << 17,
505 HIST_FIELD_FL_CONST = 1 << 18,
506 HIST_FIELD_FL_PERCENT = 1 << 19,
507 HIST_FIELD_FL_GRAPH = 1 << 20,
512 char *name[TRACING_MAP_VARS_MAX];
513 char *expr[TRACING_MAP_VARS_MAX];
516 struct hist_trigger_attrs {
527 unsigned int map_bits;
529 char *assignment_str[TRACING_MAP_VARS_MAX];
530 unsigned int n_assignments;
532 char *action_str[HIST_ACTIONS_MAX];
533 unsigned int n_actions;
535 struct var_defs var_defs;
539 struct hist_field *var;
540 struct hist_field *val;
543 struct field_var_hist {
544 struct hist_trigger_data *hist_data;
548 struct hist_trigger_data {
549 struct hist_field *fields[HIST_FIELDS_MAX];
552 unsigned int n_fields;
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;
563 struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
564 unsigned int n_var_refs;
566 struct action_data *actions[HIST_ACTIONS_MAX];
567 unsigned int n_actions;
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;
575 struct field_var *save_vars[SYNTH_FIELDS_MAX];
576 unsigned int n_save_vars;
577 unsigned int n_save_var_str;
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);
588 typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val);
603 enum handler_id handler;
604 enum action_id action;
608 unsigned int n_params;
609 char *params[SYNTH_FIELDS_MAX];
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
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;
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.
643 * var_ref refers to the variable being
644 * tracked e.g onmax($var).
646 struct hist_field *var_ref;
649 * track_var contains the 'invisible' tracking
650 * variable created to keep the current
653 struct hist_field *track_var;
655 check_track_val_fn_t check_val;
656 action_fn_t save_data;
665 unsigned int key_len;
667 struct tracing_map_elt elt;
669 struct action_data *action_data;
670 struct hist_trigger_data *hist_data;
673 struct hist_elt_data {
676 char **field_var_str;
680 struct snapshot_context {
681 struct tracing_map_elt *elt;
686 * Returns the specific division function to use if the divisor
687 * is constant. This avoids extra branches when the trigger is hit.
689 static enum hist_field_fn hist_field_get_div_fn(struct hist_field *divisor)
691 u64 div = divisor->constant;
693 if (!(div & (div - 1)))
694 return HIST_FIELD_FN_DIV_POWER2;
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;
700 divisor->div_multiplier = div64_u64((u64)(1 << HIST_DIV_SHIFT), div);
701 return HIST_FIELD_FN_DIV_MULT_SHIFT;
704 static void track_data_free(struct track_data *track_data)
706 struct hist_elt_data *elt_data;
711 kfree(track_data->key);
713 elt_data = track_data->elt.private_data;
715 kfree(elt_data->comm);
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)
726 struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
727 struct hist_elt_data *elt_data;
730 return ERR_PTR(-ENOMEM);
732 data->key = kzalloc(key_len, GFP_KERNEL);
734 track_data_free(data);
735 return ERR_PTR(-ENOMEM);
738 data->key_len = key_len;
739 data->action_data = action_data;
740 data->hist_data = hist_data;
742 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
744 track_data_free(data);
745 return ERR_PTR(-ENOMEM);
748 data->elt.private_data = elt_data;
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);
759 #define HIST_PREFIX "hist:"
761 static char *last_cmd;
762 static char last_cmd_loc[MAX_FILTER_STR_VAL];
764 static int errpos(char *str)
766 if (!str || !last_cmd)
769 return err_pos(last_cmd, str);
772 static void last_cmd_set(struct trace_event_file *file, char *str)
774 const char *system = NULL, *name = NULL;
775 struct trace_event_call *call;
781 /* sizeof() contains the nul byte */
782 len = sizeof(HIST_PREFIX) + strlen(str);
784 last_cmd = kzalloc(len, GFP_KERNEL);
788 strcpy(last_cmd, HIST_PREFIX);
789 /* Again, sizeof() contains the nul byte */
790 len -= sizeof(HIST_PREFIX);
791 strncat(last_cmd, str, len);
794 call = file->event_call;
795 system = call->class->system;
797 name = trace_event_name(call);
804 snprintf(last_cmd_loc, MAX_FILTER_STR_VAL, HIST_PREFIX "%s:%s", system, name);
807 static void hist_err(struct trace_array *tr, u8 err_type, u16 err_pos)
812 tracing_log_err(tr, last_cmd_loc, last_cmd, err_text,
816 static void hist_err_clear(void)
820 last_cmd_loc[0] = '\0';
823 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
824 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)
829 struct tracepoint *tp = event->tp;
831 if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
832 struct tracepoint_func *probe_func_ptr;
833 synth_probe_func_t probe_func;
836 if (!(cpu_online(raw_smp_processor_id())))
839 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
840 if (probe_func_ptr) {
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);
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)
856 struct synth_event *event = data->synth_event;
858 trace_synth(event, var_ref_vals, data->var_ref_idx);
861 struct hist_var_data {
862 struct list_head list;
863 struct hist_trigger_data *hist_data;
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,
872 struct hist_trigger_data *hist_data = hist_field->hist_data;
873 struct trace_array *tr = hist_data->event_file->tr;
875 u64 ts = ring_buffer_event_time_stamp(buffer, rbe);
877 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
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,
889 int cpu = smp_processor_id();
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
900 * Check the given VAR_REF field to see whether or not it references
901 * the given variable associated with the given trigger.
903 * Return: The VAR_REF field if it does reference the variable, NULL if not
905 static struct hist_field *
906 check_field_for_var_ref(struct hist_field *hist_field,
907 struct hist_trigger_data *var_data,
908 unsigned int var_idx)
910 WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
912 if (hist_field && hist_field->var.idx == var_idx &&
913 hist_field->var.hist_data == var_data)
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
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
929 * Return: The VAR_REF field referencing the variable if so, NULL if not
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)
935 struct hist_field *hist_field;
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))
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
952 * Check to see whether the given variable is currently referenced by
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.
959 * Return: The VAR_REF field referencing the variable if so, NULL if not
961 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
962 unsigned int var_idx)
964 struct trace_array *tr = hist_data->event_file->tr;
965 struct hist_field *found = NULL;
966 struct hist_var_data *var_data;
968 list_for_each_entry(var_data, &tr->hist_vars, list) {
969 if (var_data->hist_data == hist_data)
971 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
980 * check_var_refs - Check if there is a reference to any of trigger's variables
981 * @hist_data: The hist trigger
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
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.
990 * Return: True if there is a reference to any of trigger's variables
992 static bool check_var_refs(struct hist_trigger_data *hist_data)
994 struct hist_field *field;
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)) {
1011 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
1013 struct trace_array *tr = hist_data->event_file->tr;
1014 struct hist_var_data *var_data, *found = NULL;
1016 list_for_each_entry(var_data, &tr->hist_vars, list) {
1017 if (var_data->hist_data == hist_data) {
1026 static bool field_has_hist_vars(struct hist_field *hist_field,
1037 if (hist_field->flags & HIST_FIELD_FL_VAR ||
1038 hist_field->flags & HIST_FIELD_FL_VAR_REF)
1041 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
1042 struct hist_field *operand;
1044 operand = hist_field->operands[i];
1045 if (field_has_hist_vars(operand, level + 1))
1052 static bool has_hist_vars(struct hist_trigger_data *hist_data)
1054 struct hist_field *hist_field;
1057 for_each_hist_field(i, hist_data) {
1058 hist_field = hist_data->fields[i];
1059 if (field_has_hist_vars(hist_field, 0))
1066 static int save_hist_vars(struct hist_trigger_data *hist_data)
1068 struct trace_array *tr = hist_data->event_file->tr;
1069 struct hist_var_data *var_data;
1071 var_data = find_hist_vars(hist_data);
1075 if (tracing_check_open_get_tr(tr))
1078 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
1080 trace_array_put(tr);
1084 var_data->hist_data = hist_data;
1085 list_add(&var_data->list, &tr->hist_vars);
1090 static void remove_hist_vars(struct hist_trigger_data *hist_data)
1092 struct trace_array *tr = hist_data->event_file->tr;
1093 struct hist_var_data *var_data;
1095 var_data = find_hist_vars(hist_data);
1099 if (WARN_ON(check_var_refs(hist_data)))
1102 list_del(&var_data->list);
1106 trace_array_put(tr);
1109 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
1110 const char *var_name)
1112 struct hist_field *hist_field, *found = NULL;
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) {
1127 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
1128 struct trace_event_file *file,
1129 const char *var_name)
1131 struct hist_trigger_data *test_data;
1132 struct event_trigger_data *test;
1133 struct hist_field *hist_field;
1135 lockdep_assert_held(&event_mutex);
1137 hist_field = find_var_field(hist_data, var_name);
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);
1153 static struct trace_event_file *find_var_file(struct trace_array *tr,
1158 struct hist_trigger_data *var_hist_data;
1159 struct hist_var_data *var_data;
1160 struct trace_event_file *file, *found = NULL;
1163 return find_event_file(tr, system, event_name);
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;
1171 if (find_var_field(var_hist_data, var_name)) {
1173 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name));
1184 static struct hist_field *find_file_var(struct trace_event_file *file,
1185 const char *var_name)
1187 struct hist_trigger_data *test_data;
1188 struct event_trigger_data *test;
1189 struct hist_field *hist_field;
1191 lockdep_assert_held(&event_mutex);
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);
1205 static struct hist_field *
1206 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
1208 struct trace_array *tr = hist_data->event_file->tr;
1209 struct hist_field *hist_field, *found = NULL;
1210 struct trace_event_file *file;
1213 for (i = 0; i < hist_data->n_actions; i++) {
1214 struct action_data *data = hist_data->actions[i];
1216 if (data->handler == HANDLER_ONMATCH) {
1217 char *system = data->match_data.event_system;
1218 char *event_name = data->match_data.event;
1220 file = find_var_file(tr, system, event_name, var_name);
1223 hist_field = find_file_var(file, var_name);
1226 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE,
1228 return ERR_PTR(-EINVAL);
1238 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
1243 struct trace_array *tr = hist_data->event_file->tr;
1244 struct hist_field *hist_field = NULL;
1245 struct trace_event_file *file;
1247 if (!system || !event_name) {
1248 hist_field = find_match_var(hist_data, var_name);
1249 if (IS_ERR(hist_field))
1255 file = find_var_file(tr, system, event_name, var_name);
1259 hist_field = find_file_var(file, var_name);
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,
1270 struct hist_elt_data *elt_data;
1273 if (WARN_ON_ONCE(!elt))
1276 elt_data = elt->private_data;
1277 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
1282 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
1283 u64 *var_ref_vals, bool self)
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;
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;
1297 if (var_data == NULL) {
1302 if ((self && var_data != hist_data) ||
1303 (!self && var_data == hist_data))
1306 var_elt = tracing_map_lookup(var_data->map, key);
1312 if (!tracing_map_var_set(var_elt, var_idx)) {
1317 if (self || !hist_field->read_once)
1318 var_val = tracing_map_read_var(var_elt, var_idx);
1320 var_val = tracing_map_read_var_once(var_elt, var_idx);
1322 var_ref_vals[i] = var_val;
1328 static const char *hist_field_name(struct hist_field *field,
1331 const char *field_name = "";
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];
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;
1356 field_name = field->name;
1357 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1358 field_name = "common_timestamp";
1359 else if (field->flags & HIST_FIELD_FL_STACKTRACE)
1360 field_name = "stacktrace";
1361 else if (field->flags & HIST_FIELD_FL_HITCOUNT)
1362 field_name = "hitcount";
1364 if (field_name == NULL)
1370 static enum hist_field_fn select_value_fn(int field_size, int field_is_signed)
1372 switch (field_size) {
1374 if (field_is_signed)
1375 return HIST_FIELD_FN_S64;
1377 return HIST_FIELD_FN_U64;
1379 if (field_is_signed)
1380 return HIST_FIELD_FN_S32;
1382 return HIST_FIELD_FN_U32;
1384 if (field_is_signed)
1385 return HIST_FIELD_FN_S16;
1387 return HIST_FIELD_FN_U16;
1389 if (field_is_signed)
1390 return HIST_FIELD_FN_S8;
1392 return HIST_FIELD_FN_U8;
1395 return HIST_FIELD_FN_NOP;
1398 static int parse_map_size(char *str)
1400 unsigned long size, map_bits;
1403 ret = kstrtoul(str, 0, &size);
1407 map_bits = ilog2(roundup_pow_of_two(size));
1408 if (map_bits < TRACING_MAP_BITS_MIN ||
1409 map_bits > TRACING_MAP_BITS_MAX)
1417 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
1424 for (i = 0; i < attrs->n_assignments; i++)
1425 kfree(attrs->assignment_str[i]);
1427 for (i = 0; i < attrs->n_actions; i++)
1428 kfree(attrs->action_str[i]);
1431 kfree(attrs->sort_key_str);
1432 kfree(attrs->keys_str);
1433 kfree(attrs->vals_str);
1434 kfree(attrs->clock);
1438 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
1442 if (attrs->n_actions >= HIST_ACTIONS_MAX)
1445 if ((str_has_prefix(str, "onmatch(")) ||
1446 (str_has_prefix(str, "onmax(")) ||
1447 (str_has_prefix(str, "onchange("))) {
1448 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
1449 if (!attrs->action_str[attrs->n_actions]) {
1459 static int parse_assignment(struct trace_array *tr,
1460 char *str, struct hist_trigger_attrs *attrs)
1464 if ((len = str_has_prefix(str, "key=")) ||
1465 (len = str_has_prefix(str, "keys="))) {
1466 attrs->keys_str = kstrdup(str + len, GFP_KERNEL);
1467 if (!attrs->keys_str) {
1471 } else if ((len = str_has_prefix(str, "val=")) ||
1472 (len = str_has_prefix(str, "vals=")) ||
1473 (len = str_has_prefix(str, "values="))) {
1474 attrs->vals_str = kstrdup(str + len, GFP_KERNEL);
1475 if (!attrs->vals_str) {
1479 } else if ((len = str_has_prefix(str, "sort="))) {
1480 attrs->sort_key_str = kstrdup(str + len, GFP_KERNEL);
1481 if (!attrs->sort_key_str) {
1485 } else if (str_has_prefix(str, "name=")) {
1486 attrs->name = kstrdup(str, GFP_KERNEL);
1491 } else if ((len = str_has_prefix(str, "clock="))) {
1494 str = strstrip(str);
1495 attrs->clock = kstrdup(str, GFP_KERNEL);
1496 if (!attrs->clock) {
1500 } else if ((len = str_has_prefix(str, "size="))) {
1501 int map_bits = parse_map_size(str + len);
1507 attrs->map_bits = map_bits;
1511 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
1512 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str));
1517 assignment = kstrdup(str, GFP_KERNEL);
1523 attrs->assignment_str[attrs->n_assignments++] = assignment;
1529 static struct hist_trigger_attrs *
1530 parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str)
1532 struct hist_trigger_attrs *attrs;
1535 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1537 return ERR_PTR(-ENOMEM);
1539 while (trigger_str) {
1540 char *str = strsep(&trigger_str, ":");
1543 rhs = strchr(str, '=');
1545 if (!strlen(++rhs)) {
1547 hist_err(tr, HIST_ERR_EMPTY_ASSIGNMENT, errpos(str));
1550 ret = parse_assignment(tr, str, attrs);
1553 } else if (strcmp(str, "nohitcount") == 0 ||
1554 strcmp(str, "NOHC") == 0)
1555 attrs->no_hitcount = true;
1556 else if (strcmp(str, "pause") == 0)
1557 attrs->pause = true;
1558 else if ((strcmp(str, "cont") == 0) ||
1559 (strcmp(str, "continue") == 0))
1561 else if (strcmp(str, "clear") == 0)
1562 attrs->clear = true;
1564 ret = parse_action(str, attrs);
1570 if (!attrs->keys_str) {
1575 if (!attrs->clock) {
1576 attrs->clock = kstrdup("global", GFP_KERNEL);
1577 if (!attrs->clock) {
1585 destroy_hist_trigger_attrs(attrs);
1587 return ERR_PTR(ret);
1590 static inline void save_comm(char *comm, struct task_struct *task)
1593 strcpy(comm, "<idle>");
1597 if (WARN_ON_ONCE(task->pid < 0)) {
1598 strcpy(comm, "<XXX>");
1602 strncpy(comm, task->comm, TASK_COMM_LEN);
1605 static void hist_elt_data_free(struct hist_elt_data *elt_data)
1609 for (i = 0; i < elt_data->n_field_var_str; i++)
1610 kfree(elt_data->field_var_str[i]);
1612 kfree(elt_data->field_var_str);
1614 kfree(elt_data->comm);
1618 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
1620 struct hist_elt_data *elt_data = elt->private_data;
1622 hist_elt_data_free(elt_data);
1625 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
1627 struct hist_trigger_data *hist_data = elt->map->private_data;
1628 unsigned int size = TASK_COMM_LEN;
1629 struct hist_elt_data *elt_data;
1630 struct hist_field *hist_field;
1631 unsigned int i, n_str;
1633 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
1637 for_each_hist_field(i, hist_data) {
1638 hist_field = hist_data->fields[i];
1640 if (hist_field->flags & HIST_FIELD_FL_EXECNAME) {
1641 elt_data->comm = kzalloc(size, GFP_KERNEL);
1642 if (!elt_data->comm) {
1650 n_str = hist_data->n_field_var_str + hist_data->n_save_var_str +
1651 hist_data->n_var_str;
1652 if (n_str > SYNTH_FIELDS_MAX) {
1653 hist_elt_data_free(elt_data);
1657 BUILD_BUG_ON(STR_VAR_LEN_MAX & (sizeof(u64) - 1));
1659 size = STR_VAR_LEN_MAX;
1661 elt_data->field_var_str = kcalloc(n_str, sizeof(char *), GFP_KERNEL);
1662 if (!elt_data->field_var_str) {
1663 hist_elt_data_free(elt_data);
1666 elt_data->n_field_var_str = n_str;
1668 for (i = 0; i < n_str; i++) {
1669 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
1670 if (!elt_data->field_var_str[i]) {
1671 hist_elt_data_free(elt_data);
1676 elt->private_data = elt_data;
1681 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
1683 struct hist_elt_data *elt_data = elt->private_data;
1686 save_comm(elt_data->comm, current);
1689 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
1690 .elt_alloc = hist_trigger_elt_data_alloc,
1691 .elt_free = hist_trigger_elt_data_free,
1692 .elt_init = hist_trigger_elt_data_init,
1695 static const char *get_hist_field_flags(struct hist_field *hist_field)
1697 const char *flags_str = NULL;
1699 if (hist_field->flags & HIST_FIELD_FL_HEX)
1701 else if (hist_field->flags & HIST_FIELD_FL_SYM)
1703 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
1704 flags_str = "sym-offset";
1705 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
1706 flags_str = "execname";
1707 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
1708 flags_str = "syscall";
1709 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
1711 else if (hist_field->flags & HIST_FIELD_FL_BUCKET)
1712 flags_str = "buckets";
1713 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
1714 flags_str = "usecs";
1715 else if (hist_field->flags & HIST_FIELD_FL_PERCENT)
1716 flags_str = "percent";
1717 else if (hist_field->flags & HIST_FIELD_FL_GRAPH)
1718 flags_str = "graph";
1719 else if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
1720 flags_str = "stacktrace";
1725 static void expr_field_str(struct hist_field *field, char *expr)
1727 if (field->flags & HIST_FIELD_FL_VAR_REF)
1729 else if (field->flags & HIST_FIELD_FL_CONST) {
1730 char str[HIST_CONST_DIGITS_MAX];
1732 snprintf(str, HIST_CONST_DIGITS_MAX, "%llu", field->constant);
1736 strcat(expr, hist_field_name(field, 0));
1738 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
1739 const char *flags_str = get_hist_field_flags(field);
1743 strcat(expr, flags_str);
1748 static char *expr_str(struct hist_field *field, unsigned int level)
1755 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
1759 if (!field->operands[0]) {
1760 expr_field_str(field, expr);
1764 if (field->operator == FIELD_OP_UNARY_MINUS) {
1768 subexpr = expr_str(field->operands[0], ++level);
1773 strcat(expr, subexpr);
1781 expr_field_str(field->operands[0], expr);
1783 switch (field->operator) {
1784 case FIELD_OP_MINUS:
1801 expr_field_str(field->operands[1], expr);
1807 * If field_op != FIELD_OP_NONE, *sep points to the root operator
1808 * of the expression tree to be evaluated.
1810 static int contains_operator(char *str, char **sep)
1812 enum field_op_id field_op = FIELD_OP_NONE;
1813 char *minus_op, *plus_op, *div_op, *mult_op;
1817 * Report the last occurrence of the operators first, so that the
1818 * expression is evaluated left to right. This is important since
1819 * subtraction and division are not associative.
1822 * 64/8/4/2 is 1, i.e 64/8/4/2 = ((64/8)/4)/2
1823 * 14-7-5-2 is 0, i.e 14-7-5-2 = ((14-7)-5)-2
1827 * First, find lower precedence addition and subtraction
1828 * since the expression will be evaluated recursively.
1830 minus_op = strrchr(str, '-');
1833 * Unary minus is not supported in sub-expressions. If
1834 * present, it is always the next root operator.
1836 if (minus_op == str) {
1837 field_op = FIELD_OP_UNARY_MINUS;
1841 field_op = FIELD_OP_MINUS;
1844 plus_op = strrchr(str, '+');
1845 if (plus_op || minus_op) {
1847 * For operators of the same precedence use to rightmost as the
1848 * root, so that the expression is evaluated left to right.
1850 if (plus_op > minus_op)
1851 field_op = FIELD_OP_PLUS;
1856 * Multiplication and division have higher precedence than addition and
1859 div_op = strrchr(str, '/');
1861 field_op = FIELD_OP_DIV;
1863 mult_op = strrchr(str, '*');
1865 * For operators of the same precedence use to rightmost as the
1866 * root, so that the expression is evaluated left to right.
1868 if (mult_op > div_op)
1869 field_op = FIELD_OP_MULT;
1874 case FIELD_OP_UNARY_MINUS:
1875 case FIELD_OP_MINUS:
1897 static void get_hist_field(struct hist_field *hist_field)
1902 static void __destroy_hist_field(struct hist_field *hist_field)
1904 if (--hist_field->ref > 1)
1907 kfree(hist_field->var.name);
1908 kfree(hist_field->name);
1910 /* Can likely be a const */
1911 kfree_const(hist_field->type);
1913 kfree(hist_field->system);
1914 kfree(hist_field->event_name);
1919 static void destroy_hist_field(struct hist_field *hist_field,
1930 if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
1931 return; /* var refs will be destroyed separately */
1933 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
1934 destroy_hist_field(hist_field->operands[i], level + 1);
1936 __destroy_hist_field(hist_field);
1939 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
1940 struct ftrace_event_field *field,
1941 unsigned long flags,
1944 struct hist_field *hist_field;
1946 if (field && is_function_field(field))
1949 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
1953 hist_field->ref = 1;
1955 hist_field->hist_data = hist_data;
1957 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
1958 goto out; /* caller will populate */
1960 if (flags & HIST_FIELD_FL_VAR_REF) {
1961 hist_field->fn_num = HIST_FIELD_FN_VAR_REF;
1965 if (flags & HIST_FIELD_FL_HITCOUNT) {
1966 hist_field->fn_num = HIST_FIELD_FN_COUNTER;
1967 hist_field->size = sizeof(u64);
1968 hist_field->type = "u64";
1972 if (flags & HIST_FIELD_FL_CONST) {
1973 hist_field->fn_num = HIST_FIELD_FN_CONST;
1974 hist_field->size = sizeof(u64);
1975 hist_field->type = kstrdup("u64", GFP_KERNEL);
1976 if (!hist_field->type)
1981 if (flags & HIST_FIELD_FL_STACKTRACE) {
1982 hist_field->fn_num = HIST_FIELD_FN_NOP;
1983 hist_field->size = HIST_STACKTRACE_SIZE;
1984 hist_field->type = kstrdup_const("unsigned long[]", GFP_KERNEL);
1985 if (!hist_field->type)
1990 if (flags & (HIST_FIELD_FL_LOG2 | HIST_FIELD_FL_BUCKET)) {
1991 unsigned long fl = flags & ~(HIST_FIELD_FL_LOG2 | HIST_FIELD_FL_BUCKET);
1992 hist_field->fn_num = flags & HIST_FIELD_FL_LOG2 ? HIST_FIELD_FN_LOG2 :
1993 HIST_FIELD_FN_BUCKET;
1994 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
1995 if (!hist_field->operands[0])
1997 hist_field->size = hist_field->operands[0]->size;
1998 hist_field->type = kstrdup_const(hist_field->operands[0]->type, GFP_KERNEL);
1999 if (!hist_field->type)
2004 if (flags & HIST_FIELD_FL_TIMESTAMP) {
2005 hist_field->fn_num = HIST_FIELD_FN_TIMESTAMP;
2006 hist_field->size = sizeof(u64);
2007 hist_field->type = "u64";
2011 if (flags & HIST_FIELD_FL_CPU) {
2012 hist_field->fn_num = HIST_FIELD_FN_CPU;
2013 hist_field->size = sizeof(int);
2014 hist_field->type = "unsigned int";
2018 if (WARN_ON_ONCE(!field))
2021 /* Pointers to strings are just pointers and dangerous to dereference */
2022 if (is_string_field(field) &&
2023 (field->filter_type != FILTER_PTR_STRING)) {
2024 flags |= HIST_FIELD_FL_STRING;
2026 hist_field->size = MAX_FILTER_STR_VAL;
2027 hist_field->type = kstrdup_const(field->type, GFP_KERNEL);
2028 if (!hist_field->type)
2031 if (field->filter_type == FILTER_STATIC_STRING) {
2032 hist_field->fn_num = HIST_FIELD_FN_STRING;
2033 hist_field->size = field->size;
2034 } else if (field->filter_type == FILTER_DYN_STRING) {
2035 hist_field->fn_num = HIST_FIELD_FN_DYNSTRING;
2036 } else if (field->filter_type == FILTER_RDYN_STRING)
2037 hist_field->fn_num = HIST_FIELD_FN_RELDYNSTRING;
2039 hist_field->fn_num = HIST_FIELD_FN_PSTRING;
2041 hist_field->size = field->size;
2042 hist_field->is_signed = field->is_signed;
2043 hist_field->type = kstrdup_const(field->type, GFP_KERNEL);
2044 if (!hist_field->type)
2047 hist_field->fn_num = select_value_fn(field->size,
2049 if (hist_field->fn_num == HIST_FIELD_FN_NOP) {
2050 destroy_hist_field(hist_field, 0);
2055 hist_field->field = field;
2056 hist_field->flags = flags;
2059 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
2060 if (!hist_field->var.name)
2066 destroy_hist_field(hist_field, 0);
2070 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
2074 for (i = 0; i < HIST_FIELDS_MAX; i++) {
2075 if (hist_data->fields[i]) {
2076 destroy_hist_field(hist_data->fields[i], 0);
2077 hist_data->fields[i] = NULL;
2081 for (i = 0; i < hist_data->n_var_refs; i++) {
2082 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
2083 __destroy_hist_field(hist_data->var_refs[i]);
2084 hist_data->var_refs[i] = NULL;
2088 static int init_var_ref(struct hist_field *ref_field,
2089 struct hist_field *var_field,
2090 char *system, char *event_name)
2094 ref_field->var.idx = var_field->var.idx;
2095 ref_field->var.hist_data = var_field->hist_data;
2096 ref_field->size = var_field->size;
2097 ref_field->is_signed = var_field->is_signed;
2098 ref_field->flags |= var_field->flags &
2099 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2102 ref_field->system = kstrdup(system, GFP_KERNEL);
2103 if (!ref_field->system)
2108 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
2109 if (!ref_field->event_name) {
2115 if (var_field->var.name) {
2116 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
2117 if (!ref_field->name) {
2121 } else if (var_field->name) {
2122 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
2123 if (!ref_field->name) {
2129 ref_field->type = kstrdup_const(var_field->type, GFP_KERNEL);
2130 if (!ref_field->type) {
2137 kfree(ref_field->system);
2138 ref_field->system = NULL;
2139 kfree(ref_field->event_name);
2140 ref_field->event_name = NULL;
2141 kfree(ref_field->name);
2142 ref_field->name = NULL;
2147 static int find_var_ref_idx(struct hist_trigger_data *hist_data,
2148 struct hist_field *var_field)
2150 struct hist_field *ref_field;
2153 for (i = 0; i < hist_data->n_var_refs; i++) {
2154 ref_field = hist_data->var_refs[i];
2155 if (ref_field->var.idx == var_field->var.idx &&
2156 ref_field->var.hist_data == var_field->hist_data)
2164 * create_var_ref - Create a variable reference and attach it to trigger
2165 * @hist_data: The trigger that will be referencing the variable
2166 * @var_field: The VAR field to create a reference to
2167 * @system: The optional system string
2168 * @event_name: The optional event_name string
2170 * Given a variable hist_field, create a VAR_REF hist_field that
2171 * represents a reference to it.
2173 * This function also adds the reference to the trigger that
2174 * now references the variable.
2176 * Return: The VAR_REF field if successful, NULL if not
2178 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
2179 struct hist_field *var_field,
2180 char *system, char *event_name)
2182 unsigned long flags = HIST_FIELD_FL_VAR_REF;
2183 struct hist_field *ref_field;
2186 /* Check if the variable already exists */
2187 for (i = 0; i < hist_data->n_var_refs; i++) {
2188 ref_field = hist_data->var_refs[i];
2189 if (ref_field->var.idx == var_field->var.idx &&
2190 ref_field->var.hist_data == var_field->hist_data) {
2191 get_hist_field(ref_field);
2195 /* Sanity check to avoid out-of-bound write on 'hist_data->var_refs' */
2196 if (hist_data->n_var_refs >= TRACING_MAP_VARS_MAX)
2198 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
2200 if (init_var_ref(ref_field, var_field, system, event_name)) {
2201 destroy_hist_field(ref_field, 0);
2205 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
2206 ref_field->var_ref_idx = hist_data->n_var_refs++;
2212 static bool is_var_ref(char *var_name)
2214 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
2220 static char *field_name_from_var(struct hist_trigger_data *hist_data,
2226 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
2227 name = hist_data->attrs->var_defs.name[i];
2229 if (strcmp(var_name, name) == 0) {
2230 field = hist_data->attrs->var_defs.expr[i];
2231 if (contains_operator(field, NULL) || is_var_ref(field))
2240 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
2241 char *system, char *event_name,
2244 struct trace_event_call *call;
2246 if (system && event_name) {
2247 call = hist_data->event_file->event_call;
2249 if (strcmp(system, call->class->system) != 0)
2252 if (strcmp(event_name, trace_event_name(call)) != 0)
2256 if (!!system != !!event_name)
2259 if (!is_var_ref(var_name))
2264 return field_name_from_var(hist_data, var_name);
2267 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
2268 char *system, char *event_name,
2271 struct hist_field *var_field = NULL, *ref_field = NULL;
2272 struct trace_array *tr = hist_data->event_file->tr;
2274 if (!is_var_ref(var_name))
2279 var_field = find_event_var(hist_data, system, event_name, var_name);
2281 ref_field = create_var_ref(hist_data, var_field,
2282 system, event_name);
2285 hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name));
2290 static struct ftrace_event_field *
2291 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
2292 char *field_str, unsigned long *flags, unsigned long *buckets)
2294 struct ftrace_event_field *field = NULL;
2295 char *field_name, *modifier, *str;
2296 struct trace_array *tr = file->tr;
2298 modifier = str = kstrdup(field_str, GFP_KERNEL);
2300 return ERR_PTR(-ENOMEM);
2302 field_name = strsep(&modifier, ".");
2304 if (strcmp(modifier, "hex") == 0)
2305 *flags |= HIST_FIELD_FL_HEX;
2306 else if (strcmp(modifier, "sym") == 0)
2307 *flags |= HIST_FIELD_FL_SYM;
2309 * 'sym-offset' occurrences in the trigger string are modified
2310 * to 'symXoffset' to simplify arithmetic expression parsing.
2312 else if (strcmp(modifier, "symXoffset") == 0)
2313 *flags |= HIST_FIELD_FL_SYM_OFFSET;
2314 else if ((strcmp(modifier, "execname") == 0) &&
2315 (strcmp(field_name, "common_pid") == 0))
2316 *flags |= HIST_FIELD_FL_EXECNAME;
2317 else if (strcmp(modifier, "syscall") == 0)
2318 *flags |= HIST_FIELD_FL_SYSCALL;
2319 else if (strcmp(modifier, "stacktrace") == 0)
2320 *flags |= HIST_FIELD_FL_STACKTRACE;
2321 else if (strcmp(modifier, "log2") == 0)
2322 *flags |= HIST_FIELD_FL_LOG2;
2323 else if (strcmp(modifier, "usecs") == 0)
2324 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
2325 else if (strncmp(modifier, "bucket", 6) == 0) {
2330 if (*modifier == 's')
2332 if (*modifier != '=')
2335 ret = kstrtoul(modifier, 0, buckets);
2336 if (ret || !(*buckets))
2338 *flags |= HIST_FIELD_FL_BUCKET;
2339 } else if (strncmp(modifier, "percent", 7) == 0) {
2340 if (*flags & (HIST_FIELD_FL_VAR | HIST_FIELD_FL_KEY))
2342 *flags |= HIST_FIELD_FL_PERCENT;
2343 } else if (strncmp(modifier, "graph", 5) == 0) {
2344 if (*flags & (HIST_FIELD_FL_VAR | HIST_FIELD_FL_KEY))
2346 *flags |= HIST_FIELD_FL_GRAPH;
2349 hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier));
2350 field = ERR_PTR(-EINVAL);
2355 if (strcmp(field_name, "common_timestamp") == 0) {
2356 *flags |= HIST_FIELD_FL_TIMESTAMP;
2357 hist_data->enable_timestamps = true;
2358 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2359 hist_data->attrs->ts_in_usecs = true;
2360 } else if (strcmp(field_name, "stacktrace") == 0) {
2361 *flags |= HIST_FIELD_FL_STACKTRACE;
2362 } else if (strcmp(field_name, "common_cpu") == 0)
2363 *flags |= HIST_FIELD_FL_CPU;
2364 else if (strcmp(field_name, "hitcount") == 0)
2365 *flags |= HIST_FIELD_FL_HITCOUNT;
2367 field = trace_find_event_field(file->event_call, field_name);
2368 if (!field || !field->size) {
2370 * For backward compatibility, if field_name
2371 * was "cpu", then we treat this the same as
2372 * common_cpu. This also works for "CPU".
2374 if (field && field->filter_type == FILTER_CPU) {
2375 *flags |= HIST_FIELD_FL_CPU;
2377 hist_err(tr, HIST_ERR_FIELD_NOT_FOUND,
2378 errpos(field_name));
2379 field = ERR_PTR(-EINVAL);
2390 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2391 struct hist_field *var_ref,
2394 struct hist_field *alias = NULL;
2395 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2397 alias = create_hist_field(hist_data, NULL, flags, var_name);
2401 alias->fn_num = var_ref->fn_num;
2402 alias->operands[0] = var_ref;
2404 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2405 destroy_hist_field(alias, 0);
2409 alias->var_ref_idx = var_ref->var_ref_idx;
2414 static struct hist_field *parse_const(struct hist_trigger_data *hist_data,
2415 char *str, char *var_name,
2416 unsigned long *flags)
2418 struct trace_array *tr = hist_data->event_file->tr;
2419 struct hist_field *field = NULL;
2422 if (kstrtoull(str, 0, &constant)) {
2423 hist_err(tr, HIST_ERR_EXPECT_NUMBER, errpos(str));
2427 *flags |= HIST_FIELD_FL_CONST;
2428 field = create_hist_field(hist_data, NULL, *flags, var_name);
2432 field->constant = constant;
2437 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2438 struct trace_event_file *file, char *str,
2439 unsigned long *flags, char *var_name)
2441 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
2442 struct ftrace_event_field *field = NULL;
2443 struct hist_field *hist_field = NULL;
2444 unsigned long buckets = 0;
2447 if (isdigit(str[0])) {
2448 hist_field = parse_const(hist_data, str, var_name, flags);
2456 s = strchr(str, '.');
2458 s = strchr(++s, '.');
2460 ref_system = strsep(&str, ".");
2465 ref_event = strsep(&str, ".");
2474 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2476 hist_field = parse_var_ref(hist_data, ref_system,
2477 ref_event, ref_var);
2480 hist_field = create_alias(hist_data, hist_field, var_name);
2491 field = parse_field(hist_data, file, str, flags, &buckets);
2492 if (IS_ERR(field)) {
2493 ret = PTR_ERR(field);
2497 hist_field = create_hist_field(hist_data, field, *flags, var_name);
2502 hist_field->buckets = buckets;
2506 return ERR_PTR(ret);
2509 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2510 struct trace_event_file *file,
2511 char *str, unsigned long flags,
2512 char *var_name, unsigned int *n_subexprs);
2514 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2515 struct trace_event_file *file,
2516 char *str, unsigned long flags,
2517 char *var_name, unsigned int *n_subexprs)
2519 struct hist_field *operand1, *expr = NULL;
2520 unsigned long operand_flags;
2524 /* Unary minus operator, increment n_subexprs */
2527 /* we support only -(xxx) i.e. explicit parens required */
2529 if (*n_subexprs > 3) {
2530 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2535 str++; /* skip leading '-' */
2537 s = strchr(str, '(');
2545 s = strrchr(str, ')');
2547 /* unary minus not supported in sub-expressions */
2548 if (*(s+1) != '\0') {
2549 hist_err(file->tr, HIST_ERR_UNARY_MINUS_SUBEXPR,
2557 ret = -EINVAL; /* no closing ')' */
2561 flags |= HIST_FIELD_FL_EXPR;
2562 expr = create_hist_field(hist_data, NULL, flags, var_name);
2569 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, n_subexprs);
2570 if (IS_ERR(operand1)) {
2571 ret = PTR_ERR(operand1);
2574 if (operand1->flags & HIST_FIELD_FL_STRING) {
2575 /* String type can not be the operand of unary operator. */
2576 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
2577 destroy_hist_field(operand1, 0);
2582 expr->flags |= operand1->flags &
2583 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2584 expr->fn_num = HIST_FIELD_FN_UMINUS;
2585 expr->operands[0] = operand1;
2586 expr->size = operand1->size;
2587 expr->is_signed = operand1->is_signed;
2588 expr->operator = FIELD_OP_UNARY_MINUS;
2589 expr->name = expr_str(expr, 0);
2590 expr->type = kstrdup_const(operand1->type, GFP_KERNEL);
2598 destroy_hist_field(expr, 0);
2599 return ERR_PTR(ret);
2603 * If the operands are var refs, return pointers the
2604 * variable(s) referenced in var1 and var2, else NULL.
2606 static int check_expr_operands(struct trace_array *tr,
2607 struct hist_field *operand1,
2608 struct hist_field *operand2,
2609 struct hist_field **var1,
2610 struct hist_field **var2)
2612 unsigned long operand1_flags = operand1->flags;
2613 unsigned long operand2_flags = operand2->flags;
2615 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2616 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2617 struct hist_field *var;
2619 var = find_var_field(operand1->var.hist_data, operand1->name);
2622 operand1_flags = var->flags;
2626 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2627 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2628 struct hist_field *var;
2630 var = find_var_field(operand2->var.hist_data, operand2->name);
2633 operand2_flags = var->flags;
2637 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
2638 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
2639 hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0);
2646 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2647 struct trace_event_file *file,
2648 char *str, unsigned long flags,
2649 char *var_name, unsigned int *n_subexprs)
2651 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2652 struct hist_field *var1 = NULL, *var2 = NULL;
2653 unsigned long operand_flags, operand2_flags;
2654 int field_op, ret = -EINVAL;
2655 char *sep, *operand1_str;
2656 enum hist_field_fn op_fn;
2657 bool combine_consts;
2659 if (*n_subexprs > 3) {
2660 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2661 return ERR_PTR(-EINVAL);
2664 field_op = contains_operator(str, &sep);
2666 if (field_op == FIELD_OP_NONE)
2667 return parse_atom(hist_data, file, str, &flags, var_name);
2669 if (field_op == FIELD_OP_UNARY_MINUS)
2670 return parse_unary(hist_data, file, str, flags, var_name, n_subexprs);
2672 /* Binary operator found, increment n_subexprs */
2675 /* Split the expression string at the root operator */
2677 return ERR_PTR(-EINVAL);
2683 /* Binary operator requires both operands */
2684 if (*operand1_str == '\0' || *str == '\0')
2685 return ERR_PTR(-EINVAL);
2689 /* LHS of string is an expression e.g. a+b in a+b+c */
2690 operand1 = parse_expr(hist_data, file, operand1_str, operand_flags, NULL, n_subexprs);
2691 if (IS_ERR(operand1))
2692 return ERR_CAST(operand1);
2694 if (operand1->flags & HIST_FIELD_FL_STRING) {
2695 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(operand1_str));
2700 /* RHS of string is another expression e.g. c in a+b+c */
2702 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, n_subexprs);
2703 if (IS_ERR(operand2)) {
2704 ret = PTR_ERR(operand2);
2707 if (operand2->flags & HIST_FIELD_FL_STRING) {
2708 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
2714 case FIELD_OP_MINUS:
2715 op_fn = HIST_FIELD_FN_MINUS;
2718 op_fn = HIST_FIELD_FN_PLUS;
2721 op_fn = HIST_FIELD_FN_DIV;
2724 op_fn = HIST_FIELD_FN_MULT;
2731 ret = check_expr_operands(file->tr, operand1, operand2, &var1, &var2);
2735 operand_flags = var1 ? var1->flags : operand1->flags;
2736 operand2_flags = var2 ? var2->flags : operand2->flags;
2739 * If both operands are constant, the expression can be
2740 * collapsed to a single constant.
2742 combine_consts = operand_flags & operand2_flags & HIST_FIELD_FL_CONST;
2744 flags |= combine_consts ? HIST_FIELD_FL_CONST : HIST_FIELD_FL_EXPR;
2746 flags |= operand1->flags &
2747 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2749 expr = create_hist_field(hist_data, NULL, flags, var_name);
2755 operand1->read_once = true;
2756 operand2->read_once = true;
2758 /* The operands are now owned and free'd by 'expr' */
2759 expr->operands[0] = operand1;
2760 expr->operands[1] = operand2;
2762 if (field_op == FIELD_OP_DIV &&
2763 operand2_flags & HIST_FIELD_FL_CONST) {
2764 u64 divisor = var2 ? var2->constant : operand2->constant;
2767 hist_err(file->tr, HIST_ERR_DIVISION_BY_ZERO, errpos(str));
2773 * Copy the divisor here so we don't have to look it up
2774 * later if this is a var ref
2776 operand2->constant = divisor;
2777 op_fn = hist_field_get_div_fn(operand2);
2780 expr->fn_num = op_fn;
2782 if (combine_consts) {
2784 expr->operands[0] = var1;
2786 expr->operands[1] = var2;
2788 expr->constant = hist_fn_call(expr, NULL, NULL, NULL, NULL);
2789 expr->fn_num = HIST_FIELD_FN_CONST;
2791 expr->operands[0] = NULL;
2792 expr->operands[1] = NULL;
2795 * var refs won't be destroyed immediately
2796 * See: destroy_hist_field()
2798 destroy_hist_field(operand2, 0);
2799 destroy_hist_field(operand1, 0);
2801 expr->name = expr_str(expr, 0);
2803 /* The operand sizes should be the same, so just pick one */
2804 expr->size = operand1->size;
2805 expr->is_signed = operand1->is_signed;
2807 expr->operator = field_op;
2808 expr->type = kstrdup_const(operand1->type, GFP_KERNEL);
2814 expr->name = expr_str(expr, 0);
2820 destroy_hist_field(operand2, 0);
2822 destroy_hist_field(operand1, 0);
2823 return ERR_PTR(ret);
2826 destroy_hist_field(expr, 0);
2827 return ERR_PTR(ret);
2830 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
2831 struct trace_event_file *file)
2833 struct event_trigger_data *test;
2835 lockdep_assert_held(&event_mutex);
2837 list_for_each_entry(test, &file->triggers, list) {
2838 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2839 if (test->private_data == hist_data)
2840 return test->filter_str;
2847 static struct event_command trigger_hist_cmd;
2848 static int event_hist_trigger_parse(struct event_command *cmd_ops,
2849 struct trace_event_file *file,
2850 char *glob, char *cmd,
2851 char *param_and_filter);
2853 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
2854 struct hist_trigger_data *hist_data,
2855 unsigned int n_keys)
2857 struct hist_field *target_hist_field, *hist_field;
2858 unsigned int n, i, j;
2860 if (hist_data->n_fields - hist_data->n_vals != n_keys)
2863 i = hist_data->n_vals;
2864 j = target_hist_data->n_vals;
2866 for (n = 0; n < n_keys; n++) {
2867 hist_field = hist_data->fields[i + n];
2868 target_hist_field = target_hist_data->fields[j + n];
2870 if (strcmp(hist_field->type, target_hist_field->type) != 0)
2872 if (hist_field->size != target_hist_field->size)
2874 if (hist_field->is_signed != target_hist_field->is_signed)
2881 static struct hist_trigger_data *
2882 find_compatible_hist(struct hist_trigger_data *target_hist_data,
2883 struct trace_event_file *file)
2885 struct hist_trigger_data *hist_data;
2886 struct event_trigger_data *test;
2887 unsigned int n_keys;
2889 lockdep_assert_held(&event_mutex);
2891 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
2893 list_for_each_entry(test, &file->triggers, list) {
2894 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2895 hist_data = test->private_data;
2897 if (compatible_keys(target_hist_data, hist_data, n_keys))
2905 static struct trace_event_file *event_file(struct trace_array *tr,
2906 char *system, char *event_name)
2908 struct trace_event_file *file;
2910 file = __find_event_file(tr, system, event_name);
2912 return ERR_PTR(-EINVAL);
2917 static struct hist_field *
2918 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
2919 char *system, char *event_name, char *field_name)
2921 struct hist_field *event_var;
2922 char *synthetic_name;
2924 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2925 if (!synthetic_name)
2926 return ERR_PTR(-ENOMEM);
2928 strcpy(synthetic_name, "synthetic_");
2929 strcat(synthetic_name, field_name);
2931 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
2933 kfree(synthetic_name);
2939 * create_field_var_hist - Automatically create a histogram and var for a field
2940 * @target_hist_data: The target hist trigger
2941 * @subsys_name: Optional subsystem name
2942 * @event_name: Optional event name
2943 * @field_name: The name of the field (and the resulting variable)
2945 * Hist trigger actions fetch data from variables, not directly from
2946 * events. However, for convenience, users are allowed to directly
2947 * specify an event field in an action, which will be automatically
2948 * converted into a variable on their behalf.
2950 * If a user specifies a field on an event that isn't the event the
2951 * histogram currently being defined (the target event histogram), the
2952 * only way that can be accomplished is if a new hist trigger is
2953 * created and the field variable defined on that.
2955 * This function creates a new histogram compatible with the target
2956 * event (meaning a histogram with the same key as the target
2957 * histogram), and creates a variable for the specified field, but
2958 * with 'synthetic_' prepended to the variable name in order to avoid
2959 * collision with normal field variables.
2961 * Return: The variable created for the field.
2963 static struct hist_field *
2964 create_field_var_hist(struct hist_trigger_data *target_hist_data,
2965 char *subsys_name, char *event_name, char *field_name)
2967 struct trace_array *tr = target_hist_data->event_file->tr;
2968 struct hist_trigger_data *hist_data;
2969 unsigned int i, n, first = true;
2970 struct field_var_hist *var_hist;
2971 struct trace_event_file *file;
2972 struct hist_field *key_field;
2973 struct hist_field *event_var;
2978 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
2979 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
2980 return ERR_PTR(-EINVAL);
2983 file = event_file(tr, subsys_name, event_name);
2986 hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name));
2987 ret = PTR_ERR(file);
2988 return ERR_PTR(ret);
2992 * Look for a histogram compatible with target. We'll use the
2993 * found histogram specification to create a new matching
2994 * histogram with our variable on it. target_hist_data is not
2995 * yet a registered histogram so we can't use that.
2997 hist_data = find_compatible_hist(target_hist_data, file);
2999 hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name));
3000 return ERR_PTR(-EINVAL);
3003 /* See if a synthetic field variable has already been created */
3004 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3005 event_name, field_name);
3006 if (!IS_ERR_OR_NULL(event_var))
3009 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
3011 return ERR_PTR(-ENOMEM);
3013 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3016 return ERR_PTR(-ENOMEM);
3019 /* Use the same keys as the compatible histogram */
3020 strcat(cmd, "keys=");
3022 for_each_hist_key_field(i, hist_data) {
3023 key_field = hist_data->fields[i];
3026 strcat(cmd, key_field->field->name);
3030 /* Create the synthetic field variable specification */
3031 strcat(cmd, ":synthetic_");
3032 strcat(cmd, field_name);
3034 strcat(cmd, field_name);
3036 /* Use the same filter as the compatible histogram */
3037 saved_filter = find_trigger_filter(hist_data, file);
3039 strcat(cmd, " if ");
3040 strcat(cmd, saved_filter);
3043 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
3044 if (!var_hist->cmd) {
3047 return ERR_PTR(-ENOMEM);
3050 /* Save the compatible histogram information */
3051 var_hist->hist_data = hist_data;
3053 /* Create the new histogram with our variable */
3054 ret = event_hist_trigger_parse(&trigger_hist_cmd, file,
3058 kfree(var_hist->cmd);
3060 hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name));
3061 return ERR_PTR(ret);
3066 /* If we can't find the variable, something went wrong */
3067 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3068 event_name, field_name);
3069 if (IS_ERR_OR_NULL(event_var)) {
3070 kfree(var_hist->cmd);
3072 hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
3073 return ERR_PTR(-EINVAL);
3076 n = target_hist_data->n_field_var_hists;
3077 target_hist_data->field_var_hists[n] = var_hist;
3078 target_hist_data->n_field_var_hists++;
3083 static struct hist_field *
3084 find_target_event_var(struct hist_trigger_data *hist_data,
3085 char *subsys_name, char *event_name, char *var_name)
3087 struct trace_event_file *file = hist_data->event_file;
3088 struct hist_field *hist_field = NULL;
3091 struct trace_event_call *call;
3096 call = file->event_call;
3098 if (strcmp(subsys_name, call->class->system) != 0)
3101 if (strcmp(event_name, trace_event_name(call)) != 0)
3105 hist_field = find_var_field(hist_data, var_name);
3110 static inline void __update_field_vars(struct tracing_map_elt *elt,
3111 struct trace_buffer *buffer,
3112 struct ring_buffer_event *rbe,
3114 struct field_var **field_vars,
3115 unsigned int n_field_vars,
3116 unsigned int field_var_str_start)
3118 struct hist_elt_data *elt_data = elt->private_data;
3119 unsigned int i, j, var_idx;
3122 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
3123 struct field_var *field_var = field_vars[i];
3124 struct hist_field *var = field_var->var;
3125 struct hist_field *val = field_var->val;
3127 var_val = hist_fn_call(val, elt, buffer, rbe, rec);
3128 var_idx = var->var.idx;
3130 if (val->flags & (HIST_FIELD_FL_STRING |
3131 HIST_FIELD_FL_STACKTRACE)) {
3132 char *str = elt_data->field_var_str[j++];
3133 char *val_str = (char *)(uintptr_t)var_val;
3136 if (val->flags & HIST_FIELD_FL_STRING) {
3137 size = min(val->size, STR_VAR_LEN_MAX);
3138 strscpy(str, val_str, size);
3140 char *stack_start = str + sizeof(unsigned long);
3143 e = stack_trace_save((void *)stack_start,
3144 HIST_STACKTRACE_DEPTH,
3145 HIST_STACKTRACE_SKIP);
3146 if (e < HIST_STACKTRACE_DEPTH - 1)
3147 ((unsigned long *)stack_start)[e] = 0;
3148 *((unsigned long *)str) = e;
3150 var_val = (u64)(uintptr_t)str;
3152 tracing_map_set_var(elt, var_idx, var_val);
3156 static void update_field_vars(struct hist_trigger_data *hist_data,
3157 struct tracing_map_elt *elt,
3158 struct trace_buffer *buffer,
3159 struct ring_buffer_event *rbe,
3162 __update_field_vars(elt, buffer, rbe, rec, hist_data->field_vars,
3163 hist_data->n_field_vars, 0);
3166 static void save_track_data_vars(struct hist_trigger_data *hist_data,
3167 struct tracing_map_elt *elt,
3168 struct trace_buffer *buffer, void *rec,
3169 struct ring_buffer_event *rbe, void *key,
3170 struct action_data *data, u64 *var_ref_vals)
3172 __update_field_vars(elt, buffer, rbe, rec, hist_data->save_vars,
3173 hist_data->n_save_vars, hist_data->n_field_var_str);
3176 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
3177 struct trace_event_file *file,
3178 char *name, int size, const char *type)
3180 struct hist_field *var;
3183 if (find_var(hist_data, file, name) && !hist_data->remove) {
3184 var = ERR_PTR(-EINVAL);
3188 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3190 var = ERR_PTR(-ENOMEM);
3194 idx = tracing_map_add_var(hist_data->map);
3197 var = ERR_PTR(-EINVAL);
3202 var->flags = HIST_FIELD_FL_VAR;
3204 var->var.hist_data = var->hist_data = hist_data;
3206 var->var.name = kstrdup(name, GFP_KERNEL);
3207 var->type = kstrdup_const(type, GFP_KERNEL);
3208 if (!var->var.name || !var->type) {
3209 kfree_const(var->type);
3210 kfree(var->var.name);
3212 var = ERR_PTR(-ENOMEM);
3218 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
3219 struct trace_event_file *file,
3222 struct hist_field *val = NULL, *var = NULL;
3223 unsigned long flags = HIST_FIELD_FL_VAR;
3224 struct trace_array *tr = file->tr;
3225 struct field_var *field_var;
3228 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
3229 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
3234 val = parse_atom(hist_data, file, field_name, &flags, NULL);
3236 hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name));
3241 var = create_var(hist_data, file, field_name, val->size, val->type);
3243 hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name));
3249 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
3257 field_var->var = var;
3258 field_var->val = val;
3262 field_var = ERR_PTR(ret);
3267 * create_target_field_var - Automatically create a variable for a field
3268 * @target_hist_data: The target hist trigger
3269 * @subsys_name: Optional subsystem name
3270 * @event_name: Optional event name
3271 * @var_name: The name of the field (and the resulting variable)
3273 * Hist trigger actions fetch data from variables, not directly from
3274 * events. However, for convenience, users are allowed to directly
3275 * specify an event field in an action, which will be automatically
3276 * converted into a variable on their behalf.
3278 * This function creates a field variable with the name var_name on
3279 * the hist trigger currently being defined on the target event. If
3280 * subsys_name and event_name are specified, this function simply
3281 * verifies that they do in fact match the target event subsystem and
3284 * Return: The variable created for the field.
3286 static struct field_var *
3287 create_target_field_var(struct hist_trigger_data *target_hist_data,
3288 char *subsys_name, char *event_name, char *var_name)
3290 struct trace_event_file *file = target_hist_data->event_file;
3293 struct trace_event_call *call;
3298 call = file->event_call;
3300 if (strcmp(subsys_name, call->class->system) != 0)
3303 if (strcmp(event_name, trace_event_name(call)) != 0)
3307 return create_field_var(target_hist_data, file, var_name);
3310 static bool check_track_val_max(u64 track_val, u64 var_val)
3312 if (var_val <= track_val)
3318 static bool check_track_val_changed(u64 track_val, u64 var_val)
3320 if (var_val == track_val)
3326 static u64 get_track_val(struct hist_trigger_data *hist_data,
3327 struct tracing_map_elt *elt,
3328 struct action_data *data)
3330 unsigned int track_var_idx = data->track_data.track_var->var.idx;
3333 track_val = tracing_map_read_var(elt, track_var_idx);
3338 static void save_track_val(struct hist_trigger_data *hist_data,
3339 struct tracing_map_elt *elt,
3340 struct action_data *data, u64 var_val)
3342 unsigned int track_var_idx = data->track_data.track_var->var.idx;
3344 tracing_map_set_var(elt, track_var_idx, var_val);
3347 static void save_track_data(struct hist_trigger_data *hist_data,
3348 struct tracing_map_elt *elt,
3349 struct trace_buffer *buffer, void *rec,
3350 struct ring_buffer_event *rbe, void *key,
3351 struct action_data *data, u64 *var_ref_vals)
3353 if (data->track_data.save_data)
3354 data->track_data.save_data(hist_data, elt, buffer, rec, rbe,
3355 key, data, var_ref_vals);
3358 static bool check_track_val(struct tracing_map_elt *elt,
3359 struct action_data *data,
3362 struct hist_trigger_data *hist_data;
3365 hist_data = data->track_data.track_var->hist_data;
3366 track_val = get_track_val(hist_data, elt, data);
3368 return data->track_data.check_val(track_val, var_val);
3371 #ifdef CONFIG_TRACER_SNAPSHOT
3372 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3374 /* called with tr->max_lock held */
3375 struct track_data *track_data = tr->cond_snapshot->cond_data;
3376 struct hist_elt_data *elt_data, *track_elt_data;
3377 struct snapshot_context *context = cond_data;
3378 struct action_data *action;
3384 action = track_data->action_data;
3386 track_val = get_track_val(track_data->hist_data, context->elt,
3387 track_data->action_data);
3389 if (!action->track_data.check_val(track_data->track_val, track_val))
3392 track_data->track_val = track_val;
3393 memcpy(track_data->key, context->key, track_data->key_len);
3395 elt_data = context->elt->private_data;
3396 track_elt_data = track_data->elt.private_data;
3398 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
3400 track_data->updated = true;
3405 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3406 struct tracing_map_elt *elt,
3407 struct trace_buffer *buffer, void *rec,
3408 struct ring_buffer_event *rbe, void *key,
3409 struct action_data *data,
3412 struct trace_event_file *file = hist_data->event_file;
3413 struct snapshot_context context;
3418 tracing_snapshot_cond(file->tr, &context);
3421 static void hist_trigger_print_key(struct seq_file *m,
3422 struct hist_trigger_data *hist_data,
3424 struct tracing_map_elt *elt);
3426 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
3430 if (!hist_data->n_actions)
3433 for (i = 0; i < hist_data->n_actions; i++) {
3434 struct action_data *data = hist_data->actions[i];
3436 if (data->action == ACTION_SNAPSHOT)
3443 static void track_data_snapshot_print(struct seq_file *m,
3444 struct hist_trigger_data *hist_data)
3446 struct trace_event_file *file = hist_data->event_file;
3447 struct track_data *track_data;
3448 struct action_data *action;
3450 track_data = tracing_cond_snapshot_data(file->tr);
3454 if (!track_data->updated)
3457 action = snapshot_action(hist_data);
3461 seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n");
3462 seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
3463 action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
3464 action->track_data.var_str, track_data->track_val);
3466 seq_puts(m, "\ttriggered by event with key: ");
3467 hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
3471 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3475 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3476 struct tracing_map_elt *elt,
3477 struct trace_buffer *buffer, void *rec,
3478 struct ring_buffer_event *rbe, void *key,
3479 struct action_data *data,
3480 u64 *var_ref_vals) {}
3481 static void track_data_snapshot_print(struct seq_file *m,
3482 struct hist_trigger_data *hist_data) {}
3483 #endif /* CONFIG_TRACER_SNAPSHOT */
3485 static void track_data_print(struct seq_file *m,
3486 struct hist_trigger_data *hist_data,
3487 struct tracing_map_elt *elt,
3488 struct action_data *data)
3490 u64 track_val = get_track_val(hist_data, elt, data);
3491 unsigned int i, save_var_idx;
3493 if (data->handler == HANDLER_ONMAX)
3494 seq_printf(m, "\n\tmax: %10llu", track_val);
3495 else if (data->handler == HANDLER_ONCHANGE)
3496 seq_printf(m, "\n\tchanged: %10llu", track_val);
3498 if (data->action == ACTION_SNAPSHOT)
3501 for (i = 0; i < hist_data->n_save_vars; i++) {
3502 struct hist_field *save_val = hist_data->save_vars[i]->val;
3503 struct hist_field *save_var = hist_data->save_vars[i]->var;
3506 save_var_idx = save_var->var.idx;
3508 val = tracing_map_read_var(elt, save_var_idx);
3510 if (save_val->flags & HIST_FIELD_FL_STRING) {
3511 seq_printf(m, " %s: %-32s", save_var->var.name,
3512 (char *)(uintptr_t)(val));
3514 seq_printf(m, " %s: %10llu", save_var->var.name, val);
3518 static void ontrack_action(struct hist_trigger_data *hist_data,
3519 struct tracing_map_elt *elt,
3520 struct trace_buffer *buffer, void *rec,
3521 struct ring_buffer_event *rbe, void *key,
3522 struct action_data *data, u64 *var_ref_vals)
3524 u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
3526 if (check_track_val(elt, data, var_val)) {
3527 save_track_val(hist_data, elt, data, var_val);
3528 save_track_data(hist_data, elt, buffer, rec, rbe,
3529 key, data, var_ref_vals);
3533 static void action_data_destroy(struct action_data *data)
3537 lockdep_assert_held(&event_mutex);
3539 kfree(data->action_name);
3541 for (i = 0; i < data->n_params; i++)
3542 kfree(data->params[i]);
3544 if (data->synth_event)
3545 data->synth_event->ref--;
3547 kfree(data->synth_event_name);
3552 static void track_data_destroy(struct hist_trigger_data *hist_data,
3553 struct action_data *data)
3555 struct trace_event_file *file = hist_data->event_file;
3557 destroy_hist_field(data->track_data.track_var, 0);
3559 if (data->action == ACTION_SNAPSHOT) {
3560 struct track_data *track_data;
3562 track_data = tracing_cond_snapshot_data(file->tr);
3563 if (track_data && track_data->hist_data == hist_data) {
3564 tracing_snapshot_cond_disable(file->tr);
3565 track_data_free(track_data);
3569 kfree(data->track_data.var_str);
3571 action_data_destroy(data);
3574 static int action_create(struct hist_trigger_data *hist_data,
3575 struct action_data *data);
3577 static int track_data_create(struct hist_trigger_data *hist_data,
3578 struct action_data *data)
3580 struct hist_field *var_field, *ref_field, *track_var = NULL;
3581 struct trace_event_file *file = hist_data->event_file;
3582 struct trace_array *tr = file->tr;
3583 char *track_data_var_str;
3586 track_data_var_str = data->track_data.var_str;
3587 if (track_data_var_str[0] != '$') {
3588 hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str));
3591 track_data_var_str++;
3593 var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
3595 hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str));
3599 ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
3603 data->track_data.var_ref = ref_field;
3605 if (data->handler == HANDLER_ONMAX)
3606 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
3607 if (IS_ERR(track_var)) {
3608 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3609 ret = PTR_ERR(track_var);
3613 if (data->handler == HANDLER_ONCHANGE)
3614 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
3615 if (IS_ERR(track_var)) {
3616 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3617 ret = PTR_ERR(track_var);
3620 data->track_data.track_var = track_var;
3622 ret = action_create(hist_data, data);
3627 static int parse_action_params(struct trace_array *tr, char *params,
3628 struct action_data *data)
3630 char *param, *saved_param;
3631 bool first_param = true;
3635 if (data->n_params >= SYNTH_FIELDS_MAX) {
3636 hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0);
3641 param = strsep(¶ms, ",");
3643 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0);
3648 param = strstrip(param);
3649 if (strlen(param) < 2) {
3650 hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param));
3655 saved_param = kstrdup(param, GFP_KERNEL);
3661 if (first_param && data->use_trace_keyword) {
3662 data->synth_event_name = saved_param;
3663 first_param = false;
3666 first_param = false;
3668 data->params[data->n_params++] = saved_param;
3674 static int action_parse(struct trace_array *tr, char *str, struct action_data *data,
3675 enum handler_id handler)
3682 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3687 action_name = strsep(&str, "(");
3688 if (!action_name || !str) {
3689 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3694 if (str_has_prefix(action_name, "save")) {
3695 char *params = strsep(&str, ")");
3698 hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0);
3703 ret = parse_action_params(tr, params, data);
3707 if (handler == HANDLER_ONMAX)
3708 data->track_data.check_val = check_track_val_max;
3709 else if (handler == HANDLER_ONCHANGE)
3710 data->track_data.check_val = check_track_val_changed;
3712 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3717 data->track_data.save_data = save_track_data_vars;
3718 data->fn = ontrack_action;
3719 data->action = ACTION_SAVE;
3720 } else if (str_has_prefix(action_name, "snapshot")) {
3721 char *params = strsep(&str, ")");
3724 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params));
3729 if (handler == HANDLER_ONMAX)
3730 data->track_data.check_val = check_track_val_max;
3731 else if (handler == HANDLER_ONCHANGE)
3732 data->track_data.check_val = check_track_val_changed;
3734 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3739 data->track_data.save_data = save_track_data_snapshot;
3740 data->fn = ontrack_action;
3741 data->action = ACTION_SNAPSHOT;
3743 char *params = strsep(&str, ")");
3745 if (str_has_prefix(action_name, "trace"))
3746 data->use_trace_keyword = true;
3749 ret = parse_action_params(tr, params, data);
3754 if (handler == HANDLER_ONMAX)
3755 data->track_data.check_val = check_track_val_max;
3756 else if (handler == HANDLER_ONCHANGE)
3757 data->track_data.check_val = check_track_val_changed;
3759 if (handler != HANDLER_ONMATCH) {
3760 data->track_data.save_data = action_trace;
3761 data->fn = ontrack_action;
3763 data->fn = action_trace;
3765 data->action = ACTION_TRACE;
3768 data->action_name = kstrdup(action_name, GFP_KERNEL);
3769 if (!data->action_name) {
3774 data->handler = handler;
3779 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
3780 char *str, enum handler_id handler)
3782 struct action_data *data;
3786 data = kzalloc(sizeof(*data), GFP_KERNEL);
3788 return ERR_PTR(-ENOMEM);
3790 var_str = strsep(&str, ")");
3791 if (!var_str || !str) {
3796 data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
3797 if (!data->track_data.var_str) {
3802 ret = action_parse(hist_data->event_file->tr, str, data, handler);
3808 track_data_destroy(hist_data, data);
3809 data = ERR_PTR(ret);
3813 static void onmatch_destroy(struct action_data *data)
3815 kfree(data->match_data.event);
3816 kfree(data->match_data.event_system);
3818 action_data_destroy(data);
3821 static void destroy_field_var(struct field_var *field_var)
3826 destroy_hist_field(field_var->var, 0);
3827 destroy_hist_field(field_var->val, 0);
3832 static void destroy_field_vars(struct hist_trigger_data *hist_data)
3836 for (i = 0; i < hist_data->n_field_vars; i++)
3837 destroy_field_var(hist_data->field_vars[i]);
3839 for (i = 0; i < hist_data->n_save_vars; i++)
3840 destroy_field_var(hist_data->save_vars[i]);
3843 static void save_field_var(struct hist_trigger_data *hist_data,
3844 struct field_var *field_var)
3846 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
3848 /* Stack traces are saved in the string storage too */
3849 if (field_var->val->flags & (HIST_FIELD_FL_STRING | HIST_FIELD_FL_STACKTRACE))
3850 hist_data->n_field_var_str++;
3854 static int check_synth_field(struct synth_event *event,
3855 struct hist_field *hist_field,
3856 unsigned int field_pos)
3858 struct synth_field *field;
3860 if (field_pos >= event->n_fields)
3863 field = event->fields[field_pos];
3866 * A dynamic string synth field can accept static or
3867 * dynamic. A static string synth field can only accept a
3868 * same-sized static string, which is checked for later.
3870 if (strstr(hist_field->type, "char[") && field->is_string
3871 && field->is_dynamic)
3874 if (strstr(hist_field->type, "long[") && field->is_stack)
3877 if (strcmp(field->type, hist_field->type) != 0) {
3878 if (field->size != hist_field->size ||
3879 (!field->is_string && field->is_signed != hist_field->is_signed))
3886 static struct hist_field *
3887 trace_action_find_var(struct hist_trigger_data *hist_data,
3888 struct action_data *data,
3889 char *system, char *event, char *var)
3891 struct trace_array *tr = hist_data->event_file->tr;
3892 struct hist_field *hist_field;
3894 var++; /* skip '$' */
3896 hist_field = find_target_event_var(hist_data, system, event, var);
3898 if (!system && data->handler == HANDLER_ONMATCH) {
3899 system = data->match_data.event_system;
3900 event = data->match_data.event;
3903 hist_field = find_event_var(hist_data, system, event, var);
3907 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var));
3912 static struct hist_field *
3913 trace_action_create_field_var(struct hist_trigger_data *hist_data,
3914 struct action_data *data, char *system,
3915 char *event, char *var)
3917 struct hist_field *hist_field = NULL;
3918 struct field_var *field_var;
3921 * First try to create a field var on the target event (the
3922 * currently being defined). This will create a variable for
3923 * unqualified fields on the target event, or if qualified,
3924 * target fields that have qualified names matching the target.
3926 field_var = create_target_field_var(hist_data, system, event, var);
3928 if (field_var && !IS_ERR(field_var)) {
3929 save_field_var(hist_data, field_var);
3930 hist_field = field_var->var;
3934 * If no explicit system.event is specified, default to
3935 * looking for fields on the onmatch(system.event.xxx)
3938 if (!system && data->handler == HANDLER_ONMATCH) {
3939 system = data->match_data.event_system;
3940 event = data->match_data.event;
3946 * At this point, we're looking at a field on another
3947 * event. Because we can't modify a hist trigger on
3948 * another event to add a variable for a field, we need
3949 * to create a new trigger on that event and create the
3950 * variable at the same time.
3952 hist_field = create_field_var_hist(hist_data, system, event, var);
3953 if (IS_ERR(hist_field))
3959 destroy_field_var(field_var);
3964 static int trace_action_create(struct hist_trigger_data *hist_data,
3965 struct action_data *data)
3967 struct trace_array *tr = hist_data->event_file->tr;
3968 char *event_name, *param, *system = NULL;
3969 struct hist_field *hist_field, *var_ref;
3971 unsigned int field_pos = 0;
3972 struct synth_event *event;
3973 char *synth_event_name;
3974 int var_ref_idx, ret = 0;
3976 lockdep_assert_held(&event_mutex);
3978 /* Sanity check to avoid out-of-bound write on 'data->var_ref_idx' */
3979 if (data->n_params > SYNTH_FIELDS_MAX)
3982 if (data->use_trace_keyword)
3983 synth_event_name = data->synth_event_name;
3985 synth_event_name = data->action_name;
3987 event = find_synth_event(synth_event_name);
3989 hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name));
3995 for (i = 0; i < data->n_params; i++) {
3998 p = param = kstrdup(data->params[i], GFP_KERNEL);
4004 system = strsep(¶m, ".");
4006 param = (char *)system;
4007 system = event_name = NULL;
4009 event_name = strsep(¶m, ".");
4017 if (param[0] == '$')
4018 hist_field = trace_action_find_var(hist_data, data,
4022 hist_field = trace_action_create_field_var(hist_data,
4034 if (check_synth_field(event, hist_field, field_pos) == 0) {
4035 var_ref = create_var_ref(hist_data, hist_field,
4036 system, event_name);
4043 var_ref_idx = find_var_ref_idx(hist_data, var_ref);
4044 if (WARN_ON(var_ref_idx < 0)) {
4050 data->var_ref_idx[i] = var_ref_idx;
4057 hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param));
4063 if (field_pos != event->n_fields) {
4064 hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name));
4069 data->synth_event = event;
4078 static int action_create(struct hist_trigger_data *hist_data,
4079 struct action_data *data)
4081 struct trace_event_file *file = hist_data->event_file;
4082 struct trace_array *tr = file->tr;
4083 struct track_data *track_data;
4084 struct field_var *field_var;
4089 if (data->action == ACTION_TRACE)
4090 return trace_action_create(hist_data, data);
4092 if (data->action == ACTION_SNAPSHOT) {
4093 track_data = track_data_alloc(hist_data->key_size, data, hist_data);
4094 if (IS_ERR(track_data)) {
4095 ret = PTR_ERR(track_data);
4099 ret = tracing_snapshot_cond_enable(file->tr, track_data,
4100 cond_snapshot_update);
4102 track_data_free(track_data);
4107 if (data->action == ACTION_SAVE) {
4108 if (hist_data->n_save_vars) {
4110 hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0);
4114 for (i = 0; i < data->n_params; i++) {
4115 param = kstrdup(data->params[i], GFP_KERNEL);
4121 field_var = create_target_field_var(hist_data, NULL, NULL, param);
4122 if (IS_ERR(field_var)) {
4123 hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL,
4125 ret = PTR_ERR(field_var);
4130 hist_data->save_vars[hist_data->n_save_vars++] = field_var;
4131 if (field_var->val->flags &
4132 (HIST_FIELD_FL_STRING | HIST_FIELD_FL_STACKTRACE))
4133 hist_data->n_save_var_str++;
4141 static int onmatch_create(struct hist_trigger_data *hist_data,
4142 struct action_data *data)
4144 return action_create(hist_data, data);
4147 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
4149 char *match_event, *match_event_system;
4150 struct action_data *data;
4153 data = kzalloc(sizeof(*data), GFP_KERNEL);
4155 return ERR_PTR(-ENOMEM);
4157 match_event = strsep(&str, ")");
4158 if (!match_event || !str) {
4159 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event));
4163 match_event_system = strsep(&match_event, ".");
4165 hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system));
4169 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
4170 hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event));
4174 data->match_data.event = kstrdup(match_event, GFP_KERNEL);
4175 if (!data->match_data.event) {
4180 data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
4181 if (!data->match_data.event_system) {
4186 ret = action_parse(tr, str, data, HANDLER_ONMATCH);
4192 onmatch_destroy(data);
4193 data = ERR_PTR(ret);
4197 static int create_hitcount_val(struct hist_trigger_data *hist_data)
4199 hist_data->fields[HITCOUNT_IDX] =
4200 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
4201 if (!hist_data->fields[HITCOUNT_IDX])
4204 hist_data->n_vals++;
4205 hist_data->n_fields++;
4207 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
4213 static int __create_val_field(struct hist_trigger_data *hist_data,
4214 unsigned int val_idx,
4215 struct trace_event_file *file,
4216 char *var_name, char *field_str,
4217 unsigned long flags)
4219 struct hist_field *hist_field;
4220 int ret = 0, n_subexprs = 0;
4222 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, &n_subexprs);
4223 if (IS_ERR(hist_field)) {
4224 ret = PTR_ERR(hist_field);
4228 hist_data->fields[val_idx] = hist_field;
4230 ++hist_data->n_vals;
4231 ++hist_data->n_fields;
4233 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4239 static int create_val_field(struct hist_trigger_data *hist_data,
4240 unsigned int val_idx,
4241 struct trace_event_file *file,
4244 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
4247 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
4250 static const char no_comm[] = "(no comm)";
4252 static u64 hist_field_execname(struct hist_field *hist_field,
4253 struct tracing_map_elt *elt,
4254 struct trace_buffer *buffer,
4255 struct ring_buffer_event *rbe,
4258 struct hist_elt_data *elt_data;
4260 if (WARN_ON_ONCE(!elt))
4261 return (u64)(unsigned long)no_comm;
4263 elt_data = elt->private_data;
4265 if (WARN_ON_ONCE(!elt_data->comm))
4266 return (u64)(unsigned long)no_comm;
4268 return (u64)(unsigned long)(elt_data->comm);
4271 static u64 hist_fn_call(struct hist_field *hist_field,
4272 struct tracing_map_elt *elt,
4273 struct trace_buffer *buffer,
4274 struct ring_buffer_event *rbe,
4277 switch (hist_field->fn_num) {
4278 case HIST_FIELD_FN_VAR_REF:
4279 return hist_field_var_ref(hist_field, elt, buffer, rbe, event);
4280 case HIST_FIELD_FN_COUNTER:
4281 return hist_field_counter(hist_field, elt, buffer, rbe, event);
4282 case HIST_FIELD_FN_CONST:
4283 return hist_field_const(hist_field, elt, buffer, rbe, event);
4284 case HIST_FIELD_FN_LOG2:
4285 return hist_field_log2(hist_field, elt, buffer, rbe, event);
4286 case HIST_FIELD_FN_BUCKET:
4287 return hist_field_bucket(hist_field, elt, buffer, rbe, event);
4288 case HIST_FIELD_FN_TIMESTAMP:
4289 return hist_field_timestamp(hist_field, elt, buffer, rbe, event);
4290 case HIST_FIELD_FN_CPU:
4291 return hist_field_cpu(hist_field, elt, buffer, rbe, event);
4292 case HIST_FIELD_FN_STRING:
4293 return hist_field_string(hist_field, elt, buffer, rbe, event);
4294 case HIST_FIELD_FN_DYNSTRING:
4295 return hist_field_dynstring(hist_field, elt, buffer, rbe, event);
4296 case HIST_FIELD_FN_RELDYNSTRING:
4297 return hist_field_reldynstring(hist_field, elt, buffer, rbe, event);
4298 case HIST_FIELD_FN_PSTRING:
4299 return hist_field_pstring(hist_field, elt, buffer, rbe, event);
4300 case HIST_FIELD_FN_S64:
4301 return hist_field_s64(hist_field, elt, buffer, rbe, event);
4302 case HIST_FIELD_FN_U64:
4303 return hist_field_u64(hist_field, elt, buffer, rbe, event);
4304 case HIST_FIELD_FN_S32:
4305 return hist_field_s32(hist_field, elt, buffer, rbe, event);
4306 case HIST_FIELD_FN_U32:
4307 return hist_field_u32(hist_field, elt, buffer, rbe, event);
4308 case HIST_FIELD_FN_S16:
4309 return hist_field_s16(hist_field, elt, buffer, rbe, event);
4310 case HIST_FIELD_FN_U16:
4311 return hist_field_u16(hist_field, elt, buffer, rbe, event);
4312 case HIST_FIELD_FN_S8:
4313 return hist_field_s8(hist_field, elt, buffer, rbe, event);
4314 case HIST_FIELD_FN_U8:
4315 return hist_field_u8(hist_field, elt, buffer, rbe, event);
4316 case HIST_FIELD_FN_UMINUS:
4317 return hist_field_unary_minus(hist_field, elt, buffer, rbe, event);
4318 case HIST_FIELD_FN_MINUS:
4319 return hist_field_minus(hist_field, elt, buffer, rbe, event);
4320 case HIST_FIELD_FN_PLUS:
4321 return hist_field_plus(hist_field, elt, buffer, rbe, event);
4322 case HIST_FIELD_FN_DIV:
4323 return hist_field_div(hist_field, elt, buffer, rbe, event);
4324 case HIST_FIELD_FN_MULT:
4325 return hist_field_mult(hist_field, elt, buffer, rbe, event);
4326 case HIST_FIELD_FN_DIV_POWER2:
4327 return div_by_power_of_two(hist_field, elt, buffer, rbe, event);
4328 case HIST_FIELD_FN_DIV_NOT_POWER2:
4329 return div_by_not_power_of_two(hist_field, elt, buffer, rbe, event);
4330 case HIST_FIELD_FN_DIV_MULT_SHIFT:
4331 return div_by_mult_and_shift(hist_field, elt, buffer, rbe, event);
4332 case HIST_FIELD_FN_EXECNAME:
4333 return hist_field_execname(hist_field, elt, buffer, rbe, event);
4339 /* Convert a var that points to common_pid.execname to a string */
4340 static void update_var_execname(struct hist_field *hist_field)
4342 hist_field->flags = HIST_FIELD_FL_STRING | HIST_FIELD_FL_VAR |
4343 HIST_FIELD_FL_EXECNAME;
4344 hist_field->size = MAX_FILTER_STR_VAL;
4345 hist_field->is_signed = 0;
4347 kfree_const(hist_field->type);
4348 hist_field->type = "char[]";
4350 hist_field->fn_num = HIST_FIELD_FN_EXECNAME;
4353 static int create_var_field(struct hist_trigger_data *hist_data,
4354 unsigned int val_idx,
4355 struct trace_event_file *file,
4356 char *var_name, char *expr_str)
4358 struct trace_array *tr = hist_data->event_file->tr;
4359 unsigned long flags = 0;
4362 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4365 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
4366 hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name));
4370 flags |= HIST_FIELD_FL_VAR;
4371 hist_data->n_vars++;
4372 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
4375 ret = __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
4377 if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_EXECNAME)
4378 update_var_execname(hist_data->fields[val_idx]);
4380 if (!ret && hist_data->fields[val_idx]->flags &
4381 (HIST_FIELD_FL_STRING | HIST_FIELD_FL_STACKTRACE))
4382 hist_data->fields[val_idx]->var_str_idx = hist_data->n_var_str++;
4387 static int create_val_fields(struct hist_trigger_data *hist_data,
4388 struct trace_event_file *file)
4390 unsigned int i, j = 1, n_hitcount = 0;
4391 char *fields_str, *field_str;
4394 ret = create_hitcount_val(hist_data);
4398 fields_str = hist_data->attrs->vals_str;
4402 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
4403 j < TRACING_MAP_VALS_MAX; i++) {
4404 field_str = strsep(&fields_str, ",");
4408 if (strcmp(field_str, "hitcount") == 0) {
4413 ret = create_val_field(hist_data, j++, file, field_str);
4418 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
4421 /* There is only raw hitcount but nohitcount suppresses it. */
4422 if (j == 1 && hist_data->attrs->no_hitcount) {
4423 hist_err(hist_data->event_file->tr, HIST_ERR_NEED_NOHC_VAL, 0);
4430 static int create_key_field(struct hist_trigger_data *hist_data,
4431 unsigned int key_idx,
4432 unsigned int key_offset,
4433 struct trace_event_file *file,
4436 struct trace_array *tr = hist_data->event_file->tr;
4437 struct hist_field *hist_field = NULL;
4438 unsigned long flags = 0;
4439 unsigned int key_size;
4440 int ret = 0, n_subexprs = 0;
4442 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
4445 flags |= HIST_FIELD_FL_KEY;
4447 if (strcmp(field_str, "stacktrace") == 0) {
4448 flags |= HIST_FIELD_FL_STACKTRACE;
4449 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
4450 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
4452 hist_field = parse_expr(hist_data, file, field_str, flags,
4454 if (IS_ERR(hist_field)) {
4455 ret = PTR_ERR(hist_field);
4459 if (field_has_hist_vars(hist_field, 0)) {
4460 hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str));
4461 destroy_hist_field(hist_field, 0);
4466 key_size = hist_field->size;
4469 hist_data->fields[key_idx] = hist_field;
4471 key_size = ALIGN(key_size, sizeof(u64));
4472 hist_data->fields[key_idx]->size = key_size;
4473 hist_data->fields[key_idx]->offset = key_offset;
4475 hist_data->key_size += key_size;
4477 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
4482 hist_data->n_keys++;
4483 hist_data->n_fields++;
4485 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
4493 static int create_key_fields(struct hist_trigger_data *hist_data,
4494 struct trace_event_file *file)
4496 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
4497 char *fields_str, *field_str;
4500 fields_str = hist_data->attrs->keys_str;
4504 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
4505 field_str = strsep(&fields_str, ",");
4508 ret = create_key_field(hist_data, i, key_offset,
4523 static int create_var_fields(struct hist_trigger_data *hist_data,
4524 struct trace_event_file *file)
4526 unsigned int i, j = hist_data->n_vals;
4529 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
4531 for (i = 0; i < n_vars; i++) {
4532 char *var_name = hist_data->attrs->var_defs.name[i];
4533 char *expr = hist_data->attrs->var_defs.expr[i];
4535 ret = create_var_field(hist_data, j++, file, var_name, expr);
4543 static void free_var_defs(struct hist_trigger_data *hist_data)
4547 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
4548 kfree(hist_data->attrs->var_defs.name[i]);
4549 kfree(hist_data->attrs->var_defs.expr[i]);
4552 hist_data->attrs->var_defs.n_vars = 0;
4555 static int parse_var_defs(struct hist_trigger_data *hist_data)
4557 struct trace_array *tr = hist_data->event_file->tr;
4558 char *s, *str, *var_name, *field_str;
4559 unsigned int i, j, n_vars = 0;
4562 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
4563 str = hist_data->attrs->assignment_str[i];
4564 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
4565 field_str = strsep(&str, ",");
4569 var_name = strsep(&field_str, "=");
4570 if (!var_name || !field_str) {
4571 hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT,
4577 if (n_vars == TRACING_MAP_VARS_MAX) {
4578 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name));
4583 s = kstrdup(var_name, GFP_KERNEL);
4588 hist_data->attrs->var_defs.name[n_vars] = s;
4590 s = kstrdup(field_str, GFP_KERNEL);
4592 kfree(hist_data->attrs->var_defs.name[n_vars]);
4593 hist_data->attrs->var_defs.name[n_vars] = NULL;
4597 hist_data->attrs->var_defs.expr[n_vars++] = s;
4599 hist_data->attrs->var_defs.n_vars = n_vars;
4605 free_var_defs(hist_data);
4610 static int create_hist_fields(struct hist_trigger_data *hist_data,
4611 struct trace_event_file *file)
4615 ret = parse_var_defs(hist_data);
4619 ret = create_val_fields(hist_data, file);
4623 ret = create_var_fields(hist_data, file);
4627 ret = create_key_fields(hist_data, file);
4630 free_var_defs(hist_data);
4635 static int is_descending(struct trace_array *tr, const char *str)
4640 if (strcmp(str, "descending") == 0)
4643 if (strcmp(str, "ascending") == 0)
4646 hist_err(tr, HIST_ERR_INVALID_SORT_MODIFIER, errpos((char *)str));
4651 static int create_sort_keys(struct hist_trigger_data *hist_data)
4653 struct trace_array *tr = hist_data->event_file->tr;
4654 char *fields_str = hist_data->attrs->sort_key_str;
4655 struct tracing_map_sort_key *sort_key;
4656 int descending, ret = 0;
4657 unsigned int i, j, k;
4659 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
4664 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
4665 struct hist_field *hist_field;
4666 char *field_str, *field_name;
4667 const char *test_name;
4669 sort_key = &hist_data->sort_keys[i];
4671 field_str = strsep(&fields_str, ",");
4677 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4681 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4682 hist_err(tr, HIST_ERR_TOO_MANY_SORT_FIELDS, errpos("sort="));
4687 field_name = strsep(&field_str, ".");
4688 if (!field_name || !*field_name) {
4690 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4694 if (strcmp(field_name, "hitcount") == 0) {
4695 descending = is_descending(tr, field_str);
4696 if (descending < 0) {
4700 sort_key->descending = descending;
4704 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4707 hist_field = hist_data->fields[j];
4708 if (hist_field->flags & HIST_FIELD_FL_VAR)
4713 test_name = hist_field_name(hist_field, 0);
4715 if (strcmp(field_name, test_name) == 0) {
4716 sort_key->field_idx = idx;
4717 descending = is_descending(tr, field_str);
4718 if (descending < 0) {
4722 sort_key->descending = descending;
4726 if (j == hist_data->n_fields) {
4728 hist_err(tr, HIST_ERR_INVALID_SORT_FIELD, errpos(field_name));
4733 hist_data->n_sort_keys = i;
4738 static void destroy_actions(struct hist_trigger_data *hist_data)
4742 for (i = 0; i < hist_data->n_actions; i++) {
4743 struct action_data *data = hist_data->actions[i];
4745 if (data->handler == HANDLER_ONMATCH)
4746 onmatch_destroy(data);
4747 else if (data->handler == HANDLER_ONMAX ||
4748 data->handler == HANDLER_ONCHANGE)
4749 track_data_destroy(hist_data, data);
4755 static int parse_actions(struct hist_trigger_data *hist_data)
4757 struct trace_array *tr = hist_data->event_file->tr;
4758 struct action_data *data;
4764 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4765 str = hist_data->attrs->action_str[i];
4767 if ((len = str_has_prefix(str, "onmatch("))) {
4768 char *action_str = str + len;
4770 data = onmatch_parse(tr, action_str);
4772 ret = PTR_ERR(data);
4775 } else if ((len = str_has_prefix(str, "onmax("))) {
4776 char *action_str = str + len;
4778 data = track_data_parse(hist_data, action_str,
4781 ret = PTR_ERR(data);
4784 } else if ((len = str_has_prefix(str, "onchange("))) {
4785 char *action_str = str + len;
4787 data = track_data_parse(hist_data, action_str,
4790 ret = PTR_ERR(data);
4798 hist_data->actions[hist_data->n_actions++] = data;
4804 static int create_actions(struct hist_trigger_data *hist_data)
4806 struct action_data *data;
4810 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4811 data = hist_data->actions[i];
4813 if (data->handler == HANDLER_ONMATCH) {
4814 ret = onmatch_create(hist_data, data);
4817 } else if (data->handler == HANDLER_ONMAX ||
4818 data->handler == HANDLER_ONCHANGE) {
4819 ret = track_data_create(hist_data, data);
4831 static void print_actions(struct seq_file *m,
4832 struct hist_trigger_data *hist_data,
4833 struct tracing_map_elt *elt)
4837 for (i = 0; i < hist_data->n_actions; i++) {
4838 struct action_data *data = hist_data->actions[i];
4840 if (data->action == ACTION_SNAPSHOT)
4843 if (data->handler == HANDLER_ONMAX ||
4844 data->handler == HANDLER_ONCHANGE)
4845 track_data_print(m, hist_data, elt, data);
4849 static void print_action_spec(struct seq_file *m,
4850 struct hist_trigger_data *hist_data,
4851 struct action_data *data)
4855 if (data->action == ACTION_SAVE) {
4856 for (i = 0; i < hist_data->n_save_vars; i++) {
4857 seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
4858 if (i < hist_data->n_save_vars - 1)
4861 } else if (data->action == ACTION_TRACE) {
4862 if (data->use_trace_keyword)
4863 seq_printf(m, "%s", data->synth_event_name);
4864 for (i = 0; i < data->n_params; i++) {
4865 if (i || data->use_trace_keyword)
4867 seq_printf(m, "%s", data->params[i]);
4872 static void print_track_data_spec(struct seq_file *m,
4873 struct hist_trigger_data *hist_data,
4874 struct action_data *data)
4876 if (data->handler == HANDLER_ONMAX)
4877 seq_puts(m, ":onmax(");
4878 else if (data->handler == HANDLER_ONCHANGE)
4879 seq_puts(m, ":onchange(");
4880 seq_printf(m, "%s", data->track_data.var_str);
4881 seq_printf(m, ").%s(", data->action_name);
4883 print_action_spec(m, hist_data, data);
4888 static void print_onmatch_spec(struct seq_file *m,
4889 struct hist_trigger_data *hist_data,
4890 struct action_data *data)
4892 seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
4893 data->match_data.event);
4895 seq_printf(m, "%s(", data->action_name);
4897 print_action_spec(m, hist_data, data);
4902 static bool actions_match(struct hist_trigger_data *hist_data,
4903 struct hist_trigger_data *hist_data_test)
4907 if (hist_data->n_actions != hist_data_test->n_actions)
4910 for (i = 0; i < hist_data->n_actions; i++) {
4911 struct action_data *data = hist_data->actions[i];
4912 struct action_data *data_test = hist_data_test->actions[i];
4913 char *action_name, *action_name_test;
4915 if (data->handler != data_test->handler)
4917 if (data->action != data_test->action)
4920 if (data->n_params != data_test->n_params)
4923 for (j = 0; j < data->n_params; j++) {
4924 if (strcmp(data->params[j], data_test->params[j]) != 0)
4928 if (data->use_trace_keyword)
4929 action_name = data->synth_event_name;
4931 action_name = data->action_name;
4933 if (data_test->use_trace_keyword)
4934 action_name_test = data_test->synth_event_name;
4936 action_name_test = data_test->action_name;
4938 if (strcmp(action_name, action_name_test) != 0)
4941 if (data->handler == HANDLER_ONMATCH) {
4942 if (strcmp(data->match_data.event_system,
4943 data_test->match_data.event_system) != 0)
4945 if (strcmp(data->match_data.event,
4946 data_test->match_data.event) != 0)
4948 } else if (data->handler == HANDLER_ONMAX ||
4949 data->handler == HANDLER_ONCHANGE) {
4950 if (strcmp(data->track_data.var_str,
4951 data_test->track_data.var_str) != 0)
4960 static void print_actions_spec(struct seq_file *m,
4961 struct hist_trigger_data *hist_data)
4965 for (i = 0; i < hist_data->n_actions; i++) {
4966 struct action_data *data = hist_data->actions[i];
4968 if (data->handler == HANDLER_ONMATCH)
4969 print_onmatch_spec(m, hist_data, data);
4970 else if (data->handler == HANDLER_ONMAX ||
4971 data->handler == HANDLER_ONCHANGE)
4972 print_track_data_spec(m, hist_data, data);
4976 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
4980 for (i = 0; i < hist_data->n_field_var_hists; i++) {
4981 kfree(hist_data->field_var_hists[i]->cmd);
4982 kfree(hist_data->field_var_hists[i]);
4986 static void destroy_hist_data(struct hist_trigger_data *hist_data)
4991 destroy_hist_trigger_attrs(hist_data->attrs);
4992 destroy_hist_fields(hist_data);
4993 tracing_map_destroy(hist_data->map);
4995 destroy_actions(hist_data);
4996 destroy_field_vars(hist_data);
4997 destroy_field_var_hists(hist_data);
5002 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
5004 struct tracing_map *map = hist_data->map;
5005 struct ftrace_event_field *field;
5006 struct hist_field *hist_field;
5009 for_each_hist_field(i, hist_data) {
5010 hist_field = hist_data->fields[i];
5011 if (hist_field->flags & HIST_FIELD_FL_KEY) {
5012 tracing_map_cmp_fn_t cmp_fn;
5014 field = hist_field->field;
5016 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
5017 cmp_fn = tracing_map_cmp_none;
5018 else if (!field || hist_field->flags & HIST_FIELD_FL_CPU)
5019 cmp_fn = tracing_map_cmp_num(hist_field->size,
5020 hist_field->is_signed);
5021 else if (is_string_field(field))
5022 cmp_fn = tracing_map_cmp_string;
5024 cmp_fn = tracing_map_cmp_num(field->size,
5026 idx = tracing_map_add_key_field(map,
5029 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
5030 idx = tracing_map_add_sum_field(map);
5035 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5036 idx = tracing_map_add_var(map);
5039 hist_field->var.idx = idx;
5040 hist_field->var.hist_data = hist_data;
5047 static struct hist_trigger_data *
5048 create_hist_data(unsigned int map_bits,
5049 struct hist_trigger_attrs *attrs,
5050 struct trace_event_file *file,
5053 const struct tracing_map_ops *map_ops = NULL;
5054 struct hist_trigger_data *hist_data;
5057 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
5059 return ERR_PTR(-ENOMEM);
5061 hist_data->attrs = attrs;
5062 hist_data->remove = remove;
5063 hist_data->event_file = file;
5065 ret = parse_actions(hist_data);
5069 ret = create_hist_fields(hist_data, file);
5073 ret = create_sort_keys(hist_data);
5077 map_ops = &hist_trigger_elt_data_ops;
5079 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
5080 map_ops, hist_data);
5081 if (IS_ERR(hist_data->map)) {
5082 ret = PTR_ERR(hist_data->map);
5083 hist_data->map = NULL;
5087 ret = create_tracing_map_fields(hist_data);
5093 hist_data->attrs = NULL;
5095 destroy_hist_data(hist_data);
5097 hist_data = ERR_PTR(ret);
5102 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
5103 struct tracing_map_elt *elt,
5104 struct trace_buffer *buffer, void *rec,
5105 struct ring_buffer_event *rbe,
5108 struct hist_elt_data *elt_data;
5109 struct hist_field *hist_field;
5110 unsigned int i, var_idx;
5113 elt_data = elt->private_data;
5114 elt_data->var_ref_vals = var_ref_vals;
5116 for_each_hist_val_field(i, hist_data) {
5117 hist_field = hist_data->fields[i];
5118 hist_val = hist_fn_call(hist_field, elt, buffer, rbe, rec);
5119 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5120 var_idx = hist_field->var.idx;
5122 if (hist_field->flags &
5123 (HIST_FIELD_FL_STRING | HIST_FIELD_FL_STACKTRACE)) {
5124 unsigned int str_start, var_str_idx, idx;
5125 char *str, *val_str;
5128 str_start = hist_data->n_field_var_str +
5129 hist_data->n_save_var_str;
5130 var_str_idx = hist_field->var_str_idx;
5131 idx = str_start + var_str_idx;
5133 str = elt_data->field_var_str[idx];
5134 val_str = (char *)(uintptr_t)hist_val;
5136 if (hist_field->flags & HIST_FIELD_FL_STRING) {
5137 size = min(hist_field->size, STR_VAR_LEN_MAX);
5138 strscpy(str, val_str, size);
5140 char *stack_start = str + sizeof(unsigned long);
5143 e = stack_trace_save((void *)stack_start,
5144 HIST_STACKTRACE_DEPTH,
5145 HIST_STACKTRACE_SKIP);
5146 if (e < HIST_STACKTRACE_DEPTH - 1)
5147 ((unsigned long *)stack_start)[e] = 0;
5148 *((unsigned long *)str) = e;
5150 hist_val = (u64)(uintptr_t)str;
5152 tracing_map_set_var(elt, var_idx, hist_val);
5155 tracing_map_update_sum(elt, i, hist_val);
5158 for_each_hist_key_field(i, hist_data) {
5159 hist_field = hist_data->fields[i];
5160 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5161 hist_val = hist_fn_call(hist_field, elt, buffer, rbe, rec);
5162 var_idx = hist_field->var.idx;
5163 tracing_map_set_var(elt, var_idx, hist_val);
5167 update_field_vars(hist_data, elt, buffer, rbe, rec);
5170 static inline void add_to_key(char *compound_key, void *key,
5171 struct hist_field *key_field, void *rec)
5173 size_t size = key_field->size;
5175 if (key_field->flags & HIST_FIELD_FL_STRING) {
5176 struct ftrace_event_field *field;
5178 field = key_field->field;
5179 if (field->filter_type == FILTER_DYN_STRING ||
5180 field->filter_type == FILTER_RDYN_STRING)
5181 size = *(u32 *)(rec + field->offset) >> 16;
5182 else if (field->filter_type == FILTER_STATIC_STRING)
5185 /* ensure NULL-termination */
5186 if (size > key_field->size - 1)
5187 size = key_field->size - 1;
5189 strncpy(compound_key + key_field->offset, (char *)key, size);
5191 memcpy(compound_key + key_field->offset, key, size);
5195 hist_trigger_actions(struct hist_trigger_data *hist_data,
5196 struct tracing_map_elt *elt,
5197 struct trace_buffer *buffer, void *rec,
5198 struct ring_buffer_event *rbe, void *key,
5201 struct action_data *data;
5204 for (i = 0; i < hist_data->n_actions; i++) {
5205 data = hist_data->actions[i];
5206 data->fn(hist_data, elt, buffer, rec, rbe, key, data, var_ref_vals);
5210 static void event_hist_trigger(struct event_trigger_data *data,
5211 struct trace_buffer *buffer, void *rec,
5212 struct ring_buffer_event *rbe)
5214 struct hist_trigger_data *hist_data = data->private_data;
5215 bool use_compound_key = (hist_data->n_keys > 1);
5216 unsigned long entries[HIST_STACKTRACE_DEPTH];
5217 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
5218 char compound_key[HIST_KEY_SIZE_MAX];
5219 struct tracing_map_elt *elt = NULL;
5220 struct hist_field *key_field;
5228 memset(compound_key, 0, hist_data->key_size);
5230 for_each_hist_key_field(i, hist_data) {
5231 key_field = hist_data->fields[i];
5233 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5234 memset(entries, 0, HIST_STACKTRACE_SIZE);
5235 stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
5236 HIST_STACKTRACE_SKIP);
5239 field_contents = hist_fn_call(key_field, elt, buffer, rbe, rec);
5240 if (key_field->flags & HIST_FIELD_FL_STRING) {
5241 key = (void *)(unsigned long)field_contents;
5242 use_compound_key = true;
5244 key = (void *)&field_contents;
5247 if (use_compound_key)
5248 add_to_key(compound_key, key, key_field, rec);
5251 if (use_compound_key)
5254 if (hist_data->n_var_refs &&
5255 !resolve_var_refs(hist_data, key, var_ref_vals, false))
5258 elt = tracing_map_insert(hist_data->map, key);
5262 hist_trigger_elt_update(hist_data, elt, buffer, rec, rbe, var_ref_vals);
5264 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
5265 hist_trigger_actions(hist_data, elt, buffer, rec, rbe, key, var_ref_vals);
5268 static void hist_trigger_stacktrace_print(struct seq_file *m,
5269 unsigned long *stacktrace_entries,
5270 unsigned int max_entries)
5272 unsigned int spaces = 8;
5275 for (i = 0; i < max_entries; i++) {
5276 if (!stacktrace_entries[i])
5279 seq_printf(m, "%*c", 1 + spaces, ' ');
5280 seq_printf(m, "%pS\n", (void*)stacktrace_entries[i]);
5284 static void hist_trigger_print_key(struct seq_file *m,
5285 struct hist_trigger_data *hist_data,
5287 struct tracing_map_elt *elt)
5289 struct hist_field *key_field;
5290 bool multiline = false;
5291 const char *field_name;
5297 for_each_hist_key_field(i, hist_data) {
5298 key_field = hist_data->fields[i];
5300 if (i > hist_data->n_vals)
5303 field_name = hist_field_name(key_field, 0);
5305 if (key_field->flags & HIST_FIELD_FL_HEX) {
5306 uval = *(u64 *)(key + key_field->offset);
5307 seq_printf(m, "%s: %llx", field_name, uval);
5308 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
5309 uval = *(u64 *)(key + key_field->offset);
5310 seq_printf(m, "%s: [%llx] %-45ps", field_name,
5311 uval, (void *)(uintptr_t)uval);
5312 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
5313 uval = *(u64 *)(key + key_field->offset);
5314 seq_printf(m, "%s: [%llx] %-55pS", field_name,
5315 uval, (void *)(uintptr_t)uval);
5316 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
5317 struct hist_elt_data *elt_data = elt->private_data;
5320 if (WARN_ON_ONCE(!elt_data))
5323 comm = elt_data->comm;
5325 uval = *(u64 *)(key + key_field->offset);
5326 seq_printf(m, "%s: %-16s[%10llu]", field_name,
5328 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
5329 const char *syscall_name;
5331 uval = *(u64 *)(key + key_field->offset);
5332 syscall_name = get_syscall_name(uval);
5334 syscall_name = "unknown_syscall";
5336 seq_printf(m, "%s: %-30s[%3llu]", field_name,
5337 syscall_name, uval);
5338 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5339 seq_puts(m, "stacktrace:\n");
5340 hist_trigger_stacktrace_print(m,
5341 key + key_field->offset,
5342 HIST_STACKTRACE_DEPTH);
5344 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
5345 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
5346 *(u64 *)(key + key_field->offset));
5347 } else if (key_field->flags & HIST_FIELD_FL_BUCKET) {
5348 unsigned long buckets = key_field->buckets;
5349 uval = *(u64 *)(key + key_field->offset);
5350 seq_printf(m, "%s: ~ %llu-%llu", field_name,
5351 uval, uval + buckets -1);
5352 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
5353 seq_printf(m, "%s: %-50s", field_name,
5354 (char *)(key + key_field->offset));
5356 uval = *(u64 *)(key + key_field->offset);
5357 seq_printf(m, "%s: %10llu", field_name, uval);
5367 /* Get the 100 times of the percentage of @val in @total */
5368 static inline unsigned int __get_percentage(u64 val, u64 total)
5373 if (val < (U64_MAX / 10000))
5374 return (unsigned int)div64_ul(val * 10000, total);
5376 total = div64_u64(total, 10000);
5380 return (unsigned int)div64_ul(val, total);
5382 return val ? UINT_MAX : 0;
5385 #define BAR_CHAR '#'
5387 static inline const char *__fill_bar_str(char *buf, int size, u64 val, u64 max)
5389 unsigned int len = __get_percentage(val, max);
5392 if (len == UINT_MAX) {
5393 snprintf(buf, size, "[ERROR]");
5397 len = len * size / 10000;
5398 for (i = 0; i < len && i < size; i++)
5407 struct hist_val_stat {
5412 static void hist_trigger_print_val(struct seq_file *m, unsigned int idx,
5413 const char *field_name, unsigned long flags,
5414 struct hist_val_stat *stats,
5415 struct tracing_map_elt *elt)
5417 u64 val = tracing_map_read_sum(elt, idx);
5421 if (flags & HIST_FIELD_FL_PERCENT) {
5422 pc = __get_percentage(val, stats[idx].total);
5424 seq_printf(m, " %s (%%):[ERROR]", field_name);
5426 seq_printf(m, " %s (%%): %3u.%02u", field_name,
5427 pc / 100, pc % 100);
5428 } else if (flags & HIST_FIELD_FL_GRAPH) {
5429 seq_printf(m, " %s: %20s", field_name,
5430 __fill_bar_str(bar, 20, val, stats[idx].max));
5431 } else if (flags & HIST_FIELD_FL_HEX) {
5432 seq_printf(m, " %s: %10llx", field_name, val);
5434 seq_printf(m, " %s: %10llu", field_name, val);
5438 static void hist_trigger_entry_print(struct seq_file *m,
5439 struct hist_trigger_data *hist_data,
5440 struct hist_val_stat *stats,
5442 struct tracing_map_elt *elt)
5444 const char *field_name;
5445 unsigned int i = HITCOUNT_IDX;
5446 unsigned long flags;
5448 hist_trigger_print_key(m, hist_data, key, elt);
5450 /* At first, show the raw hitcount if !nohitcount */
5451 if (!hist_data->attrs->no_hitcount)
5452 hist_trigger_print_val(m, i, "hitcount", 0, stats, elt);
5454 for (i = 1; i < hist_data->n_vals; i++) {
5455 field_name = hist_field_name(hist_data->fields[i], 0);
5456 flags = hist_data->fields[i]->flags;
5457 if (flags & HIST_FIELD_FL_VAR || flags & HIST_FIELD_FL_EXPR)
5461 hist_trigger_print_val(m, i, field_name, flags, stats, elt);
5464 print_actions(m, hist_data, elt);
5469 static int print_entries(struct seq_file *m,
5470 struct hist_trigger_data *hist_data)
5472 struct tracing_map_sort_entry **sort_entries = NULL;
5473 struct tracing_map *map = hist_data->map;
5474 int i, j, n_entries;
5475 struct hist_val_stat *stats = NULL;
5478 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
5479 hist_data->n_sort_keys,
5484 /* Calculate the max and the total for each field if needed. */
5485 for (j = 0; j < hist_data->n_vals; j++) {
5486 if (!(hist_data->fields[j]->flags &
5487 (HIST_FIELD_FL_PERCENT | HIST_FIELD_FL_GRAPH)))
5490 stats = kcalloc(hist_data->n_vals, sizeof(*stats),
5493 n_entries = -ENOMEM;
5497 for (i = 0; i < n_entries; i++) {
5498 val = tracing_map_read_sum(sort_entries[i]->elt, j);
5499 stats[j].total += val;
5500 if (stats[j].max < val)
5505 for (i = 0; i < n_entries; i++)
5506 hist_trigger_entry_print(m, hist_data, stats,
5507 sort_entries[i]->key,
5508 sort_entries[i]->elt);
5512 tracing_map_destroy_sort_entries(sort_entries, n_entries);
5517 static void hist_trigger_show(struct seq_file *m,
5518 struct event_trigger_data *data, int n)
5520 struct hist_trigger_data *hist_data;
5524 seq_puts(m, "\n\n");
5526 seq_puts(m, "# event histogram\n#\n# trigger info: ");
5527 data->ops->print(m, data);
5528 seq_puts(m, "#\n\n");
5530 hist_data = data->private_data;
5531 n_entries = print_entries(m, hist_data);
5535 track_data_snapshot_print(m, hist_data);
5537 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
5538 (u64)atomic64_read(&hist_data->map->hits),
5539 n_entries, (u64)atomic64_read(&hist_data->map->drops));
5542 static int hist_show(struct seq_file *m, void *v)
5544 struct event_trigger_data *data;
5545 struct trace_event_file *event_file;
5548 mutex_lock(&event_mutex);
5550 event_file = event_file_data(m->private);
5551 if (unlikely(!event_file)) {
5556 list_for_each_entry(data, &event_file->triggers, list) {
5557 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5558 hist_trigger_show(m, data, n++);
5562 mutex_unlock(&event_mutex);
5567 static int event_hist_open(struct inode *inode, struct file *file)
5571 ret = security_locked_down(LOCKDOWN_TRACEFS);
5575 return single_open(file, hist_show, file);
5578 const struct file_operations event_hist_fops = {
5579 .open = event_hist_open,
5581 .llseek = seq_lseek,
5582 .release = single_release,
5585 #ifdef CONFIG_HIST_TRIGGERS_DEBUG
5586 static void hist_field_debug_show_flags(struct seq_file *m,
5587 unsigned long flags)
5589 seq_puts(m, " flags:\n");
5591 if (flags & HIST_FIELD_FL_KEY)
5592 seq_puts(m, " HIST_FIELD_FL_KEY\n");
5593 else if (flags & HIST_FIELD_FL_HITCOUNT)
5594 seq_puts(m, " VAL: HIST_FIELD_FL_HITCOUNT\n");
5595 else if (flags & HIST_FIELD_FL_VAR)
5596 seq_puts(m, " HIST_FIELD_FL_VAR\n");
5597 else if (flags & HIST_FIELD_FL_VAR_REF)
5598 seq_puts(m, " HIST_FIELD_FL_VAR_REF\n");
5600 seq_puts(m, " VAL: normal u64 value\n");
5602 if (flags & HIST_FIELD_FL_ALIAS)
5603 seq_puts(m, " HIST_FIELD_FL_ALIAS\n");
5604 else if (flags & HIST_FIELD_FL_CONST)
5605 seq_puts(m, " HIST_FIELD_FL_CONST\n");
5608 static int hist_field_debug_show(struct seq_file *m,
5609 struct hist_field *field, unsigned long flags)
5611 if ((field->flags & flags) != flags) {
5612 seq_printf(m, "ERROR: bad flags - %lx\n", flags);
5616 hist_field_debug_show_flags(m, field->flags);
5618 seq_printf(m, " ftrace_event_field name: %s\n",
5619 field->field->name);
5621 if (field->flags & HIST_FIELD_FL_VAR) {
5622 seq_printf(m, " var.name: %s\n", field->var.name);
5623 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
5627 if (field->flags & HIST_FIELD_FL_CONST)
5628 seq_printf(m, " constant: %llu\n", field->constant);
5630 if (field->flags & HIST_FIELD_FL_ALIAS)
5631 seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n",
5632 field->var_ref_idx);
5634 if (field->flags & HIST_FIELD_FL_VAR_REF) {
5635 seq_printf(m, " name: %s\n", field->name);
5636 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
5638 seq_printf(m, " var.hist_data: %p\n", field->var.hist_data);
5639 seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n",
5640 field->var_ref_idx);
5642 seq_printf(m, " system: %s\n", field->system);
5643 if (field->event_name)
5644 seq_printf(m, " event_name: %s\n", field->event_name);
5647 seq_printf(m, " type: %s\n", field->type);
5648 seq_printf(m, " size: %u\n", field->size);
5649 seq_printf(m, " is_signed: %u\n", field->is_signed);
5654 static int field_var_debug_show(struct seq_file *m,
5655 struct field_var *field_var, unsigned int i,
5658 const char *vars_name = save_vars ? "save_vars" : "field_vars";
5659 struct hist_field *field;
5662 seq_printf(m, "\n hist_data->%s[%d]:\n", vars_name, i);
5664 field = field_var->var;
5666 seq_printf(m, "\n %s[%d].var:\n", vars_name, i);
5668 hist_field_debug_show_flags(m, field->flags);
5669 seq_printf(m, " var.name: %s\n", field->var.name);
5670 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
5673 field = field_var->val;
5675 seq_printf(m, "\n %s[%d].val:\n", vars_name, i);
5677 seq_printf(m, " ftrace_event_field name: %s\n",
5678 field->field->name);
5684 seq_printf(m, " type: %s\n", field->type);
5685 seq_printf(m, " size: %u\n", field->size);
5686 seq_printf(m, " is_signed: %u\n", field->is_signed);
5691 static int hist_action_debug_show(struct seq_file *m,
5692 struct action_data *data, int i)
5696 if (data->handler == HANDLER_ONMAX ||
5697 data->handler == HANDLER_ONCHANGE) {
5698 seq_printf(m, "\n hist_data->actions[%d].track_data.var_ref:\n", i);
5699 ret = hist_field_debug_show(m, data->track_data.var_ref,
5700 HIST_FIELD_FL_VAR_REF);
5704 seq_printf(m, "\n hist_data->actions[%d].track_data.track_var:\n", i);
5705 ret = hist_field_debug_show(m, data->track_data.track_var,
5711 if (data->handler == HANDLER_ONMATCH) {
5712 seq_printf(m, "\n hist_data->actions[%d].match_data.event_system: %s\n",
5713 i, data->match_data.event_system);
5714 seq_printf(m, " hist_data->actions[%d].match_data.event: %s\n",
5715 i, data->match_data.event);
5721 static int hist_actions_debug_show(struct seq_file *m,
5722 struct hist_trigger_data *hist_data)
5726 if (hist_data->n_actions)
5727 seq_puts(m, "\n action tracking variables (for onmax()/onchange()/onmatch()):\n");
5729 for (i = 0; i < hist_data->n_actions; i++) {
5730 struct action_data *action = hist_data->actions[i];
5732 ret = hist_action_debug_show(m, action, i);
5737 if (hist_data->n_save_vars)
5738 seq_puts(m, "\n save action variables (save() params):\n");
5740 for (i = 0; i < hist_data->n_save_vars; i++) {
5741 ret = field_var_debug_show(m, hist_data->save_vars[i], i, true);
5749 static void hist_trigger_debug_show(struct seq_file *m,
5750 struct event_trigger_data *data, int n)
5752 struct hist_trigger_data *hist_data;
5756 seq_puts(m, "\n\n");
5758 seq_puts(m, "# event histogram\n#\n# trigger info: ");
5759 data->ops->print(m, data);
5760 seq_puts(m, "#\n\n");
5762 hist_data = data->private_data;
5764 seq_printf(m, "hist_data: %p\n\n", hist_data);
5765 seq_printf(m, " n_vals: %u\n", hist_data->n_vals);
5766 seq_printf(m, " n_keys: %u\n", hist_data->n_keys);
5767 seq_printf(m, " n_fields: %u\n", hist_data->n_fields);
5769 seq_puts(m, "\n val fields:\n\n");
5771 seq_puts(m, " hist_data->fields[0]:\n");
5772 ret = hist_field_debug_show(m, hist_data->fields[0],
5773 HIST_FIELD_FL_HITCOUNT);
5777 for (i = 1; i < hist_data->n_vals; i++) {
5778 seq_printf(m, "\n hist_data->fields[%d]:\n", i);
5779 ret = hist_field_debug_show(m, hist_data->fields[i], 0);
5784 seq_puts(m, "\n key fields:\n");
5786 for (i = hist_data->n_vals; i < hist_data->n_fields; i++) {
5787 seq_printf(m, "\n hist_data->fields[%d]:\n", i);
5788 ret = hist_field_debug_show(m, hist_data->fields[i],
5794 if (hist_data->n_var_refs)
5795 seq_puts(m, "\n variable reference fields:\n");
5797 for (i = 0; i < hist_data->n_var_refs; i++) {
5798 seq_printf(m, "\n hist_data->var_refs[%d]:\n", i);
5799 ret = hist_field_debug_show(m, hist_data->var_refs[i],
5800 HIST_FIELD_FL_VAR_REF);
5805 if (hist_data->n_field_vars)
5806 seq_puts(m, "\n field variables:\n");
5808 for (i = 0; i < hist_data->n_field_vars; i++) {
5809 ret = field_var_debug_show(m, hist_data->field_vars[i], i, false);
5814 ret = hist_actions_debug_show(m, hist_data);
5819 static int hist_debug_show(struct seq_file *m, void *v)
5821 struct event_trigger_data *data;
5822 struct trace_event_file *event_file;
5825 mutex_lock(&event_mutex);
5827 event_file = event_file_data(m->private);
5828 if (unlikely(!event_file)) {
5833 list_for_each_entry(data, &event_file->triggers, list) {
5834 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5835 hist_trigger_debug_show(m, data, n++);
5839 mutex_unlock(&event_mutex);
5844 static int event_hist_debug_open(struct inode *inode, struct file *file)
5848 ret = security_locked_down(LOCKDOWN_TRACEFS);
5852 return single_open(file, hist_debug_show, file);
5855 const struct file_operations event_hist_debug_fops = {
5856 .open = event_hist_debug_open,
5858 .llseek = seq_lseek,
5859 .release = single_release,
5863 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
5865 const char *field_name = hist_field_name(hist_field, 0);
5867 if (hist_field->var.name)
5868 seq_printf(m, "%s=", hist_field->var.name);
5870 if (hist_field->flags & HIST_FIELD_FL_CPU)
5871 seq_puts(m, "common_cpu");
5872 else if (hist_field->flags & HIST_FIELD_FL_CONST)
5873 seq_printf(m, "%llu", hist_field->constant);
5874 else if (field_name) {
5875 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
5876 hist_field->flags & HIST_FIELD_FL_ALIAS)
5878 seq_printf(m, "%s", field_name);
5879 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
5880 seq_puts(m, "common_timestamp");
5882 if (hist_field->flags) {
5883 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
5884 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
5885 const char *flags = get_hist_field_flags(hist_field);
5888 seq_printf(m, ".%s", flags);
5891 if (hist_field->buckets)
5892 seq_printf(m, "=%ld", hist_field->buckets);
5895 static int event_hist_trigger_print(struct seq_file *m,
5896 struct event_trigger_data *data)
5898 struct hist_trigger_data *hist_data = data->private_data;
5899 struct hist_field *field;
5900 bool have_var = false;
5901 bool show_val = false;
5904 seq_puts(m, HIST_PREFIX);
5907 seq_printf(m, "%s:", data->name);
5909 seq_puts(m, "keys=");
5911 for_each_hist_key_field(i, hist_data) {
5912 field = hist_data->fields[i];
5914 if (i > hist_data->n_vals)
5917 if (field->flags & HIST_FIELD_FL_STACKTRACE)
5918 seq_puts(m, "stacktrace");
5920 hist_field_print(m, field);
5923 seq_puts(m, ":vals=");
5925 for_each_hist_val_field(i, hist_data) {
5926 field = hist_data->fields[i];
5927 if (field->flags & HIST_FIELD_FL_VAR) {
5932 if (i == HITCOUNT_IDX) {
5933 if (hist_data->attrs->no_hitcount)
5935 seq_puts(m, "hitcount");
5939 hist_field_print(m, field);
5949 for_each_hist_val_field(i, hist_data) {
5950 field = hist_data->fields[i];
5952 if (field->flags & HIST_FIELD_FL_VAR) {
5955 hist_field_print(m, field);
5960 seq_puts(m, ":sort=");
5962 for (i = 0; i < hist_data->n_sort_keys; i++) {
5963 struct tracing_map_sort_key *sort_key;
5964 unsigned int idx, first_key_idx;
5967 first_key_idx = hist_data->n_vals - hist_data->n_vars;
5969 sort_key = &hist_data->sort_keys[i];
5970 idx = sort_key->field_idx;
5972 if (WARN_ON(idx >= HIST_FIELDS_MAX))
5978 if (idx == HITCOUNT_IDX)
5979 seq_puts(m, "hitcount");
5981 if (idx >= first_key_idx)
5982 idx += hist_data->n_vars;
5983 hist_field_print(m, hist_data->fields[idx]);
5986 if (sort_key->descending)
5987 seq_puts(m, ".descending");
5989 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
5990 if (hist_data->enable_timestamps)
5991 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
5992 if (hist_data->attrs->no_hitcount)
5993 seq_puts(m, ":nohitcount");
5995 print_actions_spec(m, hist_data);
5997 if (data->filter_str)
5998 seq_printf(m, " if %s", data->filter_str);
6001 seq_puts(m, " [paused]");
6003 seq_puts(m, " [active]");
6010 static int event_hist_trigger_init(struct event_trigger_data *data)
6012 struct hist_trigger_data *hist_data = data->private_data;
6014 if (!data->ref && hist_data->attrs->name)
6015 save_named_trigger(hist_data->attrs->name, data);
6022 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
6024 struct trace_event_file *file;
6029 for (i = 0; i < hist_data->n_field_var_hists; i++) {
6030 file = hist_data->field_var_hists[i]->hist_data->event_file;
6031 cmd = hist_data->field_var_hists[i]->cmd;
6032 ret = event_hist_trigger_parse(&trigger_hist_cmd, file,
6033 "!hist", "hist", cmd);
6034 WARN_ON_ONCE(ret < 0);
6038 static void event_hist_trigger_free(struct event_trigger_data *data)
6040 struct hist_trigger_data *hist_data = data->private_data;
6042 if (WARN_ON_ONCE(data->ref <= 0))
6048 del_named_trigger(data);
6050 trigger_data_free(data);
6052 remove_hist_vars(hist_data);
6054 unregister_field_var_hists(hist_data);
6056 destroy_hist_data(hist_data);
6060 static struct event_trigger_ops event_hist_trigger_ops = {
6061 .trigger = event_hist_trigger,
6062 .print = event_hist_trigger_print,
6063 .init = event_hist_trigger_init,
6064 .free = event_hist_trigger_free,
6067 static int event_hist_trigger_named_init(struct event_trigger_data *data)
6071 save_named_trigger(data->named_data->name, data);
6073 event_hist_trigger_init(data->named_data);
6078 static void event_hist_trigger_named_free(struct event_trigger_data *data)
6080 if (WARN_ON_ONCE(data->ref <= 0))
6083 event_hist_trigger_free(data->named_data);
6087 del_named_trigger(data);
6088 trigger_data_free(data);
6092 static struct event_trigger_ops event_hist_trigger_named_ops = {
6093 .trigger = event_hist_trigger,
6094 .print = event_hist_trigger_print,
6095 .init = event_hist_trigger_named_init,
6096 .free = event_hist_trigger_named_free,
6099 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
6102 return &event_hist_trigger_ops;
6105 static void hist_clear(struct event_trigger_data *data)
6107 struct hist_trigger_data *hist_data = data->private_data;
6110 pause_named_trigger(data);
6112 tracepoint_synchronize_unregister();
6114 tracing_map_clear(hist_data->map);
6117 unpause_named_trigger(data);
6120 static bool compatible_field(struct ftrace_event_field *field,
6121 struct ftrace_event_field *test_field)
6123 if (field == test_field)
6125 if (field == NULL || test_field == NULL)
6127 if (strcmp(field->name, test_field->name) != 0)
6129 if (strcmp(field->type, test_field->type) != 0)
6131 if (field->size != test_field->size)
6133 if (field->is_signed != test_field->is_signed)
6139 static bool hist_trigger_match(struct event_trigger_data *data,
6140 struct event_trigger_data *data_test,
6141 struct event_trigger_data *named_data,
6144 struct tracing_map_sort_key *sort_key, *sort_key_test;
6145 struct hist_trigger_data *hist_data, *hist_data_test;
6146 struct hist_field *key_field, *key_field_test;
6149 if (named_data && (named_data != data_test) &&
6150 (named_data != data_test->named_data))
6153 if (!named_data && is_named_trigger(data_test))
6156 hist_data = data->private_data;
6157 hist_data_test = data_test->private_data;
6159 if (hist_data->n_vals != hist_data_test->n_vals ||
6160 hist_data->n_fields != hist_data_test->n_fields ||
6161 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
6164 if (!ignore_filter) {
6165 if ((data->filter_str && !data_test->filter_str) ||
6166 (!data->filter_str && data_test->filter_str))
6170 for_each_hist_field(i, hist_data) {
6171 key_field = hist_data->fields[i];
6172 key_field_test = hist_data_test->fields[i];
6174 if (key_field->flags != key_field_test->flags)
6176 if (!compatible_field(key_field->field, key_field_test->field))
6178 if (key_field->offset != key_field_test->offset)
6180 if (key_field->size != key_field_test->size)
6182 if (key_field->is_signed != key_field_test->is_signed)
6184 if (!!key_field->var.name != !!key_field_test->var.name)
6186 if (key_field->var.name &&
6187 strcmp(key_field->var.name, key_field_test->var.name) != 0)
6191 for (i = 0; i < hist_data->n_sort_keys; i++) {
6192 sort_key = &hist_data->sort_keys[i];
6193 sort_key_test = &hist_data_test->sort_keys[i];
6195 if (sort_key->field_idx != sort_key_test->field_idx ||
6196 sort_key->descending != sort_key_test->descending)
6200 if (!ignore_filter && data->filter_str &&
6201 (strcmp(data->filter_str, data_test->filter_str) != 0))
6204 if (!actions_match(hist_data, hist_data_test))
6210 static bool existing_hist_update_only(char *glob,
6211 struct event_trigger_data *data,
6212 struct trace_event_file *file)
6214 struct hist_trigger_data *hist_data = data->private_data;
6215 struct event_trigger_data *test, *named_data = NULL;
6216 bool updated = false;
6218 if (!hist_data->attrs->pause && !hist_data->attrs->cont &&
6219 !hist_data->attrs->clear)
6222 if (hist_data->attrs->name) {
6223 named_data = find_named_trigger(hist_data->attrs->name);
6225 if (!hist_trigger_match(data, named_data, named_data,
6231 if (hist_data->attrs->name && !named_data)
6234 list_for_each_entry(test, &file->triggers, list) {
6235 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6236 if (!hist_trigger_match(data, test, named_data, false))
6238 if (hist_data->attrs->pause)
6239 test->paused = true;
6240 else if (hist_data->attrs->cont)
6241 test->paused = false;
6242 else if (hist_data->attrs->clear)
6252 static int hist_register_trigger(char *glob,
6253 struct event_trigger_data *data,
6254 struct trace_event_file *file)
6256 struct hist_trigger_data *hist_data = data->private_data;
6257 struct event_trigger_data *test, *named_data = NULL;
6258 struct trace_array *tr = file->tr;
6261 if (hist_data->attrs->name) {
6262 named_data = find_named_trigger(hist_data->attrs->name);
6264 if (!hist_trigger_match(data, named_data, named_data,
6266 hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name));
6273 if (hist_data->attrs->name && !named_data)
6276 lockdep_assert_held(&event_mutex);
6278 list_for_each_entry(test, &file->triggers, list) {
6279 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6280 if (hist_trigger_match(data, test, named_data, false)) {
6281 hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
6288 if (hist_data->attrs->cont || hist_data->attrs->clear) {
6289 hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0);
6294 if (hist_data->attrs->pause)
6295 data->paused = true;
6298 data->private_data = named_data->private_data;
6299 set_named_trigger_data(data, named_data);
6300 data->ops = &event_hist_trigger_named_ops;
6303 if (data->ops->init) {
6304 ret = data->ops->init(data);
6309 if (hist_data->enable_timestamps) {
6310 char *clock = hist_data->attrs->clock;
6312 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
6314 hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock));
6318 tracing_set_filter_buffering(file->tr, true);
6322 destroy_hist_data(hist_data);
6327 static int hist_trigger_enable(struct event_trigger_data *data,
6328 struct trace_event_file *file)
6332 list_add_tail_rcu(&data->list, &file->triggers);
6334 update_cond_flag(file);
6336 if (trace_event_trigger_enable_disable(file, 1) < 0) {
6337 list_del_rcu(&data->list);
6338 update_cond_flag(file);
6345 static bool have_hist_trigger_match(struct event_trigger_data *data,
6346 struct trace_event_file *file)
6348 struct hist_trigger_data *hist_data = data->private_data;
6349 struct event_trigger_data *test, *named_data = NULL;
6352 lockdep_assert_held(&event_mutex);
6354 if (hist_data->attrs->name)
6355 named_data = find_named_trigger(hist_data->attrs->name);
6357 list_for_each_entry(test, &file->triggers, list) {
6358 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6359 if (hist_trigger_match(data, test, named_data, false)) {
6369 static bool hist_trigger_check_refs(struct event_trigger_data *data,
6370 struct trace_event_file *file)
6372 struct hist_trigger_data *hist_data = data->private_data;
6373 struct event_trigger_data *test, *named_data = NULL;
6375 lockdep_assert_held(&event_mutex);
6377 if (hist_data->attrs->name)
6378 named_data = find_named_trigger(hist_data->attrs->name);
6380 list_for_each_entry(test, &file->triggers, list) {
6381 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6382 if (!hist_trigger_match(data, test, named_data, false))
6384 hist_data = test->private_data;
6385 if (check_var_refs(hist_data))
6394 static void hist_unregister_trigger(char *glob,
6395 struct event_trigger_data *data,
6396 struct trace_event_file *file)
6398 struct event_trigger_data *test = NULL, *iter, *named_data = NULL;
6399 struct hist_trigger_data *hist_data = data->private_data;
6401 lockdep_assert_held(&event_mutex);
6403 if (hist_data->attrs->name)
6404 named_data = find_named_trigger(hist_data->attrs->name);
6406 list_for_each_entry(iter, &file->triggers, list) {
6407 if (iter->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6408 if (!hist_trigger_match(data, iter, named_data, false))
6411 list_del_rcu(&test->list);
6412 trace_event_trigger_enable_disable(file, 0);
6413 update_cond_flag(file);
6418 if (test && test->ops->free)
6419 test->ops->free(test);
6421 if (hist_data->enable_timestamps) {
6422 if (!hist_data->remove || test)
6423 tracing_set_filter_buffering(file->tr, false);
6427 static bool hist_file_check_refs(struct trace_event_file *file)
6429 struct hist_trigger_data *hist_data;
6430 struct event_trigger_data *test;
6432 lockdep_assert_held(&event_mutex);
6434 list_for_each_entry(test, &file->triggers, list) {
6435 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6436 hist_data = test->private_data;
6437 if (check_var_refs(hist_data))
6445 static void hist_unreg_all(struct trace_event_file *file)
6447 struct event_trigger_data *test, *n;
6448 struct hist_trigger_data *hist_data;
6449 struct synth_event *se;
6450 const char *se_name;
6452 lockdep_assert_held(&event_mutex);
6454 if (hist_file_check_refs(file))
6457 list_for_each_entry_safe(test, n, &file->triggers, list) {
6458 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6459 hist_data = test->private_data;
6460 list_del_rcu(&test->list);
6461 trace_event_trigger_enable_disable(file, 0);
6463 se_name = trace_event_name(file->event_call);
6464 se = find_synth_event(se_name);
6468 update_cond_flag(file);
6469 if (hist_data->enable_timestamps)
6470 tracing_set_filter_buffering(file->tr, false);
6471 if (test->ops->free)
6472 test->ops->free(test);
6477 static int event_hist_trigger_parse(struct event_command *cmd_ops,
6478 struct trace_event_file *file,
6479 char *glob, char *cmd,
6480 char *param_and_filter)
6482 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
6483 struct event_trigger_data *trigger_data;
6484 struct hist_trigger_attrs *attrs;
6485 struct hist_trigger_data *hist_data;
6486 char *param, *filter, *p, *start;
6487 struct synth_event *se;
6488 const char *se_name;
6492 lockdep_assert_held(&event_mutex);
6499 last_cmd_set(file, param_and_filter);
6502 remove = event_trigger_check_remove(glob);
6504 if (event_trigger_empty_param(param_and_filter))
6508 * separate the trigger from the filter (k:v [if filter])
6509 * allowing for whitespace in the trigger
6511 p = param = param_and_filter;
6513 p = strstr(p, "if");
6516 if (p == param_and_filter)
6518 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
6522 if (p >= param_and_filter + strlen(param_and_filter) - (sizeof("if") - 1) - 1)
6524 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
6535 filter = strstrip(p);
6536 param = strstrip(param);
6540 * To simplify arithmetic expression parsing, replace occurrences of
6541 * '.sym-offset' modifier with '.symXoffset'
6543 start = strstr(param, ".sym-offset");
6546 start = strstr(start + 11, ".sym-offset");
6549 attrs = parse_hist_trigger_attrs(file->tr, param);
6551 return PTR_ERR(attrs);
6553 if (attrs->map_bits)
6554 hist_trigger_bits = attrs->map_bits;
6556 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
6557 if (IS_ERR(hist_data)) {
6558 destroy_hist_trigger_attrs(attrs);
6559 return PTR_ERR(hist_data);
6562 trigger_data = event_trigger_alloc(cmd_ops, cmd, param, hist_data);
6563 if (!trigger_data) {
6568 ret = event_trigger_set_filter(cmd_ops, file, filter, trigger_data);
6573 if (!have_hist_trigger_match(trigger_data, file))
6576 if (hist_trigger_check_refs(trigger_data, file)) {
6581 event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
6582 se_name = trace_event_name(file->event_call);
6583 se = find_synth_event(se_name);
6590 if (existing_hist_update_only(glob, trigger_data, file))
6593 ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
6597 if (get_named_trigger_data(trigger_data))
6600 if (has_hist_vars(hist_data))
6601 save_hist_vars(hist_data);
6603 ret = create_actions(hist_data);
6607 ret = tracing_map_init(hist_data->map);
6611 ret = hist_trigger_enable(trigger_data, file);
6615 se_name = trace_event_name(file->event_call);
6616 se = find_synth_event(se_name);
6620 if (ret == 0 && glob[0])
6625 event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
6627 event_trigger_reset_filter(cmd_ops, trigger_data);
6629 remove_hist_vars(hist_data);
6631 kfree(trigger_data);
6633 destroy_hist_data(hist_data);
6637 static struct event_command trigger_hist_cmd = {
6639 .trigger_type = ETT_EVENT_HIST,
6640 .flags = EVENT_CMD_FL_NEEDS_REC,
6641 .parse = event_hist_trigger_parse,
6642 .reg = hist_register_trigger,
6643 .unreg = hist_unregister_trigger,
6644 .unreg_all = hist_unreg_all,
6645 .get_trigger_ops = event_hist_get_trigger_ops,
6646 .set_filter = set_trigger_filter,
6649 __init int register_trigger_hist_cmd(void)
6653 ret = register_event_command(&trigger_hist_cmd);
6660 hist_enable_trigger(struct event_trigger_data *data,
6661 struct trace_buffer *buffer, void *rec,
6662 struct ring_buffer_event *event)
6664 struct enable_trigger_data *enable_data = data->private_data;
6665 struct event_trigger_data *test;
6667 list_for_each_entry_rcu(test, &enable_data->file->triggers, list,
6668 lockdep_is_held(&event_mutex)) {
6669 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6670 if (enable_data->enable)
6671 test->paused = false;
6673 test->paused = true;
6679 hist_enable_count_trigger(struct event_trigger_data *data,
6680 struct trace_buffer *buffer, void *rec,
6681 struct ring_buffer_event *event)
6686 if (data->count != -1)
6689 hist_enable_trigger(data, buffer, rec, event);
6692 static struct event_trigger_ops hist_enable_trigger_ops = {
6693 .trigger = hist_enable_trigger,
6694 .print = event_enable_trigger_print,
6695 .init = event_trigger_init,
6696 .free = event_enable_trigger_free,
6699 static struct event_trigger_ops hist_enable_count_trigger_ops = {
6700 .trigger = hist_enable_count_trigger,
6701 .print = event_enable_trigger_print,
6702 .init = event_trigger_init,
6703 .free = event_enable_trigger_free,
6706 static struct event_trigger_ops hist_disable_trigger_ops = {
6707 .trigger = hist_enable_trigger,
6708 .print = event_enable_trigger_print,
6709 .init = event_trigger_init,
6710 .free = event_enable_trigger_free,
6713 static struct event_trigger_ops hist_disable_count_trigger_ops = {
6714 .trigger = hist_enable_count_trigger,
6715 .print = event_enable_trigger_print,
6716 .init = event_trigger_init,
6717 .free = event_enable_trigger_free,
6720 static struct event_trigger_ops *
6721 hist_enable_get_trigger_ops(char *cmd, char *param)
6723 struct event_trigger_ops *ops;
6726 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
6729 ops = param ? &hist_enable_count_trigger_ops :
6730 &hist_enable_trigger_ops;
6732 ops = param ? &hist_disable_count_trigger_ops :
6733 &hist_disable_trigger_ops;
6738 static void hist_enable_unreg_all(struct trace_event_file *file)
6740 struct event_trigger_data *test, *n;
6742 list_for_each_entry_safe(test, n, &file->triggers, list) {
6743 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
6744 list_del_rcu(&test->list);
6745 update_cond_flag(file);
6746 trace_event_trigger_enable_disable(file, 0);
6747 if (test->ops->free)
6748 test->ops->free(test);
6753 static struct event_command trigger_hist_enable_cmd = {
6754 .name = ENABLE_HIST_STR,
6755 .trigger_type = ETT_HIST_ENABLE,
6756 .parse = event_enable_trigger_parse,
6757 .reg = event_enable_register_trigger,
6758 .unreg = event_enable_unregister_trigger,
6759 .unreg_all = hist_enable_unreg_all,
6760 .get_trigger_ops = hist_enable_get_trigger_ops,
6761 .set_filter = set_trigger_filter,
6764 static struct event_command trigger_hist_disable_cmd = {
6765 .name = DISABLE_HIST_STR,
6766 .trigger_type = ETT_HIST_ENABLE,
6767 .parse = event_enable_trigger_parse,
6768 .reg = event_enable_register_trigger,
6769 .unreg = event_enable_unregister_trigger,
6770 .unreg_all = hist_enable_unreg_all,
6771 .get_trigger_ops = hist_enable_get_trigger_ops,
6772 .set_filter = set_trigger_filter,
6775 static __init void unregister_trigger_hist_enable_disable_cmds(void)
6777 unregister_event_command(&trigger_hist_enable_cmd);
6778 unregister_event_command(&trigger_hist_disable_cmd);
6781 __init int register_trigger_hist_enable_disable_cmds(void)
6785 ret = register_event_command(&trigger_hist_enable_cmd);
6786 if (WARN_ON(ret < 0))
6788 ret = register_event_command(&trigger_hist_disable_cmd);
6789 if (WARN_ON(ret < 0))
6790 unregister_trigger_hist_enable_disable_cmds();