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,
142 * A hist_var (histogram variable) contains variable information for
143 * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF
144 * flag set. A hist_var has a variable name e.g. ts0, and is
145 * associated with a given histogram trigger, as specified by
146 * hist_data. The hist_var idx is the unique index assigned to the
147 * variable by the hist trigger's tracing_map. The idx is what is
148 * used to set a variable's value and, by a variable reference, to
153 struct hist_trigger_data *hist_data;
158 struct ftrace_event_field *field;
160 unsigned long buckets;
162 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
163 struct hist_trigger_data *hist_data;
164 enum hist_field_fn fn_num;
168 unsigned int is_signed;
171 * Variable fields contain variable-specific info in var.
174 enum field_op_id operator;
179 * The name field is used for EXPR and VAR_REF fields. VAR
180 * fields contain the variable name in var.name.
185 * When a histogram trigger is hit, if it has any references
186 * to variables, the values of those variables are collected
187 * into a var_ref_vals array by resolve_var_refs(). The
188 * current value of each variable is read from the tracing_map
189 * using the hist field's hist_var.idx and entered into the
190 * var_ref_idx entry i.e. var_ref_vals[var_ref_idx].
192 unsigned int var_ref_idx;
195 unsigned int var_str_idx;
197 /* Numeric literals are represented as u64 */
199 /* Used to optimize division by constants */
203 static u64 hist_fn_call(struct hist_field *hist_field,
204 struct tracing_map_elt *elt,
205 struct trace_buffer *buffer,
206 struct ring_buffer_event *rbe,
209 static u64 hist_field_const(struct hist_field *field,
210 struct tracing_map_elt *elt,
211 struct trace_buffer *buffer,
212 struct ring_buffer_event *rbe,
215 return field->constant;
218 static u64 hist_field_counter(struct hist_field *field,
219 struct tracing_map_elt *elt,
220 struct trace_buffer *buffer,
221 struct ring_buffer_event *rbe,
227 static u64 hist_field_string(struct hist_field *hist_field,
228 struct tracing_map_elt *elt,
229 struct trace_buffer *buffer,
230 struct ring_buffer_event *rbe,
233 char *addr = (char *)(event + hist_field->field->offset);
235 return (u64)(unsigned long)addr;
238 static u64 hist_field_dynstring(struct hist_field *hist_field,
239 struct tracing_map_elt *elt,
240 struct trace_buffer *buffer,
241 struct ring_buffer_event *rbe,
244 u32 str_item = *(u32 *)(event + hist_field->field->offset);
245 int str_loc = str_item & 0xffff;
246 char *addr = (char *)(event + str_loc);
248 return (u64)(unsigned long)addr;
251 static u64 hist_field_reldynstring(struct hist_field *hist_field,
252 struct tracing_map_elt *elt,
253 struct trace_buffer *buffer,
254 struct ring_buffer_event *rbe,
257 u32 *item = event + hist_field->field->offset;
258 u32 str_item = *item;
259 int str_loc = str_item & 0xffff;
260 char *addr = (char *)&item[1] + str_loc;
262 return (u64)(unsigned long)addr;
265 static u64 hist_field_pstring(struct hist_field *hist_field,
266 struct tracing_map_elt *elt,
267 struct trace_buffer *buffer,
268 struct ring_buffer_event *rbe,
271 char **addr = (char **)(event + hist_field->field->offset);
273 return (u64)(unsigned long)*addr;
276 static u64 hist_field_log2(struct hist_field *hist_field,
277 struct tracing_map_elt *elt,
278 struct trace_buffer *buffer,
279 struct ring_buffer_event *rbe,
282 struct hist_field *operand = hist_field->operands[0];
284 u64 val = hist_fn_call(operand, elt, buffer, rbe, event);
286 return (u64) ilog2(roundup_pow_of_two(val));
289 static u64 hist_field_bucket(struct hist_field *hist_field,
290 struct tracing_map_elt *elt,
291 struct trace_buffer *buffer,
292 struct ring_buffer_event *rbe,
295 struct hist_field *operand = hist_field->operands[0];
296 unsigned long buckets = hist_field->buckets;
298 u64 val = hist_fn_call(operand, elt, buffer, rbe, event);
300 if (WARN_ON_ONCE(!buckets))
304 val = div64_ul(val, buckets);
306 val = (u64)((unsigned long)val / buckets);
307 return val * buckets;
310 static u64 hist_field_plus(struct hist_field *hist_field,
311 struct tracing_map_elt *elt,
312 struct trace_buffer *buffer,
313 struct ring_buffer_event *rbe,
316 struct hist_field *operand1 = hist_field->operands[0];
317 struct hist_field *operand2 = hist_field->operands[1];
319 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
320 u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
325 static u64 hist_field_minus(struct hist_field *hist_field,
326 struct tracing_map_elt *elt,
327 struct trace_buffer *buffer,
328 struct ring_buffer_event *rbe,
331 struct hist_field *operand1 = hist_field->operands[0];
332 struct hist_field *operand2 = hist_field->operands[1];
334 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
335 u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
340 static u64 hist_field_div(struct hist_field *hist_field,
341 struct tracing_map_elt *elt,
342 struct trace_buffer *buffer,
343 struct ring_buffer_event *rbe,
346 struct hist_field *operand1 = hist_field->operands[0];
347 struct hist_field *operand2 = hist_field->operands[1];
349 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
350 u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
352 /* Return -1 for the undefined case */
356 /* Use shift if the divisor is a power of 2 */
357 if (!(val2 & (val2 - 1)))
358 return val1 >> __ffs64(val2);
360 return div64_u64(val1, val2);
363 static u64 div_by_power_of_two(struct hist_field *hist_field,
364 struct tracing_map_elt *elt,
365 struct trace_buffer *buffer,
366 struct ring_buffer_event *rbe,
369 struct hist_field *operand1 = hist_field->operands[0];
370 struct hist_field *operand2 = hist_field->operands[1];
372 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
374 return val1 >> __ffs64(operand2->constant);
377 static u64 div_by_not_power_of_two(struct hist_field *hist_field,
378 struct tracing_map_elt *elt,
379 struct trace_buffer *buffer,
380 struct ring_buffer_event *rbe,
383 struct hist_field *operand1 = hist_field->operands[0];
384 struct hist_field *operand2 = hist_field->operands[1];
386 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
388 return div64_u64(val1, operand2->constant);
391 static u64 div_by_mult_and_shift(struct hist_field *hist_field,
392 struct tracing_map_elt *elt,
393 struct trace_buffer *buffer,
394 struct ring_buffer_event *rbe,
397 struct hist_field *operand1 = hist_field->operands[0];
398 struct hist_field *operand2 = hist_field->operands[1];
400 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
403 * If the divisor is a constant, do a multiplication and shift instead.
405 * Choose Z = some power of 2. If Y <= Z, then:
406 * X / Y = (X * (Z / Y)) / Z
408 * (Z / Y) is a constant (mult) which is calculated at parse time, so:
409 * X / Y = (X * mult) / Z
411 * The division by Z can be replaced by a shift since Z is a power of 2:
412 * X / Y = (X * mult) >> HIST_DIV_SHIFT
414 * As long, as X < Z the results will not be off by more than 1.
416 if (val1 < (1 << HIST_DIV_SHIFT)) {
417 u64 mult = operand2->div_multiplier;
419 return (val1 * mult + ((1 << HIST_DIV_SHIFT) - 1)) >> HIST_DIV_SHIFT;
422 return div64_u64(val1, operand2->constant);
425 static u64 hist_field_mult(struct hist_field *hist_field,
426 struct tracing_map_elt *elt,
427 struct trace_buffer *buffer,
428 struct ring_buffer_event *rbe,
431 struct hist_field *operand1 = hist_field->operands[0];
432 struct hist_field *operand2 = hist_field->operands[1];
434 u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
435 u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
440 static u64 hist_field_unary_minus(struct hist_field *hist_field,
441 struct tracing_map_elt *elt,
442 struct trace_buffer *buffer,
443 struct ring_buffer_event *rbe,
446 struct hist_field *operand = hist_field->operands[0];
448 s64 sval = (s64)hist_fn_call(operand, elt, buffer, rbe, event);
449 u64 val = (u64)-sval;
454 #define DEFINE_HIST_FIELD_FN(type) \
455 static u64 hist_field_##type(struct hist_field *hist_field, \
456 struct tracing_map_elt *elt, \
457 struct trace_buffer *buffer, \
458 struct ring_buffer_event *rbe, \
461 type *addr = (type *)(event + hist_field->field->offset); \
463 return (u64)(unsigned long)*addr; \
466 DEFINE_HIST_FIELD_FN(s64);
467 DEFINE_HIST_FIELD_FN(u64);
468 DEFINE_HIST_FIELD_FN(s32);
469 DEFINE_HIST_FIELD_FN(u32);
470 DEFINE_HIST_FIELD_FN(s16);
471 DEFINE_HIST_FIELD_FN(u16);
472 DEFINE_HIST_FIELD_FN(s8);
473 DEFINE_HIST_FIELD_FN(u8);
475 #define for_each_hist_field(i, hist_data) \
476 for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
478 #define for_each_hist_val_field(i, hist_data) \
479 for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
481 #define for_each_hist_key_field(i, hist_data) \
482 for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
484 #define HITCOUNT_IDX 0
485 #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
487 enum hist_field_flags {
488 HIST_FIELD_FL_HITCOUNT = 1 << 0,
489 HIST_FIELD_FL_KEY = 1 << 1,
490 HIST_FIELD_FL_STRING = 1 << 2,
491 HIST_FIELD_FL_HEX = 1 << 3,
492 HIST_FIELD_FL_SYM = 1 << 4,
493 HIST_FIELD_FL_SYM_OFFSET = 1 << 5,
494 HIST_FIELD_FL_EXECNAME = 1 << 6,
495 HIST_FIELD_FL_SYSCALL = 1 << 7,
496 HIST_FIELD_FL_STACKTRACE = 1 << 8,
497 HIST_FIELD_FL_LOG2 = 1 << 9,
498 HIST_FIELD_FL_TIMESTAMP = 1 << 10,
499 HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11,
500 HIST_FIELD_FL_VAR = 1 << 12,
501 HIST_FIELD_FL_EXPR = 1 << 13,
502 HIST_FIELD_FL_VAR_REF = 1 << 14,
503 HIST_FIELD_FL_CPU = 1 << 15,
504 HIST_FIELD_FL_ALIAS = 1 << 16,
505 HIST_FIELD_FL_BUCKET = 1 << 17,
506 HIST_FIELD_FL_CONST = 1 << 18,
507 HIST_FIELD_FL_PERCENT = 1 << 19,
508 HIST_FIELD_FL_GRAPH = 1 << 20,
513 char *name[TRACING_MAP_VARS_MAX];
514 char *expr[TRACING_MAP_VARS_MAX];
517 struct hist_trigger_attrs {
528 unsigned int map_bits;
530 char *assignment_str[TRACING_MAP_VARS_MAX];
531 unsigned int n_assignments;
533 char *action_str[HIST_ACTIONS_MAX];
534 unsigned int n_actions;
536 struct var_defs var_defs;
540 struct hist_field *var;
541 struct hist_field *val;
544 struct field_var_hist {
545 struct hist_trigger_data *hist_data;
549 struct hist_trigger_data {
550 struct hist_field *fields[HIST_FIELDS_MAX];
553 unsigned int n_fields;
555 unsigned int n_var_str;
556 unsigned int key_size;
557 struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX];
558 unsigned int n_sort_keys;
559 struct trace_event_file *event_file;
560 struct hist_trigger_attrs *attrs;
561 struct tracing_map *map;
562 bool enable_timestamps;
564 struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
565 unsigned int n_var_refs;
567 struct action_data *actions[HIST_ACTIONS_MAX];
568 unsigned int n_actions;
570 struct field_var *field_vars[SYNTH_FIELDS_MAX];
571 unsigned int n_field_vars;
572 unsigned int n_field_var_str;
573 struct field_var_hist *field_var_hists[SYNTH_FIELDS_MAX];
574 unsigned int n_field_var_hists;
576 struct field_var *save_vars[SYNTH_FIELDS_MAX];
577 unsigned int n_save_vars;
578 unsigned int n_save_var_str;
583 typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
584 struct tracing_map_elt *elt,
585 struct trace_buffer *buffer, void *rec,
586 struct ring_buffer_event *rbe, void *key,
587 struct action_data *data, u64 *var_ref_vals);
589 typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val);
604 enum handler_id handler;
605 enum action_id action;
609 unsigned int n_params;
610 char *params[SYNTH_FIELDS_MAX];
613 * When a histogram trigger is hit, the values of any
614 * references to variables, including variables being passed
615 * as parameters to synthetic events, are collected into a
616 * var_ref_vals array. This var_ref_idx array is an array of
617 * indices into the var_ref_vals array, one for each synthetic
618 * event param, and is passed to the synthetic event
621 unsigned int var_ref_idx[SYNTH_FIELDS_MAX];
622 struct synth_event *synth_event;
623 bool use_trace_keyword;
624 char *synth_event_name;
634 * var_str contains the $-unstripped variable
635 * name referenced by var_ref, and used when
636 * printing the action. Because var_ref
637 * creation is deferred to create_actions(),
638 * we need a per-action way to save it until
639 * then, thus var_str.
644 * var_ref refers to the variable being
645 * tracked e.g onmax($var).
647 struct hist_field *var_ref;
650 * track_var contains the 'invisible' tracking
651 * variable created to keep the current
654 struct hist_field *track_var;
656 check_track_val_fn_t check_val;
657 action_fn_t save_data;
666 unsigned int key_len;
668 struct tracing_map_elt elt;
670 struct action_data *action_data;
671 struct hist_trigger_data *hist_data;
674 struct hist_elt_data {
677 char **field_var_str;
681 struct snapshot_context {
682 struct tracing_map_elt *elt;
687 * Returns the specific division function to use if the divisor
688 * is constant. This avoids extra branches when the trigger is hit.
690 static enum hist_field_fn hist_field_get_div_fn(struct hist_field *divisor)
692 u64 div = divisor->constant;
694 if (!(div & (div - 1)))
695 return HIST_FIELD_FN_DIV_POWER2;
697 /* If the divisor is too large, do a regular division */
698 if (div > (1 << HIST_DIV_SHIFT))
699 return HIST_FIELD_FN_DIV_NOT_POWER2;
701 divisor->div_multiplier = div64_u64((u64)(1 << HIST_DIV_SHIFT), div);
702 return HIST_FIELD_FN_DIV_MULT_SHIFT;
705 static void track_data_free(struct track_data *track_data)
707 struct hist_elt_data *elt_data;
712 kfree(track_data->key);
714 elt_data = track_data->elt.private_data;
716 kfree(elt_data->comm);
723 static struct track_data *track_data_alloc(unsigned int key_len,
724 struct action_data *action_data,
725 struct hist_trigger_data *hist_data)
727 struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
728 struct hist_elt_data *elt_data;
731 return ERR_PTR(-ENOMEM);
733 data->key = kzalloc(key_len, GFP_KERNEL);
735 track_data_free(data);
736 return ERR_PTR(-ENOMEM);
739 data->key_len = key_len;
740 data->action_data = action_data;
741 data->hist_data = hist_data;
743 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
745 track_data_free(data);
746 return ERR_PTR(-ENOMEM);
749 data->elt.private_data = elt_data;
751 elt_data->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL);
752 if (!elt_data->comm) {
753 track_data_free(data);
754 return ERR_PTR(-ENOMEM);
760 #define HIST_PREFIX "hist:"
762 static char *last_cmd;
763 static char last_cmd_loc[MAX_FILTER_STR_VAL];
765 static int errpos(char *str)
767 if (!str || !last_cmd)
770 return err_pos(last_cmd, str);
773 static void last_cmd_set(struct trace_event_file *file, char *str)
775 const char *system = NULL, *name = NULL;
776 struct trace_event_call *call;
782 /* sizeof() contains the nul byte */
783 len = sizeof(HIST_PREFIX) + strlen(str);
785 last_cmd = kzalloc(len, GFP_KERNEL);
789 strcpy(last_cmd, HIST_PREFIX);
790 /* Again, sizeof() contains the nul byte */
791 len -= sizeof(HIST_PREFIX);
792 strncat(last_cmd, str, len);
795 call = file->event_call;
796 system = call->class->system;
798 name = trace_event_name(call);
805 snprintf(last_cmd_loc, MAX_FILTER_STR_VAL, HIST_PREFIX "%s:%s", system, name);
808 static void hist_err(struct trace_array *tr, u8 err_type, u16 err_pos)
813 tracing_log_err(tr, last_cmd_loc, last_cmd, err_text,
817 static void hist_err_clear(void)
821 last_cmd_loc[0] = '\0';
824 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
825 unsigned int *var_ref_idx);
827 static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
828 unsigned int *var_ref_idx)
830 struct tracepoint *tp = event->tp;
832 if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
833 struct tracepoint_func *probe_func_ptr;
834 synth_probe_func_t probe_func;
837 if (!(cpu_online(raw_smp_processor_id())))
840 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
841 if (probe_func_ptr) {
843 probe_func = probe_func_ptr->func;
844 __data = probe_func_ptr->data;
845 probe_func(__data, var_ref_vals, var_ref_idx);
846 } while ((++probe_func_ptr)->func);
851 static void action_trace(struct hist_trigger_data *hist_data,
852 struct tracing_map_elt *elt,
853 struct trace_buffer *buffer, void *rec,
854 struct ring_buffer_event *rbe, void *key,
855 struct action_data *data, u64 *var_ref_vals)
857 struct synth_event *event = data->synth_event;
859 trace_synth(event, var_ref_vals, data->var_ref_idx);
862 struct hist_var_data {
863 struct list_head list;
864 struct hist_trigger_data *hist_data;
867 static u64 hist_field_timestamp(struct hist_field *hist_field,
868 struct tracing_map_elt *elt,
869 struct trace_buffer *buffer,
870 struct ring_buffer_event *rbe,
873 struct hist_trigger_data *hist_data = hist_field->hist_data;
874 struct trace_array *tr = hist_data->event_file->tr;
876 u64 ts = ring_buffer_event_time_stamp(buffer, rbe);
878 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
884 static u64 hist_field_cpu(struct hist_field *hist_field,
885 struct tracing_map_elt *elt,
886 struct trace_buffer *buffer,
887 struct ring_buffer_event *rbe,
890 int cpu = smp_processor_id();
896 * check_field_for_var_ref - Check if a VAR_REF field references a variable
897 * @hist_field: The VAR_REF field to check
898 * @var_data: The hist trigger that owns the variable
899 * @var_idx: The trigger variable identifier
901 * Check the given VAR_REF field to see whether or not it references
902 * the given variable associated with the given trigger.
904 * Return: The VAR_REF field if it does reference the variable, NULL if not
906 static struct hist_field *
907 check_field_for_var_ref(struct hist_field *hist_field,
908 struct hist_trigger_data *var_data,
909 unsigned int var_idx)
911 WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
913 if (hist_field && hist_field->var.idx == var_idx &&
914 hist_field->var.hist_data == var_data)
921 * find_var_ref - Check if a trigger has a reference to a trigger variable
922 * @hist_data: The hist trigger that might have a reference to the variable
923 * @var_data: The hist trigger that owns the variable
924 * @var_idx: The trigger variable identifier
926 * Check the list of var_refs[] on the first hist trigger to see
927 * whether any of them are references to the variable on the second
930 * Return: The VAR_REF field referencing the variable if so, NULL if not
932 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
933 struct hist_trigger_data *var_data,
934 unsigned int var_idx)
936 struct hist_field *hist_field;
939 for (i = 0; i < hist_data->n_var_refs; i++) {
940 hist_field = hist_data->var_refs[i];
941 if (check_field_for_var_ref(hist_field, var_data, var_idx))
949 * find_any_var_ref - Check if there is a reference to a given trigger variable
950 * @hist_data: The hist trigger
951 * @var_idx: The trigger variable identifier
953 * Check to see whether the given variable is currently referenced by
956 * The trigger the variable is defined on is explicitly excluded - the
957 * assumption being that a self-reference doesn't prevent a trigger
958 * from being removed.
960 * Return: The VAR_REF field referencing the variable if so, NULL if not
962 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
963 unsigned int var_idx)
965 struct trace_array *tr = hist_data->event_file->tr;
966 struct hist_field *found = NULL;
967 struct hist_var_data *var_data;
969 list_for_each_entry(var_data, &tr->hist_vars, list) {
970 if (var_data->hist_data == hist_data)
972 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
981 * check_var_refs - Check if there is a reference to any of trigger's variables
982 * @hist_data: The hist trigger
984 * A trigger can define one or more variables. If any one of them is
985 * currently referenced by any other trigger, this function will
988 * Typically used to determine whether or not a trigger can be removed
989 * - if there are any references to a trigger's variables, it cannot.
991 * Return: True if there is a reference to any of trigger's variables
993 static bool check_var_refs(struct hist_trigger_data *hist_data)
995 struct hist_field *field;
999 for_each_hist_field(i, hist_data) {
1000 field = hist_data->fields[i];
1001 if (field && field->flags & HIST_FIELD_FL_VAR) {
1002 if (find_any_var_ref(hist_data, field->var.idx)) {
1012 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
1014 struct trace_array *tr = hist_data->event_file->tr;
1015 struct hist_var_data *var_data, *found = NULL;
1017 list_for_each_entry(var_data, &tr->hist_vars, list) {
1018 if (var_data->hist_data == hist_data) {
1027 static bool field_has_hist_vars(struct hist_field *hist_field,
1038 if (hist_field->flags & HIST_FIELD_FL_VAR ||
1039 hist_field->flags & HIST_FIELD_FL_VAR_REF)
1042 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
1043 struct hist_field *operand;
1045 operand = hist_field->operands[i];
1046 if (field_has_hist_vars(operand, level + 1))
1053 static bool has_hist_vars(struct hist_trigger_data *hist_data)
1055 struct hist_field *hist_field;
1058 for_each_hist_field(i, hist_data) {
1059 hist_field = hist_data->fields[i];
1060 if (field_has_hist_vars(hist_field, 0))
1067 static int save_hist_vars(struct hist_trigger_data *hist_data)
1069 struct trace_array *tr = hist_data->event_file->tr;
1070 struct hist_var_data *var_data;
1072 var_data = find_hist_vars(hist_data);
1076 if (tracing_check_open_get_tr(tr))
1079 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
1081 trace_array_put(tr);
1085 var_data->hist_data = hist_data;
1086 list_add(&var_data->list, &tr->hist_vars);
1091 static void remove_hist_vars(struct hist_trigger_data *hist_data)
1093 struct trace_array *tr = hist_data->event_file->tr;
1094 struct hist_var_data *var_data;
1096 var_data = find_hist_vars(hist_data);
1100 if (WARN_ON(check_var_refs(hist_data)))
1103 list_del(&var_data->list);
1107 trace_array_put(tr);
1110 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
1111 const char *var_name)
1113 struct hist_field *hist_field, *found = NULL;
1116 for_each_hist_field(i, hist_data) {
1117 hist_field = hist_data->fields[i];
1118 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
1119 strcmp(hist_field->var.name, var_name) == 0) {
1128 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
1129 struct trace_event_file *file,
1130 const char *var_name)
1132 struct hist_trigger_data *test_data;
1133 struct event_trigger_data *test;
1134 struct hist_field *hist_field;
1136 lockdep_assert_held(&event_mutex);
1138 hist_field = find_var_field(hist_data, var_name);
1142 list_for_each_entry(test, &file->triggers, list) {
1143 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1144 test_data = test->private_data;
1145 hist_field = find_var_field(test_data, var_name);
1154 static struct trace_event_file *find_var_file(struct trace_array *tr,
1159 struct hist_trigger_data *var_hist_data;
1160 struct hist_var_data *var_data;
1161 struct trace_event_file *file, *found = NULL;
1164 return find_event_file(tr, system, event_name);
1166 list_for_each_entry(var_data, &tr->hist_vars, list) {
1167 var_hist_data = var_data->hist_data;
1168 file = var_hist_data->event_file;
1172 if (find_var_field(var_hist_data, var_name)) {
1174 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name));
1185 static struct hist_field *find_file_var(struct trace_event_file *file,
1186 const char *var_name)
1188 struct hist_trigger_data *test_data;
1189 struct event_trigger_data *test;
1190 struct hist_field *hist_field;
1192 lockdep_assert_held(&event_mutex);
1194 list_for_each_entry(test, &file->triggers, list) {
1195 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1196 test_data = test->private_data;
1197 hist_field = find_var_field(test_data, var_name);
1206 static struct hist_field *
1207 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
1209 struct trace_array *tr = hist_data->event_file->tr;
1210 struct hist_field *hist_field, *found = NULL;
1211 struct trace_event_file *file;
1214 for (i = 0; i < hist_data->n_actions; i++) {
1215 struct action_data *data = hist_data->actions[i];
1217 if (data->handler == HANDLER_ONMATCH) {
1218 char *system = data->match_data.event_system;
1219 char *event_name = data->match_data.event;
1221 file = find_var_file(tr, system, event_name, var_name);
1224 hist_field = find_file_var(file, var_name);
1227 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE,
1229 return ERR_PTR(-EINVAL);
1239 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
1244 struct trace_array *tr = hist_data->event_file->tr;
1245 struct hist_field *hist_field = NULL;
1246 struct trace_event_file *file;
1248 if (!system || !event_name) {
1249 hist_field = find_match_var(hist_data, var_name);
1250 if (IS_ERR(hist_field))
1256 file = find_var_file(tr, system, event_name, var_name);
1260 hist_field = find_file_var(file, var_name);
1265 static u64 hist_field_var_ref(struct hist_field *hist_field,
1266 struct tracing_map_elt *elt,
1267 struct trace_buffer *buffer,
1268 struct ring_buffer_event *rbe,
1271 struct hist_elt_data *elt_data;
1274 if (WARN_ON_ONCE(!elt))
1277 elt_data = elt->private_data;
1278 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
1283 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
1284 u64 *var_ref_vals, bool self)
1286 struct hist_trigger_data *var_data;
1287 struct tracing_map_elt *var_elt;
1288 struct hist_field *hist_field;
1289 unsigned int i, var_idx;
1290 bool resolved = true;
1293 for (i = 0; i < hist_data->n_var_refs; i++) {
1294 hist_field = hist_data->var_refs[i];
1295 var_idx = hist_field->var.idx;
1296 var_data = hist_field->var.hist_data;
1298 if (var_data == NULL) {
1303 if ((self && var_data != hist_data) ||
1304 (!self && var_data == hist_data))
1307 var_elt = tracing_map_lookup(var_data->map, key);
1313 if (!tracing_map_var_set(var_elt, var_idx)) {
1318 if (self || !hist_field->read_once)
1319 var_val = tracing_map_read_var(var_elt, var_idx);
1321 var_val = tracing_map_read_var_once(var_elt, var_idx);
1323 var_ref_vals[i] = var_val;
1329 static const char *hist_field_name(struct hist_field *field,
1332 const char *field_name = "";
1334 if (WARN_ON_ONCE(!field))
1341 field_name = field->field->name;
1342 else if (field->flags & HIST_FIELD_FL_LOG2 ||
1343 field->flags & HIST_FIELD_FL_ALIAS ||
1344 field->flags & HIST_FIELD_FL_BUCKET)
1345 field_name = hist_field_name(field->operands[0], ++level);
1346 else if (field->flags & HIST_FIELD_FL_CPU)
1347 field_name = "common_cpu";
1348 else if (field->flags & HIST_FIELD_FL_EXPR ||
1349 field->flags & HIST_FIELD_FL_VAR_REF) {
1350 if (field->system) {
1351 static char full_name[MAX_FILTER_STR_VAL];
1353 strcat(full_name, field->system);
1354 strcat(full_name, ".");
1355 strcat(full_name, field->event_name);
1356 strcat(full_name, ".");
1357 strcat(full_name, field->name);
1358 field_name = full_name;
1360 field_name = field->name;
1361 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1362 field_name = "common_timestamp";
1363 else if (field->flags & HIST_FIELD_FL_STACKTRACE) {
1365 field_name = field->field->name;
1367 field_name = "common_stacktrace";
1368 } else if (field->flags & HIST_FIELD_FL_HITCOUNT)
1369 field_name = "hitcount";
1371 if (field_name == NULL)
1377 static enum hist_field_fn select_value_fn(int field_size, int field_is_signed)
1379 switch (field_size) {
1381 if (field_is_signed)
1382 return HIST_FIELD_FN_S64;
1384 return HIST_FIELD_FN_U64;
1386 if (field_is_signed)
1387 return HIST_FIELD_FN_S32;
1389 return HIST_FIELD_FN_U32;
1391 if (field_is_signed)
1392 return HIST_FIELD_FN_S16;
1394 return HIST_FIELD_FN_U16;
1396 if (field_is_signed)
1397 return HIST_FIELD_FN_S8;
1399 return HIST_FIELD_FN_U8;
1402 return HIST_FIELD_FN_NOP;
1405 static int parse_map_size(char *str)
1407 unsigned long size, map_bits;
1410 ret = kstrtoul(str, 0, &size);
1414 map_bits = ilog2(roundup_pow_of_two(size));
1415 if (map_bits < TRACING_MAP_BITS_MIN ||
1416 map_bits > TRACING_MAP_BITS_MAX)
1424 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
1431 for (i = 0; i < attrs->n_assignments; i++)
1432 kfree(attrs->assignment_str[i]);
1434 for (i = 0; i < attrs->n_actions; i++)
1435 kfree(attrs->action_str[i]);
1438 kfree(attrs->sort_key_str);
1439 kfree(attrs->keys_str);
1440 kfree(attrs->vals_str);
1441 kfree(attrs->clock);
1445 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
1449 if (attrs->n_actions >= HIST_ACTIONS_MAX)
1452 if ((str_has_prefix(str, "onmatch(")) ||
1453 (str_has_prefix(str, "onmax(")) ||
1454 (str_has_prefix(str, "onchange("))) {
1455 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
1456 if (!attrs->action_str[attrs->n_actions]) {
1466 static int parse_assignment(struct trace_array *tr,
1467 char *str, struct hist_trigger_attrs *attrs)
1471 if ((len = str_has_prefix(str, "key=")) ||
1472 (len = str_has_prefix(str, "keys="))) {
1473 attrs->keys_str = kstrdup(str + len, GFP_KERNEL);
1474 if (!attrs->keys_str) {
1478 } else if ((len = str_has_prefix(str, "val=")) ||
1479 (len = str_has_prefix(str, "vals=")) ||
1480 (len = str_has_prefix(str, "values="))) {
1481 attrs->vals_str = kstrdup(str + len, GFP_KERNEL);
1482 if (!attrs->vals_str) {
1486 } else if ((len = str_has_prefix(str, "sort="))) {
1487 attrs->sort_key_str = kstrdup(str + len, GFP_KERNEL);
1488 if (!attrs->sort_key_str) {
1492 } else if (str_has_prefix(str, "name=")) {
1493 attrs->name = kstrdup(str, GFP_KERNEL);
1498 } else if ((len = str_has_prefix(str, "clock="))) {
1501 str = strstrip(str);
1502 attrs->clock = kstrdup(str, GFP_KERNEL);
1503 if (!attrs->clock) {
1507 } else if ((len = str_has_prefix(str, "size="))) {
1508 int map_bits = parse_map_size(str + len);
1514 attrs->map_bits = map_bits;
1518 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
1519 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str));
1524 assignment = kstrdup(str, GFP_KERNEL);
1530 attrs->assignment_str[attrs->n_assignments++] = assignment;
1536 static struct hist_trigger_attrs *
1537 parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str)
1539 struct hist_trigger_attrs *attrs;
1542 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1544 return ERR_PTR(-ENOMEM);
1546 while (trigger_str) {
1547 char *str = strsep(&trigger_str, ":");
1550 rhs = strchr(str, '=');
1552 if (!strlen(++rhs)) {
1554 hist_err(tr, HIST_ERR_EMPTY_ASSIGNMENT, errpos(str));
1557 ret = parse_assignment(tr, str, attrs);
1560 } else if (strcmp(str, "nohitcount") == 0 ||
1561 strcmp(str, "NOHC") == 0)
1562 attrs->no_hitcount = true;
1563 else if (strcmp(str, "pause") == 0)
1564 attrs->pause = true;
1565 else if ((strcmp(str, "cont") == 0) ||
1566 (strcmp(str, "continue") == 0))
1568 else if (strcmp(str, "clear") == 0)
1569 attrs->clear = true;
1571 ret = parse_action(str, attrs);
1577 if (!attrs->keys_str) {
1582 if (!attrs->clock) {
1583 attrs->clock = kstrdup("global", GFP_KERNEL);
1584 if (!attrs->clock) {
1592 destroy_hist_trigger_attrs(attrs);
1594 return ERR_PTR(ret);
1597 static inline void save_comm(char *comm, struct task_struct *task)
1600 strcpy(comm, "<idle>");
1604 if (WARN_ON_ONCE(task->pid < 0)) {
1605 strcpy(comm, "<XXX>");
1609 strncpy(comm, task->comm, TASK_COMM_LEN);
1612 static void hist_elt_data_free(struct hist_elt_data *elt_data)
1616 for (i = 0; i < elt_data->n_field_var_str; i++)
1617 kfree(elt_data->field_var_str[i]);
1619 kfree(elt_data->field_var_str);
1621 kfree(elt_data->comm);
1625 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
1627 struct hist_elt_data *elt_data = elt->private_data;
1629 hist_elt_data_free(elt_data);
1632 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
1634 struct hist_trigger_data *hist_data = elt->map->private_data;
1635 unsigned int size = TASK_COMM_LEN;
1636 struct hist_elt_data *elt_data;
1637 struct hist_field *hist_field;
1638 unsigned int i, n_str;
1640 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
1644 for_each_hist_field(i, hist_data) {
1645 hist_field = hist_data->fields[i];
1647 if (hist_field->flags & HIST_FIELD_FL_EXECNAME) {
1648 elt_data->comm = kzalloc(size, GFP_KERNEL);
1649 if (!elt_data->comm) {
1657 n_str = hist_data->n_field_var_str + hist_data->n_save_var_str +
1658 hist_data->n_var_str;
1659 if (n_str > SYNTH_FIELDS_MAX) {
1660 hist_elt_data_free(elt_data);
1664 BUILD_BUG_ON(STR_VAR_LEN_MAX & (sizeof(u64) - 1));
1666 size = STR_VAR_LEN_MAX;
1668 elt_data->field_var_str = kcalloc(n_str, sizeof(char *), GFP_KERNEL);
1669 if (!elt_data->field_var_str) {
1670 hist_elt_data_free(elt_data);
1673 elt_data->n_field_var_str = n_str;
1675 for (i = 0; i < n_str; i++) {
1676 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
1677 if (!elt_data->field_var_str[i]) {
1678 hist_elt_data_free(elt_data);
1683 elt->private_data = elt_data;
1688 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
1690 struct hist_elt_data *elt_data = elt->private_data;
1693 save_comm(elt_data->comm, current);
1696 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
1697 .elt_alloc = hist_trigger_elt_data_alloc,
1698 .elt_free = hist_trigger_elt_data_free,
1699 .elt_init = hist_trigger_elt_data_init,
1702 static const char *get_hist_field_flags(struct hist_field *hist_field)
1704 const char *flags_str = NULL;
1706 if (hist_field->flags & HIST_FIELD_FL_HEX)
1708 else if (hist_field->flags & HIST_FIELD_FL_SYM)
1710 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
1711 flags_str = "sym-offset";
1712 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
1713 flags_str = "execname";
1714 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
1715 flags_str = "syscall";
1716 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
1718 else if (hist_field->flags & HIST_FIELD_FL_BUCKET)
1719 flags_str = "buckets";
1720 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
1721 flags_str = "usecs";
1722 else if (hist_field->flags & HIST_FIELD_FL_PERCENT)
1723 flags_str = "percent";
1724 else if (hist_field->flags & HIST_FIELD_FL_GRAPH)
1725 flags_str = "graph";
1726 else if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
1727 flags_str = "stacktrace";
1732 static void expr_field_str(struct hist_field *field, char *expr)
1734 if (field->flags & HIST_FIELD_FL_VAR_REF)
1736 else if (field->flags & HIST_FIELD_FL_CONST) {
1737 char str[HIST_CONST_DIGITS_MAX];
1739 snprintf(str, HIST_CONST_DIGITS_MAX, "%llu", field->constant);
1743 strcat(expr, hist_field_name(field, 0));
1745 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
1746 const char *flags_str = get_hist_field_flags(field);
1750 strcat(expr, flags_str);
1755 static char *expr_str(struct hist_field *field, unsigned int level)
1762 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
1766 if (!field->operands[0]) {
1767 expr_field_str(field, expr);
1771 if (field->operator == FIELD_OP_UNARY_MINUS) {
1775 subexpr = expr_str(field->operands[0], ++level);
1780 strcat(expr, subexpr);
1788 expr_field_str(field->operands[0], expr);
1790 switch (field->operator) {
1791 case FIELD_OP_MINUS:
1808 expr_field_str(field->operands[1], expr);
1814 * If field_op != FIELD_OP_NONE, *sep points to the root operator
1815 * of the expression tree to be evaluated.
1817 static int contains_operator(char *str, char **sep)
1819 enum field_op_id field_op = FIELD_OP_NONE;
1820 char *minus_op, *plus_op, *div_op, *mult_op;
1824 * Report the last occurrence of the operators first, so that the
1825 * expression is evaluated left to right. This is important since
1826 * subtraction and division are not associative.
1829 * 64/8/4/2 is 1, i.e 64/8/4/2 = ((64/8)/4)/2
1830 * 14-7-5-2 is 0, i.e 14-7-5-2 = ((14-7)-5)-2
1834 * First, find lower precedence addition and subtraction
1835 * since the expression will be evaluated recursively.
1837 minus_op = strrchr(str, '-');
1840 * Unary minus is not supported in sub-expressions. If
1841 * present, it is always the next root operator.
1843 if (minus_op == str) {
1844 field_op = FIELD_OP_UNARY_MINUS;
1848 field_op = FIELD_OP_MINUS;
1851 plus_op = strrchr(str, '+');
1852 if (plus_op || minus_op) {
1854 * For operators of the same precedence use to rightmost as the
1855 * root, so that the expression is evaluated left to right.
1857 if (plus_op > minus_op)
1858 field_op = FIELD_OP_PLUS;
1863 * Multiplication and division have higher precedence than addition and
1866 div_op = strrchr(str, '/');
1868 field_op = FIELD_OP_DIV;
1870 mult_op = strrchr(str, '*');
1872 * For operators of the same precedence use to rightmost as the
1873 * root, so that the expression is evaluated left to right.
1875 if (mult_op > div_op)
1876 field_op = FIELD_OP_MULT;
1881 case FIELD_OP_UNARY_MINUS:
1882 case FIELD_OP_MINUS:
1904 static void get_hist_field(struct hist_field *hist_field)
1909 static void __destroy_hist_field(struct hist_field *hist_field)
1911 if (--hist_field->ref > 1)
1914 kfree(hist_field->var.name);
1915 kfree(hist_field->name);
1917 /* Can likely be a const */
1918 kfree_const(hist_field->type);
1920 kfree(hist_field->system);
1921 kfree(hist_field->event_name);
1926 static void destroy_hist_field(struct hist_field *hist_field,
1937 if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
1938 return; /* var refs will be destroyed separately */
1940 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
1941 destroy_hist_field(hist_field->operands[i], level + 1);
1943 __destroy_hist_field(hist_field);
1946 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
1947 struct ftrace_event_field *field,
1948 unsigned long flags,
1951 struct hist_field *hist_field;
1953 if (field && is_function_field(field))
1956 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
1960 hist_field->ref = 1;
1962 hist_field->hist_data = hist_data;
1964 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
1965 goto out; /* caller will populate */
1967 if (flags & HIST_FIELD_FL_VAR_REF) {
1968 hist_field->fn_num = HIST_FIELD_FN_VAR_REF;
1972 if (flags & HIST_FIELD_FL_HITCOUNT) {
1973 hist_field->fn_num = HIST_FIELD_FN_COUNTER;
1974 hist_field->size = sizeof(u64);
1975 hist_field->type = "u64";
1979 if (flags & HIST_FIELD_FL_CONST) {
1980 hist_field->fn_num = HIST_FIELD_FN_CONST;
1981 hist_field->size = sizeof(u64);
1982 hist_field->type = kstrdup("u64", GFP_KERNEL);
1983 if (!hist_field->type)
1988 if (flags & HIST_FIELD_FL_STACKTRACE) {
1990 hist_field->fn_num = HIST_FIELD_FN_STACK;
1992 hist_field->fn_num = HIST_FIELD_FN_NOP;
1993 hist_field->size = HIST_STACKTRACE_SIZE;
1994 hist_field->type = kstrdup_const("unsigned long[]", GFP_KERNEL);
1995 if (!hist_field->type)
2000 if (flags & (HIST_FIELD_FL_LOG2 | HIST_FIELD_FL_BUCKET)) {
2001 unsigned long fl = flags & ~(HIST_FIELD_FL_LOG2 | HIST_FIELD_FL_BUCKET);
2002 hist_field->fn_num = flags & HIST_FIELD_FL_LOG2 ? HIST_FIELD_FN_LOG2 :
2003 HIST_FIELD_FN_BUCKET;
2004 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
2005 if (!hist_field->operands[0])
2007 hist_field->size = hist_field->operands[0]->size;
2008 hist_field->type = kstrdup_const(hist_field->operands[0]->type, GFP_KERNEL);
2009 if (!hist_field->type)
2014 if (flags & HIST_FIELD_FL_TIMESTAMP) {
2015 hist_field->fn_num = HIST_FIELD_FN_TIMESTAMP;
2016 hist_field->size = sizeof(u64);
2017 hist_field->type = "u64";
2021 if (flags & HIST_FIELD_FL_CPU) {
2022 hist_field->fn_num = HIST_FIELD_FN_CPU;
2023 hist_field->size = sizeof(int);
2024 hist_field->type = "unsigned int";
2028 if (WARN_ON_ONCE(!field))
2031 /* Pointers to strings are just pointers and dangerous to dereference */
2032 if (is_string_field(field) &&
2033 (field->filter_type != FILTER_PTR_STRING)) {
2034 flags |= HIST_FIELD_FL_STRING;
2036 hist_field->size = MAX_FILTER_STR_VAL;
2037 hist_field->type = kstrdup_const(field->type, GFP_KERNEL);
2038 if (!hist_field->type)
2041 if (field->filter_type == FILTER_STATIC_STRING) {
2042 hist_field->fn_num = HIST_FIELD_FN_STRING;
2043 hist_field->size = field->size;
2044 } else if (field->filter_type == FILTER_DYN_STRING) {
2045 hist_field->fn_num = HIST_FIELD_FN_DYNSTRING;
2046 } else if (field->filter_type == FILTER_RDYN_STRING)
2047 hist_field->fn_num = HIST_FIELD_FN_RELDYNSTRING;
2049 hist_field->fn_num = HIST_FIELD_FN_PSTRING;
2051 hist_field->size = field->size;
2052 hist_field->is_signed = field->is_signed;
2053 hist_field->type = kstrdup_const(field->type, GFP_KERNEL);
2054 if (!hist_field->type)
2057 hist_field->fn_num = select_value_fn(field->size,
2059 if (hist_field->fn_num == HIST_FIELD_FN_NOP) {
2060 destroy_hist_field(hist_field, 0);
2065 hist_field->field = field;
2066 hist_field->flags = flags;
2069 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
2070 if (!hist_field->var.name)
2076 destroy_hist_field(hist_field, 0);
2080 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
2084 for (i = 0; i < HIST_FIELDS_MAX; i++) {
2085 if (hist_data->fields[i]) {
2086 destroy_hist_field(hist_data->fields[i], 0);
2087 hist_data->fields[i] = NULL;
2091 for (i = 0; i < hist_data->n_var_refs; i++) {
2092 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
2093 __destroy_hist_field(hist_data->var_refs[i]);
2094 hist_data->var_refs[i] = NULL;
2098 static int init_var_ref(struct hist_field *ref_field,
2099 struct hist_field *var_field,
2100 char *system, char *event_name)
2104 ref_field->var.idx = var_field->var.idx;
2105 ref_field->var.hist_data = var_field->hist_data;
2106 ref_field->size = var_field->size;
2107 ref_field->is_signed = var_field->is_signed;
2108 ref_field->flags |= var_field->flags &
2109 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2112 ref_field->system = kstrdup(system, GFP_KERNEL);
2113 if (!ref_field->system)
2118 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
2119 if (!ref_field->event_name) {
2125 if (var_field->var.name) {
2126 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
2127 if (!ref_field->name) {
2131 } else if (var_field->name) {
2132 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
2133 if (!ref_field->name) {
2139 ref_field->type = kstrdup_const(var_field->type, GFP_KERNEL);
2140 if (!ref_field->type) {
2147 kfree(ref_field->system);
2148 ref_field->system = NULL;
2149 kfree(ref_field->event_name);
2150 ref_field->event_name = NULL;
2151 kfree(ref_field->name);
2152 ref_field->name = NULL;
2157 static int find_var_ref_idx(struct hist_trigger_data *hist_data,
2158 struct hist_field *var_field)
2160 struct hist_field *ref_field;
2163 for (i = 0; i < hist_data->n_var_refs; i++) {
2164 ref_field = hist_data->var_refs[i];
2165 if (ref_field->var.idx == var_field->var.idx &&
2166 ref_field->var.hist_data == var_field->hist_data)
2174 * create_var_ref - Create a variable reference and attach it to trigger
2175 * @hist_data: The trigger that will be referencing the variable
2176 * @var_field: The VAR field to create a reference to
2177 * @system: The optional system string
2178 * @event_name: The optional event_name string
2180 * Given a variable hist_field, create a VAR_REF hist_field that
2181 * represents a reference to it.
2183 * This function also adds the reference to the trigger that
2184 * now references the variable.
2186 * Return: The VAR_REF field if successful, NULL if not
2188 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
2189 struct hist_field *var_field,
2190 char *system, char *event_name)
2192 unsigned long flags = HIST_FIELD_FL_VAR_REF;
2193 struct hist_field *ref_field;
2196 /* Check if the variable already exists */
2197 for (i = 0; i < hist_data->n_var_refs; i++) {
2198 ref_field = hist_data->var_refs[i];
2199 if (ref_field->var.idx == var_field->var.idx &&
2200 ref_field->var.hist_data == var_field->hist_data) {
2201 get_hist_field(ref_field);
2205 /* Sanity check to avoid out-of-bound write on 'hist_data->var_refs' */
2206 if (hist_data->n_var_refs >= TRACING_MAP_VARS_MAX)
2208 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
2210 if (init_var_ref(ref_field, var_field, system, event_name)) {
2211 destroy_hist_field(ref_field, 0);
2215 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
2216 ref_field->var_ref_idx = hist_data->n_var_refs++;
2222 static bool is_var_ref(char *var_name)
2224 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
2230 static char *field_name_from_var(struct hist_trigger_data *hist_data,
2236 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
2237 name = hist_data->attrs->var_defs.name[i];
2239 if (strcmp(var_name, name) == 0) {
2240 field = hist_data->attrs->var_defs.expr[i];
2241 if (contains_operator(field, NULL) || is_var_ref(field))
2250 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
2251 char *system, char *event_name,
2254 struct trace_event_call *call;
2256 if (system && event_name) {
2257 call = hist_data->event_file->event_call;
2259 if (strcmp(system, call->class->system) != 0)
2262 if (strcmp(event_name, trace_event_name(call)) != 0)
2266 if (!!system != !!event_name)
2269 if (!is_var_ref(var_name))
2274 return field_name_from_var(hist_data, var_name);
2277 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
2278 char *system, char *event_name,
2281 struct hist_field *var_field = NULL, *ref_field = NULL;
2282 struct trace_array *tr = hist_data->event_file->tr;
2284 if (!is_var_ref(var_name))
2289 var_field = find_event_var(hist_data, system, event_name, var_name);
2291 ref_field = create_var_ref(hist_data, var_field,
2292 system, event_name);
2295 hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name));
2300 static struct ftrace_event_field *
2301 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
2302 char *field_str, unsigned long *flags, unsigned long *buckets)
2304 struct ftrace_event_field *field = NULL;
2305 char *field_name, *modifier, *str;
2306 struct trace_array *tr = file->tr;
2308 modifier = str = kstrdup(field_str, GFP_KERNEL);
2310 return ERR_PTR(-ENOMEM);
2312 field_name = strsep(&modifier, ".");
2314 if (strcmp(modifier, "hex") == 0)
2315 *flags |= HIST_FIELD_FL_HEX;
2316 else if (strcmp(modifier, "sym") == 0)
2317 *flags |= HIST_FIELD_FL_SYM;
2319 * 'sym-offset' occurrences in the trigger string are modified
2320 * to 'symXoffset' to simplify arithmetic expression parsing.
2322 else if (strcmp(modifier, "symXoffset") == 0)
2323 *flags |= HIST_FIELD_FL_SYM_OFFSET;
2324 else if ((strcmp(modifier, "execname") == 0) &&
2325 (strcmp(field_name, "common_pid") == 0))
2326 *flags |= HIST_FIELD_FL_EXECNAME;
2327 else if (strcmp(modifier, "syscall") == 0)
2328 *flags |= HIST_FIELD_FL_SYSCALL;
2329 else if (strcmp(modifier, "stacktrace") == 0)
2330 *flags |= HIST_FIELD_FL_STACKTRACE;
2331 else if (strcmp(modifier, "log2") == 0)
2332 *flags |= HIST_FIELD_FL_LOG2;
2333 else if (strcmp(modifier, "usecs") == 0)
2334 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
2335 else if (strncmp(modifier, "bucket", 6) == 0) {
2340 if (*modifier == 's')
2342 if (*modifier != '=')
2345 ret = kstrtoul(modifier, 0, buckets);
2346 if (ret || !(*buckets))
2348 *flags |= HIST_FIELD_FL_BUCKET;
2349 } else if (strncmp(modifier, "percent", 7) == 0) {
2350 if (*flags & (HIST_FIELD_FL_VAR | HIST_FIELD_FL_KEY))
2352 *flags |= HIST_FIELD_FL_PERCENT;
2353 } else if (strncmp(modifier, "graph", 5) == 0) {
2354 if (*flags & (HIST_FIELD_FL_VAR | HIST_FIELD_FL_KEY))
2356 *flags |= HIST_FIELD_FL_GRAPH;
2359 hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier));
2360 field = ERR_PTR(-EINVAL);
2365 if (strcmp(field_name, "common_timestamp") == 0) {
2366 *flags |= HIST_FIELD_FL_TIMESTAMP;
2367 hist_data->enable_timestamps = true;
2368 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2369 hist_data->attrs->ts_in_usecs = true;
2370 } else if (strcmp(field_name, "common_stacktrace") == 0) {
2371 *flags |= HIST_FIELD_FL_STACKTRACE;
2372 } else if (strcmp(field_name, "common_cpu") == 0)
2373 *flags |= HIST_FIELD_FL_CPU;
2374 else if (strcmp(field_name, "hitcount") == 0)
2375 *flags |= HIST_FIELD_FL_HITCOUNT;
2377 field = trace_find_event_field(file->event_call, field_name);
2378 if (!field || !field->size) {
2380 * For backward compatibility, if field_name
2381 * was "cpu" or "stacktrace", then we treat this
2382 * the same as common_cpu and common_stacktrace
2383 * respectively. This also works for "CPU", and
2386 if (field && field->filter_type == FILTER_CPU) {
2387 *flags |= HIST_FIELD_FL_CPU;
2388 } else if (field && field->filter_type == FILTER_STACKTRACE) {
2389 *flags |= HIST_FIELD_FL_STACKTRACE;
2391 hist_err(tr, HIST_ERR_FIELD_NOT_FOUND,
2392 errpos(field_name));
2393 field = ERR_PTR(-EINVAL);
2404 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2405 struct hist_field *var_ref,
2408 struct hist_field *alias = NULL;
2409 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2411 alias = create_hist_field(hist_data, NULL, flags, var_name);
2415 alias->fn_num = var_ref->fn_num;
2416 alias->operands[0] = var_ref;
2418 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2419 destroy_hist_field(alias, 0);
2423 alias->var_ref_idx = var_ref->var_ref_idx;
2428 static struct hist_field *parse_const(struct hist_trigger_data *hist_data,
2429 char *str, char *var_name,
2430 unsigned long *flags)
2432 struct trace_array *tr = hist_data->event_file->tr;
2433 struct hist_field *field = NULL;
2436 if (kstrtoull(str, 0, &constant)) {
2437 hist_err(tr, HIST_ERR_EXPECT_NUMBER, errpos(str));
2441 *flags |= HIST_FIELD_FL_CONST;
2442 field = create_hist_field(hist_data, NULL, *flags, var_name);
2446 field->constant = constant;
2451 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2452 struct trace_event_file *file, char *str,
2453 unsigned long *flags, char *var_name)
2455 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
2456 struct ftrace_event_field *field = NULL;
2457 struct hist_field *hist_field = NULL;
2458 unsigned long buckets = 0;
2461 if (isdigit(str[0])) {
2462 hist_field = parse_const(hist_data, str, var_name, flags);
2470 s = strchr(str, '.');
2472 s = strchr(++s, '.');
2474 ref_system = strsep(&str, ".");
2479 ref_event = strsep(&str, ".");
2488 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2490 hist_field = parse_var_ref(hist_data, ref_system,
2491 ref_event, ref_var);
2494 hist_field = create_alias(hist_data, hist_field, var_name);
2505 field = parse_field(hist_data, file, str, flags, &buckets);
2506 if (IS_ERR(field)) {
2507 ret = PTR_ERR(field);
2511 hist_field = create_hist_field(hist_data, field, *flags, var_name);
2516 hist_field->buckets = buckets;
2520 return ERR_PTR(ret);
2523 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2524 struct trace_event_file *file,
2525 char *str, unsigned long flags,
2526 char *var_name, unsigned int *n_subexprs);
2528 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2529 struct trace_event_file *file,
2530 char *str, unsigned long flags,
2531 char *var_name, unsigned int *n_subexprs)
2533 struct hist_field *operand1, *expr = NULL;
2534 unsigned long operand_flags;
2538 /* Unary minus operator, increment n_subexprs */
2541 /* we support only -(xxx) i.e. explicit parens required */
2543 if (*n_subexprs > 3) {
2544 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2549 str++; /* skip leading '-' */
2551 s = strchr(str, '(');
2559 s = strrchr(str, ')');
2561 /* unary minus not supported in sub-expressions */
2562 if (*(s+1) != '\0') {
2563 hist_err(file->tr, HIST_ERR_UNARY_MINUS_SUBEXPR,
2571 ret = -EINVAL; /* no closing ')' */
2575 flags |= HIST_FIELD_FL_EXPR;
2576 expr = create_hist_field(hist_data, NULL, flags, var_name);
2583 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, n_subexprs);
2584 if (IS_ERR(operand1)) {
2585 ret = PTR_ERR(operand1);
2588 if (operand1->flags & HIST_FIELD_FL_STRING) {
2589 /* String type can not be the operand of unary operator. */
2590 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
2591 destroy_hist_field(operand1, 0);
2596 expr->flags |= operand1->flags &
2597 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2598 expr->fn_num = HIST_FIELD_FN_UMINUS;
2599 expr->operands[0] = operand1;
2600 expr->size = operand1->size;
2601 expr->is_signed = operand1->is_signed;
2602 expr->operator = FIELD_OP_UNARY_MINUS;
2603 expr->name = expr_str(expr, 0);
2604 expr->type = kstrdup_const(operand1->type, GFP_KERNEL);
2612 destroy_hist_field(expr, 0);
2613 return ERR_PTR(ret);
2617 * If the operands are var refs, return pointers the
2618 * variable(s) referenced in var1 and var2, else NULL.
2620 static int check_expr_operands(struct trace_array *tr,
2621 struct hist_field *operand1,
2622 struct hist_field *operand2,
2623 struct hist_field **var1,
2624 struct hist_field **var2)
2626 unsigned long operand1_flags = operand1->flags;
2627 unsigned long operand2_flags = operand2->flags;
2629 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2630 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2631 struct hist_field *var;
2633 var = find_var_field(operand1->var.hist_data, operand1->name);
2636 operand1_flags = var->flags;
2640 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2641 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2642 struct hist_field *var;
2644 var = find_var_field(operand2->var.hist_data, operand2->name);
2647 operand2_flags = var->flags;
2651 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
2652 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
2653 hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0);
2660 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2661 struct trace_event_file *file,
2662 char *str, unsigned long flags,
2663 char *var_name, unsigned int *n_subexprs)
2665 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2666 struct hist_field *var1 = NULL, *var2 = NULL;
2667 unsigned long operand_flags, operand2_flags;
2668 int field_op, ret = -EINVAL;
2669 char *sep, *operand1_str;
2670 enum hist_field_fn op_fn;
2671 bool combine_consts;
2673 if (*n_subexprs > 3) {
2674 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2675 return ERR_PTR(-EINVAL);
2678 field_op = contains_operator(str, &sep);
2680 if (field_op == FIELD_OP_NONE)
2681 return parse_atom(hist_data, file, str, &flags, var_name);
2683 if (field_op == FIELD_OP_UNARY_MINUS)
2684 return parse_unary(hist_data, file, str, flags, var_name, n_subexprs);
2686 /* Binary operator found, increment n_subexprs */
2689 /* Split the expression string at the root operator */
2691 return ERR_PTR(-EINVAL);
2697 /* Binary operator requires both operands */
2698 if (*operand1_str == '\0' || *str == '\0')
2699 return ERR_PTR(-EINVAL);
2703 /* LHS of string is an expression e.g. a+b in a+b+c */
2704 operand1 = parse_expr(hist_data, file, operand1_str, operand_flags, NULL, n_subexprs);
2705 if (IS_ERR(operand1))
2706 return ERR_CAST(operand1);
2708 if (operand1->flags & HIST_FIELD_FL_STRING) {
2709 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(operand1_str));
2714 /* RHS of string is another expression e.g. c in a+b+c */
2716 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, n_subexprs);
2717 if (IS_ERR(operand2)) {
2718 ret = PTR_ERR(operand2);
2721 if (operand2->flags & HIST_FIELD_FL_STRING) {
2722 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
2728 case FIELD_OP_MINUS:
2729 op_fn = HIST_FIELD_FN_MINUS;
2732 op_fn = HIST_FIELD_FN_PLUS;
2735 op_fn = HIST_FIELD_FN_DIV;
2738 op_fn = HIST_FIELD_FN_MULT;
2745 ret = check_expr_operands(file->tr, operand1, operand2, &var1, &var2);
2749 operand_flags = var1 ? var1->flags : operand1->flags;
2750 operand2_flags = var2 ? var2->flags : operand2->flags;
2753 * If both operands are constant, the expression can be
2754 * collapsed to a single constant.
2756 combine_consts = operand_flags & operand2_flags & HIST_FIELD_FL_CONST;
2758 flags |= combine_consts ? HIST_FIELD_FL_CONST : HIST_FIELD_FL_EXPR;
2760 flags |= operand1->flags &
2761 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2763 expr = create_hist_field(hist_data, NULL, flags, var_name);
2769 operand1->read_once = true;
2770 operand2->read_once = true;
2772 /* The operands are now owned and free'd by 'expr' */
2773 expr->operands[0] = operand1;
2774 expr->operands[1] = operand2;
2776 if (field_op == FIELD_OP_DIV &&
2777 operand2_flags & HIST_FIELD_FL_CONST) {
2778 u64 divisor = var2 ? var2->constant : operand2->constant;
2781 hist_err(file->tr, HIST_ERR_DIVISION_BY_ZERO, errpos(str));
2787 * Copy the divisor here so we don't have to look it up
2788 * later if this is a var ref
2790 operand2->constant = divisor;
2791 op_fn = hist_field_get_div_fn(operand2);
2794 expr->fn_num = op_fn;
2796 if (combine_consts) {
2798 expr->operands[0] = var1;
2800 expr->operands[1] = var2;
2802 expr->constant = hist_fn_call(expr, NULL, NULL, NULL, NULL);
2803 expr->fn_num = HIST_FIELD_FN_CONST;
2805 expr->operands[0] = NULL;
2806 expr->operands[1] = NULL;
2809 * var refs won't be destroyed immediately
2810 * See: destroy_hist_field()
2812 destroy_hist_field(operand2, 0);
2813 destroy_hist_field(operand1, 0);
2815 expr->name = expr_str(expr, 0);
2817 /* The operand sizes should be the same, so just pick one */
2818 expr->size = operand1->size;
2819 expr->is_signed = operand1->is_signed;
2821 expr->operator = field_op;
2822 expr->type = kstrdup_const(operand1->type, GFP_KERNEL);
2828 expr->name = expr_str(expr, 0);
2834 destroy_hist_field(operand2, 0);
2836 destroy_hist_field(operand1, 0);
2837 return ERR_PTR(ret);
2840 destroy_hist_field(expr, 0);
2841 return ERR_PTR(ret);
2844 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
2845 struct trace_event_file *file)
2847 struct event_trigger_data *test;
2849 lockdep_assert_held(&event_mutex);
2851 list_for_each_entry(test, &file->triggers, list) {
2852 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2853 if (test->private_data == hist_data)
2854 return test->filter_str;
2861 static struct event_command trigger_hist_cmd;
2862 static int event_hist_trigger_parse(struct event_command *cmd_ops,
2863 struct trace_event_file *file,
2864 char *glob, char *cmd,
2865 char *param_and_filter);
2867 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
2868 struct hist_trigger_data *hist_data,
2869 unsigned int n_keys)
2871 struct hist_field *target_hist_field, *hist_field;
2872 unsigned int n, i, j;
2874 if (hist_data->n_fields - hist_data->n_vals != n_keys)
2877 i = hist_data->n_vals;
2878 j = target_hist_data->n_vals;
2880 for (n = 0; n < n_keys; n++) {
2881 hist_field = hist_data->fields[i + n];
2882 target_hist_field = target_hist_data->fields[j + n];
2884 if (strcmp(hist_field->type, target_hist_field->type) != 0)
2886 if (hist_field->size != target_hist_field->size)
2888 if (hist_field->is_signed != target_hist_field->is_signed)
2895 static struct hist_trigger_data *
2896 find_compatible_hist(struct hist_trigger_data *target_hist_data,
2897 struct trace_event_file *file)
2899 struct hist_trigger_data *hist_data;
2900 struct event_trigger_data *test;
2901 unsigned int n_keys;
2903 lockdep_assert_held(&event_mutex);
2905 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
2907 list_for_each_entry(test, &file->triggers, list) {
2908 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2909 hist_data = test->private_data;
2911 if (compatible_keys(target_hist_data, hist_data, n_keys))
2919 static struct trace_event_file *event_file(struct trace_array *tr,
2920 char *system, char *event_name)
2922 struct trace_event_file *file;
2924 file = __find_event_file(tr, system, event_name);
2926 return ERR_PTR(-EINVAL);
2931 static struct hist_field *
2932 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
2933 char *system, char *event_name, char *field_name)
2935 struct hist_field *event_var;
2936 char *synthetic_name;
2938 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2939 if (!synthetic_name)
2940 return ERR_PTR(-ENOMEM);
2942 strcpy(synthetic_name, "synthetic_");
2943 strcat(synthetic_name, field_name);
2945 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
2947 kfree(synthetic_name);
2953 * create_field_var_hist - Automatically create a histogram and var for a field
2954 * @target_hist_data: The target hist trigger
2955 * @subsys_name: Optional subsystem name
2956 * @event_name: Optional event name
2957 * @field_name: The name of the field (and the resulting variable)
2959 * Hist trigger actions fetch data from variables, not directly from
2960 * events. However, for convenience, users are allowed to directly
2961 * specify an event field in an action, which will be automatically
2962 * converted into a variable on their behalf.
2964 * If a user specifies a field on an event that isn't the event the
2965 * histogram currently being defined (the target event histogram), the
2966 * only way that can be accomplished is if a new hist trigger is
2967 * created and the field variable defined on that.
2969 * This function creates a new histogram compatible with the target
2970 * event (meaning a histogram with the same key as the target
2971 * histogram), and creates a variable for the specified field, but
2972 * with 'synthetic_' prepended to the variable name in order to avoid
2973 * collision with normal field variables.
2975 * Return: The variable created for the field.
2977 static struct hist_field *
2978 create_field_var_hist(struct hist_trigger_data *target_hist_data,
2979 char *subsys_name, char *event_name, char *field_name)
2981 struct trace_array *tr = target_hist_data->event_file->tr;
2982 struct hist_trigger_data *hist_data;
2983 unsigned int i, n, first = true;
2984 struct field_var_hist *var_hist;
2985 struct trace_event_file *file;
2986 struct hist_field *key_field;
2987 struct hist_field *event_var;
2992 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
2993 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
2994 return ERR_PTR(-EINVAL);
2997 file = event_file(tr, subsys_name, event_name);
3000 hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name));
3001 ret = PTR_ERR(file);
3002 return ERR_PTR(ret);
3006 * Look for a histogram compatible with target. We'll use the
3007 * found histogram specification to create a new matching
3008 * histogram with our variable on it. target_hist_data is not
3009 * yet a registered histogram so we can't use that.
3011 hist_data = find_compatible_hist(target_hist_data, file);
3013 hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name));
3014 return ERR_PTR(-EINVAL);
3017 /* See if a synthetic field variable has already been created */
3018 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3019 event_name, field_name);
3020 if (!IS_ERR_OR_NULL(event_var))
3023 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
3025 return ERR_PTR(-ENOMEM);
3027 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3030 return ERR_PTR(-ENOMEM);
3033 /* Use the same keys as the compatible histogram */
3034 strcat(cmd, "keys=");
3036 for_each_hist_key_field(i, hist_data) {
3037 key_field = hist_data->fields[i];
3040 strcat(cmd, key_field->field->name);
3044 /* Create the synthetic field variable specification */
3045 strcat(cmd, ":synthetic_");
3046 strcat(cmd, field_name);
3048 strcat(cmd, field_name);
3050 /* Use the same filter as the compatible histogram */
3051 saved_filter = find_trigger_filter(hist_data, file);
3053 strcat(cmd, " if ");
3054 strcat(cmd, saved_filter);
3057 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
3058 if (!var_hist->cmd) {
3061 return ERR_PTR(-ENOMEM);
3064 /* Save the compatible histogram information */
3065 var_hist->hist_data = hist_data;
3067 /* Create the new histogram with our variable */
3068 ret = event_hist_trigger_parse(&trigger_hist_cmd, file,
3072 kfree(var_hist->cmd);
3074 hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name));
3075 return ERR_PTR(ret);
3080 /* If we can't find the variable, something went wrong */
3081 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3082 event_name, field_name);
3083 if (IS_ERR_OR_NULL(event_var)) {
3084 kfree(var_hist->cmd);
3086 hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
3087 return ERR_PTR(-EINVAL);
3090 n = target_hist_data->n_field_var_hists;
3091 target_hist_data->field_var_hists[n] = var_hist;
3092 target_hist_data->n_field_var_hists++;
3097 static struct hist_field *
3098 find_target_event_var(struct hist_trigger_data *hist_data,
3099 char *subsys_name, char *event_name, char *var_name)
3101 struct trace_event_file *file = hist_data->event_file;
3102 struct hist_field *hist_field = NULL;
3105 struct trace_event_call *call;
3110 call = file->event_call;
3112 if (strcmp(subsys_name, call->class->system) != 0)
3115 if (strcmp(event_name, trace_event_name(call)) != 0)
3119 hist_field = find_var_field(hist_data, var_name);
3124 static inline void __update_field_vars(struct tracing_map_elt *elt,
3125 struct trace_buffer *buffer,
3126 struct ring_buffer_event *rbe,
3128 struct field_var **field_vars,
3129 unsigned int n_field_vars,
3130 unsigned int field_var_str_start)
3132 struct hist_elt_data *elt_data = elt->private_data;
3133 unsigned int i, j, var_idx;
3136 /* Make sure stacktrace can fit in the string variable length */
3137 BUILD_BUG_ON((HIST_STACKTRACE_DEPTH + 1) * sizeof(long) >= STR_VAR_LEN_MAX);
3139 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
3140 struct field_var *field_var = field_vars[i];
3141 struct hist_field *var = field_var->var;
3142 struct hist_field *val = field_var->val;
3144 var_val = hist_fn_call(val, elt, buffer, rbe, rec);
3145 var_idx = var->var.idx;
3147 if (val->flags & (HIST_FIELD_FL_STRING |
3148 HIST_FIELD_FL_STACKTRACE)) {
3149 char *str = elt_data->field_var_str[j++];
3150 char *val_str = (char *)(uintptr_t)var_val;
3153 if (val->flags & HIST_FIELD_FL_STRING) {
3154 size = min(val->size, STR_VAR_LEN_MAX);
3155 strscpy(str, val_str, size);
3157 char *stack_start = str + sizeof(unsigned long);
3160 e = stack_trace_save((void *)stack_start,
3161 HIST_STACKTRACE_DEPTH,
3162 HIST_STACKTRACE_SKIP);
3163 if (e < HIST_STACKTRACE_DEPTH - 1)
3164 ((unsigned long *)stack_start)[e] = 0;
3165 *((unsigned long *)str) = e;
3167 var_val = (u64)(uintptr_t)str;
3169 tracing_map_set_var(elt, var_idx, var_val);
3173 static void update_field_vars(struct hist_trigger_data *hist_data,
3174 struct tracing_map_elt *elt,
3175 struct trace_buffer *buffer,
3176 struct ring_buffer_event *rbe,
3179 __update_field_vars(elt, buffer, rbe, rec, hist_data->field_vars,
3180 hist_data->n_field_vars, 0);
3183 static void save_track_data_vars(struct hist_trigger_data *hist_data,
3184 struct tracing_map_elt *elt,
3185 struct trace_buffer *buffer, void *rec,
3186 struct ring_buffer_event *rbe, void *key,
3187 struct action_data *data, u64 *var_ref_vals)
3189 __update_field_vars(elt, buffer, rbe, rec, hist_data->save_vars,
3190 hist_data->n_save_vars, hist_data->n_field_var_str);
3193 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
3194 struct trace_event_file *file,
3195 char *name, int size, const char *type)
3197 struct hist_field *var;
3200 if (find_var(hist_data, file, name) && !hist_data->remove) {
3201 var = ERR_PTR(-EINVAL);
3205 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3207 var = ERR_PTR(-ENOMEM);
3211 idx = tracing_map_add_var(hist_data->map);
3214 var = ERR_PTR(-EINVAL);
3219 var->flags = HIST_FIELD_FL_VAR;
3221 var->var.hist_data = var->hist_data = hist_data;
3223 var->var.name = kstrdup(name, GFP_KERNEL);
3224 var->type = kstrdup_const(type, GFP_KERNEL);
3225 if (!var->var.name || !var->type) {
3226 kfree_const(var->type);
3227 kfree(var->var.name);
3229 var = ERR_PTR(-ENOMEM);
3235 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
3236 struct trace_event_file *file,
3239 struct hist_field *val = NULL, *var = NULL;
3240 unsigned long flags = HIST_FIELD_FL_VAR;
3241 struct trace_array *tr = file->tr;
3242 struct field_var *field_var;
3245 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
3246 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
3251 val = parse_atom(hist_data, file, field_name, &flags, NULL);
3253 hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name));
3258 var = create_var(hist_data, file, field_name, val->size, val->type);
3260 hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name));
3266 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
3274 field_var->var = var;
3275 field_var->val = val;
3279 field_var = ERR_PTR(ret);
3284 * create_target_field_var - Automatically create a variable for a field
3285 * @target_hist_data: The target hist trigger
3286 * @subsys_name: Optional subsystem name
3287 * @event_name: Optional event name
3288 * @var_name: The name of the field (and the resulting variable)
3290 * Hist trigger actions fetch data from variables, not directly from
3291 * events. However, for convenience, users are allowed to directly
3292 * specify an event field in an action, which will be automatically
3293 * converted into a variable on their behalf.
3295 * This function creates a field variable with the name var_name on
3296 * the hist trigger currently being defined on the target event. If
3297 * subsys_name and event_name are specified, this function simply
3298 * verifies that they do in fact match the target event subsystem and
3301 * Return: The variable created for the field.
3303 static struct field_var *
3304 create_target_field_var(struct hist_trigger_data *target_hist_data,
3305 char *subsys_name, char *event_name, char *var_name)
3307 struct trace_event_file *file = target_hist_data->event_file;
3310 struct trace_event_call *call;
3315 call = file->event_call;
3317 if (strcmp(subsys_name, call->class->system) != 0)
3320 if (strcmp(event_name, trace_event_name(call)) != 0)
3324 return create_field_var(target_hist_data, file, var_name);
3327 static bool check_track_val_max(u64 track_val, u64 var_val)
3329 if (var_val <= track_val)
3335 static bool check_track_val_changed(u64 track_val, u64 var_val)
3337 if (var_val == track_val)
3343 static u64 get_track_val(struct hist_trigger_data *hist_data,
3344 struct tracing_map_elt *elt,
3345 struct action_data *data)
3347 unsigned int track_var_idx = data->track_data.track_var->var.idx;
3350 track_val = tracing_map_read_var(elt, track_var_idx);
3355 static void save_track_val(struct hist_trigger_data *hist_data,
3356 struct tracing_map_elt *elt,
3357 struct action_data *data, u64 var_val)
3359 unsigned int track_var_idx = data->track_data.track_var->var.idx;
3361 tracing_map_set_var(elt, track_var_idx, var_val);
3364 static void save_track_data(struct hist_trigger_data *hist_data,
3365 struct tracing_map_elt *elt,
3366 struct trace_buffer *buffer, void *rec,
3367 struct ring_buffer_event *rbe, void *key,
3368 struct action_data *data, u64 *var_ref_vals)
3370 if (data->track_data.save_data)
3371 data->track_data.save_data(hist_data, elt, buffer, rec, rbe,
3372 key, data, var_ref_vals);
3375 static bool check_track_val(struct tracing_map_elt *elt,
3376 struct action_data *data,
3379 struct hist_trigger_data *hist_data;
3382 hist_data = data->track_data.track_var->hist_data;
3383 track_val = get_track_val(hist_data, elt, data);
3385 return data->track_data.check_val(track_val, var_val);
3388 #ifdef CONFIG_TRACER_SNAPSHOT
3389 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3391 /* called with tr->max_lock held */
3392 struct track_data *track_data = tr->cond_snapshot->cond_data;
3393 struct hist_elt_data *elt_data, *track_elt_data;
3394 struct snapshot_context *context = cond_data;
3395 struct action_data *action;
3401 action = track_data->action_data;
3403 track_val = get_track_val(track_data->hist_data, context->elt,
3404 track_data->action_data);
3406 if (!action->track_data.check_val(track_data->track_val, track_val))
3409 track_data->track_val = track_val;
3410 memcpy(track_data->key, context->key, track_data->key_len);
3412 elt_data = context->elt->private_data;
3413 track_elt_data = track_data->elt.private_data;
3415 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
3417 track_data->updated = true;
3422 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3423 struct tracing_map_elt *elt,
3424 struct trace_buffer *buffer, void *rec,
3425 struct ring_buffer_event *rbe, void *key,
3426 struct action_data *data,
3429 struct trace_event_file *file = hist_data->event_file;
3430 struct snapshot_context context;
3435 tracing_snapshot_cond(file->tr, &context);
3438 static void hist_trigger_print_key(struct seq_file *m,
3439 struct hist_trigger_data *hist_data,
3441 struct tracing_map_elt *elt);
3443 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
3447 if (!hist_data->n_actions)
3450 for (i = 0; i < hist_data->n_actions; i++) {
3451 struct action_data *data = hist_data->actions[i];
3453 if (data->action == ACTION_SNAPSHOT)
3460 static void track_data_snapshot_print(struct seq_file *m,
3461 struct hist_trigger_data *hist_data)
3463 struct trace_event_file *file = hist_data->event_file;
3464 struct track_data *track_data;
3465 struct action_data *action;
3467 track_data = tracing_cond_snapshot_data(file->tr);
3471 if (!track_data->updated)
3474 action = snapshot_action(hist_data);
3478 seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n");
3479 seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
3480 action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
3481 action->track_data.var_str, track_data->track_val);
3483 seq_puts(m, "\ttriggered by event with key: ");
3484 hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
3488 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3492 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3493 struct tracing_map_elt *elt,
3494 struct trace_buffer *buffer, void *rec,
3495 struct ring_buffer_event *rbe, void *key,
3496 struct action_data *data,
3497 u64 *var_ref_vals) {}
3498 static void track_data_snapshot_print(struct seq_file *m,
3499 struct hist_trigger_data *hist_data) {}
3500 #endif /* CONFIG_TRACER_SNAPSHOT */
3502 static void track_data_print(struct seq_file *m,
3503 struct hist_trigger_data *hist_data,
3504 struct tracing_map_elt *elt,
3505 struct action_data *data)
3507 u64 track_val = get_track_val(hist_data, elt, data);
3508 unsigned int i, save_var_idx;
3510 if (data->handler == HANDLER_ONMAX)
3511 seq_printf(m, "\n\tmax: %10llu", track_val);
3512 else if (data->handler == HANDLER_ONCHANGE)
3513 seq_printf(m, "\n\tchanged: %10llu", track_val);
3515 if (data->action == ACTION_SNAPSHOT)
3518 for (i = 0; i < hist_data->n_save_vars; i++) {
3519 struct hist_field *save_val = hist_data->save_vars[i]->val;
3520 struct hist_field *save_var = hist_data->save_vars[i]->var;
3523 save_var_idx = save_var->var.idx;
3525 val = tracing_map_read_var(elt, save_var_idx);
3527 if (save_val->flags & HIST_FIELD_FL_STRING) {
3528 seq_printf(m, " %s: %-32s", save_var->var.name,
3529 (char *)(uintptr_t)(val));
3531 seq_printf(m, " %s: %10llu", save_var->var.name, val);
3535 static void ontrack_action(struct hist_trigger_data *hist_data,
3536 struct tracing_map_elt *elt,
3537 struct trace_buffer *buffer, void *rec,
3538 struct ring_buffer_event *rbe, void *key,
3539 struct action_data *data, u64 *var_ref_vals)
3541 u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
3543 if (check_track_val(elt, data, var_val)) {
3544 save_track_val(hist_data, elt, data, var_val);
3545 save_track_data(hist_data, elt, buffer, rec, rbe,
3546 key, data, var_ref_vals);
3550 static void action_data_destroy(struct action_data *data)
3554 lockdep_assert_held(&event_mutex);
3556 kfree(data->action_name);
3558 for (i = 0; i < data->n_params; i++)
3559 kfree(data->params[i]);
3561 if (data->synth_event)
3562 data->synth_event->ref--;
3564 kfree(data->synth_event_name);
3569 static void track_data_destroy(struct hist_trigger_data *hist_data,
3570 struct action_data *data)
3572 struct trace_event_file *file = hist_data->event_file;
3574 destroy_hist_field(data->track_data.track_var, 0);
3576 if (data->action == ACTION_SNAPSHOT) {
3577 struct track_data *track_data;
3579 track_data = tracing_cond_snapshot_data(file->tr);
3580 if (track_data && track_data->hist_data == hist_data) {
3581 tracing_snapshot_cond_disable(file->tr);
3582 track_data_free(track_data);
3586 kfree(data->track_data.var_str);
3588 action_data_destroy(data);
3591 static int action_create(struct hist_trigger_data *hist_data,
3592 struct action_data *data);
3594 static int track_data_create(struct hist_trigger_data *hist_data,
3595 struct action_data *data)
3597 struct hist_field *var_field, *ref_field, *track_var = NULL;
3598 struct trace_event_file *file = hist_data->event_file;
3599 struct trace_array *tr = file->tr;
3600 char *track_data_var_str;
3603 track_data_var_str = data->track_data.var_str;
3604 if (track_data_var_str[0] != '$') {
3605 hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str));
3608 track_data_var_str++;
3610 var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
3612 hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str));
3616 ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
3620 data->track_data.var_ref = ref_field;
3622 if (data->handler == HANDLER_ONMAX)
3623 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
3624 if (IS_ERR(track_var)) {
3625 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3626 ret = PTR_ERR(track_var);
3630 if (data->handler == HANDLER_ONCHANGE)
3631 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
3632 if (IS_ERR(track_var)) {
3633 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3634 ret = PTR_ERR(track_var);
3637 data->track_data.track_var = track_var;
3639 ret = action_create(hist_data, data);
3644 static int parse_action_params(struct trace_array *tr, char *params,
3645 struct action_data *data)
3647 char *param, *saved_param;
3648 bool first_param = true;
3652 if (data->n_params >= SYNTH_FIELDS_MAX) {
3653 hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0);
3658 param = strsep(¶ms, ",");
3660 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0);
3665 param = strstrip(param);
3666 if (strlen(param) < 2) {
3667 hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param));
3672 saved_param = kstrdup(param, GFP_KERNEL);
3678 if (first_param && data->use_trace_keyword) {
3679 data->synth_event_name = saved_param;
3680 first_param = false;
3683 first_param = false;
3685 data->params[data->n_params++] = saved_param;
3691 static int action_parse(struct trace_array *tr, char *str, struct action_data *data,
3692 enum handler_id handler)
3699 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3704 action_name = strsep(&str, "(");
3705 if (!action_name || !str) {
3706 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3711 if (str_has_prefix(action_name, "save")) {
3712 char *params = strsep(&str, ")");
3715 hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0);
3720 ret = parse_action_params(tr, params, data);
3724 if (handler == HANDLER_ONMAX)
3725 data->track_data.check_val = check_track_val_max;
3726 else if (handler == HANDLER_ONCHANGE)
3727 data->track_data.check_val = check_track_val_changed;
3729 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3734 data->track_data.save_data = save_track_data_vars;
3735 data->fn = ontrack_action;
3736 data->action = ACTION_SAVE;
3737 } else if (str_has_prefix(action_name, "snapshot")) {
3738 char *params = strsep(&str, ")");
3741 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params));
3746 if (handler == HANDLER_ONMAX)
3747 data->track_data.check_val = check_track_val_max;
3748 else if (handler == HANDLER_ONCHANGE)
3749 data->track_data.check_val = check_track_val_changed;
3751 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3756 data->track_data.save_data = save_track_data_snapshot;
3757 data->fn = ontrack_action;
3758 data->action = ACTION_SNAPSHOT;
3760 char *params = strsep(&str, ")");
3762 if (str_has_prefix(action_name, "trace"))
3763 data->use_trace_keyword = true;
3766 ret = parse_action_params(tr, params, data);
3771 if (handler == HANDLER_ONMAX)
3772 data->track_data.check_val = check_track_val_max;
3773 else if (handler == HANDLER_ONCHANGE)
3774 data->track_data.check_val = check_track_val_changed;
3776 if (handler != HANDLER_ONMATCH) {
3777 data->track_data.save_data = action_trace;
3778 data->fn = ontrack_action;
3780 data->fn = action_trace;
3782 data->action = ACTION_TRACE;
3785 data->action_name = kstrdup(action_name, GFP_KERNEL);
3786 if (!data->action_name) {
3791 data->handler = handler;
3796 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
3797 char *str, enum handler_id handler)
3799 struct action_data *data;
3803 data = kzalloc(sizeof(*data), GFP_KERNEL);
3805 return ERR_PTR(-ENOMEM);
3807 var_str = strsep(&str, ")");
3808 if (!var_str || !str) {
3813 data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
3814 if (!data->track_data.var_str) {
3819 ret = action_parse(hist_data->event_file->tr, str, data, handler);
3825 track_data_destroy(hist_data, data);
3826 data = ERR_PTR(ret);
3830 static void onmatch_destroy(struct action_data *data)
3832 kfree(data->match_data.event);
3833 kfree(data->match_data.event_system);
3835 action_data_destroy(data);
3838 static void destroy_field_var(struct field_var *field_var)
3843 destroy_hist_field(field_var->var, 0);
3844 destroy_hist_field(field_var->val, 0);
3849 static void destroy_field_vars(struct hist_trigger_data *hist_data)
3853 for (i = 0; i < hist_data->n_field_vars; i++)
3854 destroy_field_var(hist_data->field_vars[i]);
3856 for (i = 0; i < hist_data->n_save_vars; i++)
3857 destroy_field_var(hist_data->save_vars[i]);
3860 static void save_field_var(struct hist_trigger_data *hist_data,
3861 struct field_var *field_var)
3863 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
3865 /* Stack traces are saved in the string storage too */
3866 if (field_var->val->flags & (HIST_FIELD_FL_STRING | HIST_FIELD_FL_STACKTRACE))
3867 hist_data->n_field_var_str++;
3871 static int check_synth_field(struct synth_event *event,
3872 struct hist_field *hist_field,
3873 unsigned int field_pos)
3875 struct synth_field *field;
3877 if (field_pos >= event->n_fields)
3880 field = event->fields[field_pos];
3883 * A dynamic string synth field can accept static or
3884 * dynamic. A static string synth field can only accept a
3885 * same-sized static string, which is checked for later.
3887 if (strstr(hist_field->type, "char[") && field->is_string
3888 && field->is_dynamic)
3891 if (strstr(hist_field->type, "long[") && field->is_stack)
3894 if (strcmp(field->type, hist_field->type) != 0) {
3895 if (field->size != hist_field->size ||
3896 (!field->is_string && field->is_signed != hist_field->is_signed))
3903 static struct hist_field *
3904 trace_action_find_var(struct hist_trigger_data *hist_data,
3905 struct action_data *data,
3906 char *system, char *event, char *var)
3908 struct trace_array *tr = hist_data->event_file->tr;
3909 struct hist_field *hist_field;
3911 var++; /* skip '$' */
3913 hist_field = find_target_event_var(hist_data, system, event, var);
3915 if (!system && data->handler == HANDLER_ONMATCH) {
3916 system = data->match_data.event_system;
3917 event = data->match_data.event;
3920 hist_field = find_event_var(hist_data, system, event, var);
3924 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var));
3929 static struct hist_field *
3930 trace_action_create_field_var(struct hist_trigger_data *hist_data,
3931 struct action_data *data, char *system,
3932 char *event, char *var)
3934 struct hist_field *hist_field = NULL;
3935 struct field_var *field_var;
3938 * First try to create a field var on the target event (the
3939 * currently being defined). This will create a variable for
3940 * unqualified fields on the target event, or if qualified,
3941 * target fields that have qualified names matching the target.
3943 field_var = create_target_field_var(hist_data, system, event, var);
3945 if (field_var && !IS_ERR(field_var)) {
3946 save_field_var(hist_data, field_var);
3947 hist_field = field_var->var;
3951 * If no explicit system.event is specified, default to
3952 * looking for fields on the onmatch(system.event.xxx)
3955 if (!system && data->handler == HANDLER_ONMATCH) {
3956 system = data->match_data.event_system;
3957 event = data->match_data.event;
3963 * At this point, we're looking at a field on another
3964 * event. Because we can't modify a hist trigger on
3965 * another event to add a variable for a field, we need
3966 * to create a new trigger on that event and create the
3967 * variable at the same time.
3969 hist_field = create_field_var_hist(hist_data, system, event, var);
3970 if (IS_ERR(hist_field))
3976 destroy_field_var(field_var);
3981 static int trace_action_create(struct hist_trigger_data *hist_data,
3982 struct action_data *data)
3984 struct trace_array *tr = hist_data->event_file->tr;
3985 char *event_name, *param, *system = NULL;
3986 struct hist_field *hist_field, *var_ref;
3988 unsigned int field_pos = 0;
3989 struct synth_event *event;
3990 char *synth_event_name;
3991 int var_ref_idx, ret = 0;
3993 lockdep_assert_held(&event_mutex);
3995 /* Sanity check to avoid out-of-bound write on 'data->var_ref_idx' */
3996 if (data->n_params > SYNTH_FIELDS_MAX)
3999 if (data->use_trace_keyword)
4000 synth_event_name = data->synth_event_name;
4002 synth_event_name = data->action_name;
4004 event = find_synth_event(synth_event_name);
4006 hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name));
4012 for (i = 0; i < data->n_params; i++) {
4015 p = param = kstrdup(data->params[i], GFP_KERNEL);
4021 system = strsep(¶m, ".");
4023 param = (char *)system;
4024 system = event_name = NULL;
4026 event_name = strsep(¶m, ".");
4034 if (param[0] == '$')
4035 hist_field = trace_action_find_var(hist_data, data,
4039 hist_field = trace_action_create_field_var(hist_data,
4051 if (check_synth_field(event, hist_field, field_pos) == 0) {
4052 var_ref = create_var_ref(hist_data, hist_field,
4053 system, event_name);
4060 var_ref_idx = find_var_ref_idx(hist_data, var_ref);
4061 if (WARN_ON(var_ref_idx < 0)) {
4067 data->var_ref_idx[i] = var_ref_idx;
4074 hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param));
4080 if (field_pos != event->n_fields) {
4081 hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name));
4086 data->synth_event = event;
4095 static int action_create(struct hist_trigger_data *hist_data,
4096 struct action_data *data)
4098 struct trace_event_file *file = hist_data->event_file;
4099 struct trace_array *tr = file->tr;
4100 struct track_data *track_data;
4101 struct field_var *field_var;
4106 if (data->action == ACTION_TRACE)
4107 return trace_action_create(hist_data, data);
4109 if (data->action == ACTION_SNAPSHOT) {
4110 track_data = track_data_alloc(hist_data->key_size, data, hist_data);
4111 if (IS_ERR(track_data)) {
4112 ret = PTR_ERR(track_data);
4116 ret = tracing_snapshot_cond_enable(file->tr, track_data,
4117 cond_snapshot_update);
4119 track_data_free(track_data);
4124 if (data->action == ACTION_SAVE) {
4125 if (hist_data->n_save_vars) {
4127 hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0);
4131 for (i = 0; i < data->n_params; i++) {
4132 param = kstrdup(data->params[i], GFP_KERNEL);
4138 field_var = create_target_field_var(hist_data, NULL, NULL, param);
4139 if (IS_ERR(field_var)) {
4140 hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL,
4142 ret = PTR_ERR(field_var);
4147 hist_data->save_vars[hist_data->n_save_vars++] = field_var;
4148 if (field_var->val->flags &
4149 (HIST_FIELD_FL_STRING | HIST_FIELD_FL_STACKTRACE))
4150 hist_data->n_save_var_str++;
4158 static int onmatch_create(struct hist_trigger_data *hist_data,
4159 struct action_data *data)
4161 return action_create(hist_data, data);
4164 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
4166 char *match_event, *match_event_system;
4167 struct action_data *data;
4170 data = kzalloc(sizeof(*data), GFP_KERNEL);
4172 return ERR_PTR(-ENOMEM);
4174 match_event = strsep(&str, ")");
4175 if (!match_event || !str) {
4176 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event));
4180 match_event_system = strsep(&match_event, ".");
4182 hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system));
4186 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
4187 hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event));
4191 data->match_data.event = kstrdup(match_event, GFP_KERNEL);
4192 if (!data->match_data.event) {
4197 data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
4198 if (!data->match_data.event_system) {
4203 ret = action_parse(tr, str, data, HANDLER_ONMATCH);
4209 onmatch_destroy(data);
4210 data = ERR_PTR(ret);
4214 static int create_hitcount_val(struct hist_trigger_data *hist_data)
4216 hist_data->fields[HITCOUNT_IDX] =
4217 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
4218 if (!hist_data->fields[HITCOUNT_IDX])
4221 hist_data->n_vals++;
4222 hist_data->n_fields++;
4224 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
4230 static int __create_val_field(struct hist_trigger_data *hist_data,
4231 unsigned int val_idx,
4232 struct trace_event_file *file,
4233 char *var_name, char *field_str,
4234 unsigned long flags)
4236 struct hist_field *hist_field;
4237 int ret = 0, n_subexprs = 0;
4239 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, &n_subexprs);
4240 if (IS_ERR(hist_field)) {
4241 ret = PTR_ERR(hist_field);
4245 /* values and variables should not have some modifiers */
4246 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4248 if (hist_field->flags & (HIST_FIELD_FL_GRAPH | HIST_FIELD_FL_PERCENT |
4249 HIST_FIELD_FL_BUCKET | HIST_FIELD_FL_LOG2))
4253 if (hist_field->flags & (HIST_FIELD_FL_GRAPH | HIST_FIELD_FL_PERCENT |
4254 HIST_FIELD_FL_BUCKET | HIST_FIELD_FL_LOG2 |
4255 HIST_FIELD_FL_SYM | HIST_FIELD_FL_SYM_OFFSET |
4256 HIST_FIELD_FL_SYSCALL | HIST_FIELD_FL_STACKTRACE))
4260 hist_data->fields[val_idx] = hist_field;
4262 ++hist_data->n_vals;
4263 ++hist_data->n_fields;
4265 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4270 hist_err(file->tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(field_str));
4274 static int create_val_field(struct hist_trigger_data *hist_data,
4275 unsigned int val_idx,
4276 struct trace_event_file *file,
4279 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
4282 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
4285 static const char no_comm[] = "(no comm)";
4287 static u64 hist_field_execname(struct hist_field *hist_field,
4288 struct tracing_map_elt *elt,
4289 struct trace_buffer *buffer,
4290 struct ring_buffer_event *rbe,
4293 struct hist_elt_data *elt_data;
4295 if (WARN_ON_ONCE(!elt))
4296 return (u64)(unsigned long)no_comm;
4298 elt_data = elt->private_data;
4300 if (WARN_ON_ONCE(!elt_data->comm))
4301 return (u64)(unsigned long)no_comm;
4303 return (u64)(unsigned long)(elt_data->comm);
4306 static u64 hist_field_stack(struct hist_field *hist_field,
4307 struct tracing_map_elt *elt,
4308 struct trace_buffer *buffer,
4309 struct ring_buffer_event *rbe,
4312 u32 str_item = *(u32 *)(event + hist_field->field->offset);
4313 int str_loc = str_item & 0xffff;
4314 char *addr = (char *)(event + str_loc);
4316 return (u64)(unsigned long)addr;
4319 static u64 hist_fn_call(struct hist_field *hist_field,
4320 struct tracing_map_elt *elt,
4321 struct trace_buffer *buffer,
4322 struct ring_buffer_event *rbe,
4325 switch (hist_field->fn_num) {
4326 case HIST_FIELD_FN_VAR_REF:
4327 return hist_field_var_ref(hist_field, elt, buffer, rbe, event);
4328 case HIST_FIELD_FN_COUNTER:
4329 return hist_field_counter(hist_field, elt, buffer, rbe, event);
4330 case HIST_FIELD_FN_CONST:
4331 return hist_field_const(hist_field, elt, buffer, rbe, event);
4332 case HIST_FIELD_FN_LOG2:
4333 return hist_field_log2(hist_field, elt, buffer, rbe, event);
4334 case HIST_FIELD_FN_BUCKET:
4335 return hist_field_bucket(hist_field, elt, buffer, rbe, event);
4336 case HIST_FIELD_FN_TIMESTAMP:
4337 return hist_field_timestamp(hist_field, elt, buffer, rbe, event);
4338 case HIST_FIELD_FN_CPU:
4339 return hist_field_cpu(hist_field, elt, buffer, rbe, event);
4340 case HIST_FIELD_FN_STRING:
4341 return hist_field_string(hist_field, elt, buffer, rbe, event);
4342 case HIST_FIELD_FN_DYNSTRING:
4343 return hist_field_dynstring(hist_field, elt, buffer, rbe, event);
4344 case HIST_FIELD_FN_RELDYNSTRING:
4345 return hist_field_reldynstring(hist_field, elt, buffer, rbe, event);
4346 case HIST_FIELD_FN_PSTRING:
4347 return hist_field_pstring(hist_field, elt, buffer, rbe, event);
4348 case HIST_FIELD_FN_S64:
4349 return hist_field_s64(hist_field, elt, buffer, rbe, event);
4350 case HIST_FIELD_FN_U64:
4351 return hist_field_u64(hist_field, elt, buffer, rbe, event);
4352 case HIST_FIELD_FN_S32:
4353 return hist_field_s32(hist_field, elt, buffer, rbe, event);
4354 case HIST_FIELD_FN_U32:
4355 return hist_field_u32(hist_field, elt, buffer, rbe, event);
4356 case HIST_FIELD_FN_S16:
4357 return hist_field_s16(hist_field, elt, buffer, rbe, event);
4358 case HIST_FIELD_FN_U16:
4359 return hist_field_u16(hist_field, elt, buffer, rbe, event);
4360 case HIST_FIELD_FN_S8:
4361 return hist_field_s8(hist_field, elt, buffer, rbe, event);
4362 case HIST_FIELD_FN_U8:
4363 return hist_field_u8(hist_field, elt, buffer, rbe, event);
4364 case HIST_FIELD_FN_UMINUS:
4365 return hist_field_unary_minus(hist_field, elt, buffer, rbe, event);
4366 case HIST_FIELD_FN_MINUS:
4367 return hist_field_minus(hist_field, elt, buffer, rbe, event);
4368 case HIST_FIELD_FN_PLUS:
4369 return hist_field_plus(hist_field, elt, buffer, rbe, event);
4370 case HIST_FIELD_FN_DIV:
4371 return hist_field_div(hist_field, elt, buffer, rbe, event);
4372 case HIST_FIELD_FN_MULT:
4373 return hist_field_mult(hist_field, elt, buffer, rbe, event);
4374 case HIST_FIELD_FN_DIV_POWER2:
4375 return div_by_power_of_two(hist_field, elt, buffer, rbe, event);
4376 case HIST_FIELD_FN_DIV_NOT_POWER2:
4377 return div_by_not_power_of_two(hist_field, elt, buffer, rbe, event);
4378 case HIST_FIELD_FN_DIV_MULT_SHIFT:
4379 return div_by_mult_and_shift(hist_field, elt, buffer, rbe, event);
4380 case HIST_FIELD_FN_EXECNAME:
4381 return hist_field_execname(hist_field, elt, buffer, rbe, event);
4382 case HIST_FIELD_FN_STACK:
4383 return hist_field_stack(hist_field, elt, buffer, rbe, event);
4389 /* Convert a var that points to common_pid.execname to a string */
4390 static void update_var_execname(struct hist_field *hist_field)
4392 hist_field->flags = HIST_FIELD_FL_STRING | HIST_FIELD_FL_VAR |
4393 HIST_FIELD_FL_EXECNAME;
4394 hist_field->size = MAX_FILTER_STR_VAL;
4395 hist_field->is_signed = 0;
4397 kfree_const(hist_field->type);
4398 hist_field->type = "char[]";
4400 hist_field->fn_num = HIST_FIELD_FN_EXECNAME;
4403 static int create_var_field(struct hist_trigger_data *hist_data,
4404 unsigned int val_idx,
4405 struct trace_event_file *file,
4406 char *var_name, char *expr_str)
4408 struct trace_array *tr = hist_data->event_file->tr;
4409 unsigned long flags = 0;
4412 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4415 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
4416 hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name));
4420 flags |= HIST_FIELD_FL_VAR;
4421 hist_data->n_vars++;
4422 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
4425 ret = __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
4427 if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_EXECNAME)
4428 update_var_execname(hist_data->fields[val_idx]);
4430 if (!ret && hist_data->fields[val_idx]->flags &
4431 (HIST_FIELD_FL_STRING | HIST_FIELD_FL_STACKTRACE))
4432 hist_data->fields[val_idx]->var_str_idx = hist_data->n_var_str++;
4437 static int create_val_fields(struct hist_trigger_data *hist_data,
4438 struct trace_event_file *file)
4440 unsigned int i, j = 1, n_hitcount = 0;
4441 char *fields_str, *field_str;
4444 ret = create_hitcount_val(hist_data);
4448 fields_str = hist_data->attrs->vals_str;
4452 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
4453 j < TRACING_MAP_VALS_MAX; i++) {
4454 field_str = strsep(&fields_str, ",");
4458 if (strcmp(field_str, "hitcount") == 0) {
4463 ret = create_val_field(hist_data, j++, file, field_str);
4468 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
4471 /* There is only raw hitcount but nohitcount suppresses it. */
4472 if (j == 1 && hist_data->attrs->no_hitcount) {
4473 hist_err(hist_data->event_file->tr, HIST_ERR_NEED_NOHC_VAL, 0);
4480 static int create_key_field(struct hist_trigger_data *hist_data,
4481 unsigned int key_idx,
4482 unsigned int key_offset,
4483 struct trace_event_file *file,
4486 struct trace_array *tr = hist_data->event_file->tr;
4487 struct hist_field *hist_field = NULL;
4488 unsigned long flags = 0;
4489 unsigned int key_size;
4490 int ret = 0, n_subexprs = 0;
4492 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
4495 flags |= HIST_FIELD_FL_KEY;
4497 if (strcmp(field_str, "stacktrace") == 0) {
4498 flags |= HIST_FIELD_FL_STACKTRACE;
4499 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
4500 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
4502 hist_field = parse_expr(hist_data, file, field_str, flags,
4504 if (IS_ERR(hist_field)) {
4505 ret = PTR_ERR(hist_field);
4509 if (field_has_hist_vars(hist_field, 0)) {
4510 hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str));
4511 destroy_hist_field(hist_field, 0);
4516 key_size = hist_field->size;
4519 hist_data->fields[key_idx] = hist_field;
4521 key_size = ALIGN(key_size, sizeof(u64));
4522 hist_data->fields[key_idx]->size = key_size;
4523 hist_data->fields[key_idx]->offset = key_offset;
4525 hist_data->key_size += key_size;
4527 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
4532 hist_data->n_keys++;
4533 hist_data->n_fields++;
4535 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
4543 static int create_key_fields(struct hist_trigger_data *hist_data,
4544 struct trace_event_file *file)
4546 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
4547 char *fields_str, *field_str;
4550 fields_str = hist_data->attrs->keys_str;
4554 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
4555 field_str = strsep(&fields_str, ",");
4558 ret = create_key_field(hist_data, i, key_offset,
4573 static int create_var_fields(struct hist_trigger_data *hist_data,
4574 struct trace_event_file *file)
4576 unsigned int i, j = hist_data->n_vals;
4579 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
4581 for (i = 0; i < n_vars; i++) {
4582 char *var_name = hist_data->attrs->var_defs.name[i];
4583 char *expr = hist_data->attrs->var_defs.expr[i];
4585 ret = create_var_field(hist_data, j++, file, var_name, expr);
4593 static void free_var_defs(struct hist_trigger_data *hist_data)
4597 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
4598 kfree(hist_data->attrs->var_defs.name[i]);
4599 kfree(hist_data->attrs->var_defs.expr[i]);
4602 hist_data->attrs->var_defs.n_vars = 0;
4605 static int parse_var_defs(struct hist_trigger_data *hist_data)
4607 struct trace_array *tr = hist_data->event_file->tr;
4608 char *s, *str, *var_name, *field_str;
4609 unsigned int i, j, n_vars = 0;
4612 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
4613 str = hist_data->attrs->assignment_str[i];
4614 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
4615 field_str = strsep(&str, ",");
4619 var_name = strsep(&field_str, "=");
4620 if (!var_name || !field_str) {
4621 hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT,
4627 if (n_vars == TRACING_MAP_VARS_MAX) {
4628 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name));
4633 s = kstrdup(var_name, GFP_KERNEL);
4638 hist_data->attrs->var_defs.name[n_vars] = s;
4640 s = kstrdup(field_str, GFP_KERNEL);
4642 kfree(hist_data->attrs->var_defs.name[n_vars]);
4643 hist_data->attrs->var_defs.name[n_vars] = NULL;
4647 hist_data->attrs->var_defs.expr[n_vars++] = s;
4649 hist_data->attrs->var_defs.n_vars = n_vars;
4655 free_var_defs(hist_data);
4660 static int create_hist_fields(struct hist_trigger_data *hist_data,
4661 struct trace_event_file *file)
4665 ret = parse_var_defs(hist_data);
4669 ret = create_val_fields(hist_data, file);
4673 ret = create_var_fields(hist_data, file);
4677 ret = create_key_fields(hist_data, file);
4680 free_var_defs(hist_data);
4685 static int is_descending(struct trace_array *tr, const char *str)
4690 if (strcmp(str, "descending") == 0)
4693 if (strcmp(str, "ascending") == 0)
4696 hist_err(tr, HIST_ERR_INVALID_SORT_MODIFIER, errpos((char *)str));
4701 static int create_sort_keys(struct hist_trigger_data *hist_data)
4703 struct trace_array *tr = hist_data->event_file->tr;
4704 char *fields_str = hist_data->attrs->sort_key_str;
4705 struct tracing_map_sort_key *sort_key;
4706 int descending, ret = 0;
4707 unsigned int i, j, k;
4709 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
4714 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
4715 struct hist_field *hist_field;
4716 char *field_str, *field_name;
4717 const char *test_name;
4719 sort_key = &hist_data->sort_keys[i];
4721 field_str = strsep(&fields_str, ",");
4727 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4731 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4732 hist_err(tr, HIST_ERR_TOO_MANY_SORT_FIELDS, errpos("sort="));
4737 field_name = strsep(&field_str, ".");
4738 if (!field_name || !*field_name) {
4740 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4744 if (strcmp(field_name, "hitcount") == 0) {
4745 descending = is_descending(tr, field_str);
4746 if (descending < 0) {
4750 sort_key->descending = descending;
4754 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4757 hist_field = hist_data->fields[j];
4758 if (hist_field->flags & HIST_FIELD_FL_VAR)
4763 test_name = hist_field_name(hist_field, 0);
4765 if (strcmp(field_name, test_name) == 0) {
4766 sort_key->field_idx = idx;
4767 descending = is_descending(tr, field_str);
4768 if (descending < 0) {
4772 sort_key->descending = descending;
4776 if (j == hist_data->n_fields) {
4778 hist_err(tr, HIST_ERR_INVALID_SORT_FIELD, errpos(field_name));
4783 hist_data->n_sort_keys = i;
4788 static void destroy_actions(struct hist_trigger_data *hist_data)
4792 for (i = 0; i < hist_data->n_actions; i++) {
4793 struct action_data *data = hist_data->actions[i];
4795 if (data->handler == HANDLER_ONMATCH)
4796 onmatch_destroy(data);
4797 else if (data->handler == HANDLER_ONMAX ||
4798 data->handler == HANDLER_ONCHANGE)
4799 track_data_destroy(hist_data, data);
4805 static int parse_actions(struct hist_trigger_data *hist_data)
4807 struct trace_array *tr = hist_data->event_file->tr;
4808 struct action_data *data;
4814 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4815 str = hist_data->attrs->action_str[i];
4817 if ((len = str_has_prefix(str, "onmatch("))) {
4818 char *action_str = str + len;
4820 data = onmatch_parse(tr, action_str);
4822 ret = PTR_ERR(data);
4825 } else if ((len = str_has_prefix(str, "onmax("))) {
4826 char *action_str = str + len;
4828 data = track_data_parse(hist_data, action_str,
4831 ret = PTR_ERR(data);
4834 } else if ((len = str_has_prefix(str, "onchange("))) {
4835 char *action_str = str + len;
4837 data = track_data_parse(hist_data, action_str,
4840 ret = PTR_ERR(data);
4848 hist_data->actions[hist_data->n_actions++] = data;
4854 static int create_actions(struct hist_trigger_data *hist_data)
4856 struct action_data *data;
4860 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4861 data = hist_data->actions[i];
4863 if (data->handler == HANDLER_ONMATCH) {
4864 ret = onmatch_create(hist_data, data);
4867 } else if (data->handler == HANDLER_ONMAX ||
4868 data->handler == HANDLER_ONCHANGE) {
4869 ret = track_data_create(hist_data, data);
4881 static void print_actions(struct seq_file *m,
4882 struct hist_trigger_data *hist_data,
4883 struct tracing_map_elt *elt)
4887 for (i = 0; i < hist_data->n_actions; i++) {
4888 struct action_data *data = hist_data->actions[i];
4890 if (data->action == ACTION_SNAPSHOT)
4893 if (data->handler == HANDLER_ONMAX ||
4894 data->handler == HANDLER_ONCHANGE)
4895 track_data_print(m, hist_data, elt, data);
4899 static void print_action_spec(struct seq_file *m,
4900 struct hist_trigger_data *hist_data,
4901 struct action_data *data)
4905 if (data->action == ACTION_SAVE) {
4906 for (i = 0; i < hist_data->n_save_vars; i++) {
4907 seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
4908 if (i < hist_data->n_save_vars - 1)
4911 } else if (data->action == ACTION_TRACE) {
4912 if (data->use_trace_keyword)
4913 seq_printf(m, "%s", data->synth_event_name);
4914 for (i = 0; i < data->n_params; i++) {
4915 if (i || data->use_trace_keyword)
4917 seq_printf(m, "%s", data->params[i]);
4922 static void print_track_data_spec(struct seq_file *m,
4923 struct hist_trigger_data *hist_data,
4924 struct action_data *data)
4926 if (data->handler == HANDLER_ONMAX)
4927 seq_puts(m, ":onmax(");
4928 else if (data->handler == HANDLER_ONCHANGE)
4929 seq_puts(m, ":onchange(");
4930 seq_printf(m, "%s", data->track_data.var_str);
4931 seq_printf(m, ").%s(", data->action_name);
4933 print_action_spec(m, hist_data, data);
4938 static void print_onmatch_spec(struct seq_file *m,
4939 struct hist_trigger_data *hist_data,
4940 struct action_data *data)
4942 seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
4943 data->match_data.event);
4945 seq_printf(m, "%s(", data->action_name);
4947 print_action_spec(m, hist_data, data);
4952 static bool actions_match(struct hist_trigger_data *hist_data,
4953 struct hist_trigger_data *hist_data_test)
4957 if (hist_data->n_actions != hist_data_test->n_actions)
4960 for (i = 0; i < hist_data->n_actions; i++) {
4961 struct action_data *data = hist_data->actions[i];
4962 struct action_data *data_test = hist_data_test->actions[i];
4963 char *action_name, *action_name_test;
4965 if (data->handler != data_test->handler)
4967 if (data->action != data_test->action)
4970 if (data->n_params != data_test->n_params)
4973 for (j = 0; j < data->n_params; j++) {
4974 if (strcmp(data->params[j], data_test->params[j]) != 0)
4978 if (data->use_trace_keyword)
4979 action_name = data->synth_event_name;
4981 action_name = data->action_name;
4983 if (data_test->use_trace_keyword)
4984 action_name_test = data_test->synth_event_name;
4986 action_name_test = data_test->action_name;
4988 if (strcmp(action_name, action_name_test) != 0)
4991 if (data->handler == HANDLER_ONMATCH) {
4992 if (strcmp(data->match_data.event_system,
4993 data_test->match_data.event_system) != 0)
4995 if (strcmp(data->match_data.event,
4996 data_test->match_data.event) != 0)
4998 } else if (data->handler == HANDLER_ONMAX ||
4999 data->handler == HANDLER_ONCHANGE) {
5000 if (strcmp(data->track_data.var_str,
5001 data_test->track_data.var_str) != 0)
5010 static void print_actions_spec(struct seq_file *m,
5011 struct hist_trigger_data *hist_data)
5015 for (i = 0; i < hist_data->n_actions; i++) {
5016 struct action_data *data = hist_data->actions[i];
5018 if (data->handler == HANDLER_ONMATCH)
5019 print_onmatch_spec(m, hist_data, data);
5020 else if (data->handler == HANDLER_ONMAX ||
5021 data->handler == HANDLER_ONCHANGE)
5022 print_track_data_spec(m, hist_data, data);
5026 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
5030 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5031 kfree(hist_data->field_var_hists[i]->cmd);
5032 kfree(hist_data->field_var_hists[i]);
5036 static void destroy_hist_data(struct hist_trigger_data *hist_data)
5041 destroy_hist_trigger_attrs(hist_data->attrs);
5042 destroy_hist_fields(hist_data);
5043 tracing_map_destroy(hist_data->map);
5045 destroy_actions(hist_data);
5046 destroy_field_vars(hist_data);
5047 destroy_field_var_hists(hist_data);
5052 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
5054 struct tracing_map *map = hist_data->map;
5055 struct ftrace_event_field *field;
5056 struct hist_field *hist_field;
5059 for_each_hist_field(i, hist_data) {
5060 hist_field = hist_data->fields[i];
5061 if (hist_field->flags & HIST_FIELD_FL_KEY) {
5062 tracing_map_cmp_fn_t cmp_fn;
5064 field = hist_field->field;
5066 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
5067 cmp_fn = tracing_map_cmp_none;
5068 else if (!field || hist_field->flags & HIST_FIELD_FL_CPU)
5069 cmp_fn = tracing_map_cmp_num(hist_field->size,
5070 hist_field->is_signed);
5071 else if (is_string_field(field))
5072 cmp_fn = tracing_map_cmp_string;
5074 cmp_fn = tracing_map_cmp_num(field->size,
5076 idx = tracing_map_add_key_field(map,
5079 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
5080 idx = tracing_map_add_sum_field(map);
5085 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5086 idx = tracing_map_add_var(map);
5089 hist_field->var.idx = idx;
5090 hist_field->var.hist_data = hist_data;
5097 static struct hist_trigger_data *
5098 create_hist_data(unsigned int map_bits,
5099 struct hist_trigger_attrs *attrs,
5100 struct trace_event_file *file,
5103 const struct tracing_map_ops *map_ops = NULL;
5104 struct hist_trigger_data *hist_data;
5107 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
5109 return ERR_PTR(-ENOMEM);
5111 hist_data->attrs = attrs;
5112 hist_data->remove = remove;
5113 hist_data->event_file = file;
5115 ret = parse_actions(hist_data);
5119 ret = create_hist_fields(hist_data, file);
5123 ret = create_sort_keys(hist_data);
5127 map_ops = &hist_trigger_elt_data_ops;
5129 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
5130 map_ops, hist_data);
5131 if (IS_ERR(hist_data->map)) {
5132 ret = PTR_ERR(hist_data->map);
5133 hist_data->map = NULL;
5137 ret = create_tracing_map_fields(hist_data);
5143 hist_data->attrs = NULL;
5145 destroy_hist_data(hist_data);
5147 hist_data = ERR_PTR(ret);
5152 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
5153 struct tracing_map_elt *elt,
5154 struct trace_buffer *buffer, void *rec,
5155 struct ring_buffer_event *rbe,
5158 struct hist_elt_data *elt_data;
5159 struct hist_field *hist_field;
5160 unsigned int i, var_idx;
5163 elt_data = elt->private_data;
5164 elt_data->var_ref_vals = var_ref_vals;
5166 for_each_hist_val_field(i, hist_data) {
5167 hist_field = hist_data->fields[i];
5168 hist_val = hist_fn_call(hist_field, elt, buffer, rbe, rec);
5169 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5170 var_idx = hist_field->var.idx;
5172 if (hist_field->flags &
5173 (HIST_FIELD_FL_STRING | HIST_FIELD_FL_STACKTRACE)) {
5174 unsigned int str_start, var_str_idx, idx;
5175 char *str, *val_str;
5178 str_start = hist_data->n_field_var_str +
5179 hist_data->n_save_var_str;
5180 var_str_idx = hist_field->var_str_idx;
5181 idx = str_start + var_str_idx;
5183 str = elt_data->field_var_str[idx];
5184 val_str = (char *)(uintptr_t)hist_val;
5186 if (hist_field->flags & HIST_FIELD_FL_STRING) {
5187 size = min(hist_field->size, STR_VAR_LEN_MAX);
5188 strscpy(str, val_str, size);
5190 char *stack_start = str + sizeof(unsigned long);
5193 e = stack_trace_save((void *)stack_start,
5194 HIST_STACKTRACE_DEPTH,
5195 HIST_STACKTRACE_SKIP);
5196 if (e < HIST_STACKTRACE_DEPTH - 1)
5197 ((unsigned long *)stack_start)[e] = 0;
5198 *((unsigned long *)str) = e;
5200 hist_val = (u64)(uintptr_t)str;
5202 tracing_map_set_var(elt, var_idx, hist_val);
5205 tracing_map_update_sum(elt, i, hist_val);
5208 for_each_hist_key_field(i, hist_data) {
5209 hist_field = hist_data->fields[i];
5210 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5211 hist_val = hist_fn_call(hist_field, elt, buffer, rbe, rec);
5212 var_idx = hist_field->var.idx;
5213 tracing_map_set_var(elt, var_idx, hist_val);
5217 update_field_vars(hist_data, elt, buffer, rbe, rec);
5220 static inline void add_to_key(char *compound_key, void *key,
5221 struct hist_field *key_field, void *rec)
5223 size_t size = key_field->size;
5225 if (key_field->flags & HIST_FIELD_FL_STRING) {
5226 struct ftrace_event_field *field;
5228 field = key_field->field;
5229 if (field->filter_type == FILTER_DYN_STRING ||
5230 field->filter_type == FILTER_RDYN_STRING)
5231 size = *(u32 *)(rec + field->offset) >> 16;
5232 else if (field->filter_type == FILTER_STATIC_STRING)
5235 /* ensure NULL-termination */
5236 if (size > key_field->size - 1)
5237 size = key_field->size - 1;
5239 strncpy(compound_key + key_field->offset, (char *)key, size);
5241 memcpy(compound_key + key_field->offset, key, size);
5245 hist_trigger_actions(struct hist_trigger_data *hist_data,
5246 struct tracing_map_elt *elt,
5247 struct trace_buffer *buffer, void *rec,
5248 struct ring_buffer_event *rbe, void *key,
5251 struct action_data *data;
5254 for (i = 0; i < hist_data->n_actions; i++) {
5255 data = hist_data->actions[i];
5256 data->fn(hist_data, elt, buffer, rec, rbe, key, data, var_ref_vals);
5260 static void event_hist_trigger(struct event_trigger_data *data,
5261 struct trace_buffer *buffer, void *rec,
5262 struct ring_buffer_event *rbe)
5264 struct hist_trigger_data *hist_data = data->private_data;
5265 bool use_compound_key = (hist_data->n_keys > 1);
5266 unsigned long entries[HIST_STACKTRACE_DEPTH];
5267 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
5268 char compound_key[HIST_KEY_SIZE_MAX];
5269 struct tracing_map_elt *elt = NULL;
5270 struct hist_field *key_field;
5278 memset(compound_key, 0, hist_data->key_size);
5280 for_each_hist_key_field(i, hist_data) {
5281 key_field = hist_data->fields[i];
5283 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5284 memset(entries, 0, HIST_STACKTRACE_SIZE);
5285 if (key_field->field) {
5286 unsigned long *stack, n_entries;
5288 field_contents = hist_fn_call(key_field, elt, buffer, rbe, rec);
5289 stack = (unsigned long *)(long)field_contents;
5291 memcpy(entries, ++stack, n_entries * sizeof(unsigned long));
5293 stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
5294 HIST_STACKTRACE_SKIP);
5298 field_contents = hist_fn_call(key_field, elt, buffer, rbe, rec);
5299 if (key_field->flags & HIST_FIELD_FL_STRING) {
5300 key = (void *)(unsigned long)field_contents;
5301 use_compound_key = true;
5303 key = (void *)&field_contents;
5306 if (use_compound_key)
5307 add_to_key(compound_key, key, key_field, rec);
5310 if (use_compound_key)
5313 if (hist_data->n_var_refs &&
5314 !resolve_var_refs(hist_data, key, var_ref_vals, false))
5317 elt = tracing_map_insert(hist_data->map, key);
5321 hist_trigger_elt_update(hist_data, elt, buffer, rec, rbe, var_ref_vals);
5323 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
5324 hist_trigger_actions(hist_data, elt, buffer, rec, rbe, key, var_ref_vals);
5327 static void hist_trigger_stacktrace_print(struct seq_file *m,
5328 unsigned long *stacktrace_entries,
5329 unsigned int max_entries)
5331 unsigned int spaces = 8;
5334 for (i = 0; i < max_entries; i++) {
5335 if (!stacktrace_entries[i])
5338 seq_printf(m, "%*c", 1 + spaces, ' ');
5339 seq_printf(m, "%pS\n", (void*)stacktrace_entries[i]);
5343 static void hist_trigger_print_key(struct seq_file *m,
5344 struct hist_trigger_data *hist_data,
5346 struct tracing_map_elt *elt)
5348 struct hist_field *key_field;
5349 bool multiline = false;
5350 const char *field_name;
5356 for_each_hist_key_field(i, hist_data) {
5357 key_field = hist_data->fields[i];
5359 if (i > hist_data->n_vals)
5362 field_name = hist_field_name(key_field, 0);
5364 if (key_field->flags & HIST_FIELD_FL_HEX) {
5365 uval = *(u64 *)(key + key_field->offset);
5366 seq_printf(m, "%s: %llx", field_name, uval);
5367 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
5368 uval = *(u64 *)(key + key_field->offset);
5369 seq_printf(m, "%s: [%llx] %-45ps", field_name,
5370 uval, (void *)(uintptr_t)uval);
5371 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
5372 uval = *(u64 *)(key + key_field->offset);
5373 seq_printf(m, "%s: [%llx] %-55pS", field_name,
5374 uval, (void *)(uintptr_t)uval);
5375 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
5376 struct hist_elt_data *elt_data = elt->private_data;
5379 if (WARN_ON_ONCE(!elt_data))
5382 comm = elt_data->comm;
5384 uval = *(u64 *)(key + key_field->offset);
5385 seq_printf(m, "%s: %-16s[%10llu]", field_name,
5387 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
5388 const char *syscall_name;
5390 uval = *(u64 *)(key + key_field->offset);
5391 syscall_name = get_syscall_name(uval);
5393 syscall_name = "unknown_syscall";
5395 seq_printf(m, "%s: %-30s[%3llu]", field_name,
5396 syscall_name, uval);
5397 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5398 if (key_field->field)
5399 seq_printf(m, "%s.stacktrace", key_field->field->name);
5401 seq_puts(m, "common_stacktrace:\n");
5402 hist_trigger_stacktrace_print(m,
5403 key + key_field->offset,
5404 HIST_STACKTRACE_DEPTH);
5406 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
5407 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
5408 *(u64 *)(key + key_field->offset));
5409 } else if (key_field->flags & HIST_FIELD_FL_BUCKET) {
5410 unsigned long buckets = key_field->buckets;
5411 uval = *(u64 *)(key + key_field->offset);
5412 seq_printf(m, "%s: ~ %llu-%llu", field_name,
5413 uval, uval + buckets -1);
5414 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
5415 seq_printf(m, "%s: %-50s", field_name,
5416 (char *)(key + key_field->offset));
5418 uval = *(u64 *)(key + key_field->offset);
5419 seq_printf(m, "%s: %10llu", field_name, uval);
5429 /* Get the 100 times of the percentage of @val in @total */
5430 static inline unsigned int __get_percentage(u64 val, u64 total)
5435 if (val < (U64_MAX / 10000))
5436 return (unsigned int)div64_ul(val * 10000, total);
5438 total = div64_u64(total, 10000);
5442 return (unsigned int)div64_ul(val, total);
5444 return val ? UINT_MAX : 0;
5447 #define BAR_CHAR '#'
5449 static inline const char *__fill_bar_str(char *buf, int size, u64 val, u64 max)
5451 unsigned int len = __get_percentage(val, max);
5454 if (len == UINT_MAX) {
5455 snprintf(buf, size, "[ERROR]");
5459 len = len * size / 10000;
5460 for (i = 0; i < len && i < size; i++)
5469 struct hist_val_stat {
5474 static void hist_trigger_print_val(struct seq_file *m, unsigned int idx,
5475 const char *field_name, unsigned long flags,
5476 struct hist_val_stat *stats,
5477 struct tracing_map_elt *elt)
5479 u64 val = tracing_map_read_sum(elt, idx);
5483 if (flags & HIST_FIELD_FL_PERCENT) {
5484 pc = __get_percentage(val, stats[idx].total);
5486 seq_printf(m, " %s (%%):[ERROR]", field_name);
5488 seq_printf(m, " %s (%%): %3u.%02u", field_name,
5489 pc / 100, pc % 100);
5490 } else if (flags & HIST_FIELD_FL_GRAPH) {
5491 seq_printf(m, " %s: %20s", field_name,
5492 __fill_bar_str(bar, 20, val, stats[idx].max));
5493 } else if (flags & HIST_FIELD_FL_HEX) {
5494 seq_printf(m, " %s: %10llx", field_name, val);
5496 seq_printf(m, " %s: %10llu", field_name, val);
5500 static void hist_trigger_entry_print(struct seq_file *m,
5501 struct hist_trigger_data *hist_data,
5502 struct hist_val_stat *stats,
5504 struct tracing_map_elt *elt)
5506 const char *field_name;
5507 unsigned int i = HITCOUNT_IDX;
5508 unsigned long flags;
5510 hist_trigger_print_key(m, hist_data, key, elt);
5512 /* At first, show the raw hitcount if !nohitcount */
5513 if (!hist_data->attrs->no_hitcount)
5514 hist_trigger_print_val(m, i, "hitcount", 0, stats, elt);
5516 for (i = 1; i < hist_data->n_vals; i++) {
5517 field_name = hist_field_name(hist_data->fields[i], 0);
5518 flags = hist_data->fields[i]->flags;
5519 if (flags & HIST_FIELD_FL_VAR || flags & HIST_FIELD_FL_EXPR)
5523 hist_trigger_print_val(m, i, field_name, flags, stats, elt);
5526 print_actions(m, hist_data, elt);
5531 static int print_entries(struct seq_file *m,
5532 struct hist_trigger_data *hist_data)
5534 struct tracing_map_sort_entry **sort_entries = NULL;
5535 struct tracing_map *map = hist_data->map;
5536 int i, j, n_entries;
5537 struct hist_val_stat *stats = NULL;
5540 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
5541 hist_data->n_sort_keys,
5546 /* Calculate the max and the total for each field if needed. */
5547 for (j = 0; j < hist_data->n_vals; j++) {
5548 if (!(hist_data->fields[j]->flags &
5549 (HIST_FIELD_FL_PERCENT | HIST_FIELD_FL_GRAPH)))
5552 stats = kcalloc(hist_data->n_vals, sizeof(*stats),
5555 n_entries = -ENOMEM;
5559 for (i = 0; i < n_entries; i++) {
5560 val = tracing_map_read_sum(sort_entries[i]->elt, j);
5561 stats[j].total += val;
5562 if (stats[j].max < val)
5567 for (i = 0; i < n_entries; i++)
5568 hist_trigger_entry_print(m, hist_data, stats,
5569 sort_entries[i]->key,
5570 sort_entries[i]->elt);
5574 tracing_map_destroy_sort_entries(sort_entries, n_entries);
5579 static void hist_trigger_show(struct seq_file *m,
5580 struct event_trigger_data *data, int n)
5582 struct hist_trigger_data *hist_data;
5586 seq_puts(m, "\n\n");
5588 seq_puts(m, "# event histogram\n#\n# trigger info: ");
5589 data->ops->print(m, data);
5590 seq_puts(m, "#\n\n");
5592 hist_data = data->private_data;
5593 n_entries = print_entries(m, hist_data);
5597 track_data_snapshot_print(m, hist_data);
5599 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
5600 (u64)atomic64_read(&hist_data->map->hits),
5601 n_entries, (u64)atomic64_read(&hist_data->map->drops));
5604 static int hist_show(struct seq_file *m, void *v)
5606 struct event_trigger_data *data;
5607 struct trace_event_file *event_file;
5610 mutex_lock(&event_mutex);
5612 event_file = event_file_data(m->private);
5613 if (unlikely(!event_file)) {
5618 list_for_each_entry(data, &event_file->triggers, list) {
5619 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5620 hist_trigger_show(m, data, n++);
5624 mutex_unlock(&event_mutex);
5629 static int event_hist_open(struct inode *inode, struct file *file)
5633 ret = security_locked_down(LOCKDOWN_TRACEFS);
5637 return single_open(file, hist_show, file);
5640 const struct file_operations event_hist_fops = {
5641 .open = event_hist_open,
5643 .llseek = seq_lseek,
5644 .release = single_release,
5647 #ifdef CONFIG_HIST_TRIGGERS_DEBUG
5648 static void hist_field_debug_show_flags(struct seq_file *m,
5649 unsigned long flags)
5651 seq_puts(m, " flags:\n");
5653 if (flags & HIST_FIELD_FL_KEY)
5654 seq_puts(m, " HIST_FIELD_FL_KEY\n");
5655 else if (flags & HIST_FIELD_FL_HITCOUNT)
5656 seq_puts(m, " VAL: HIST_FIELD_FL_HITCOUNT\n");
5657 else if (flags & HIST_FIELD_FL_VAR)
5658 seq_puts(m, " HIST_FIELD_FL_VAR\n");
5659 else if (flags & HIST_FIELD_FL_VAR_REF)
5660 seq_puts(m, " HIST_FIELD_FL_VAR_REF\n");
5662 seq_puts(m, " VAL: normal u64 value\n");
5664 if (flags & HIST_FIELD_FL_ALIAS)
5665 seq_puts(m, " HIST_FIELD_FL_ALIAS\n");
5666 else if (flags & HIST_FIELD_FL_CONST)
5667 seq_puts(m, " HIST_FIELD_FL_CONST\n");
5670 static int hist_field_debug_show(struct seq_file *m,
5671 struct hist_field *field, unsigned long flags)
5673 if ((field->flags & flags) != flags) {
5674 seq_printf(m, "ERROR: bad flags - %lx\n", flags);
5678 hist_field_debug_show_flags(m, field->flags);
5680 seq_printf(m, " ftrace_event_field name: %s\n",
5681 field->field->name);
5683 if (field->flags & HIST_FIELD_FL_VAR) {
5684 seq_printf(m, " var.name: %s\n", field->var.name);
5685 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
5689 if (field->flags & HIST_FIELD_FL_CONST)
5690 seq_printf(m, " constant: %llu\n", field->constant);
5692 if (field->flags & HIST_FIELD_FL_ALIAS)
5693 seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n",
5694 field->var_ref_idx);
5696 if (field->flags & HIST_FIELD_FL_VAR_REF) {
5697 seq_printf(m, " name: %s\n", field->name);
5698 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
5700 seq_printf(m, " var.hist_data: %p\n", field->var.hist_data);
5701 seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n",
5702 field->var_ref_idx);
5704 seq_printf(m, " system: %s\n", field->system);
5705 if (field->event_name)
5706 seq_printf(m, " event_name: %s\n", field->event_name);
5709 seq_printf(m, " type: %s\n", field->type);
5710 seq_printf(m, " size: %u\n", field->size);
5711 seq_printf(m, " is_signed: %u\n", field->is_signed);
5716 static int field_var_debug_show(struct seq_file *m,
5717 struct field_var *field_var, unsigned int i,
5720 const char *vars_name = save_vars ? "save_vars" : "field_vars";
5721 struct hist_field *field;
5724 seq_printf(m, "\n hist_data->%s[%d]:\n", vars_name, i);
5726 field = field_var->var;
5728 seq_printf(m, "\n %s[%d].var:\n", vars_name, i);
5730 hist_field_debug_show_flags(m, field->flags);
5731 seq_printf(m, " var.name: %s\n", field->var.name);
5732 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
5735 field = field_var->val;
5737 seq_printf(m, "\n %s[%d].val:\n", vars_name, i);
5739 seq_printf(m, " ftrace_event_field name: %s\n",
5740 field->field->name);
5746 seq_printf(m, " type: %s\n", field->type);
5747 seq_printf(m, " size: %u\n", field->size);
5748 seq_printf(m, " is_signed: %u\n", field->is_signed);
5753 static int hist_action_debug_show(struct seq_file *m,
5754 struct action_data *data, int i)
5758 if (data->handler == HANDLER_ONMAX ||
5759 data->handler == HANDLER_ONCHANGE) {
5760 seq_printf(m, "\n hist_data->actions[%d].track_data.var_ref:\n", i);
5761 ret = hist_field_debug_show(m, data->track_data.var_ref,
5762 HIST_FIELD_FL_VAR_REF);
5766 seq_printf(m, "\n hist_data->actions[%d].track_data.track_var:\n", i);
5767 ret = hist_field_debug_show(m, data->track_data.track_var,
5773 if (data->handler == HANDLER_ONMATCH) {
5774 seq_printf(m, "\n hist_data->actions[%d].match_data.event_system: %s\n",
5775 i, data->match_data.event_system);
5776 seq_printf(m, " hist_data->actions[%d].match_data.event: %s\n",
5777 i, data->match_data.event);
5783 static int hist_actions_debug_show(struct seq_file *m,
5784 struct hist_trigger_data *hist_data)
5788 if (hist_data->n_actions)
5789 seq_puts(m, "\n action tracking variables (for onmax()/onchange()/onmatch()):\n");
5791 for (i = 0; i < hist_data->n_actions; i++) {
5792 struct action_data *action = hist_data->actions[i];
5794 ret = hist_action_debug_show(m, action, i);
5799 if (hist_data->n_save_vars)
5800 seq_puts(m, "\n save action variables (save() params):\n");
5802 for (i = 0; i < hist_data->n_save_vars; i++) {
5803 ret = field_var_debug_show(m, hist_data->save_vars[i], i, true);
5811 static void hist_trigger_debug_show(struct seq_file *m,
5812 struct event_trigger_data *data, int n)
5814 struct hist_trigger_data *hist_data;
5818 seq_puts(m, "\n\n");
5820 seq_puts(m, "# event histogram\n#\n# trigger info: ");
5821 data->ops->print(m, data);
5822 seq_puts(m, "#\n\n");
5824 hist_data = data->private_data;
5826 seq_printf(m, "hist_data: %p\n\n", hist_data);
5827 seq_printf(m, " n_vals: %u\n", hist_data->n_vals);
5828 seq_printf(m, " n_keys: %u\n", hist_data->n_keys);
5829 seq_printf(m, " n_fields: %u\n", hist_data->n_fields);
5831 seq_puts(m, "\n val fields:\n\n");
5833 seq_puts(m, " hist_data->fields[0]:\n");
5834 ret = hist_field_debug_show(m, hist_data->fields[0],
5835 HIST_FIELD_FL_HITCOUNT);
5839 for (i = 1; i < hist_data->n_vals; i++) {
5840 seq_printf(m, "\n hist_data->fields[%d]:\n", i);
5841 ret = hist_field_debug_show(m, hist_data->fields[i], 0);
5846 seq_puts(m, "\n key fields:\n");
5848 for (i = hist_data->n_vals; i < hist_data->n_fields; i++) {
5849 seq_printf(m, "\n hist_data->fields[%d]:\n", i);
5850 ret = hist_field_debug_show(m, hist_data->fields[i],
5856 if (hist_data->n_var_refs)
5857 seq_puts(m, "\n variable reference fields:\n");
5859 for (i = 0; i < hist_data->n_var_refs; i++) {
5860 seq_printf(m, "\n hist_data->var_refs[%d]:\n", i);
5861 ret = hist_field_debug_show(m, hist_data->var_refs[i],
5862 HIST_FIELD_FL_VAR_REF);
5867 if (hist_data->n_field_vars)
5868 seq_puts(m, "\n field variables:\n");
5870 for (i = 0; i < hist_data->n_field_vars; i++) {
5871 ret = field_var_debug_show(m, hist_data->field_vars[i], i, false);
5876 ret = hist_actions_debug_show(m, hist_data);
5881 static int hist_debug_show(struct seq_file *m, void *v)
5883 struct event_trigger_data *data;
5884 struct trace_event_file *event_file;
5887 mutex_lock(&event_mutex);
5889 event_file = event_file_data(m->private);
5890 if (unlikely(!event_file)) {
5895 list_for_each_entry(data, &event_file->triggers, list) {
5896 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5897 hist_trigger_debug_show(m, data, n++);
5901 mutex_unlock(&event_mutex);
5906 static int event_hist_debug_open(struct inode *inode, struct file *file)
5910 ret = security_locked_down(LOCKDOWN_TRACEFS);
5914 return single_open(file, hist_debug_show, file);
5917 const struct file_operations event_hist_debug_fops = {
5918 .open = event_hist_debug_open,
5920 .llseek = seq_lseek,
5921 .release = single_release,
5925 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
5927 const char *field_name = hist_field_name(hist_field, 0);
5929 if (hist_field->var.name)
5930 seq_printf(m, "%s=", hist_field->var.name);
5932 if (hist_field->flags & HIST_FIELD_FL_CPU)
5933 seq_puts(m, "common_cpu");
5934 else if (hist_field->flags & HIST_FIELD_FL_CONST)
5935 seq_printf(m, "%llu", hist_field->constant);
5936 else if (field_name) {
5937 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
5938 hist_field->flags & HIST_FIELD_FL_ALIAS)
5940 seq_printf(m, "%s", field_name);
5941 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
5942 seq_puts(m, "common_timestamp");
5944 if (hist_field->flags) {
5945 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
5946 !(hist_field->flags & HIST_FIELD_FL_EXPR) &&
5947 !(hist_field->flags & HIST_FIELD_FL_STACKTRACE)) {
5948 const char *flags = get_hist_field_flags(hist_field);
5951 seq_printf(m, ".%s", flags);
5954 if (hist_field->buckets)
5955 seq_printf(m, "=%ld", hist_field->buckets);
5958 static int event_hist_trigger_print(struct seq_file *m,
5959 struct event_trigger_data *data)
5961 struct hist_trigger_data *hist_data = data->private_data;
5962 struct hist_field *field;
5963 bool have_var = false;
5964 bool show_val = false;
5967 seq_puts(m, HIST_PREFIX);
5970 seq_printf(m, "%s:", data->name);
5972 seq_puts(m, "keys=");
5974 for_each_hist_key_field(i, hist_data) {
5975 field = hist_data->fields[i];
5977 if (i > hist_data->n_vals)
5980 if (field->flags & HIST_FIELD_FL_STACKTRACE) {
5982 seq_printf(m, "%s.stacktrace", field->field->name);
5984 seq_puts(m, "common_stacktrace");
5986 hist_field_print(m, field);
5989 seq_puts(m, ":vals=");
5991 for_each_hist_val_field(i, hist_data) {
5992 field = hist_data->fields[i];
5993 if (field->flags & HIST_FIELD_FL_VAR) {
5998 if (i == HITCOUNT_IDX) {
5999 if (hist_data->attrs->no_hitcount)
6001 seq_puts(m, "hitcount");
6005 hist_field_print(m, field);
6015 for_each_hist_val_field(i, hist_data) {
6016 field = hist_data->fields[i];
6018 if (field->flags & HIST_FIELD_FL_VAR) {
6021 hist_field_print(m, field);
6026 seq_puts(m, ":sort=");
6028 for (i = 0; i < hist_data->n_sort_keys; i++) {
6029 struct tracing_map_sort_key *sort_key;
6030 unsigned int idx, first_key_idx;
6033 first_key_idx = hist_data->n_vals - hist_data->n_vars;
6035 sort_key = &hist_data->sort_keys[i];
6036 idx = sort_key->field_idx;
6038 if (WARN_ON(idx >= HIST_FIELDS_MAX))
6044 if (idx == HITCOUNT_IDX)
6045 seq_puts(m, "hitcount");
6047 if (idx >= first_key_idx)
6048 idx += hist_data->n_vars;
6049 hist_field_print(m, hist_data->fields[idx]);
6052 if (sort_key->descending)
6053 seq_puts(m, ".descending");
6055 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
6056 if (hist_data->enable_timestamps)
6057 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
6058 if (hist_data->attrs->no_hitcount)
6059 seq_puts(m, ":nohitcount");
6061 print_actions_spec(m, hist_data);
6063 if (data->filter_str)
6064 seq_printf(m, " if %s", data->filter_str);
6067 seq_puts(m, " [paused]");
6069 seq_puts(m, " [active]");
6076 static int event_hist_trigger_init(struct event_trigger_data *data)
6078 struct hist_trigger_data *hist_data = data->private_data;
6080 if (!data->ref && hist_data->attrs->name)
6081 save_named_trigger(hist_data->attrs->name, data);
6088 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
6090 struct trace_event_file *file;
6095 for (i = 0; i < hist_data->n_field_var_hists; i++) {
6096 file = hist_data->field_var_hists[i]->hist_data->event_file;
6097 cmd = hist_data->field_var_hists[i]->cmd;
6098 ret = event_hist_trigger_parse(&trigger_hist_cmd, file,
6099 "!hist", "hist", cmd);
6100 WARN_ON_ONCE(ret < 0);
6104 static void event_hist_trigger_free(struct event_trigger_data *data)
6106 struct hist_trigger_data *hist_data = data->private_data;
6108 if (WARN_ON_ONCE(data->ref <= 0))
6114 del_named_trigger(data);
6116 trigger_data_free(data);
6118 remove_hist_vars(hist_data);
6120 unregister_field_var_hists(hist_data);
6122 destroy_hist_data(hist_data);
6126 static struct event_trigger_ops event_hist_trigger_ops = {
6127 .trigger = event_hist_trigger,
6128 .print = event_hist_trigger_print,
6129 .init = event_hist_trigger_init,
6130 .free = event_hist_trigger_free,
6133 static int event_hist_trigger_named_init(struct event_trigger_data *data)
6137 save_named_trigger(data->named_data->name, data);
6139 event_hist_trigger_init(data->named_data);
6144 static void event_hist_trigger_named_free(struct event_trigger_data *data)
6146 if (WARN_ON_ONCE(data->ref <= 0))
6149 event_hist_trigger_free(data->named_data);
6153 del_named_trigger(data);
6154 trigger_data_free(data);
6158 static struct event_trigger_ops event_hist_trigger_named_ops = {
6159 .trigger = event_hist_trigger,
6160 .print = event_hist_trigger_print,
6161 .init = event_hist_trigger_named_init,
6162 .free = event_hist_trigger_named_free,
6165 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
6168 return &event_hist_trigger_ops;
6171 static void hist_clear(struct event_trigger_data *data)
6173 struct hist_trigger_data *hist_data = data->private_data;
6176 pause_named_trigger(data);
6178 tracepoint_synchronize_unregister();
6180 tracing_map_clear(hist_data->map);
6183 unpause_named_trigger(data);
6186 static bool compatible_field(struct ftrace_event_field *field,
6187 struct ftrace_event_field *test_field)
6189 if (field == test_field)
6191 if (field == NULL || test_field == NULL)
6193 if (strcmp(field->name, test_field->name) != 0)
6195 if (strcmp(field->type, test_field->type) != 0)
6197 if (field->size != test_field->size)
6199 if (field->is_signed != test_field->is_signed)
6205 static bool hist_trigger_match(struct event_trigger_data *data,
6206 struct event_trigger_data *data_test,
6207 struct event_trigger_data *named_data,
6210 struct tracing_map_sort_key *sort_key, *sort_key_test;
6211 struct hist_trigger_data *hist_data, *hist_data_test;
6212 struct hist_field *key_field, *key_field_test;
6215 if (named_data && (named_data != data_test) &&
6216 (named_data != data_test->named_data))
6219 if (!named_data && is_named_trigger(data_test))
6222 hist_data = data->private_data;
6223 hist_data_test = data_test->private_data;
6225 if (hist_data->n_vals != hist_data_test->n_vals ||
6226 hist_data->n_fields != hist_data_test->n_fields ||
6227 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
6230 if (!ignore_filter) {
6231 if ((data->filter_str && !data_test->filter_str) ||
6232 (!data->filter_str && data_test->filter_str))
6236 for_each_hist_field(i, hist_data) {
6237 key_field = hist_data->fields[i];
6238 key_field_test = hist_data_test->fields[i];
6240 if (key_field->flags != key_field_test->flags)
6242 if (!compatible_field(key_field->field, key_field_test->field))
6244 if (key_field->offset != key_field_test->offset)
6246 if (key_field->size != key_field_test->size)
6248 if (key_field->is_signed != key_field_test->is_signed)
6250 if (!!key_field->var.name != !!key_field_test->var.name)
6252 if (key_field->var.name &&
6253 strcmp(key_field->var.name, key_field_test->var.name) != 0)
6257 for (i = 0; i < hist_data->n_sort_keys; i++) {
6258 sort_key = &hist_data->sort_keys[i];
6259 sort_key_test = &hist_data_test->sort_keys[i];
6261 if (sort_key->field_idx != sort_key_test->field_idx ||
6262 sort_key->descending != sort_key_test->descending)
6266 if (!ignore_filter && data->filter_str &&
6267 (strcmp(data->filter_str, data_test->filter_str) != 0))
6270 if (!actions_match(hist_data, hist_data_test))
6276 static bool existing_hist_update_only(char *glob,
6277 struct event_trigger_data *data,
6278 struct trace_event_file *file)
6280 struct hist_trigger_data *hist_data = data->private_data;
6281 struct event_trigger_data *test, *named_data = NULL;
6282 bool updated = false;
6284 if (!hist_data->attrs->pause && !hist_data->attrs->cont &&
6285 !hist_data->attrs->clear)
6288 if (hist_data->attrs->name) {
6289 named_data = find_named_trigger(hist_data->attrs->name);
6291 if (!hist_trigger_match(data, named_data, named_data,
6297 if (hist_data->attrs->name && !named_data)
6300 list_for_each_entry(test, &file->triggers, list) {
6301 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6302 if (!hist_trigger_match(data, test, named_data, false))
6304 if (hist_data->attrs->pause)
6305 test->paused = true;
6306 else if (hist_data->attrs->cont)
6307 test->paused = false;
6308 else if (hist_data->attrs->clear)
6318 static int hist_register_trigger(char *glob,
6319 struct event_trigger_data *data,
6320 struct trace_event_file *file)
6322 struct hist_trigger_data *hist_data = data->private_data;
6323 struct event_trigger_data *test, *named_data = NULL;
6324 struct trace_array *tr = file->tr;
6327 if (hist_data->attrs->name) {
6328 named_data = find_named_trigger(hist_data->attrs->name);
6330 if (!hist_trigger_match(data, named_data, named_data,
6332 hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name));
6339 if (hist_data->attrs->name && !named_data)
6342 lockdep_assert_held(&event_mutex);
6344 list_for_each_entry(test, &file->triggers, list) {
6345 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6346 if (hist_trigger_match(data, test, named_data, false)) {
6347 hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
6354 if (hist_data->attrs->cont || hist_data->attrs->clear) {
6355 hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0);
6360 if (hist_data->attrs->pause)
6361 data->paused = true;
6364 data->private_data = named_data->private_data;
6365 set_named_trigger_data(data, named_data);
6366 data->ops = &event_hist_trigger_named_ops;
6369 if (data->ops->init) {
6370 ret = data->ops->init(data);
6375 if (hist_data->enable_timestamps) {
6376 char *clock = hist_data->attrs->clock;
6378 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
6380 hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock));
6384 tracing_set_filter_buffering(file->tr, true);
6388 destroy_hist_data(hist_data);
6393 static int hist_trigger_enable(struct event_trigger_data *data,
6394 struct trace_event_file *file)
6398 list_add_tail_rcu(&data->list, &file->triggers);
6400 update_cond_flag(file);
6402 if (trace_event_trigger_enable_disable(file, 1) < 0) {
6403 list_del_rcu(&data->list);
6404 update_cond_flag(file);
6411 static bool have_hist_trigger_match(struct event_trigger_data *data,
6412 struct trace_event_file *file)
6414 struct hist_trigger_data *hist_data = data->private_data;
6415 struct event_trigger_data *test, *named_data = NULL;
6418 lockdep_assert_held(&event_mutex);
6420 if (hist_data->attrs->name)
6421 named_data = find_named_trigger(hist_data->attrs->name);
6423 list_for_each_entry(test, &file->triggers, list) {
6424 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6425 if (hist_trigger_match(data, test, named_data, false)) {
6435 static bool hist_trigger_check_refs(struct event_trigger_data *data,
6436 struct trace_event_file *file)
6438 struct hist_trigger_data *hist_data = data->private_data;
6439 struct event_trigger_data *test, *named_data = NULL;
6441 lockdep_assert_held(&event_mutex);
6443 if (hist_data->attrs->name)
6444 named_data = find_named_trigger(hist_data->attrs->name);
6446 list_for_each_entry(test, &file->triggers, list) {
6447 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6448 if (!hist_trigger_match(data, test, named_data, false))
6450 hist_data = test->private_data;
6451 if (check_var_refs(hist_data))
6460 static void hist_unregister_trigger(char *glob,
6461 struct event_trigger_data *data,
6462 struct trace_event_file *file)
6464 struct event_trigger_data *test = NULL, *iter, *named_data = NULL;
6465 struct hist_trigger_data *hist_data = data->private_data;
6467 lockdep_assert_held(&event_mutex);
6469 if (hist_data->attrs->name)
6470 named_data = find_named_trigger(hist_data->attrs->name);
6472 list_for_each_entry(iter, &file->triggers, list) {
6473 if (iter->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6474 if (!hist_trigger_match(data, iter, named_data, false))
6477 list_del_rcu(&test->list);
6478 trace_event_trigger_enable_disable(file, 0);
6479 update_cond_flag(file);
6484 if (test && test->ops->free)
6485 test->ops->free(test);
6487 if (hist_data->enable_timestamps) {
6488 if (!hist_data->remove || test)
6489 tracing_set_filter_buffering(file->tr, false);
6493 static bool hist_file_check_refs(struct trace_event_file *file)
6495 struct hist_trigger_data *hist_data;
6496 struct event_trigger_data *test;
6498 lockdep_assert_held(&event_mutex);
6500 list_for_each_entry(test, &file->triggers, list) {
6501 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6502 hist_data = test->private_data;
6503 if (check_var_refs(hist_data))
6511 static void hist_unreg_all(struct trace_event_file *file)
6513 struct event_trigger_data *test, *n;
6514 struct hist_trigger_data *hist_data;
6515 struct synth_event *se;
6516 const char *se_name;
6518 lockdep_assert_held(&event_mutex);
6520 if (hist_file_check_refs(file))
6523 list_for_each_entry_safe(test, n, &file->triggers, list) {
6524 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6525 hist_data = test->private_data;
6526 list_del_rcu(&test->list);
6527 trace_event_trigger_enable_disable(file, 0);
6529 se_name = trace_event_name(file->event_call);
6530 se = find_synth_event(se_name);
6534 update_cond_flag(file);
6535 if (hist_data->enable_timestamps)
6536 tracing_set_filter_buffering(file->tr, false);
6537 if (test->ops->free)
6538 test->ops->free(test);
6543 static int event_hist_trigger_parse(struct event_command *cmd_ops,
6544 struct trace_event_file *file,
6545 char *glob, char *cmd,
6546 char *param_and_filter)
6548 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
6549 struct event_trigger_data *trigger_data;
6550 struct hist_trigger_attrs *attrs;
6551 struct hist_trigger_data *hist_data;
6552 char *param, *filter, *p, *start;
6553 struct synth_event *se;
6554 const char *se_name;
6558 lockdep_assert_held(&event_mutex);
6565 last_cmd_set(file, param_and_filter);
6568 remove = event_trigger_check_remove(glob);
6570 if (event_trigger_empty_param(param_and_filter))
6574 * separate the trigger from the filter (k:v [if filter])
6575 * allowing for whitespace in the trigger
6577 p = param = param_and_filter;
6579 p = strstr(p, "if");
6582 if (p == param_and_filter)
6584 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
6588 if (p >= param_and_filter + strlen(param_and_filter) - (sizeof("if") - 1) - 1)
6590 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
6601 filter = strstrip(p);
6602 param = strstrip(param);
6606 * To simplify arithmetic expression parsing, replace occurrences of
6607 * '.sym-offset' modifier with '.symXoffset'
6609 start = strstr(param, ".sym-offset");
6612 start = strstr(start + 11, ".sym-offset");
6615 attrs = parse_hist_trigger_attrs(file->tr, param);
6617 return PTR_ERR(attrs);
6619 if (attrs->map_bits)
6620 hist_trigger_bits = attrs->map_bits;
6622 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
6623 if (IS_ERR(hist_data)) {
6624 destroy_hist_trigger_attrs(attrs);
6625 return PTR_ERR(hist_data);
6628 trigger_data = event_trigger_alloc(cmd_ops, cmd, param, hist_data);
6629 if (!trigger_data) {
6634 ret = event_trigger_set_filter(cmd_ops, file, filter, trigger_data);
6639 if (!have_hist_trigger_match(trigger_data, file))
6642 if (hist_trigger_check_refs(trigger_data, file)) {
6647 event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
6648 se_name = trace_event_name(file->event_call);
6649 se = find_synth_event(se_name);
6656 if (existing_hist_update_only(glob, trigger_data, file))
6659 ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
6663 if (get_named_trigger_data(trigger_data))
6666 if (has_hist_vars(hist_data))
6667 save_hist_vars(hist_data);
6669 ret = create_actions(hist_data);
6673 ret = tracing_map_init(hist_data->map);
6677 ret = hist_trigger_enable(trigger_data, file);
6681 se_name = trace_event_name(file->event_call);
6682 se = find_synth_event(se_name);
6686 if (ret == 0 && glob[0])
6691 event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
6693 event_trigger_reset_filter(cmd_ops, trigger_data);
6695 remove_hist_vars(hist_data);
6697 kfree(trigger_data);
6699 destroy_hist_data(hist_data);
6703 static struct event_command trigger_hist_cmd = {
6705 .trigger_type = ETT_EVENT_HIST,
6706 .flags = EVENT_CMD_FL_NEEDS_REC,
6707 .parse = event_hist_trigger_parse,
6708 .reg = hist_register_trigger,
6709 .unreg = hist_unregister_trigger,
6710 .unreg_all = hist_unreg_all,
6711 .get_trigger_ops = event_hist_get_trigger_ops,
6712 .set_filter = set_trigger_filter,
6715 __init int register_trigger_hist_cmd(void)
6719 ret = register_event_command(&trigger_hist_cmd);
6726 hist_enable_trigger(struct event_trigger_data *data,
6727 struct trace_buffer *buffer, void *rec,
6728 struct ring_buffer_event *event)
6730 struct enable_trigger_data *enable_data = data->private_data;
6731 struct event_trigger_data *test;
6733 list_for_each_entry_rcu(test, &enable_data->file->triggers, list,
6734 lockdep_is_held(&event_mutex)) {
6735 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6736 if (enable_data->enable)
6737 test->paused = false;
6739 test->paused = true;
6745 hist_enable_count_trigger(struct event_trigger_data *data,
6746 struct trace_buffer *buffer, void *rec,
6747 struct ring_buffer_event *event)
6752 if (data->count != -1)
6755 hist_enable_trigger(data, buffer, rec, event);
6758 static struct event_trigger_ops hist_enable_trigger_ops = {
6759 .trigger = hist_enable_trigger,
6760 .print = event_enable_trigger_print,
6761 .init = event_trigger_init,
6762 .free = event_enable_trigger_free,
6765 static struct event_trigger_ops hist_enable_count_trigger_ops = {
6766 .trigger = hist_enable_count_trigger,
6767 .print = event_enable_trigger_print,
6768 .init = event_trigger_init,
6769 .free = event_enable_trigger_free,
6772 static struct event_trigger_ops hist_disable_trigger_ops = {
6773 .trigger = hist_enable_trigger,
6774 .print = event_enable_trigger_print,
6775 .init = event_trigger_init,
6776 .free = event_enable_trigger_free,
6779 static struct event_trigger_ops hist_disable_count_trigger_ops = {
6780 .trigger = hist_enable_count_trigger,
6781 .print = event_enable_trigger_print,
6782 .init = event_trigger_init,
6783 .free = event_enable_trigger_free,
6786 static struct event_trigger_ops *
6787 hist_enable_get_trigger_ops(char *cmd, char *param)
6789 struct event_trigger_ops *ops;
6792 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
6795 ops = param ? &hist_enable_count_trigger_ops :
6796 &hist_enable_trigger_ops;
6798 ops = param ? &hist_disable_count_trigger_ops :
6799 &hist_disable_trigger_ops;
6804 static void hist_enable_unreg_all(struct trace_event_file *file)
6806 struct event_trigger_data *test, *n;
6808 list_for_each_entry_safe(test, n, &file->triggers, list) {
6809 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
6810 list_del_rcu(&test->list);
6811 update_cond_flag(file);
6812 trace_event_trigger_enable_disable(file, 0);
6813 if (test->ops->free)
6814 test->ops->free(test);
6819 static struct event_command trigger_hist_enable_cmd = {
6820 .name = ENABLE_HIST_STR,
6821 .trigger_type = ETT_HIST_ENABLE,
6822 .parse = event_enable_trigger_parse,
6823 .reg = event_enable_register_trigger,
6824 .unreg = event_enable_unregister_trigger,
6825 .unreg_all = hist_enable_unreg_all,
6826 .get_trigger_ops = hist_enable_get_trigger_ops,
6827 .set_filter = set_trigger_filter,
6830 static struct event_command trigger_hist_disable_cmd = {
6831 .name = DISABLE_HIST_STR,
6832 .trigger_type = ETT_HIST_ENABLE,
6833 .parse = event_enable_trigger_parse,
6834 .reg = event_enable_register_trigger,
6835 .unreg = event_enable_unregister_trigger,
6836 .unreg_all = hist_enable_unreg_all,
6837 .get_trigger_ops = hist_enable_get_trigger_ops,
6838 .set_filter = set_trigger_filter,
6841 static __init void unregister_trigger_hist_enable_disable_cmds(void)
6843 unregister_event_command(&trigger_hist_enable_cmd);
6844 unregister_event_command(&trigger_hist_disable_cmd);
6847 __init int register_trigger_hist_enable_disable_cmds(void)
6851 ret = register_event_command(&trigger_hist_enable_cmd);
6852 if (WARN_ON(ret < 0))
6854 ret = register_event_command(&trigger_hist_disable_cmd);
6855 if (WARN_ON(ret < 0))
6856 unregister_trigger_hist_enable_disable_cmds();