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"),
75 #define C(a, b) HIST_ERR_##a
82 static const char *err_text[] = { ERRORS };
86 typedef u64 (*hist_field_fn_t) (struct hist_field *field,
87 struct tracing_map_elt *elt,
88 struct trace_buffer *buffer,
89 struct ring_buffer_event *rbe,
92 #define HIST_FIELD_OPERANDS_MAX 2
93 #define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
94 #define HIST_ACTIONS_MAX 8
95 #define HIST_CONST_DIGITS_MAX 21
96 #define HIST_DIV_SHIFT 20 /* For optimizing division by constants */
102 FIELD_OP_UNARY_MINUS,
108 * A hist_var (histogram variable) contains variable information for
109 * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF
110 * flag set. A hist_var has a variable name e.g. ts0, and is
111 * associated with a given histogram trigger, as specified by
112 * hist_data. The hist_var idx is the unique index assigned to the
113 * variable by the hist trigger's tracing_map. The idx is what is
114 * used to set a variable's value and, by a variable reference, to
119 struct hist_trigger_data *hist_data;
124 struct ftrace_event_field *field;
130 unsigned int is_signed;
131 unsigned long buckets;
133 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
134 struct hist_trigger_data *hist_data;
137 * Variable fields contain variable-specific info in var.
140 enum field_op_id operator;
145 * The name field is used for EXPR and VAR_REF fields. VAR
146 * fields contain the variable name in var.name.
151 * When a histogram trigger is hit, if it has any references
152 * to variables, the values of those variables are collected
153 * into a var_ref_vals array by resolve_var_refs(). The
154 * current value of each variable is read from the tracing_map
155 * using the hist field's hist_var.idx and entered into the
156 * var_ref_idx entry i.e. var_ref_vals[var_ref_idx].
158 unsigned int var_ref_idx;
161 unsigned int var_str_idx;
163 /* Numeric literals are represented as u64 */
165 /* Used to optimize division by constants */
169 static u64 hist_field_none(struct hist_field *field,
170 struct tracing_map_elt *elt,
171 struct trace_buffer *buffer,
172 struct ring_buffer_event *rbe,
178 static u64 hist_field_const(struct hist_field *field,
179 struct tracing_map_elt *elt,
180 struct trace_buffer *buffer,
181 struct ring_buffer_event *rbe,
184 return field->constant;
187 static u64 hist_field_counter(struct hist_field *field,
188 struct tracing_map_elt *elt,
189 struct trace_buffer *buffer,
190 struct ring_buffer_event *rbe,
196 static u64 hist_field_string(struct hist_field *hist_field,
197 struct tracing_map_elt *elt,
198 struct trace_buffer *buffer,
199 struct ring_buffer_event *rbe,
202 char *addr = (char *)(event + hist_field->field->offset);
204 return (u64)(unsigned long)addr;
207 static u64 hist_field_dynstring(struct hist_field *hist_field,
208 struct tracing_map_elt *elt,
209 struct trace_buffer *buffer,
210 struct ring_buffer_event *rbe,
213 u32 str_item = *(u32 *)(event + hist_field->field->offset);
214 int str_loc = str_item & 0xffff;
215 char *addr = (char *)(event + str_loc);
217 return (u64)(unsigned long)addr;
220 static u64 hist_field_pstring(struct hist_field *hist_field,
221 struct tracing_map_elt *elt,
222 struct trace_buffer *buffer,
223 struct ring_buffer_event *rbe,
226 char **addr = (char **)(event + hist_field->field->offset);
228 return (u64)(unsigned long)*addr;
231 static u64 hist_field_log2(struct hist_field *hist_field,
232 struct tracing_map_elt *elt,
233 struct trace_buffer *buffer,
234 struct ring_buffer_event *rbe,
237 struct hist_field *operand = hist_field->operands[0];
239 u64 val = operand->fn(operand, elt, buffer, rbe, event);
241 return (u64) ilog2(roundup_pow_of_two(val));
244 static u64 hist_field_bucket(struct hist_field *hist_field,
245 struct tracing_map_elt *elt,
246 struct trace_buffer *buffer,
247 struct ring_buffer_event *rbe,
250 struct hist_field *operand = hist_field->operands[0];
251 unsigned long buckets = hist_field->buckets;
253 u64 val = operand->fn(operand, elt, buffer, rbe, event);
255 if (WARN_ON_ONCE(!buckets))
259 val = div64_ul(val, buckets);
261 val = (u64)((unsigned long)val / buckets);
262 return val * buckets;
265 static u64 hist_field_plus(struct hist_field *hist_field,
266 struct tracing_map_elt *elt,
267 struct trace_buffer *buffer,
268 struct ring_buffer_event *rbe,
271 struct hist_field *operand1 = hist_field->operands[0];
272 struct hist_field *operand2 = hist_field->operands[1];
274 u64 val1 = operand1->fn(operand1, elt, buffer, rbe, event);
275 u64 val2 = operand2->fn(operand2, elt, buffer, rbe, event);
280 static u64 hist_field_minus(struct hist_field *hist_field,
281 struct tracing_map_elt *elt,
282 struct trace_buffer *buffer,
283 struct ring_buffer_event *rbe,
286 struct hist_field *operand1 = hist_field->operands[0];
287 struct hist_field *operand2 = hist_field->operands[1];
289 u64 val1 = operand1->fn(operand1, elt, buffer, rbe, event);
290 u64 val2 = operand2->fn(operand2, elt, buffer, rbe, event);
295 static u64 hist_field_div(struct hist_field *hist_field,
296 struct tracing_map_elt *elt,
297 struct trace_buffer *buffer,
298 struct ring_buffer_event *rbe,
301 struct hist_field *operand1 = hist_field->operands[0];
302 struct hist_field *operand2 = hist_field->operands[1];
304 u64 val1 = operand1->fn(operand1, elt, buffer, rbe, event);
305 u64 val2 = operand2->fn(operand2, elt, buffer, rbe, event);
307 /* Return -1 for the undefined case */
311 /* Use shift if the divisor is a power of 2 */
312 if (!(val2 & (val2 - 1)))
313 return val1 >> __ffs64(val2);
315 return div64_u64(val1, val2);
318 static u64 div_by_power_of_two(struct hist_field *hist_field,
319 struct tracing_map_elt *elt,
320 struct trace_buffer *buffer,
321 struct ring_buffer_event *rbe,
324 struct hist_field *operand1 = hist_field->operands[0];
325 struct hist_field *operand2 = hist_field->operands[1];
327 u64 val1 = operand1->fn(operand1, elt, buffer, rbe, event);
329 return val1 >> __ffs64(operand2->constant);
332 static u64 div_by_not_power_of_two(struct hist_field *hist_field,
333 struct tracing_map_elt *elt,
334 struct trace_buffer *buffer,
335 struct ring_buffer_event *rbe,
338 struct hist_field *operand1 = hist_field->operands[0];
339 struct hist_field *operand2 = hist_field->operands[1];
341 u64 val1 = operand1->fn(operand1, elt, buffer, rbe, event);
343 return div64_u64(val1, operand2->constant);
346 static u64 div_by_mult_and_shift(struct hist_field *hist_field,
347 struct tracing_map_elt *elt,
348 struct trace_buffer *buffer,
349 struct ring_buffer_event *rbe,
352 struct hist_field *operand1 = hist_field->operands[0];
353 struct hist_field *operand2 = hist_field->operands[1];
355 u64 val1 = operand1->fn(operand1, elt, buffer, rbe, event);
358 * If the divisor is a constant, do a multiplication and shift instead.
360 * Choose Z = some power of 2. If Y <= Z, then:
361 * X / Y = (X * (Z / Y)) / Z
363 * (Z / Y) is a constant (mult) which is calculated at parse time, so:
364 * X / Y = (X * mult) / Z
366 * The division by Z can be replaced by a shift since Z is a power of 2:
367 * X / Y = (X * mult) >> HIST_DIV_SHIFT
369 * As long, as X < Z the results will not be off by more than 1.
371 if (val1 < (1 << HIST_DIV_SHIFT)) {
372 u64 mult = operand2->div_multiplier;
374 return (val1 * mult + ((1 << HIST_DIV_SHIFT) - 1)) >> HIST_DIV_SHIFT;
377 return div64_u64(val1, operand2->constant);
380 static u64 hist_field_mult(struct hist_field *hist_field,
381 struct tracing_map_elt *elt,
382 struct trace_buffer *buffer,
383 struct ring_buffer_event *rbe,
386 struct hist_field *operand1 = hist_field->operands[0];
387 struct hist_field *operand2 = hist_field->operands[1];
389 u64 val1 = operand1->fn(operand1, elt, buffer, rbe, event);
390 u64 val2 = operand2->fn(operand2, elt, buffer, rbe, event);
395 static u64 hist_field_unary_minus(struct hist_field *hist_field,
396 struct tracing_map_elt *elt,
397 struct trace_buffer *buffer,
398 struct ring_buffer_event *rbe,
401 struct hist_field *operand = hist_field->operands[0];
403 s64 sval = (s64)operand->fn(operand, elt, buffer, rbe, event);
404 u64 val = (u64)-sval;
409 #define DEFINE_HIST_FIELD_FN(type) \
410 static u64 hist_field_##type(struct hist_field *hist_field, \
411 struct tracing_map_elt *elt, \
412 struct trace_buffer *buffer, \
413 struct ring_buffer_event *rbe, \
416 type *addr = (type *)(event + hist_field->field->offset); \
418 return (u64)(unsigned long)*addr; \
421 DEFINE_HIST_FIELD_FN(s64);
422 DEFINE_HIST_FIELD_FN(u64);
423 DEFINE_HIST_FIELD_FN(s32);
424 DEFINE_HIST_FIELD_FN(u32);
425 DEFINE_HIST_FIELD_FN(s16);
426 DEFINE_HIST_FIELD_FN(u16);
427 DEFINE_HIST_FIELD_FN(s8);
428 DEFINE_HIST_FIELD_FN(u8);
430 #define for_each_hist_field(i, hist_data) \
431 for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
433 #define for_each_hist_val_field(i, hist_data) \
434 for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
436 #define for_each_hist_key_field(i, hist_data) \
437 for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
439 #define HIST_STACKTRACE_DEPTH 16
440 #define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
441 #define HIST_STACKTRACE_SKIP 5
443 #define HITCOUNT_IDX 0
444 #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
446 enum hist_field_flags {
447 HIST_FIELD_FL_HITCOUNT = 1 << 0,
448 HIST_FIELD_FL_KEY = 1 << 1,
449 HIST_FIELD_FL_STRING = 1 << 2,
450 HIST_FIELD_FL_HEX = 1 << 3,
451 HIST_FIELD_FL_SYM = 1 << 4,
452 HIST_FIELD_FL_SYM_OFFSET = 1 << 5,
453 HIST_FIELD_FL_EXECNAME = 1 << 6,
454 HIST_FIELD_FL_SYSCALL = 1 << 7,
455 HIST_FIELD_FL_STACKTRACE = 1 << 8,
456 HIST_FIELD_FL_LOG2 = 1 << 9,
457 HIST_FIELD_FL_TIMESTAMP = 1 << 10,
458 HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11,
459 HIST_FIELD_FL_VAR = 1 << 12,
460 HIST_FIELD_FL_EXPR = 1 << 13,
461 HIST_FIELD_FL_VAR_REF = 1 << 14,
462 HIST_FIELD_FL_CPU = 1 << 15,
463 HIST_FIELD_FL_ALIAS = 1 << 16,
464 HIST_FIELD_FL_BUCKET = 1 << 17,
465 HIST_FIELD_FL_CONST = 1 << 18,
470 char *name[TRACING_MAP_VARS_MAX];
471 char *expr[TRACING_MAP_VARS_MAX];
474 struct hist_trigger_attrs {
484 unsigned int map_bits;
486 char *assignment_str[TRACING_MAP_VARS_MAX];
487 unsigned int n_assignments;
489 char *action_str[HIST_ACTIONS_MAX];
490 unsigned int n_actions;
492 struct var_defs var_defs;
496 struct hist_field *var;
497 struct hist_field *val;
500 struct field_var_hist {
501 struct hist_trigger_data *hist_data;
505 struct hist_trigger_data {
506 struct hist_field *fields[HIST_FIELDS_MAX];
509 unsigned int n_fields;
511 unsigned int n_var_str;
512 unsigned int key_size;
513 struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX];
514 unsigned int n_sort_keys;
515 struct trace_event_file *event_file;
516 struct hist_trigger_attrs *attrs;
517 struct tracing_map *map;
518 bool enable_timestamps;
520 struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
521 unsigned int n_var_refs;
523 struct action_data *actions[HIST_ACTIONS_MAX];
524 unsigned int n_actions;
526 struct field_var *field_vars[SYNTH_FIELDS_MAX];
527 unsigned int n_field_vars;
528 unsigned int n_field_var_str;
529 struct field_var_hist *field_var_hists[SYNTH_FIELDS_MAX];
530 unsigned int n_field_var_hists;
532 struct field_var *save_vars[SYNTH_FIELDS_MAX];
533 unsigned int n_save_vars;
534 unsigned int n_save_var_str;
539 typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
540 struct tracing_map_elt *elt,
541 struct trace_buffer *buffer, void *rec,
542 struct ring_buffer_event *rbe, void *key,
543 struct action_data *data, u64 *var_ref_vals);
545 typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val);
560 enum handler_id handler;
561 enum action_id action;
565 unsigned int n_params;
566 char *params[SYNTH_FIELDS_MAX];
569 * When a histogram trigger is hit, the values of any
570 * references to variables, including variables being passed
571 * as parameters to synthetic events, are collected into a
572 * var_ref_vals array. This var_ref_idx array is an array of
573 * indices into the var_ref_vals array, one for each synthetic
574 * event param, and is passed to the synthetic event
577 unsigned int var_ref_idx[TRACING_MAP_VARS_MAX];
578 struct synth_event *synth_event;
579 bool use_trace_keyword;
580 char *synth_event_name;
590 * var_str contains the $-unstripped variable
591 * name referenced by var_ref, and used when
592 * printing the action. Because var_ref
593 * creation is deferred to create_actions(),
594 * we need a per-action way to save it until
595 * then, thus var_str.
600 * var_ref refers to the variable being
601 * tracked e.g onmax($var).
603 struct hist_field *var_ref;
606 * track_var contains the 'invisible' tracking
607 * variable created to keep the current
610 struct hist_field *track_var;
612 check_track_val_fn_t check_val;
613 action_fn_t save_data;
622 unsigned int key_len;
624 struct tracing_map_elt elt;
626 struct action_data *action_data;
627 struct hist_trigger_data *hist_data;
630 struct hist_elt_data {
633 char **field_var_str;
637 struct snapshot_context {
638 struct tracing_map_elt *elt;
643 * Returns the specific division function to use if the divisor
644 * is constant. This avoids extra branches when the trigger is hit.
646 static hist_field_fn_t hist_field_get_div_fn(struct hist_field *divisor)
648 u64 div = divisor->constant;
650 if (!(div & (div - 1)))
651 return div_by_power_of_two;
653 /* If the divisor is too large, do a regular division */
654 if (div > (1 << HIST_DIV_SHIFT))
655 return div_by_not_power_of_two;
657 divisor->div_multiplier = div64_u64((u64)(1 << HIST_DIV_SHIFT), div);
658 return div_by_mult_and_shift;
661 static void track_data_free(struct track_data *track_data)
663 struct hist_elt_data *elt_data;
668 kfree(track_data->key);
670 elt_data = track_data->elt.private_data;
672 kfree(elt_data->comm);
679 static struct track_data *track_data_alloc(unsigned int key_len,
680 struct action_data *action_data,
681 struct hist_trigger_data *hist_data)
683 struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
684 struct hist_elt_data *elt_data;
687 return ERR_PTR(-ENOMEM);
689 data->key = kzalloc(key_len, GFP_KERNEL);
691 track_data_free(data);
692 return ERR_PTR(-ENOMEM);
695 data->key_len = key_len;
696 data->action_data = action_data;
697 data->hist_data = hist_data;
699 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
701 track_data_free(data);
702 return ERR_PTR(-ENOMEM);
705 data->elt.private_data = elt_data;
707 elt_data->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL);
708 if (!elt_data->comm) {
709 track_data_free(data);
710 return ERR_PTR(-ENOMEM);
716 static char last_cmd[MAX_FILTER_STR_VAL];
717 static char last_cmd_loc[MAX_FILTER_STR_VAL];
719 static int errpos(char *str)
721 return err_pos(last_cmd, str);
724 static void last_cmd_set(struct trace_event_file *file, char *str)
726 const char *system = NULL, *name = NULL;
727 struct trace_event_call *call;
732 strcpy(last_cmd, "hist:");
733 strncat(last_cmd, str, MAX_FILTER_STR_VAL - 1 - sizeof("hist:"));
736 call = file->event_call;
737 system = call->class->system;
739 name = trace_event_name(call);
746 snprintf(last_cmd_loc, MAX_FILTER_STR_VAL, "hist:%s:%s", system, name);
749 static void hist_err(struct trace_array *tr, u8 err_type, u8 err_pos)
751 tracing_log_err(tr, last_cmd_loc, last_cmd, err_text,
755 static void hist_err_clear(void)
758 last_cmd_loc[0] = '\0';
761 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
762 unsigned int *var_ref_idx);
764 static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
765 unsigned int *var_ref_idx)
767 struct tracepoint *tp = event->tp;
769 if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
770 struct tracepoint_func *probe_func_ptr;
771 synth_probe_func_t probe_func;
774 if (!(cpu_online(raw_smp_processor_id())))
777 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
778 if (probe_func_ptr) {
780 probe_func = probe_func_ptr->func;
781 __data = probe_func_ptr->data;
782 probe_func(__data, var_ref_vals, var_ref_idx);
783 } while ((++probe_func_ptr)->func);
788 static void action_trace(struct hist_trigger_data *hist_data,
789 struct tracing_map_elt *elt,
790 struct trace_buffer *buffer, void *rec,
791 struct ring_buffer_event *rbe, void *key,
792 struct action_data *data, u64 *var_ref_vals)
794 struct synth_event *event = data->synth_event;
796 trace_synth(event, var_ref_vals, data->var_ref_idx);
799 struct hist_var_data {
800 struct list_head list;
801 struct hist_trigger_data *hist_data;
804 static u64 hist_field_timestamp(struct hist_field *hist_field,
805 struct tracing_map_elt *elt,
806 struct trace_buffer *buffer,
807 struct ring_buffer_event *rbe,
810 struct hist_trigger_data *hist_data = hist_field->hist_data;
811 struct trace_array *tr = hist_data->event_file->tr;
813 u64 ts = ring_buffer_event_time_stamp(buffer, rbe);
815 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
821 static u64 hist_field_cpu(struct hist_field *hist_field,
822 struct tracing_map_elt *elt,
823 struct trace_buffer *buffer,
824 struct ring_buffer_event *rbe,
827 int cpu = smp_processor_id();
833 * check_field_for_var_ref - Check if a VAR_REF field references a variable
834 * @hist_field: The VAR_REF field to check
835 * @var_data: The hist trigger that owns the variable
836 * @var_idx: The trigger variable identifier
838 * Check the given VAR_REF field to see whether or not it references
839 * the given variable associated with the given trigger.
841 * Return: The VAR_REF field if it does reference the variable, NULL if not
843 static struct hist_field *
844 check_field_for_var_ref(struct hist_field *hist_field,
845 struct hist_trigger_data *var_data,
846 unsigned int var_idx)
848 WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
850 if (hist_field && hist_field->var.idx == var_idx &&
851 hist_field->var.hist_data == var_data)
858 * find_var_ref - Check if a trigger has a reference to a trigger variable
859 * @hist_data: The hist trigger that might have a reference to the variable
860 * @var_data: The hist trigger that owns the variable
861 * @var_idx: The trigger variable identifier
863 * Check the list of var_refs[] on the first hist trigger to see
864 * whether any of them are references to the variable on the second
867 * Return: The VAR_REF field referencing the variable if so, NULL if not
869 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
870 struct hist_trigger_data *var_data,
871 unsigned int var_idx)
873 struct hist_field *hist_field;
876 for (i = 0; i < hist_data->n_var_refs; i++) {
877 hist_field = hist_data->var_refs[i];
878 if (check_field_for_var_ref(hist_field, var_data, var_idx))
886 * find_any_var_ref - Check if there is a reference to a given trigger variable
887 * @hist_data: The hist trigger
888 * @var_idx: The trigger variable identifier
890 * Check to see whether the given variable is currently referenced by
893 * The trigger the variable is defined on is explicitly excluded - the
894 * assumption being that a self-reference doesn't prevent a trigger
895 * from being removed.
897 * Return: The VAR_REF field referencing the variable if so, NULL if not
899 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
900 unsigned int var_idx)
902 struct trace_array *tr = hist_data->event_file->tr;
903 struct hist_field *found = NULL;
904 struct hist_var_data *var_data;
906 list_for_each_entry(var_data, &tr->hist_vars, list) {
907 if (var_data->hist_data == hist_data)
909 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
918 * check_var_refs - Check if there is a reference to any of trigger's variables
919 * @hist_data: The hist trigger
921 * A trigger can define one or more variables. If any one of them is
922 * currently referenced by any other trigger, this function will
925 * Typically used to determine whether or not a trigger can be removed
926 * - if there are any references to a trigger's variables, it cannot.
928 * Return: True if there is a reference to any of trigger's variables
930 static bool check_var_refs(struct hist_trigger_data *hist_data)
932 struct hist_field *field;
936 for_each_hist_field(i, hist_data) {
937 field = hist_data->fields[i];
938 if (field && field->flags & HIST_FIELD_FL_VAR) {
939 if (find_any_var_ref(hist_data, field->var.idx)) {
949 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
951 struct trace_array *tr = hist_data->event_file->tr;
952 struct hist_var_data *var_data, *found = NULL;
954 list_for_each_entry(var_data, &tr->hist_vars, list) {
955 if (var_data->hist_data == hist_data) {
964 static bool field_has_hist_vars(struct hist_field *hist_field,
975 if (hist_field->flags & HIST_FIELD_FL_VAR ||
976 hist_field->flags & HIST_FIELD_FL_VAR_REF)
979 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
980 struct hist_field *operand;
982 operand = hist_field->operands[i];
983 if (field_has_hist_vars(operand, level + 1))
990 static bool has_hist_vars(struct hist_trigger_data *hist_data)
992 struct hist_field *hist_field;
995 for_each_hist_field(i, hist_data) {
996 hist_field = hist_data->fields[i];
997 if (field_has_hist_vars(hist_field, 0))
1004 static int save_hist_vars(struct hist_trigger_data *hist_data)
1006 struct trace_array *tr = hist_data->event_file->tr;
1007 struct hist_var_data *var_data;
1009 var_data = find_hist_vars(hist_data);
1013 if (tracing_check_open_get_tr(tr))
1016 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
1018 trace_array_put(tr);
1022 var_data->hist_data = hist_data;
1023 list_add(&var_data->list, &tr->hist_vars);
1028 static void remove_hist_vars(struct hist_trigger_data *hist_data)
1030 struct trace_array *tr = hist_data->event_file->tr;
1031 struct hist_var_data *var_data;
1033 var_data = find_hist_vars(hist_data);
1037 if (WARN_ON(check_var_refs(hist_data)))
1040 list_del(&var_data->list);
1044 trace_array_put(tr);
1047 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
1048 const char *var_name)
1050 struct hist_field *hist_field, *found = NULL;
1053 for_each_hist_field(i, hist_data) {
1054 hist_field = hist_data->fields[i];
1055 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
1056 strcmp(hist_field->var.name, var_name) == 0) {
1065 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
1066 struct trace_event_file *file,
1067 const char *var_name)
1069 struct hist_trigger_data *test_data;
1070 struct event_trigger_data *test;
1071 struct hist_field *hist_field;
1073 lockdep_assert_held(&event_mutex);
1075 hist_field = find_var_field(hist_data, var_name);
1079 list_for_each_entry(test, &file->triggers, list) {
1080 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1081 test_data = test->private_data;
1082 hist_field = find_var_field(test_data, var_name);
1091 static struct trace_event_file *find_var_file(struct trace_array *tr,
1096 struct hist_trigger_data *var_hist_data;
1097 struct hist_var_data *var_data;
1098 struct trace_event_file *file, *found = NULL;
1101 return find_event_file(tr, system, event_name);
1103 list_for_each_entry(var_data, &tr->hist_vars, list) {
1104 var_hist_data = var_data->hist_data;
1105 file = var_hist_data->event_file;
1109 if (find_var_field(var_hist_data, var_name)) {
1111 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name));
1122 static struct hist_field *find_file_var(struct trace_event_file *file,
1123 const char *var_name)
1125 struct hist_trigger_data *test_data;
1126 struct event_trigger_data *test;
1127 struct hist_field *hist_field;
1129 lockdep_assert_held(&event_mutex);
1131 list_for_each_entry(test, &file->triggers, list) {
1132 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1133 test_data = test->private_data;
1134 hist_field = find_var_field(test_data, var_name);
1143 static struct hist_field *
1144 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
1146 struct trace_array *tr = hist_data->event_file->tr;
1147 struct hist_field *hist_field, *found = NULL;
1148 struct trace_event_file *file;
1151 for (i = 0; i < hist_data->n_actions; i++) {
1152 struct action_data *data = hist_data->actions[i];
1154 if (data->handler == HANDLER_ONMATCH) {
1155 char *system = data->match_data.event_system;
1156 char *event_name = data->match_data.event;
1158 file = find_var_file(tr, system, event_name, var_name);
1161 hist_field = find_file_var(file, var_name);
1164 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE,
1166 return ERR_PTR(-EINVAL);
1176 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
1181 struct trace_array *tr = hist_data->event_file->tr;
1182 struct hist_field *hist_field = NULL;
1183 struct trace_event_file *file;
1185 if (!system || !event_name) {
1186 hist_field = find_match_var(hist_data, var_name);
1187 if (IS_ERR(hist_field))
1193 file = find_var_file(tr, system, event_name, var_name);
1197 hist_field = find_file_var(file, var_name);
1202 static u64 hist_field_var_ref(struct hist_field *hist_field,
1203 struct tracing_map_elt *elt,
1204 struct trace_buffer *buffer,
1205 struct ring_buffer_event *rbe,
1208 struct hist_elt_data *elt_data;
1211 if (WARN_ON_ONCE(!elt))
1214 elt_data = elt->private_data;
1215 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
1220 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
1221 u64 *var_ref_vals, bool self)
1223 struct hist_trigger_data *var_data;
1224 struct tracing_map_elt *var_elt;
1225 struct hist_field *hist_field;
1226 unsigned int i, var_idx;
1227 bool resolved = true;
1230 for (i = 0; i < hist_data->n_var_refs; i++) {
1231 hist_field = hist_data->var_refs[i];
1232 var_idx = hist_field->var.idx;
1233 var_data = hist_field->var.hist_data;
1235 if (var_data == NULL) {
1240 if ((self && var_data != hist_data) ||
1241 (!self && var_data == hist_data))
1244 var_elt = tracing_map_lookup(var_data->map, key);
1250 if (!tracing_map_var_set(var_elt, var_idx)) {
1255 if (self || !hist_field->read_once)
1256 var_val = tracing_map_read_var(var_elt, var_idx);
1258 var_val = tracing_map_read_var_once(var_elt, var_idx);
1260 var_ref_vals[i] = var_val;
1266 static const char *hist_field_name(struct hist_field *field,
1269 const char *field_name = "";
1275 field_name = field->field->name;
1276 else if (field->flags & HIST_FIELD_FL_LOG2 ||
1277 field->flags & HIST_FIELD_FL_ALIAS ||
1278 field->flags & HIST_FIELD_FL_BUCKET)
1279 field_name = hist_field_name(field->operands[0], ++level);
1280 else if (field->flags & HIST_FIELD_FL_CPU)
1281 field_name = "common_cpu";
1282 else if (field->flags & HIST_FIELD_FL_EXPR ||
1283 field->flags & HIST_FIELD_FL_VAR_REF) {
1284 if (field->system) {
1285 static char full_name[MAX_FILTER_STR_VAL];
1287 strcat(full_name, field->system);
1288 strcat(full_name, ".");
1289 strcat(full_name, field->event_name);
1290 strcat(full_name, ".");
1291 strcat(full_name, field->name);
1292 field_name = full_name;
1294 field_name = field->name;
1295 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1296 field_name = "common_timestamp";
1298 if (field_name == NULL)
1304 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
1306 hist_field_fn_t fn = NULL;
1308 switch (field_size) {
1310 if (field_is_signed)
1311 fn = hist_field_s64;
1313 fn = hist_field_u64;
1316 if (field_is_signed)
1317 fn = hist_field_s32;
1319 fn = hist_field_u32;
1322 if (field_is_signed)
1323 fn = hist_field_s16;
1325 fn = hist_field_u16;
1328 if (field_is_signed)
1338 static int parse_map_size(char *str)
1340 unsigned long size, map_bits;
1343 ret = kstrtoul(str, 0, &size);
1347 map_bits = ilog2(roundup_pow_of_two(size));
1348 if (map_bits < TRACING_MAP_BITS_MIN ||
1349 map_bits > TRACING_MAP_BITS_MAX)
1357 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
1364 for (i = 0; i < attrs->n_assignments; i++)
1365 kfree(attrs->assignment_str[i]);
1367 for (i = 0; i < attrs->n_actions; i++)
1368 kfree(attrs->action_str[i]);
1371 kfree(attrs->sort_key_str);
1372 kfree(attrs->keys_str);
1373 kfree(attrs->vals_str);
1374 kfree(attrs->clock);
1378 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
1382 if (attrs->n_actions >= HIST_ACTIONS_MAX)
1385 if ((str_has_prefix(str, "onmatch(")) ||
1386 (str_has_prefix(str, "onmax(")) ||
1387 (str_has_prefix(str, "onchange("))) {
1388 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
1389 if (!attrs->action_str[attrs->n_actions]) {
1399 static int parse_assignment(struct trace_array *tr,
1400 char *str, struct hist_trigger_attrs *attrs)
1404 if ((len = str_has_prefix(str, "key=")) ||
1405 (len = str_has_prefix(str, "keys="))) {
1406 attrs->keys_str = kstrdup(str + len, GFP_KERNEL);
1407 if (!attrs->keys_str) {
1411 } else if ((len = str_has_prefix(str, "val=")) ||
1412 (len = str_has_prefix(str, "vals=")) ||
1413 (len = str_has_prefix(str, "values="))) {
1414 attrs->vals_str = kstrdup(str + len, GFP_KERNEL);
1415 if (!attrs->vals_str) {
1419 } else if ((len = str_has_prefix(str, "sort="))) {
1420 attrs->sort_key_str = kstrdup(str + len, GFP_KERNEL);
1421 if (!attrs->sort_key_str) {
1425 } else if (str_has_prefix(str, "name=")) {
1426 attrs->name = kstrdup(str, GFP_KERNEL);
1431 } else if ((len = str_has_prefix(str, "clock="))) {
1434 str = strstrip(str);
1435 attrs->clock = kstrdup(str, GFP_KERNEL);
1436 if (!attrs->clock) {
1440 } else if ((len = str_has_prefix(str, "size="))) {
1441 int map_bits = parse_map_size(str + len);
1447 attrs->map_bits = map_bits;
1451 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
1452 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str));
1457 assignment = kstrdup(str, GFP_KERNEL);
1463 attrs->assignment_str[attrs->n_assignments++] = assignment;
1469 static struct hist_trigger_attrs *
1470 parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str)
1472 struct hist_trigger_attrs *attrs;
1475 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1477 return ERR_PTR(-ENOMEM);
1479 while (trigger_str) {
1480 char *str = strsep(&trigger_str, ":");
1483 rhs = strchr(str, '=');
1485 if (!strlen(++rhs)) {
1487 hist_err(tr, HIST_ERR_EMPTY_ASSIGNMENT, errpos(str));
1490 ret = parse_assignment(tr, str, attrs);
1493 } else if (strcmp(str, "pause") == 0)
1494 attrs->pause = true;
1495 else if ((strcmp(str, "cont") == 0) ||
1496 (strcmp(str, "continue") == 0))
1498 else if (strcmp(str, "clear") == 0)
1499 attrs->clear = true;
1501 ret = parse_action(str, attrs);
1507 if (!attrs->keys_str) {
1512 if (!attrs->clock) {
1513 attrs->clock = kstrdup("global", GFP_KERNEL);
1514 if (!attrs->clock) {
1522 destroy_hist_trigger_attrs(attrs);
1524 return ERR_PTR(ret);
1527 static inline void save_comm(char *comm, struct task_struct *task)
1530 strcpy(comm, "<idle>");
1534 if (WARN_ON_ONCE(task->pid < 0)) {
1535 strcpy(comm, "<XXX>");
1539 strncpy(comm, task->comm, TASK_COMM_LEN);
1542 static void hist_elt_data_free(struct hist_elt_data *elt_data)
1546 for (i = 0; i < elt_data->n_field_var_str; i++)
1547 kfree(elt_data->field_var_str[i]);
1549 kfree(elt_data->field_var_str);
1551 kfree(elt_data->comm);
1555 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
1557 struct hist_elt_data *elt_data = elt->private_data;
1559 hist_elt_data_free(elt_data);
1562 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
1564 struct hist_trigger_data *hist_data = elt->map->private_data;
1565 unsigned int size = TASK_COMM_LEN;
1566 struct hist_elt_data *elt_data;
1567 struct hist_field *hist_field;
1568 unsigned int i, n_str;
1570 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
1574 for_each_hist_field(i, hist_data) {
1575 hist_field = hist_data->fields[i];
1577 if (hist_field->flags & HIST_FIELD_FL_EXECNAME) {
1578 elt_data->comm = kzalloc(size, GFP_KERNEL);
1579 if (!elt_data->comm) {
1587 n_str = hist_data->n_field_var_str + hist_data->n_save_var_str +
1588 hist_data->n_var_str;
1589 if (n_str > SYNTH_FIELDS_MAX) {
1590 hist_elt_data_free(elt_data);
1594 BUILD_BUG_ON(STR_VAR_LEN_MAX & (sizeof(u64) - 1));
1596 size = STR_VAR_LEN_MAX;
1598 elt_data->field_var_str = kcalloc(n_str, sizeof(char *), GFP_KERNEL);
1599 if (!elt_data->field_var_str) {
1600 hist_elt_data_free(elt_data);
1603 elt_data->n_field_var_str = n_str;
1605 for (i = 0; i < n_str; i++) {
1606 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
1607 if (!elt_data->field_var_str[i]) {
1608 hist_elt_data_free(elt_data);
1613 elt->private_data = elt_data;
1618 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
1620 struct hist_elt_data *elt_data = elt->private_data;
1623 save_comm(elt_data->comm, current);
1626 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
1627 .elt_alloc = hist_trigger_elt_data_alloc,
1628 .elt_free = hist_trigger_elt_data_free,
1629 .elt_init = hist_trigger_elt_data_init,
1632 static const char *get_hist_field_flags(struct hist_field *hist_field)
1634 const char *flags_str = NULL;
1636 if (hist_field->flags & HIST_FIELD_FL_HEX)
1638 else if (hist_field->flags & HIST_FIELD_FL_SYM)
1640 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
1641 flags_str = "sym-offset";
1642 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
1643 flags_str = "execname";
1644 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
1645 flags_str = "syscall";
1646 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
1648 else if (hist_field->flags & HIST_FIELD_FL_BUCKET)
1649 flags_str = "buckets";
1650 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
1651 flags_str = "usecs";
1656 static void expr_field_str(struct hist_field *field, char *expr)
1658 if (field->flags & HIST_FIELD_FL_VAR_REF)
1660 else if (field->flags & HIST_FIELD_FL_CONST) {
1661 char str[HIST_CONST_DIGITS_MAX];
1663 snprintf(str, HIST_CONST_DIGITS_MAX, "%llu", field->constant);
1667 strcat(expr, hist_field_name(field, 0));
1669 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
1670 const char *flags_str = get_hist_field_flags(field);
1674 strcat(expr, flags_str);
1679 static char *expr_str(struct hist_field *field, unsigned int level)
1686 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
1690 if (!field->operands[0]) {
1691 expr_field_str(field, expr);
1695 if (field->operator == FIELD_OP_UNARY_MINUS) {
1699 subexpr = expr_str(field->operands[0], ++level);
1704 strcat(expr, subexpr);
1712 expr_field_str(field->operands[0], expr);
1714 switch (field->operator) {
1715 case FIELD_OP_MINUS:
1732 expr_field_str(field->operands[1], expr);
1738 * If field_op != FIELD_OP_NONE, *sep points to the root operator
1739 * of the expression tree to be evaluated.
1741 static int contains_operator(char *str, char **sep)
1743 enum field_op_id field_op = FIELD_OP_NONE;
1744 char *minus_op, *plus_op, *div_op, *mult_op;
1748 * Report the last occurrence of the operators first, so that the
1749 * expression is evaluated left to right. This is important since
1750 * subtraction and division are not associative.
1753 * 64/8/4/2 is 1, i.e 64/8/4/2 = ((64/8)/4)/2
1754 * 14-7-5-2 is 0, i.e 14-7-5-2 = ((14-7)-5)-2
1758 * First, find lower precedence addition and subtraction
1759 * since the expression will be evaluated recursively.
1761 minus_op = strrchr(str, '-');
1764 * Unary minus is not supported in sub-expressions. If
1765 * present, it is always the next root operator.
1767 if (minus_op == str) {
1768 field_op = FIELD_OP_UNARY_MINUS;
1772 field_op = FIELD_OP_MINUS;
1775 plus_op = strrchr(str, '+');
1776 if (plus_op || minus_op) {
1778 * For operators of the same precedence use to rightmost as the
1779 * root, so that the expression is evaluated left to right.
1781 if (plus_op > minus_op)
1782 field_op = FIELD_OP_PLUS;
1787 * Multiplication and division have higher precedence than addition and
1790 div_op = strrchr(str, '/');
1792 field_op = FIELD_OP_DIV;
1794 mult_op = strrchr(str, '*');
1796 * For operators of the same precedence use to rightmost as the
1797 * root, so that the expression is evaluated left to right.
1799 if (mult_op > div_op)
1800 field_op = FIELD_OP_MULT;
1805 case FIELD_OP_UNARY_MINUS:
1806 case FIELD_OP_MINUS:
1828 static void get_hist_field(struct hist_field *hist_field)
1833 static void __destroy_hist_field(struct hist_field *hist_field)
1835 if (--hist_field->ref > 1)
1838 kfree(hist_field->var.name);
1839 kfree(hist_field->name);
1841 /* Can likely be a const */
1842 kfree_const(hist_field->type);
1844 kfree(hist_field->system);
1845 kfree(hist_field->event_name);
1850 static void destroy_hist_field(struct hist_field *hist_field,
1861 if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
1862 return; /* var refs will be destroyed separately */
1864 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
1865 destroy_hist_field(hist_field->operands[i], level + 1);
1867 __destroy_hist_field(hist_field);
1870 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
1871 struct ftrace_event_field *field,
1872 unsigned long flags,
1875 struct hist_field *hist_field;
1877 if (field && is_function_field(field))
1880 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
1884 hist_field->ref = 1;
1886 hist_field->hist_data = hist_data;
1888 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
1889 goto out; /* caller will populate */
1891 if (flags & HIST_FIELD_FL_VAR_REF) {
1892 hist_field->fn = hist_field_var_ref;
1896 if (flags & HIST_FIELD_FL_HITCOUNT) {
1897 hist_field->fn = hist_field_counter;
1898 hist_field->size = sizeof(u64);
1899 hist_field->type = "u64";
1903 if (flags & HIST_FIELD_FL_CONST) {
1904 hist_field->fn = hist_field_const;
1905 hist_field->size = sizeof(u64);
1906 hist_field->type = kstrdup("u64", GFP_KERNEL);
1907 if (!hist_field->type)
1912 if (flags & HIST_FIELD_FL_STACKTRACE) {
1913 hist_field->fn = hist_field_none;
1917 if (flags & (HIST_FIELD_FL_LOG2 | HIST_FIELD_FL_BUCKET)) {
1918 unsigned long fl = flags & ~(HIST_FIELD_FL_LOG2 | HIST_FIELD_FL_BUCKET);
1919 hist_field->fn = flags & HIST_FIELD_FL_LOG2 ? hist_field_log2 :
1921 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
1922 hist_field->size = hist_field->operands[0]->size;
1923 hist_field->type = kstrdup_const(hist_field->operands[0]->type, GFP_KERNEL);
1924 if (!hist_field->type)
1929 if (flags & HIST_FIELD_FL_TIMESTAMP) {
1930 hist_field->fn = hist_field_timestamp;
1931 hist_field->size = sizeof(u64);
1932 hist_field->type = "u64";
1936 if (flags & HIST_FIELD_FL_CPU) {
1937 hist_field->fn = hist_field_cpu;
1938 hist_field->size = sizeof(int);
1939 hist_field->type = "unsigned int";
1943 if (WARN_ON_ONCE(!field))
1946 /* Pointers to strings are just pointers and dangerous to dereference */
1947 if (is_string_field(field) &&
1948 (field->filter_type != FILTER_PTR_STRING)) {
1949 flags |= HIST_FIELD_FL_STRING;
1951 hist_field->size = MAX_FILTER_STR_VAL;
1952 hist_field->type = kstrdup_const(field->type, GFP_KERNEL);
1953 if (!hist_field->type)
1956 if (field->filter_type == FILTER_STATIC_STRING)
1957 hist_field->fn = hist_field_string;
1958 else if (field->filter_type == FILTER_DYN_STRING)
1959 hist_field->fn = hist_field_dynstring;
1961 hist_field->fn = hist_field_pstring;
1963 hist_field->size = field->size;
1964 hist_field->is_signed = field->is_signed;
1965 hist_field->type = kstrdup_const(field->type, GFP_KERNEL);
1966 if (!hist_field->type)
1969 hist_field->fn = select_value_fn(field->size,
1971 if (!hist_field->fn) {
1972 destroy_hist_field(hist_field, 0);
1977 hist_field->field = field;
1978 hist_field->flags = flags;
1981 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
1982 if (!hist_field->var.name)
1988 destroy_hist_field(hist_field, 0);
1992 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
1996 for (i = 0; i < HIST_FIELDS_MAX; i++) {
1997 if (hist_data->fields[i]) {
1998 destroy_hist_field(hist_data->fields[i], 0);
1999 hist_data->fields[i] = NULL;
2003 for (i = 0; i < hist_data->n_var_refs; i++) {
2004 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
2005 __destroy_hist_field(hist_data->var_refs[i]);
2006 hist_data->var_refs[i] = NULL;
2010 static int init_var_ref(struct hist_field *ref_field,
2011 struct hist_field *var_field,
2012 char *system, char *event_name)
2016 ref_field->var.idx = var_field->var.idx;
2017 ref_field->var.hist_data = var_field->hist_data;
2018 ref_field->size = var_field->size;
2019 ref_field->is_signed = var_field->is_signed;
2020 ref_field->flags |= var_field->flags &
2021 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2024 ref_field->system = kstrdup(system, GFP_KERNEL);
2025 if (!ref_field->system)
2030 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
2031 if (!ref_field->event_name) {
2037 if (var_field->var.name) {
2038 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
2039 if (!ref_field->name) {
2043 } else if (var_field->name) {
2044 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
2045 if (!ref_field->name) {
2051 ref_field->type = kstrdup_const(var_field->type, GFP_KERNEL);
2052 if (!ref_field->type) {
2059 kfree(ref_field->system);
2060 kfree(ref_field->event_name);
2061 kfree(ref_field->name);
2066 static int find_var_ref_idx(struct hist_trigger_data *hist_data,
2067 struct hist_field *var_field)
2069 struct hist_field *ref_field;
2072 for (i = 0; i < hist_data->n_var_refs; i++) {
2073 ref_field = hist_data->var_refs[i];
2074 if (ref_field->var.idx == var_field->var.idx &&
2075 ref_field->var.hist_data == var_field->hist_data)
2083 * create_var_ref - Create a variable reference and attach it to trigger
2084 * @hist_data: The trigger that will be referencing the variable
2085 * @var_field: The VAR field to create a reference to
2086 * @system: The optional system string
2087 * @event_name: The optional event_name string
2089 * Given a variable hist_field, create a VAR_REF hist_field that
2090 * represents a reference to it.
2092 * This function also adds the reference to the trigger that
2093 * now references the variable.
2095 * Return: The VAR_REF field if successful, NULL if not
2097 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
2098 struct hist_field *var_field,
2099 char *system, char *event_name)
2101 unsigned long flags = HIST_FIELD_FL_VAR_REF;
2102 struct hist_field *ref_field;
2105 /* Check if the variable already exists */
2106 for (i = 0; i < hist_data->n_var_refs; i++) {
2107 ref_field = hist_data->var_refs[i];
2108 if (ref_field->var.idx == var_field->var.idx &&
2109 ref_field->var.hist_data == var_field->hist_data) {
2110 get_hist_field(ref_field);
2115 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
2117 if (init_var_ref(ref_field, var_field, system, event_name)) {
2118 destroy_hist_field(ref_field, 0);
2122 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
2123 ref_field->var_ref_idx = hist_data->n_var_refs++;
2129 static bool is_var_ref(char *var_name)
2131 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
2137 static char *field_name_from_var(struct hist_trigger_data *hist_data,
2143 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
2144 name = hist_data->attrs->var_defs.name[i];
2146 if (strcmp(var_name, name) == 0) {
2147 field = hist_data->attrs->var_defs.expr[i];
2148 if (contains_operator(field, NULL) || is_var_ref(field))
2157 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
2158 char *system, char *event_name,
2161 struct trace_event_call *call;
2163 if (system && event_name) {
2164 call = hist_data->event_file->event_call;
2166 if (strcmp(system, call->class->system) != 0)
2169 if (strcmp(event_name, trace_event_name(call)) != 0)
2173 if (!!system != !!event_name)
2176 if (!is_var_ref(var_name))
2181 return field_name_from_var(hist_data, var_name);
2184 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
2185 char *system, char *event_name,
2188 struct hist_field *var_field = NULL, *ref_field = NULL;
2189 struct trace_array *tr = hist_data->event_file->tr;
2191 if (!is_var_ref(var_name))
2196 var_field = find_event_var(hist_data, system, event_name, var_name);
2198 ref_field = create_var_ref(hist_data, var_field,
2199 system, event_name);
2202 hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name));
2207 static struct ftrace_event_field *
2208 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
2209 char *field_str, unsigned long *flags, unsigned long *buckets)
2211 struct ftrace_event_field *field = NULL;
2212 char *field_name, *modifier, *str;
2213 struct trace_array *tr = file->tr;
2215 modifier = str = kstrdup(field_str, GFP_KERNEL);
2217 return ERR_PTR(-ENOMEM);
2219 field_name = strsep(&modifier, ".");
2221 if (strcmp(modifier, "hex") == 0)
2222 *flags |= HIST_FIELD_FL_HEX;
2223 else if (strcmp(modifier, "sym") == 0)
2224 *flags |= HIST_FIELD_FL_SYM;
2226 * 'sym-offset' occurrences in the trigger string are modified
2227 * to 'symXoffset' to simplify arithmetic expression parsing.
2229 else if (strcmp(modifier, "symXoffset") == 0)
2230 *flags |= HIST_FIELD_FL_SYM_OFFSET;
2231 else if ((strcmp(modifier, "execname") == 0) &&
2232 (strcmp(field_name, "common_pid") == 0))
2233 *flags |= HIST_FIELD_FL_EXECNAME;
2234 else if (strcmp(modifier, "syscall") == 0)
2235 *flags |= HIST_FIELD_FL_SYSCALL;
2236 else if (strcmp(modifier, "log2") == 0)
2237 *flags |= HIST_FIELD_FL_LOG2;
2238 else if (strcmp(modifier, "usecs") == 0)
2239 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
2240 else if (strncmp(modifier, "bucket", 6) == 0) {
2245 if (*modifier == 's')
2247 if (*modifier != '=')
2250 ret = kstrtoul(modifier, 0, buckets);
2251 if (ret || !(*buckets))
2253 *flags |= HIST_FIELD_FL_BUCKET;
2256 hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier));
2257 field = ERR_PTR(-EINVAL);
2262 if (strcmp(field_name, "common_timestamp") == 0) {
2263 *flags |= HIST_FIELD_FL_TIMESTAMP;
2264 hist_data->enable_timestamps = true;
2265 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2266 hist_data->attrs->ts_in_usecs = true;
2267 } else if (strcmp(field_name, "common_cpu") == 0)
2268 *flags |= HIST_FIELD_FL_CPU;
2270 field = trace_find_event_field(file->event_call, field_name);
2271 if (!field || !field->size) {
2273 * For backward compatibility, if field_name
2274 * was "cpu", then we treat this the same as
2277 if (strcmp(field_name, "cpu") == 0) {
2278 *flags |= HIST_FIELD_FL_CPU;
2280 hist_err(tr, HIST_ERR_FIELD_NOT_FOUND,
2281 errpos(field_name));
2282 field = ERR_PTR(-EINVAL);
2293 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2294 struct hist_field *var_ref,
2297 struct hist_field *alias = NULL;
2298 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2300 alias = create_hist_field(hist_data, NULL, flags, var_name);
2304 alias->fn = var_ref->fn;
2305 alias->operands[0] = var_ref;
2307 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2308 destroy_hist_field(alias, 0);
2312 alias->var_ref_idx = var_ref->var_ref_idx;
2317 static struct hist_field *parse_const(struct hist_trigger_data *hist_data,
2318 char *str, char *var_name,
2319 unsigned long *flags)
2321 struct trace_array *tr = hist_data->event_file->tr;
2322 struct hist_field *field = NULL;
2325 if (kstrtoull(str, 0, &constant)) {
2326 hist_err(tr, HIST_ERR_EXPECT_NUMBER, errpos(str));
2330 *flags |= HIST_FIELD_FL_CONST;
2331 field = create_hist_field(hist_data, NULL, *flags, var_name);
2335 field->constant = constant;
2340 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2341 struct trace_event_file *file, char *str,
2342 unsigned long *flags, char *var_name)
2344 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
2345 struct ftrace_event_field *field = NULL;
2346 struct hist_field *hist_field = NULL;
2347 unsigned long buckets = 0;
2350 if (isdigit(str[0])) {
2351 hist_field = parse_const(hist_data, str, var_name, flags);
2359 s = strchr(str, '.');
2361 s = strchr(++s, '.');
2363 ref_system = strsep(&str, ".");
2368 ref_event = strsep(&str, ".");
2377 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2379 hist_field = parse_var_ref(hist_data, ref_system,
2380 ref_event, ref_var);
2383 hist_field = create_alias(hist_data, hist_field, var_name);
2394 field = parse_field(hist_data, file, str, flags, &buckets);
2395 if (IS_ERR(field)) {
2396 ret = PTR_ERR(field);
2400 hist_field = create_hist_field(hist_data, field, *flags, var_name);
2405 hist_field->buckets = buckets;
2409 return ERR_PTR(ret);
2412 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2413 struct trace_event_file *file,
2414 char *str, unsigned long flags,
2415 char *var_name, unsigned int *n_subexprs);
2417 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2418 struct trace_event_file *file,
2419 char *str, unsigned long flags,
2420 char *var_name, unsigned int *n_subexprs)
2422 struct hist_field *operand1, *expr = NULL;
2423 unsigned long operand_flags;
2427 /* Unary minus operator, increment n_subexprs */
2430 /* we support only -(xxx) i.e. explicit parens required */
2432 if (*n_subexprs > 3) {
2433 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2438 str++; /* skip leading '-' */
2440 s = strchr(str, '(');
2448 s = strrchr(str, ')');
2450 /* unary minus not supported in sub-expressions */
2451 if (*(s+1) != '\0') {
2452 hist_err(file->tr, HIST_ERR_UNARY_MINUS_SUBEXPR,
2460 ret = -EINVAL; /* no closing ')' */
2464 flags |= HIST_FIELD_FL_EXPR;
2465 expr = create_hist_field(hist_data, NULL, flags, var_name);
2472 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, n_subexprs);
2473 if (IS_ERR(operand1)) {
2474 ret = PTR_ERR(operand1);
2477 if (operand1->flags & HIST_FIELD_FL_STRING) {
2478 /* String type can not be the operand of unary operator. */
2479 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
2480 destroy_hist_field(operand1, 0);
2485 expr->flags |= operand1->flags &
2486 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2487 expr->fn = hist_field_unary_minus;
2488 expr->operands[0] = operand1;
2489 expr->operator = FIELD_OP_UNARY_MINUS;
2490 expr->name = expr_str(expr, 0);
2491 expr->type = kstrdup_const(operand1->type, GFP_KERNEL);
2499 destroy_hist_field(expr, 0);
2500 return ERR_PTR(ret);
2504 * If the operands are var refs, return pointers the
2505 * variable(s) referenced in var1 and var2, else NULL.
2507 static int check_expr_operands(struct trace_array *tr,
2508 struct hist_field *operand1,
2509 struct hist_field *operand2,
2510 struct hist_field **var1,
2511 struct hist_field **var2)
2513 unsigned long operand1_flags = operand1->flags;
2514 unsigned long operand2_flags = operand2->flags;
2516 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2517 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2518 struct hist_field *var;
2520 var = find_var_field(operand1->var.hist_data, operand1->name);
2523 operand1_flags = var->flags;
2527 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2528 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
2529 struct hist_field *var;
2531 var = find_var_field(operand2->var.hist_data, operand2->name);
2534 operand2_flags = var->flags;
2538 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
2539 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
2540 hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0);
2547 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2548 struct trace_event_file *file,
2549 char *str, unsigned long flags,
2550 char *var_name, unsigned int *n_subexprs)
2552 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
2553 struct hist_field *var1 = NULL, *var2 = NULL;
2554 unsigned long operand_flags, operand2_flags;
2555 int field_op, ret = -EINVAL;
2556 char *sep, *operand1_str;
2557 hist_field_fn_t op_fn;
2558 bool combine_consts;
2560 if (*n_subexprs > 3) {
2561 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2562 return ERR_PTR(-EINVAL);
2565 field_op = contains_operator(str, &sep);
2567 if (field_op == FIELD_OP_NONE)
2568 return parse_atom(hist_data, file, str, &flags, var_name);
2570 if (field_op == FIELD_OP_UNARY_MINUS)
2571 return parse_unary(hist_data, file, str, flags, var_name, n_subexprs);
2573 /* Binary operator found, increment n_subexprs */
2576 /* Split the expression string at the root operator */
2583 if (!operand1_str || !str)
2588 /* LHS of string is an expression e.g. a+b in a+b+c */
2589 operand1 = parse_expr(hist_data, file, operand1_str, operand_flags, NULL, n_subexprs);
2590 if (IS_ERR(operand1)) {
2591 ret = PTR_ERR(operand1);
2595 if (operand1->flags & HIST_FIELD_FL_STRING) {
2596 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(operand1_str));
2601 /* RHS of string is another expression e.g. c in a+b+c */
2603 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, n_subexprs);
2604 if (IS_ERR(operand2)) {
2605 ret = PTR_ERR(operand2);
2609 if (operand2->flags & HIST_FIELD_FL_STRING) {
2610 hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
2616 case FIELD_OP_MINUS:
2617 op_fn = hist_field_minus;
2620 op_fn = hist_field_plus;
2623 op_fn = hist_field_div;
2626 op_fn = hist_field_mult;
2633 ret = check_expr_operands(file->tr, operand1, operand2, &var1, &var2);
2637 operand_flags = var1 ? var1->flags : operand1->flags;
2638 operand2_flags = var2 ? var2->flags : operand2->flags;
2641 * If both operands are constant, the expression can be
2642 * collapsed to a single constant.
2644 combine_consts = operand_flags & operand2_flags & HIST_FIELD_FL_CONST;
2646 flags |= combine_consts ? HIST_FIELD_FL_CONST : HIST_FIELD_FL_EXPR;
2648 flags |= operand1->flags &
2649 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2651 expr = create_hist_field(hist_data, NULL, flags, var_name);
2657 operand1->read_once = true;
2658 operand2->read_once = true;
2660 expr->operands[0] = operand1;
2661 expr->operands[1] = operand2;
2663 if (field_op == FIELD_OP_DIV &&
2664 operand2_flags & HIST_FIELD_FL_CONST) {
2665 u64 divisor = var2 ? var2->constant : operand2->constant;
2668 hist_err(file->tr, HIST_ERR_DIVISION_BY_ZERO, errpos(str));
2674 * Copy the divisor here so we don't have to look it up
2675 * later if this is a var ref
2677 operand2->constant = divisor;
2678 op_fn = hist_field_get_div_fn(operand2);
2681 if (combine_consts) {
2683 expr->operands[0] = var1;
2685 expr->operands[1] = var2;
2687 expr->constant = op_fn(expr, NULL, NULL, NULL, NULL);
2689 expr->operands[0] = NULL;
2690 expr->operands[1] = NULL;
2693 * var refs won't be destroyed immediately
2694 * See: destroy_hist_field()
2696 destroy_hist_field(operand2, 0);
2697 destroy_hist_field(operand1, 0);
2699 expr->name = expr_str(expr, 0);
2703 /* The operand sizes should be the same, so just pick one */
2704 expr->size = operand1->size;
2706 expr->operator = field_op;
2707 expr->type = kstrdup_const(operand1->type, GFP_KERNEL);
2713 expr->name = expr_str(expr, 0);
2718 destroy_hist_field(operand1, 0);
2719 destroy_hist_field(operand2, 0);
2720 destroy_hist_field(expr, 0);
2722 return ERR_PTR(ret);
2725 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
2726 struct trace_event_file *file)
2728 struct event_trigger_data *test;
2730 lockdep_assert_held(&event_mutex);
2732 list_for_each_entry(test, &file->triggers, list) {
2733 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2734 if (test->private_data == hist_data)
2735 return test->filter_str;
2742 static struct event_command trigger_hist_cmd;
2743 static int event_hist_trigger_func(struct event_command *cmd_ops,
2744 struct trace_event_file *file,
2745 char *glob, char *cmd, char *param);
2747 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
2748 struct hist_trigger_data *hist_data,
2749 unsigned int n_keys)
2751 struct hist_field *target_hist_field, *hist_field;
2752 unsigned int n, i, j;
2754 if (hist_data->n_fields - hist_data->n_vals != n_keys)
2757 i = hist_data->n_vals;
2758 j = target_hist_data->n_vals;
2760 for (n = 0; n < n_keys; n++) {
2761 hist_field = hist_data->fields[i + n];
2762 target_hist_field = target_hist_data->fields[j + n];
2764 if (strcmp(hist_field->type, target_hist_field->type) != 0)
2766 if (hist_field->size != target_hist_field->size)
2768 if (hist_field->is_signed != target_hist_field->is_signed)
2775 static struct hist_trigger_data *
2776 find_compatible_hist(struct hist_trigger_data *target_hist_data,
2777 struct trace_event_file *file)
2779 struct hist_trigger_data *hist_data;
2780 struct event_trigger_data *test;
2781 unsigned int n_keys;
2783 lockdep_assert_held(&event_mutex);
2785 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
2787 list_for_each_entry(test, &file->triggers, list) {
2788 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2789 hist_data = test->private_data;
2791 if (compatible_keys(target_hist_data, hist_data, n_keys))
2799 static struct trace_event_file *event_file(struct trace_array *tr,
2800 char *system, char *event_name)
2802 struct trace_event_file *file;
2804 file = __find_event_file(tr, system, event_name);
2806 return ERR_PTR(-EINVAL);
2811 static struct hist_field *
2812 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
2813 char *system, char *event_name, char *field_name)
2815 struct hist_field *event_var;
2816 char *synthetic_name;
2818 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2819 if (!synthetic_name)
2820 return ERR_PTR(-ENOMEM);
2822 strcpy(synthetic_name, "synthetic_");
2823 strcat(synthetic_name, field_name);
2825 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
2827 kfree(synthetic_name);
2833 * create_field_var_hist - Automatically create a histogram and var for a field
2834 * @target_hist_data: The target hist trigger
2835 * @subsys_name: Optional subsystem name
2836 * @event_name: Optional event name
2837 * @field_name: The name of the field (and the resulting variable)
2839 * Hist trigger actions fetch data from variables, not directly from
2840 * events. However, for convenience, users are allowed to directly
2841 * specify an event field in an action, which will be automatically
2842 * converted into a variable on their behalf.
2844 * If a user specifies a field on an event that isn't the event the
2845 * histogram currently being defined (the target event histogram), the
2846 * only way that can be accomplished is if a new hist trigger is
2847 * created and the field variable defined on that.
2849 * This function creates a new histogram compatible with the target
2850 * event (meaning a histogram with the same key as the target
2851 * histogram), and creates a variable for the specified field, but
2852 * with 'synthetic_' prepended to the variable name in order to avoid
2853 * collision with normal field variables.
2855 * Return: The variable created for the field.
2857 static struct hist_field *
2858 create_field_var_hist(struct hist_trigger_data *target_hist_data,
2859 char *subsys_name, char *event_name, char *field_name)
2861 struct trace_array *tr = target_hist_data->event_file->tr;
2862 struct hist_trigger_data *hist_data;
2863 unsigned int i, n, first = true;
2864 struct field_var_hist *var_hist;
2865 struct trace_event_file *file;
2866 struct hist_field *key_field;
2867 struct hist_field *event_var;
2872 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
2873 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
2874 return ERR_PTR(-EINVAL);
2877 file = event_file(tr, subsys_name, event_name);
2880 hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name));
2881 ret = PTR_ERR(file);
2882 return ERR_PTR(ret);
2886 * Look for a histogram compatible with target. We'll use the
2887 * found histogram specification to create a new matching
2888 * histogram with our variable on it. target_hist_data is not
2889 * yet a registered histogram so we can't use that.
2891 hist_data = find_compatible_hist(target_hist_data, file);
2893 hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name));
2894 return ERR_PTR(-EINVAL);
2897 /* See if a synthetic field variable has already been created */
2898 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
2899 event_name, field_name);
2900 if (!IS_ERR_OR_NULL(event_var))
2903 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
2905 return ERR_PTR(-ENOMEM);
2907 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2910 return ERR_PTR(-ENOMEM);
2913 /* Use the same keys as the compatible histogram */
2914 strcat(cmd, "keys=");
2916 for_each_hist_key_field(i, hist_data) {
2917 key_field = hist_data->fields[i];
2920 strcat(cmd, key_field->field->name);
2924 /* Create the synthetic field variable specification */
2925 strcat(cmd, ":synthetic_");
2926 strcat(cmd, field_name);
2928 strcat(cmd, field_name);
2930 /* Use the same filter as the compatible histogram */
2931 saved_filter = find_trigger_filter(hist_data, file);
2933 strcat(cmd, " if ");
2934 strcat(cmd, saved_filter);
2937 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
2938 if (!var_hist->cmd) {
2941 return ERR_PTR(-ENOMEM);
2944 /* Save the compatible histogram information */
2945 var_hist->hist_data = hist_data;
2947 /* Create the new histogram with our variable */
2948 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
2952 kfree(var_hist->cmd);
2954 hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name));
2955 return ERR_PTR(ret);
2960 /* If we can't find the variable, something went wrong */
2961 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
2962 event_name, field_name);
2963 if (IS_ERR_OR_NULL(event_var)) {
2964 kfree(var_hist->cmd);
2966 hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
2967 return ERR_PTR(-EINVAL);
2970 n = target_hist_data->n_field_var_hists;
2971 target_hist_data->field_var_hists[n] = var_hist;
2972 target_hist_data->n_field_var_hists++;
2977 static struct hist_field *
2978 find_target_event_var(struct hist_trigger_data *hist_data,
2979 char *subsys_name, char *event_name, char *var_name)
2981 struct trace_event_file *file = hist_data->event_file;
2982 struct hist_field *hist_field = NULL;
2985 struct trace_event_call *call;
2990 call = file->event_call;
2992 if (strcmp(subsys_name, call->class->system) != 0)
2995 if (strcmp(event_name, trace_event_name(call)) != 0)
2999 hist_field = find_var_field(hist_data, var_name);
3004 static inline void __update_field_vars(struct tracing_map_elt *elt,
3005 struct trace_buffer *buffer,
3006 struct ring_buffer_event *rbe,
3008 struct field_var **field_vars,
3009 unsigned int n_field_vars,
3010 unsigned int field_var_str_start)
3012 struct hist_elt_data *elt_data = elt->private_data;
3013 unsigned int i, j, var_idx;
3016 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
3017 struct field_var *field_var = field_vars[i];
3018 struct hist_field *var = field_var->var;
3019 struct hist_field *val = field_var->val;
3021 var_val = val->fn(val, elt, buffer, rbe, rec);
3022 var_idx = var->var.idx;
3024 if (val->flags & HIST_FIELD_FL_STRING) {
3025 char *str = elt_data->field_var_str[j++];
3026 char *val_str = (char *)(uintptr_t)var_val;
3028 strscpy(str, val_str, STR_VAR_LEN_MAX);
3029 var_val = (u64)(uintptr_t)str;
3031 tracing_map_set_var(elt, var_idx, var_val);
3035 static void update_field_vars(struct hist_trigger_data *hist_data,
3036 struct tracing_map_elt *elt,
3037 struct trace_buffer *buffer,
3038 struct ring_buffer_event *rbe,
3041 __update_field_vars(elt, buffer, rbe, rec, hist_data->field_vars,
3042 hist_data->n_field_vars, 0);
3045 static void save_track_data_vars(struct hist_trigger_data *hist_data,
3046 struct tracing_map_elt *elt,
3047 struct trace_buffer *buffer, void *rec,
3048 struct ring_buffer_event *rbe, void *key,
3049 struct action_data *data, u64 *var_ref_vals)
3051 __update_field_vars(elt, buffer, rbe, rec, hist_data->save_vars,
3052 hist_data->n_save_vars, hist_data->n_field_var_str);
3055 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
3056 struct trace_event_file *file,
3057 char *name, int size, const char *type)
3059 struct hist_field *var;
3062 if (find_var(hist_data, file, name) && !hist_data->remove) {
3063 var = ERR_PTR(-EINVAL);
3067 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3069 var = ERR_PTR(-ENOMEM);
3073 idx = tracing_map_add_var(hist_data->map);
3076 var = ERR_PTR(-EINVAL);
3081 var->flags = HIST_FIELD_FL_VAR;
3083 var->var.hist_data = var->hist_data = hist_data;
3085 var->var.name = kstrdup(name, GFP_KERNEL);
3086 var->type = kstrdup_const(type, GFP_KERNEL);
3087 if (!var->var.name || !var->type) {
3088 kfree_const(var->type);
3089 kfree(var->var.name);
3091 var = ERR_PTR(-ENOMEM);
3097 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
3098 struct trace_event_file *file,
3101 struct hist_field *val = NULL, *var = NULL;
3102 unsigned long flags = HIST_FIELD_FL_VAR;
3103 struct trace_array *tr = file->tr;
3104 struct field_var *field_var;
3107 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
3108 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
3113 val = parse_atom(hist_data, file, field_name, &flags, NULL);
3115 hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name));
3120 var = create_var(hist_data, file, field_name, val->size, val->type);
3122 hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name));
3128 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
3136 field_var->var = var;
3137 field_var->val = val;
3141 field_var = ERR_PTR(ret);
3146 * create_target_field_var - Automatically create a variable for a field
3147 * @target_hist_data: The target hist trigger
3148 * @subsys_name: Optional subsystem name
3149 * @event_name: Optional event name
3150 * @var_name: The name of the field (and the resulting variable)
3152 * Hist trigger actions fetch data from variables, not directly from
3153 * events. However, for convenience, users are allowed to directly
3154 * specify an event field in an action, which will be automatically
3155 * converted into a variable on their behalf.
3157 * This function creates a field variable with the name var_name on
3158 * the hist trigger currently being defined on the target event. If
3159 * subsys_name and event_name are specified, this function simply
3160 * verifies that they do in fact match the target event subsystem and
3163 * Return: The variable created for the field.
3165 static struct field_var *
3166 create_target_field_var(struct hist_trigger_data *target_hist_data,
3167 char *subsys_name, char *event_name, char *var_name)
3169 struct trace_event_file *file = target_hist_data->event_file;
3172 struct trace_event_call *call;
3177 call = file->event_call;
3179 if (strcmp(subsys_name, call->class->system) != 0)
3182 if (strcmp(event_name, trace_event_name(call)) != 0)
3186 return create_field_var(target_hist_data, file, var_name);
3189 static bool check_track_val_max(u64 track_val, u64 var_val)
3191 if (var_val <= track_val)
3197 static bool check_track_val_changed(u64 track_val, u64 var_val)
3199 if (var_val == track_val)
3205 static u64 get_track_val(struct hist_trigger_data *hist_data,
3206 struct tracing_map_elt *elt,
3207 struct action_data *data)
3209 unsigned int track_var_idx = data->track_data.track_var->var.idx;
3212 track_val = tracing_map_read_var(elt, track_var_idx);
3217 static void save_track_val(struct hist_trigger_data *hist_data,
3218 struct tracing_map_elt *elt,
3219 struct action_data *data, u64 var_val)
3221 unsigned int track_var_idx = data->track_data.track_var->var.idx;
3223 tracing_map_set_var(elt, track_var_idx, var_val);
3226 static void save_track_data(struct hist_trigger_data *hist_data,
3227 struct tracing_map_elt *elt,
3228 struct trace_buffer *buffer, void *rec,
3229 struct ring_buffer_event *rbe, void *key,
3230 struct action_data *data, u64 *var_ref_vals)
3232 if (data->track_data.save_data)
3233 data->track_data.save_data(hist_data, elt, buffer, rec, rbe,
3234 key, data, var_ref_vals);
3237 static bool check_track_val(struct tracing_map_elt *elt,
3238 struct action_data *data,
3241 struct hist_trigger_data *hist_data;
3244 hist_data = data->track_data.track_var->hist_data;
3245 track_val = get_track_val(hist_data, elt, data);
3247 return data->track_data.check_val(track_val, var_val);
3250 #ifdef CONFIG_TRACER_SNAPSHOT
3251 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3253 /* called with tr->max_lock held */
3254 struct track_data *track_data = tr->cond_snapshot->cond_data;
3255 struct hist_elt_data *elt_data, *track_elt_data;
3256 struct snapshot_context *context = cond_data;
3257 struct action_data *action;
3263 action = track_data->action_data;
3265 track_val = get_track_val(track_data->hist_data, context->elt,
3266 track_data->action_data);
3268 if (!action->track_data.check_val(track_data->track_val, track_val))
3271 track_data->track_val = track_val;
3272 memcpy(track_data->key, context->key, track_data->key_len);
3274 elt_data = context->elt->private_data;
3275 track_elt_data = track_data->elt.private_data;
3277 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
3279 track_data->updated = true;
3284 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3285 struct tracing_map_elt *elt,
3286 struct trace_buffer *buffer, void *rec,
3287 struct ring_buffer_event *rbe, void *key,
3288 struct action_data *data,
3291 struct trace_event_file *file = hist_data->event_file;
3292 struct snapshot_context context;
3297 tracing_snapshot_cond(file->tr, &context);
3300 static void hist_trigger_print_key(struct seq_file *m,
3301 struct hist_trigger_data *hist_data,
3303 struct tracing_map_elt *elt);
3305 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
3309 if (!hist_data->n_actions)
3312 for (i = 0; i < hist_data->n_actions; i++) {
3313 struct action_data *data = hist_data->actions[i];
3315 if (data->action == ACTION_SNAPSHOT)
3322 static void track_data_snapshot_print(struct seq_file *m,
3323 struct hist_trigger_data *hist_data)
3325 struct trace_event_file *file = hist_data->event_file;
3326 struct track_data *track_data;
3327 struct action_data *action;
3329 track_data = tracing_cond_snapshot_data(file->tr);
3333 if (!track_data->updated)
3336 action = snapshot_action(hist_data);
3340 seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n");
3341 seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
3342 action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
3343 action->track_data.var_str, track_data->track_val);
3345 seq_puts(m, "\ttriggered by event with key: ");
3346 hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
3350 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3354 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3355 struct tracing_map_elt *elt,
3356 struct trace_buffer *buffer, void *rec,
3357 struct ring_buffer_event *rbe, void *key,
3358 struct action_data *data,
3359 u64 *var_ref_vals) {}
3360 static void track_data_snapshot_print(struct seq_file *m,
3361 struct hist_trigger_data *hist_data) {}
3362 #endif /* CONFIG_TRACER_SNAPSHOT */
3364 static void track_data_print(struct seq_file *m,
3365 struct hist_trigger_data *hist_data,
3366 struct tracing_map_elt *elt,
3367 struct action_data *data)
3369 u64 track_val = get_track_val(hist_data, elt, data);
3370 unsigned int i, save_var_idx;
3372 if (data->handler == HANDLER_ONMAX)
3373 seq_printf(m, "\n\tmax: %10llu", track_val);
3374 else if (data->handler == HANDLER_ONCHANGE)
3375 seq_printf(m, "\n\tchanged: %10llu", track_val);
3377 if (data->action == ACTION_SNAPSHOT)
3380 for (i = 0; i < hist_data->n_save_vars; i++) {
3381 struct hist_field *save_val = hist_data->save_vars[i]->val;
3382 struct hist_field *save_var = hist_data->save_vars[i]->var;
3385 save_var_idx = save_var->var.idx;
3387 val = tracing_map_read_var(elt, save_var_idx);
3389 if (save_val->flags & HIST_FIELD_FL_STRING) {
3390 seq_printf(m, " %s: %-32s", save_var->var.name,
3391 (char *)(uintptr_t)(val));
3393 seq_printf(m, " %s: %10llu", save_var->var.name, val);
3397 static void ontrack_action(struct hist_trigger_data *hist_data,
3398 struct tracing_map_elt *elt,
3399 struct trace_buffer *buffer, void *rec,
3400 struct ring_buffer_event *rbe, void *key,
3401 struct action_data *data, u64 *var_ref_vals)
3403 u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
3405 if (check_track_val(elt, data, var_val)) {
3406 save_track_val(hist_data, elt, data, var_val);
3407 save_track_data(hist_data, elt, buffer, rec, rbe,
3408 key, data, var_ref_vals);
3412 static void action_data_destroy(struct action_data *data)
3416 lockdep_assert_held(&event_mutex);
3418 kfree(data->action_name);
3420 for (i = 0; i < data->n_params; i++)
3421 kfree(data->params[i]);
3423 if (data->synth_event)
3424 data->synth_event->ref--;
3426 kfree(data->synth_event_name);
3431 static void track_data_destroy(struct hist_trigger_data *hist_data,
3432 struct action_data *data)
3434 struct trace_event_file *file = hist_data->event_file;
3436 destroy_hist_field(data->track_data.track_var, 0);
3438 if (data->action == ACTION_SNAPSHOT) {
3439 struct track_data *track_data;
3441 track_data = tracing_cond_snapshot_data(file->tr);
3442 if (track_data && track_data->hist_data == hist_data) {
3443 tracing_snapshot_cond_disable(file->tr);
3444 track_data_free(track_data);
3448 kfree(data->track_data.var_str);
3450 action_data_destroy(data);
3453 static int action_create(struct hist_trigger_data *hist_data,
3454 struct action_data *data);
3456 static int track_data_create(struct hist_trigger_data *hist_data,
3457 struct action_data *data)
3459 struct hist_field *var_field, *ref_field, *track_var = NULL;
3460 struct trace_event_file *file = hist_data->event_file;
3461 struct trace_array *tr = file->tr;
3462 char *track_data_var_str;
3465 track_data_var_str = data->track_data.var_str;
3466 if (track_data_var_str[0] != '$') {
3467 hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str));
3470 track_data_var_str++;
3472 var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
3474 hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str));
3478 ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
3482 data->track_data.var_ref = ref_field;
3484 if (data->handler == HANDLER_ONMAX)
3485 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
3486 if (IS_ERR(track_var)) {
3487 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3488 ret = PTR_ERR(track_var);
3492 if (data->handler == HANDLER_ONCHANGE)
3493 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
3494 if (IS_ERR(track_var)) {
3495 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3496 ret = PTR_ERR(track_var);
3499 data->track_data.track_var = track_var;
3501 ret = action_create(hist_data, data);
3506 static int parse_action_params(struct trace_array *tr, char *params,
3507 struct action_data *data)
3509 char *param, *saved_param;
3510 bool first_param = true;
3514 if (data->n_params >= SYNTH_FIELDS_MAX) {
3515 hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0);
3519 param = strsep(¶ms, ",");
3521 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0);
3526 param = strstrip(param);
3527 if (strlen(param) < 2) {
3528 hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param));
3533 saved_param = kstrdup(param, GFP_KERNEL);
3539 if (first_param && data->use_trace_keyword) {
3540 data->synth_event_name = saved_param;
3541 first_param = false;
3544 first_param = false;
3546 data->params[data->n_params++] = saved_param;
3552 static int action_parse(struct trace_array *tr, char *str, struct action_data *data,
3553 enum handler_id handler)
3560 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3565 action_name = strsep(&str, "(");
3566 if (!action_name || !str) {
3567 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3572 if (str_has_prefix(action_name, "save")) {
3573 char *params = strsep(&str, ")");
3576 hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0);
3581 ret = parse_action_params(tr, params, data);
3585 if (handler == HANDLER_ONMAX)
3586 data->track_data.check_val = check_track_val_max;
3587 else if (handler == HANDLER_ONCHANGE)
3588 data->track_data.check_val = check_track_val_changed;
3590 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3595 data->track_data.save_data = save_track_data_vars;
3596 data->fn = ontrack_action;
3597 data->action = ACTION_SAVE;
3598 } else if (str_has_prefix(action_name, "snapshot")) {
3599 char *params = strsep(&str, ")");
3602 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params));
3607 if (handler == HANDLER_ONMAX)
3608 data->track_data.check_val = check_track_val_max;
3609 else if (handler == HANDLER_ONCHANGE)
3610 data->track_data.check_val = check_track_val_changed;
3612 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3617 data->track_data.save_data = save_track_data_snapshot;
3618 data->fn = ontrack_action;
3619 data->action = ACTION_SNAPSHOT;
3621 char *params = strsep(&str, ")");
3623 if (str_has_prefix(action_name, "trace"))
3624 data->use_trace_keyword = true;
3627 ret = parse_action_params(tr, params, data);
3632 if (handler == HANDLER_ONMAX)
3633 data->track_data.check_val = check_track_val_max;
3634 else if (handler == HANDLER_ONCHANGE)
3635 data->track_data.check_val = check_track_val_changed;
3637 if (handler != HANDLER_ONMATCH) {
3638 data->track_data.save_data = action_trace;
3639 data->fn = ontrack_action;
3641 data->fn = action_trace;
3643 data->action = ACTION_TRACE;
3646 data->action_name = kstrdup(action_name, GFP_KERNEL);
3647 if (!data->action_name) {
3652 data->handler = handler;
3657 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
3658 char *str, enum handler_id handler)
3660 struct action_data *data;
3664 data = kzalloc(sizeof(*data), GFP_KERNEL);
3666 return ERR_PTR(-ENOMEM);
3668 var_str = strsep(&str, ")");
3669 if (!var_str || !str) {
3674 data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
3675 if (!data->track_data.var_str) {
3680 ret = action_parse(hist_data->event_file->tr, str, data, handler);
3686 track_data_destroy(hist_data, data);
3687 data = ERR_PTR(ret);
3691 static void onmatch_destroy(struct action_data *data)
3693 kfree(data->match_data.event);
3694 kfree(data->match_data.event_system);
3696 action_data_destroy(data);
3699 static void destroy_field_var(struct field_var *field_var)
3704 destroy_hist_field(field_var->var, 0);
3705 destroy_hist_field(field_var->val, 0);
3710 static void destroy_field_vars(struct hist_trigger_data *hist_data)
3714 for (i = 0; i < hist_data->n_field_vars; i++)
3715 destroy_field_var(hist_data->field_vars[i]);
3717 for (i = 0; i < hist_data->n_save_vars; i++)
3718 destroy_field_var(hist_data->save_vars[i]);
3721 static void save_field_var(struct hist_trigger_data *hist_data,
3722 struct field_var *field_var)
3724 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
3726 if (field_var->val->flags & HIST_FIELD_FL_STRING)
3727 hist_data->n_field_var_str++;
3731 static int check_synth_field(struct synth_event *event,
3732 struct hist_field *hist_field,
3733 unsigned int field_pos)
3735 struct synth_field *field;
3737 if (field_pos >= event->n_fields)
3740 field = event->fields[field_pos];
3743 * A dynamic string synth field can accept static or
3744 * dynamic. A static string synth field can only accept a
3745 * same-sized static string, which is checked for later.
3747 if (strstr(hist_field->type, "char[") && field->is_string
3748 && field->is_dynamic)
3751 if (strcmp(field->type, hist_field->type) != 0) {
3752 if (field->size != hist_field->size ||
3753 field->is_signed != hist_field->is_signed)
3760 static struct hist_field *
3761 trace_action_find_var(struct hist_trigger_data *hist_data,
3762 struct action_data *data,
3763 char *system, char *event, char *var)
3765 struct trace_array *tr = hist_data->event_file->tr;
3766 struct hist_field *hist_field;
3768 var++; /* skip '$' */
3770 hist_field = find_target_event_var(hist_data, system, event, var);
3772 if (!system && data->handler == HANDLER_ONMATCH) {
3773 system = data->match_data.event_system;
3774 event = data->match_data.event;
3777 hist_field = find_event_var(hist_data, system, event, var);
3781 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var));
3786 static struct hist_field *
3787 trace_action_create_field_var(struct hist_trigger_data *hist_data,
3788 struct action_data *data, char *system,
3789 char *event, char *var)
3791 struct hist_field *hist_field = NULL;
3792 struct field_var *field_var;
3795 * First try to create a field var on the target event (the
3796 * currently being defined). This will create a variable for
3797 * unqualified fields on the target event, or if qualified,
3798 * target fields that have qualified names matching the target.
3800 field_var = create_target_field_var(hist_data, system, event, var);
3802 if (field_var && !IS_ERR(field_var)) {
3803 save_field_var(hist_data, field_var);
3804 hist_field = field_var->var;
3808 * If no explicit system.event is specified, default to
3809 * looking for fields on the onmatch(system.event.xxx)
3812 if (!system && data->handler == HANDLER_ONMATCH) {
3813 system = data->match_data.event_system;
3814 event = data->match_data.event;
3820 * At this point, we're looking at a field on another
3821 * event. Because we can't modify a hist trigger on
3822 * another event to add a variable for a field, we need
3823 * to create a new trigger on that event and create the
3824 * variable at the same time.
3826 hist_field = create_field_var_hist(hist_data, system, event, var);
3827 if (IS_ERR(hist_field))
3833 destroy_field_var(field_var);
3838 static int trace_action_create(struct hist_trigger_data *hist_data,
3839 struct action_data *data)
3841 struct trace_array *tr = hist_data->event_file->tr;
3842 char *event_name, *param, *system = NULL;
3843 struct hist_field *hist_field, *var_ref;
3845 unsigned int field_pos = 0;
3846 struct synth_event *event;
3847 char *synth_event_name;
3848 int var_ref_idx, ret = 0;
3850 lockdep_assert_held(&event_mutex);
3852 if (data->use_trace_keyword)
3853 synth_event_name = data->synth_event_name;
3855 synth_event_name = data->action_name;
3857 event = find_synth_event(synth_event_name);
3859 hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name));
3865 for (i = 0; i < data->n_params; i++) {
3868 p = param = kstrdup(data->params[i], GFP_KERNEL);
3874 system = strsep(¶m, ".");
3876 param = (char *)system;
3877 system = event_name = NULL;
3879 event_name = strsep(¶m, ".");
3887 if (param[0] == '$')
3888 hist_field = trace_action_find_var(hist_data, data,
3892 hist_field = trace_action_create_field_var(hist_data,
3904 if (check_synth_field(event, hist_field, field_pos) == 0) {
3905 var_ref = create_var_ref(hist_data, hist_field,
3906 system, event_name);
3913 var_ref_idx = find_var_ref_idx(hist_data, var_ref);
3914 if (WARN_ON(var_ref_idx < 0)) {
3919 data->var_ref_idx[i] = var_ref_idx;
3926 hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param));
3932 if (field_pos != event->n_fields) {
3933 hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name));
3938 data->synth_event = event;
3947 static int action_create(struct hist_trigger_data *hist_data,
3948 struct action_data *data)
3950 struct trace_event_file *file = hist_data->event_file;
3951 struct trace_array *tr = file->tr;
3952 struct track_data *track_data;
3953 struct field_var *field_var;
3958 if (data->action == ACTION_TRACE)
3959 return trace_action_create(hist_data, data);
3961 if (data->action == ACTION_SNAPSHOT) {
3962 track_data = track_data_alloc(hist_data->key_size, data, hist_data);
3963 if (IS_ERR(track_data)) {
3964 ret = PTR_ERR(track_data);
3968 ret = tracing_snapshot_cond_enable(file->tr, track_data,
3969 cond_snapshot_update);
3971 track_data_free(track_data);
3976 if (data->action == ACTION_SAVE) {
3977 if (hist_data->n_save_vars) {
3979 hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0);
3983 for (i = 0; i < data->n_params; i++) {
3984 param = kstrdup(data->params[i], GFP_KERNEL);
3990 field_var = create_target_field_var(hist_data, NULL, NULL, param);
3991 if (IS_ERR(field_var)) {
3992 hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL,
3994 ret = PTR_ERR(field_var);
3999 hist_data->save_vars[hist_data->n_save_vars++] = field_var;
4000 if (field_var->val->flags & HIST_FIELD_FL_STRING)
4001 hist_data->n_save_var_str++;
4009 static int onmatch_create(struct hist_trigger_data *hist_data,
4010 struct action_data *data)
4012 return action_create(hist_data, data);
4015 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
4017 char *match_event, *match_event_system;
4018 struct action_data *data;
4021 data = kzalloc(sizeof(*data), GFP_KERNEL);
4023 return ERR_PTR(-ENOMEM);
4025 match_event = strsep(&str, ")");
4026 if (!match_event || !str) {
4027 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event));
4031 match_event_system = strsep(&match_event, ".");
4033 hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system));
4037 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
4038 hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event));
4042 data->match_data.event = kstrdup(match_event, GFP_KERNEL);
4043 if (!data->match_data.event) {
4048 data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
4049 if (!data->match_data.event_system) {
4054 ret = action_parse(tr, str, data, HANDLER_ONMATCH);
4060 onmatch_destroy(data);
4061 data = ERR_PTR(ret);
4065 static int create_hitcount_val(struct hist_trigger_data *hist_data)
4067 hist_data->fields[HITCOUNT_IDX] =
4068 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
4069 if (!hist_data->fields[HITCOUNT_IDX])
4072 hist_data->n_vals++;
4073 hist_data->n_fields++;
4075 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
4081 static int __create_val_field(struct hist_trigger_data *hist_data,
4082 unsigned int val_idx,
4083 struct trace_event_file *file,
4084 char *var_name, char *field_str,
4085 unsigned long flags)
4087 struct hist_field *hist_field;
4088 int ret = 0, n_subexprs = 0;
4090 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, &n_subexprs);
4091 if (IS_ERR(hist_field)) {
4092 ret = PTR_ERR(hist_field);
4096 hist_data->fields[val_idx] = hist_field;
4098 ++hist_data->n_vals;
4099 ++hist_data->n_fields;
4101 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4107 static int create_val_field(struct hist_trigger_data *hist_data,
4108 unsigned int val_idx,
4109 struct trace_event_file *file,
4112 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
4115 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
4118 static const char *no_comm = "(no comm)";
4120 static u64 hist_field_execname(struct hist_field *hist_field,
4121 struct tracing_map_elt *elt,
4122 struct trace_buffer *buffer,
4123 struct ring_buffer_event *rbe,
4126 struct hist_elt_data *elt_data;
4128 if (WARN_ON_ONCE(!elt))
4129 return (u64)(unsigned long)no_comm;
4131 elt_data = elt->private_data;
4133 if (WARN_ON_ONCE(!elt_data->comm))
4134 return (u64)(unsigned long)no_comm;
4136 return (u64)(unsigned long)(elt_data->comm);
4139 /* Convert a var that points to common_pid.execname to a string */
4140 static void update_var_execname(struct hist_field *hist_field)
4142 hist_field->flags = HIST_FIELD_FL_STRING | HIST_FIELD_FL_VAR |
4143 HIST_FIELD_FL_EXECNAME;
4144 hist_field->size = MAX_FILTER_STR_VAL;
4145 hist_field->is_signed = 0;
4147 kfree_const(hist_field->type);
4148 hist_field->type = "char[]";
4150 hist_field->fn = hist_field_execname;
4153 static int create_var_field(struct hist_trigger_data *hist_data,
4154 unsigned int val_idx,
4155 struct trace_event_file *file,
4156 char *var_name, char *expr_str)
4158 struct trace_array *tr = hist_data->event_file->tr;
4159 unsigned long flags = 0;
4162 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4165 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
4166 hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name));
4170 flags |= HIST_FIELD_FL_VAR;
4171 hist_data->n_vars++;
4172 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
4175 ret = __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
4177 if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_EXECNAME)
4178 update_var_execname(hist_data->fields[val_idx]);
4180 if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_STRING)
4181 hist_data->fields[val_idx]->var_str_idx = hist_data->n_var_str++;
4186 static int create_val_fields(struct hist_trigger_data *hist_data,
4187 struct trace_event_file *file)
4189 char *fields_str, *field_str;
4190 unsigned int i, j = 1;
4193 ret = create_hitcount_val(hist_data);
4197 fields_str = hist_data->attrs->vals_str;
4201 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
4202 j < TRACING_MAP_VALS_MAX; i++) {
4203 field_str = strsep(&fields_str, ",");
4207 if (strcmp(field_str, "hitcount") == 0)
4210 ret = create_val_field(hist_data, j++, file, field_str);
4215 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
4221 static int create_key_field(struct hist_trigger_data *hist_data,
4222 unsigned int key_idx,
4223 unsigned int key_offset,
4224 struct trace_event_file *file,
4227 struct trace_array *tr = hist_data->event_file->tr;
4228 struct hist_field *hist_field = NULL;
4229 unsigned long flags = 0;
4230 unsigned int key_size;
4231 int ret = 0, n_subexprs = 0;
4233 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
4236 flags |= HIST_FIELD_FL_KEY;
4238 if (strcmp(field_str, "stacktrace") == 0) {
4239 flags |= HIST_FIELD_FL_STACKTRACE;
4240 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
4241 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
4243 hist_field = parse_expr(hist_data, file, field_str, flags,
4245 if (IS_ERR(hist_field)) {
4246 ret = PTR_ERR(hist_field);
4250 if (field_has_hist_vars(hist_field, 0)) {
4251 hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str));
4252 destroy_hist_field(hist_field, 0);
4257 key_size = hist_field->size;
4260 hist_data->fields[key_idx] = hist_field;
4262 key_size = ALIGN(key_size, sizeof(u64));
4263 hist_data->fields[key_idx]->size = key_size;
4264 hist_data->fields[key_idx]->offset = key_offset;
4266 hist_data->key_size += key_size;
4268 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
4273 hist_data->n_keys++;
4274 hist_data->n_fields++;
4276 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
4284 static int create_key_fields(struct hist_trigger_data *hist_data,
4285 struct trace_event_file *file)
4287 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
4288 char *fields_str, *field_str;
4291 fields_str = hist_data->attrs->keys_str;
4295 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
4296 field_str = strsep(&fields_str, ",");
4299 ret = create_key_field(hist_data, i, key_offset,
4314 static int create_var_fields(struct hist_trigger_data *hist_data,
4315 struct trace_event_file *file)
4317 unsigned int i, j = hist_data->n_vals;
4320 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
4322 for (i = 0; i < n_vars; i++) {
4323 char *var_name = hist_data->attrs->var_defs.name[i];
4324 char *expr = hist_data->attrs->var_defs.expr[i];
4326 ret = create_var_field(hist_data, j++, file, var_name, expr);
4334 static void free_var_defs(struct hist_trigger_data *hist_data)
4338 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
4339 kfree(hist_data->attrs->var_defs.name[i]);
4340 kfree(hist_data->attrs->var_defs.expr[i]);
4343 hist_data->attrs->var_defs.n_vars = 0;
4346 static int parse_var_defs(struct hist_trigger_data *hist_data)
4348 struct trace_array *tr = hist_data->event_file->tr;
4349 char *s, *str, *var_name, *field_str;
4350 unsigned int i, j, n_vars = 0;
4353 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
4354 str = hist_data->attrs->assignment_str[i];
4355 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
4356 field_str = strsep(&str, ",");
4360 var_name = strsep(&field_str, "=");
4361 if (!var_name || !field_str) {
4362 hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT,
4368 if (n_vars == TRACING_MAP_VARS_MAX) {
4369 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name));
4374 s = kstrdup(var_name, GFP_KERNEL);
4379 hist_data->attrs->var_defs.name[n_vars] = s;
4381 s = kstrdup(field_str, GFP_KERNEL);
4386 hist_data->attrs->var_defs.expr[n_vars++] = s;
4388 hist_data->attrs->var_defs.n_vars = n_vars;
4394 free_var_defs(hist_data);
4399 static int create_hist_fields(struct hist_trigger_data *hist_data,
4400 struct trace_event_file *file)
4404 ret = parse_var_defs(hist_data);
4408 ret = create_val_fields(hist_data, file);
4412 ret = create_var_fields(hist_data, file);
4416 ret = create_key_fields(hist_data, file);
4420 free_var_defs(hist_data);
4425 static int is_descending(struct trace_array *tr, const char *str)
4430 if (strcmp(str, "descending") == 0)
4433 if (strcmp(str, "ascending") == 0)
4436 hist_err(tr, HIST_ERR_INVALID_SORT_MODIFIER, errpos((char *)str));
4441 static int create_sort_keys(struct hist_trigger_data *hist_data)
4443 struct trace_array *tr = hist_data->event_file->tr;
4444 char *fields_str = hist_data->attrs->sort_key_str;
4445 struct tracing_map_sort_key *sort_key;
4446 int descending, ret = 0;
4447 unsigned int i, j, k;
4449 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
4454 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
4455 struct hist_field *hist_field;
4456 char *field_str, *field_name;
4457 const char *test_name;
4459 sort_key = &hist_data->sort_keys[i];
4461 field_str = strsep(&fields_str, ",");
4467 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4471 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4472 hist_err(tr, HIST_ERR_TOO_MANY_SORT_FIELDS, errpos("sort="));
4477 field_name = strsep(&field_str, ".");
4478 if (!field_name || !*field_name) {
4480 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
4484 if (strcmp(field_name, "hitcount") == 0) {
4485 descending = is_descending(tr, field_str);
4486 if (descending < 0) {
4490 sort_key->descending = descending;
4494 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4497 hist_field = hist_data->fields[j];
4498 if (hist_field->flags & HIST_FIELD_FL_VAR)
4503 test_name = hist_field_name(hist_field, 0);
4505 if (strcmp(field_name, test_name) == 0) {
4506 sort_key->field_idx = idx;
4507 descending = is_descending(tr, field_str);
4508 if (descending < 0) {
4512 sort_key->descending = descending;
4516 if (j == hist_data->n_fields) {
4518 hist_err(tr, HIST_ERR_INVALID_SORT_FIELD, errpos(field_name));
4523 hist_data->n_sort_keys = i;
4528 static void destroy_actions(struct hist_trigger_data *hist_data)
4532 for (i = 0; i < hist_data->n_actions; i++) {
4533 struct action_data *data = hist_data->actions[i];
4535 if (data->handler == HANDLER_ONMATCH)
4536 onmatch_destroy(data);
4537 else if (data->handler == HANDLER_ONMAX ||
4538 data->handler == HANDLER_ONCHANGE)
4539 track_data_destroy(hist_data, data);
4545 static int parse_actions(struct hist_trigger_data *hist_data)
4547 struct trace_array *tr = hist_data->event_file->tr;
4548 struct action_data *data;
4554 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4555 str = hist_data->attrs->action_str[i];
4557 if ((len = str_has_prefix(str, "onmatch("))) {
4558 char *action_str = str + len;
4560 data = onmatch_parse(tr, action_str);
4562 ret = PTR_ERR(data);
4565 } else if ((len = str_has_prefix(str, "onmax("))) {
4566 char *action_str = str + len;
4568 data = track_data_parse(hist_data, action_str,
4571 ret = PTR_ERR(data);
4574 } else if ((len = str_has_prefix(str, "onchange("))) {
4575 char *action_str = str + len;
4577 data = track_data_parse(hist_data, action_str,
4580 ret = PTR_ERR(data);
4588 hist_data->actions[hist_data->n_actions++] = data;
4594 static int create_actions(struct hist_trigger_data *hist_data)
4596 struct action_data *data;
4600 for (i = 0; i < hist_data->attrs->n_actions; i++) {
4601 data = hist_data->actions[i];
4603 if (data->handler == HANDLER_ONMATCH) {
4604 ret = onmatch_create(hist_data, data);
4607 } else if (data->handler == HANDLER_ONMAX ||
4608 data->handler == HANDLER_ONCHANGE) {
4609 ret = track_data_create(hist_data, data);
4621 static void print_actions(struct seq_file *m,
4622 struct hist_trigger_data *hist_data,
4623 struct tracing_map_elt *elt)
4627 for (i = 0; i < hist_data->n_actions; i++) {
4628 struct action_data *data = hist_data->actions[i];
4630 if (data->action == ACTION_SNAPSHOT)
4633 if (data->handler == HANDLER_ONMAX ||
4634 data->handler == HANDLER_ONCHANGE)
4635 track_data_print(m, hist_data, elt, data);
4639 static void print_action_spec(struct seq_file *m,
4640 struct hist_trigger_data *hist_data,
4641 struct action_data *data)
4645 if (data->action == ACTION_SAVE) {
4646 for (i = 0; i < hist_data->n_save_vars; i++) {
4647 seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
4648 if (i < hist_data->n_save_vars - 1)
4651 } else if (data->action == ACTION_TRACE) {
4652 if (data->use_trace_keyword)
4653 seq_printf(m, "%s", data->synth_event_name);
4654 for (i = 0; i < data->n_params; i++) {
4655 if (i || data->use_trace_keyword)
4657 seq_printf(m, "%s", data->params[i]);
4662 static void print_track_data_spec(struct seq_file *m,
4663 struct hist_trigger_data *hist_data,
4664 struct action_data *data)
4666 if (data->handler == HANDLER_ONMAX)
4667 seq_puts(m, ":onmax(");
4668 else if (data->handler == HANDLER_ONCHANGE)
4669 seq_puts(m, ":onchange(");
4670 seq_printf(m, "%s", data->track_data.var_str);
4671 seq_printf(m, ").%s(", data->action_name);
4673 print_action_spec(m, hist_data, data);
4678 static void print_onmatch_spec(struct seq_file *m,
4679 struct hist_trigger_data *hist_data,
4680 struct action_data *data)
4682 seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
4683 data->match_data.event);
4685 seq_printf(m, "%s(", data->action_name);
4687 print_action_spec(m, hist_data, data);
4692 static bool actions_match(struct hist_trigger_data *hist_data,
4693 struct hist_trigger_data *hist_data_test)
4697 if (hist_data->n_actions != hist_data_test->n_actions)
4700 for (i = 0; i < hist_data->n_actions; i++) {
4701 struct action_data *data = hist_data->actions[i];
4702 struct action_data *data_test = hist_data_test->actions[i];
4703 char *action_name, *action_name_test;
4705 if (data->handler != data_test->handler)
4707 if (data->action != data_test->action)
4710 if (data->n_params != data_test->n_params)
4713 for (j = 0; j < data->n_params; j++) {
4714 if (strcmp(data->params[j], data_test->params[j]) != 0)
4718 if (data->use_trace_keyword)
4719 action_name = data->synth_event_name;
4721 action_name = data->action_name;
4723 if (data_test->use_trace_keyword)
4724 action_name_test = data_test->synth_event_name;
4726 action_name_test = data_test->action_name;
4728 if (strcmp(action_name, action_name_test) != 0)
4731 if (data->handler == HANDLER_ONMATCH) {
4732 if (strcmp(data->match_data.event_system,
4733 data_test->match_data.event_system) != 0)
4735 if (strcmp(data->match_data.event,
4736 data_test->match_data.event) != 0)
4738 } else if (data->handler == HANDLER_ONMAX ||
4739 data->handler == HANDLER_ONCHANGE) {
4740 if (strcmp(data->track_data.var_str,
4741 data_test->track_data.var_str) != 0)
4750 static void print_actions_spec(struct seq_file *m,
4751 struct hist_trigger_data *hist_data)
4755 for (i = 0; i < hist_data->n_actions; i++) {
4756 struct action_data *data = hist_data->actions[i];
4758 if (data->handler == HANDLER_ONMATCH)
4759 print_onmatch_spec(m, hist_data, data);
4760 else if (data->handler == HANDLER_ONMAX ||
4761 data->handler == HANDLER_ONCHANGE)
4762 print_track_data_spec(m, hist_data, data);
4766 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
4770 for (i = 0; i < hist_data->n_field_var_hists; i++) {
4771 kfree(hist_data->field_var_hists[i]->cmd);
4772 kfree(hist_data->field_var_hists[i]);
4776 static void destroy_hist_data(struct hist_trigger_data *hist_data)
4781 destroy_hist_trigger_attrs(hist_data->attrs);
4782 destroy_hist_fields(hist_data);
4783 tracing_map_destroy(hist_data->map);
4785 destroy_actions(hist_data);
4786 destroy_field_vars(hist_data);
4787 destroy_field_var_hists(hist_data);
4792 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
4794 struct tracing_map *map = hist_data->map;
4795 struct ftrace_event_field *field;
4796 struct hist_field *hist_field;
4799 for_each_hist_field(i, hist_data) {
4800 hist_field = hist_data->fields[i];
4801 if (hist_field->flags & HIST_FIELD_FL_KEY) {
4802 tracing_map_cmp_fn_t cmp_fn;
4804 field = hist_field->field;
4806 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
4807 cmp_fn = tracing_map_cmp_none;
4809 cmp_fn = tracing_map_cmp_num(hist_field->size,
4810 hist_field->is_signed);
4811 else if (is_string_field(field))
4812 cmp_fn = tracing_map_cmp_string;
4814 cmp_fn = tracing_map_cmp_num(field->size,
4816 idx = tracing_map_add_key_field(map,
4819 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
4820 idx = tracing_map_add_sum_field(map);
4825 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4826 idx = tracing_map_add_var(map);
4829 hist_field->var.idx = idx;
4830 hist_field->var.hist_data = hist_data;
4837 static struct hist_trigger_data *
4838 create_hist_data(unsigned int map_bits,
4839 struct hist_trigger_attrs *attrs,
4840 struct trace_event_file *file,
4843 const struct tracing_map_ops *map_ops = NULL;
4844 struct hist_trigger_data *hist_data;
4847 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
4849 return ERR_PTR(-ENOMEM);
4851 hist_data->attrs = attrs;
4852 hist_data->remove = remove;
4853 hist_data->event_file = file;
4855 ret = parse_actions(hist_data);
4859 ret = create_hist_fields(hist_data, file);
4863 ret = create_sort_keys(hist_data);
4867 map_ops = &hist_trigger_elt_data_ops;
4869 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
4870 map_ops, hist_data);
4871 if (IS_ERR(hist_data->map)) {
4872 ret = PTR_ERR(hist_data->map);
4873 hist_data->map = NULL;
4877 ret = create_tracing_map_fields(hist_data);
4883 hist_data->attrs = NULL;
4885 destroy_hist_data(hist_data);
4887 hist_data = ERR_PTR(ret);
4892 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
4893 struct tracing_map_elt *elt,
4894 struct trace_buffer *buffer, void *rec,
4895 struct ring_buffer_event *rbe,
4898 struct hist_elt_data *elt_data;
4899 struct hist_field *hist_field;
4900 unsigned int i, var_idx;
4903 elt_data = elt->private_data;
4904 elt_data->var_ref_vals = var_ref_vals;
4906 for_each_hist_val_field(i, hist_data) {
4907 hist_field = hist_data->fields[i];
4908 hist_val = hist_field->fn(hist_field, elt, buffer, rbe, rec);
4909 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4910 var_idx = hist_field->var.idx;
4912 if (hist_field->flags & HIST_FIELD_FL_STRING) {
4913 unsigned int str_start, var_str_idx, idx;
4914 char *str, *val_str;
4916 str_start = hist_data->n_field_var_str +
4917 hist_data->n_save_var_str;
4918 var_str_idx = hist_field->var_str_idx;
4919 idx = str_start + var_str_idx;
4921 str = elt_data->field_var_str[idx];
4922 val_str = (char *)(uintptr_t)hist_val;
4923 strscpy(str, val_str, STR_VAR_LEN_MAX);
4925 hist_val = (u64)(uintptr_t)str;
4927 tracing_map_set_var(elt, var_idx, hist_val);
4930 tracing_map_update_sum(elt, i, hist_val);
4933 for_each_hist_key_field(i, hist_data) {
4934 hist_field = hist_data->fields[i];
4935 if (hist_field->flags & HIST_FIELD_FL_VAR) {
4936 hist_val = hist_field->fn(hist_field, elt, buffer, rbe, rec);
4937 var_idx = hist_field->var.idx;
4938 tracing_map_set_var(elt, var_idx, hist_val);
4942 update_field_vars(hist_data, elt, buffer, rbe, rec);
4945 static inline void add_to_key(char *compound_key, void *key,
4946 struct hist_field *key_field, void *rec)
4948 size_t size = key_field->size;
4950 if (key_field->flags & HIST_FIELD_FL_STRING) {
4951 struct ftrace_event_field *field;
4953 field = key_field->field;
4954 if (field->filter_type == FILTER_DYN_STRING)
4955 size = *(u32 *)(rec + field->offset) >> 16;
4956 else if (field->filter_type == FILTER_STATIC_STRING)
4959 /* ensure NULL-termination */
4960 if (size > key_field->size - 1)
4961 size = key_field->size - 1;
4963 strncpy(compound_key + key_field->offset, (char *)key, size);
4965 memcpy(compound_key + key_field->offset, key, size);
4969 hist_trigger_actions(struct hist_trigger_data *hist_data,
4970 struct tracing_map_elt *elt,
4971 struct trace_buffer *buffer, void *rec,
4972 struct ring_buffer_event *rbe, void *key,
4975 struct action_data *data;
4978 for (i = 0; i < hist_data->n_actions; i++) {
4979 data = hist_data->actions[i];
4980 data->fn(hist_data, elt, buffer, rec, rbe, key, data, var_ref_vals);
4984 static void event_hist_trigger(struct event_trigger_data *data,
4985 struct trace_buffer *buffer, void *rec,
4986 struct ring_buffer_event *rbe)
4988 struct hist_trigger_data *hist_data = data->private_data;
4989 bool use_compound_key = (hist_data->n_keys > 1);
4990 unsigned long entries[HIST_STACKTRACE_DEPTH];
4991 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
4992 char compound_key[HIST_KEY_SIZE_MAX];
4993 struct tracing_map_elt *elt = NULL;
4994 struct hist_field *key_field;
4999 memset(compound_key, 0, hist_data->key_size);
5001 for_each_hist_key_field(i, hist_data) {
5002 key_field = hist_data->fields[i];
5004 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5005 memset(entries, 0, HIST_STACKTRACE_SIZE);
5006 stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
5007 HIST_STACKTRACE_SKIP);
5010 field_contents = key_field->fn(key_field, elt, buffer, rbe, rec);
5011 if (key_field->flags & HIST_FIELD_FL_STRING) {
5012 key = (void *)(unsigned long)field_contents;
5013 use_compound_key = true;
5015 key = (void *)&field_contents;
5018 if (use_compound_key)
5019 add_to_key(compound_key, key, key_field, rec);
5022 if (use_compound_key)
5025 if (hist_data->n_var_refs &&
5026 !resolve_var_refs(hist_data, key, var_ref_vals, false))
5029 elt = tracing_map_insert(hist_data->map, key);
5033 hist_trigger_elt_update(hist_data, elt, buffer, rec, rbe, var_ref_vals);
5035 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
5036 hist_trigger_actions(hist_data, elt, buffer, rec, rbe, key, var_ref_vals);
5039 static void hist_trigger_stacktrace_print(struct seq_file *m,
5040 unsigned long *stacktrace_entries,
5041 unsigned int max_entries)
5043 unsigned int spaces = 8;
5046 for (i = 0; i < max_entries; i++) {
5047 if (!stacktrace_entries[i])
5050 seq_printf(m, "%*c", 1 + spaces, ' ');
5051 seq_printf(m, "%pS\n", (void*)stacktrace_entries[i]);
5055 static void hist_trigger_print_key(struct seq_file *m,
5056 struct hist_trigger_data *hist_data,
5058 struct tracing_map_elt *elt)
5060 struct hist_field *key_field;
5061 bool multiline = false;
5062 const char *field_name;
5068 for_each_hist_key_field(i, hist_data) {
5069 key_field = hist_data->fields[i];
5071 if (i > hist_data->n_vals)
5074 field_name = hist_field_name(key_field, 0);
5076 if (key_field->flags & HIST_FIELD_FL_HEX) {
5077 uval = *(u64 *)(key + key_field->offset);
5078 seq_printf(m, "%s: %llx", field_name, uval);
5079 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
5080 uval = *(u64 *)(key + key_field->offset);
5081 seq_printf(m, "%s: [%llx] %-45ps", field_name,
5082 uval, (void *)(uintptr_t)uval);
5083 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
5084 uval = *(u64 *)(key + key_field->offset);
5085 seq_printf(m, "%s: [%llx] %-55pS", field_name,
5086 uval, (void *)(uintptr_t)uval);
5087 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
5088 struct hist_elt_data *elt_data = elt->private_data;
5091 if (WARN_ON_ONCE(!elt_data))
5094 comm = elt_data->comm;
5096 uval = *(u64 *)(key + key_field->offset);
5097 seq_printf(m, "%s: %-16s[%10llu]", field_name,
5099 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
5100 const char *syscall_name;
5102 uval = *(u64 *)(key + key_field->offset);
5103 syscall_name = get_syscall_name(uval);
5105 syscall_name = "unknown_syscall";
5107 seq_printf(m, "%s: %-30s[%3llu]", field_name,
5108 syscall_name, uval);
5109 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5110 seq_puts(m, "stacktrace:\n");
5111 hist_trigger_stacktrace_print(m,
5112 key + key_field->offset,
5113 HIST_STACKTRACE_DEPTH);
5115 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
5116 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
5117 *(u64 *)(key + key_field->offset));
5118 } else if (key_field->flags & HIST_FIELD_FL_BUCKET) {
5119 unsigned long buckets = key_field->buckets;
5120 uval = *(u64 *)(key + key_field->offset);
5121 seq_printf(m, "%s: ~ %llu-%llu", field_name,
5122 uval, uval + buckets -1);
5123 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
5124 seq_printf(m, "%s: %-50s", field_name,
5125 (char *)(key + key_field->offset));
5127 uval = *(u64 *)(key + key_field->offset);
5128 seq_printf(m, "%s: %10llu", field_name, uval);
5138 static void hist_trigger_entry_print(struct seq_file *m,
5139 struct hist_trigger_data *hist_data,
5141 struct tracing_map_elt *elt)
5143 const char *field_name;
5146 hist_trigger_print_key(m, hist_data, key, elt);
5148 seq_printf(m, " hitcount: %10llu",
5149 tracing_map_read_sum(elt, HITCOUNT_IDX));
5151 for (i = 1; i < hist_data->n_vals; i++) {
5152 field_name = hist_field_name(hist_data->fields[i], 0);
5154 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
5155 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
5158 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
5159 seq_printf(m, " %s: %10llx", field_name,
5160 tracing_map_read_sum(elt, i));
5162 seq_printf(m, " %s: %10llu", field_name,
5163 tracing_map_read_sum(elt, i));
5167 print_actions(m, hist_data, elt);
5172 static int print_entries(struct seq_file *m,
5173 struct hist_trigger_data *hist_data)
5175 struct tracing_map_sort_entry **sort_entries = NULL;
5176 struct tracing_map *map = hist_data->map;
5179 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
5180 hist_data->n_sort_keys,
5185 for (i = 0; i < n_entries; i++)
5186 hist_trigger_entry_print(m, hist_data,
5187 sort_entries[i]->key,
5188 sort_entries[i]->elt);
5190 tracing_map_destroy_sort_entries(sort_entries, n_entries);
5195 static void hist_trigger_show(struct seq_file *m,
5196 struct event_trigger_data *data, int n)
5198 struct hist_trigger_data *hist_data;
5202 seq_puts(m, "\n\n");
5204 seq_puts(m, "# event histogram\n#\n# trigger info: ");
5205 data->ops->print(m, data->ops, data);
5206 seq_puts(m, "#\n\n");
5208 hist_data = data->private_data;
5209 n_entries = print_entries(m, hist_data);
5213 track_data_snapshot_print(m, hist_data);
5215 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
5216 (u64)atomic64_read(&hist_data->map->hits),
5217 n_entries, (u64)atomic64_read(&hist_data->map->drops));
5220 static int hist_show(struct seq_file *m, void *v)
5222 struct event_trigger_data *data;
5223 struct trace_event_file *event_file;
5226 mutex_lock(&event_mutex);
5228 event_file = event_file_data(m->private);
5229 if (unlikely(!event_file)) {
5234 list_for_each_entry(data, &event_file->triggers, list) {
5235 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5236 hist_trigger_show(m, data, n++);
5240 mutex_unlock(&event_mutex);
5245 static int event_hist_open(struct inode *inode, struct file *file)
5249 ret = security_locked_down(LOCKDOWN_TRACEFS);
5253 return single_open(file, hist_show, file);
5256 const struct file_operations event_hist_fops = {
5257 .open = event_hist_open,
5259 .llseek = seq_lseek,
5260 .release = single_release,
5263 #ifdef CONFIG_HIST_TRIGGERS_DEBUG
5264 static void hist_field_debug_show_flags(struct seq_file *m,
5265 unsigned long flags)
5267 seq_puts(m, " flags:\n");
5269 if (flags & HIST_FIELD_FL_KEY)
5270 seq_puts(m, " HIST_FIELD_FL_KEY\n");
5271 else if (flags & HIST_FIELD_FL_HITCOUNT)
5272 seq_puts(m, " VAL: HIST_FIELD_FL_HITCOUNT\n");
5273 else if (flags & HIST_FIELD_FL_VAR)
5274 seq_puts(m, " HIST_FIELD_FL_VAR\n");
5275 else if (flags & HIST_FIELD_FL_VAR_REF)
5276 seq_puts(m, " HIST_FIELD_FL_VAR_REF\n");
5278 seq_puts(m, " VAL: normal u64 value\n");
5280 if (flags & HIST_FIELD_FL_ALIAS)
5281 seq_puts(m, " HIST_FIELD_FL_ALIAS\n");
5282 else if (flags & HIST_FIELD_FL_CONST)
5283 seq_puts(m, " HIST_FIELD_FL_CONST\n");
5286 static int hist_field_debug_show(struct seq_file *m,
5287 struct hist_field *field, unsigned long flags)
5289 if ((field->flags & flags) != flags) {
5290 seq_printf(m, "ERROR: bad flags - %lx\n", flags);
5294 hist_field_debug_show_flags(m, field->flags);
5296 seq_printf(m, " ftrace_event_field name: %s\n",
5297 field->field->name);
5299 if (field->flags & HIST_FIELD_FL_VAR) {
5300 seq_printf(m, " var.name: %s\n", field->var.name);
5301 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
5305 if (field->flags & HIST_FIELD_FL_CONST)
5306 seq_printf(m, " constant: %llu\n", field->constant);
5308 if (field->flags & HIST_FIELD_FL_ALIAS)
5309 seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n",
5310 field->var_ref_idx);
5312 if (field->flags & HIST_FIELD_FL_VAR_REF) {
5313 seq_printf(m, " name: %s\n", field->name);
5314 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
5316 seq_printf(m, " var.hist_data: %p\n", field->var.hist_data);
5317 seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n",
5318 field->var_ref_idx);
5320 seq_printf(m, " system: %s\n", field->system);
5321 if (field->event_name)
5322 seq_printf(m, " event_name: %s\n", field->event_name);
5325 seq_printf(m, " type: %s\n", field->type);
5326 seq_printf(m, " size: %u\n", field->size);
5327 seq_printf(m, " is_signed: %u\n", field->is_signed);
5332 static int field_var_debug_show(struct seq_file *m,
5333 struct field_var *field_var, unsigned int i,
5336 const char *vars_name = save_vars ? "save_vars" : "field_vars";
5337 struct hist_field *field;
5340 seq_printf(m, "\n hist_data->%s[%d]:\n", vars_name, i);
5342 field = field_var->var;
5344 seq_printf(m, "\n %s[%d].var:\n", vars_name, i);
5346 hist_field_debug_show_flags(m, field->flags);
5347 seq_printf(m, " var.name: %s\n", field->var.name);
5348 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
5351 field = field_var->val;
5353 seq_printf(m, "\n %s[%d].val:\n", vars_name, i);
5355 seq_printf(m, " ftrace_event_field name: %s\n",
5356 field->field->name);
5362 seq_printf(m, " type: %s\n", field->type);
5363 seq_printf(m, " size: %u\n", field->size);
5364 seq_printf(m, " is_signed: %u\n", field->is_signed);
5369 static int hist_action_debug_show(struct seq_file *m,
5370 struct action_data *data, int i)
5374 if (data->handler == HANDLER_ONMAX ||
5375 data->handler == HANDLER_ONCHANGE) {
5376 seq_printf(m, "\n hist_data->actions[%d].track_data.var_ref:\n", i);
5377 ret = hist_field_debug_show(m, data->track_data.var_ref,
5378 HIST_FIELD_FL_VAR_REF);
5382 seq_printf(m, "\n hist_data->actions[%d].track_data.track_var:\n", i);
5383 ret = hist_field_debug_show(m, data->track_data.track_var,
5389 if (data->handler == HANDLER_ONMATCH) {
5390 seq_printf(m, "\n hist_data->actions[%d].match_data.event_system: %s\n",
5391 i, data->match_data.event_system);
5392 seq_printf(m, " hist_data->actions[%d].match_data.event: %s\n",
5393 i, data->match_data.event);
5399 static int hist_actions_debug_show(struct seq_file *m,
5400 struct hist_trigger_data *hist_data)
5404 if (hist_data->n_actions)
5405 seq_puts(m, "\n action tracking variables (for onmax()/onchange()/onmatch()):\n");
5407 for (i = 0; i < hist_data->n_actions; i++) {
5408 struct action_data *action = hist_data->actions[i];
5410 ret = hist_action_debug_show(m, action, i);
5415 if (hist_data->n_save_vars)
5416 seq_puts(m, "\n save action variables (save() params):\n");
5418 for (i = 0; i < hist_data->n_save_vars; i++) {
5419 ret = field_var_debug_show(m, hist_data->save_vars[i], i, true);
5427 static void hist_trigger_debug_show(struct seq_file *m,
5428 struct event_trigger_data *data, int n)
5430 struct hist_trigger_data *hist_data;
5434 seq_puts(m, "\n\n");
5436 seq_puts(m, "# event histogram\n#\n# trigger info: ");
5437 data->ops->print(m, data->ops, data);
5438 seq_puts(m, "#\n\n");
5440 hist_data = data->private_data;
5442 seq_printf(m, "hist_data: %p\n\n", hist_data);
5443 seq_printf(m, " n_vals: %u\n", hist_data->n_vals);
5444 seq_printf(m, " n_keys: %u\n", hist_data->n_keys);
5445 seq_printf(m, " n_fields: %u\n", hist_data->n_fields);
5447 seq_puts(m, "\n val fields:\n\n");
5449 seq_puts(m, " hist_data->fields[0]:\n");
5450 ret = hist_field_debug_show(m, hist_data->fields[0],
5451 HIST_FIELD_FL_HITCOUNT);
5455 for (i = 1; i < hist_data->n_vals; i++) {
5456 seq_printf(m, "\n hist_data->fields[%d]:\n", i);
5457 ret = hist_field_debug_show(m, hist_data->fields[i], 0);
5462 seq_puts(m, "\n key fields:\n");
5464 for (i = hist_data->n_vals; i < hist_data->n_fields; i++) {
5465 seq_printf(m, "\n hist_data->fields[%d]:\n", i);
5466 ret = hist_field_debug_show(m, hist_data->fields[i],
5472 if (hist_data->n_var_refs)
5473 seq_puts(m, "\n variable reference fields:\n");
5475 for (i = 0; i < hist_data->n_var_refs; i++) {
5476 seq_printf(m, "\n hist_data->var_refs[%d]:\n", i);
5477 ret = hist_field_debug_show(m, hist_data->var_refs[i],
5478 HIST_FIELD_FL_VAR_REF);
5483 if (hist_data->n_field_vars)
5484 seq_puts(m, "\n field variables:\n");
5486 for (i = 0; i < hist_data->n_field_vars; i++) {
5487 ret = field_var_debug_show(m, hist_data->field_vars[i], i, false);
5492 ret = hist_actions_debug_show(m, hist_data);
5497 static int hist_debug_show(struct seq_file *m, void *v)
5499 struct event_trigger_data *data;
5500 struct trace_event_file *event_file;
5503 mutex_lock(&event_mutex);
5505 event_file = event_file_data(m->private);
5506 if (unlikely(!event_file)) {
5511 list_for_each_entry(data, &event_file->triggers, list) {
5512 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5513 hist_trigger_debug_show(m, data, n++);
5517 mutex_unlock(&event_mutex);
5522 static int event_hist_debug_open(struct inode *inode, struct file *file)
5526 ret = security_locked_down(LOCKDOWN_TRACEFS);
5530 return single_open(file, hist_debug_show, file);
5533 const struct file_operations event_hist_debug_fops = {
5534 .open = event_hist_debug_open,
5536 .llseek = seq_lseek,
5537 .release = single_release,
5541 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
5543 const char *field_name = hist_field_name(hist_field, 0);
5545 if (hist_field->var.name)
5546 seq_printf(m, "%s=", hist_field->var.name);
5548 if (hist_field->flags & HIST_FIELD_FL_CPU)
5549 seq_puts(m, "common_cpu");
5550 else if (hist_field->flags & HIST_FIELD_FL_CONST)
5551 seq_printf(m, "%llu", hist_field->constant);
5552 else if (field_name) {
5553 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
5554 hist_field->flags & HIST_FIELD_FL_ALIAS)
5556 seq_printf(m, "%s", field_name);
5557 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
5558 seq_puts(m, "common_timestamp");
5560 if (hist_field->flags) {
5561 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
5562 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
5563 const char *flags = get_hist_field_flags(hist_field);
5566 seq_printf(m, ".%s", flags);
5569 if (hist_field->buckets)
5570 seq_printf(m, "=%ld", hist_field->buckets);
5573 static int event_hist_trigger_print(struct seq_file *m,
5574 struct event_trigger_ops *ops,
5575 struct event_trigger_data *data)
5577 struct hist_trigger_data *hist_data = data->private_data;
5578 struct hist_field *field;
5579 bool have_var = false;
5582 seq_puts(m, "hist:");
5585 seq_printf(m, "%s:", data->name);
5587 seq_puts(m, "keys=");
5589 for_each_hist_key_field(i, hist_data) {
5590 field = hist_data->fields[i];
5592 if (i > hist_data->n_vals)
5595 if (field->flags & HIST_FIELD_FL_STACKTRACE)
5596 seq_puts(m, "stacktrace");
5598 hist_field_print(m, field);
5601 seq_puts(m, ":vals=");
5603 for_each_hist_val_field(i, hist_data) {
5604 field = hist_data->fields[i];
5605 if (field->flags & HIST_FIELD_FL_VAR) {
5610 if (i == HITCOUNT_IDX)
5611 seq_puts(m, "hitcount");
5614 hist_field_print(m, field);
5623 for_each_hist_val_field(i, hist_data) {
5624 field = hist_data->fields[i];
5626 if (field->flags & HIST_FIELD_FL_VAR) {
5629 hist_field_print(m, field);
5634 seq_puts(m, ":sort=");
5636 for (i = 0; i < hist_data->n_sort_keys; i++) {
5637 struct tracing_map_sort_key *sort_key;
5638 unsigned int idx, first_key_idx;
5641 first_key_idx = hist_data->n_vals - hist_data->n_vars;
5643 sort_key = &hist_data->sort_keys[i];
5644 idx = sort_key->field_idx;
5646 if (WARN_ON(idx >= HIST_FIELDS_MAX))
5652 if (idx == HITCOUNT_IDX)
5653 seq_puts(m, "hitcount");
5655 if (idx >= first_key_idx)
5656 idx += hist_data->n_vars;
5657 hist_field_print(m, hist_data->fields[idx]);
5660 if (sort_key->descending)
5661 seq_puts(m, ".descending");
5663 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
5664 if (hist_data->enable_timestamps)
5665 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
5667 print_actions_spec(m, hist_data);
5669 if (data->filter_str)
5670 seq_printf(m, " if %s", data->filter_str);
5673 seq_puts(m, " [paused]");
5675 seq_puts(m, " [active]");
5682 static int event_hist_trigger_init(struct event_trigger_ops *ops,
5683 struct event_trigger_data *data)
5685 struct hist_trigger_data *hist_data = data->private_data;
5687 if (!data->ref && hist_data->attrs->name)
5688 save_named_trigger(hist_data->attrs->name, data);
5695 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
5697 struct trace_event_file *file;
5702 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5703 file = hist_data->field_var_hists[i]->hist_data->event_file;
5704 cmd = hist_data->field_var_hists[i]->cmd;
5705 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
5706 "!hist", "hist", cmd);
5707 WARN_ON_ONCE(ret < 0);
5711 static void event_hist_trigger_free(struct event_trigger_ops *ops,
5712 struct event_trigger_data *data)
5714 struct hist_trigger_data *hist_data = data->private_data;
5716 if (WARN_ON_ONCE(data->ref <= 0))
5722 del_named_trigger(data);
5724 trigger_data_free(data);
5726 remove_hist_vars(hist_data);
5728 unregister_field_var_hists(hist_data);
5730 destroy_hist_data(hist_data);
5734 static struct event_trigger_ops event_hist_trigger_ops = {
5735 .func = event_hist_trigger,
5736 .print = event_hist_trigger_print,
5737 .init = event_hist_trigger_init,
5738 .free = event_hist_trigger_free,
5741 static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
5742 struct event_trigger_data *data)
5746 save_named_trigger(data->named_data->name, data);
5748 event_hist_trigger_init(ops, data->named_data);
5753 static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
5754 struct event_trigger_data *data)
5756 if (WARN_ON_ONCE(data->ref <= 0))
5759 event_hist_trigger_free(ops, data->named_data);
5763 del_named_trigger(data);
5764 trigger_data_free(data);
5768 static struct event_trigger_ops event_hist_trigger_named_ops = {
5769 .func = event_hist_trigger,
5770 .print = event_hist_trigger_print,
5771 .init = event_hist_trigger_named_init,
5772 .free = event_hist_trigger_named_free,
5775 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
5778 return &event_hist_trigger_ops;
5781 static void hist_clear(struct event_trigger_data *data)
5783 struct hist_trigger_data *hist_data = data->private_data;
5786 pause_named_trigger(data);
5788 tracepoint_synchronize_unregister();
5790 tracing_map_clear(hist_data->map);
5793 unpause_named_trigger(data);
5796 static bool compatible_field(struct ftrace_event_field *field,
5797 struct ftrace_event_field *test_field)
5799 if (field == test_field)
5801 if (field == NULL || test_field == NULL)
5803 if (strcmp(field->name, test_field->name) != 0)
5805 if (strcmp(field->type, test_field->type) != 0)
5807 if (field->size != test_field->size)
5809 if (field->is_signed != test_field->is_signed)
5815 static bool hist_trigger_match(struct event_trigger_data *data,
5816 struct event_trigger_data *data_test,
5817 struct event_trigger_data *named_data,
5820 struct tracing_map_sort_key *sort_key, *sort_key_test;
5821 struct hist_trigger_data *hist_data, *hist_data_test;
5822 struct hist_field *key_field, *key_field_test;
5825 if (named_data && (named_data != data_test) &&
5826 (named_data != data_test->named_data))
5829 if (!named_data && is_named_trigger(data_test))
5832 hist_data = data->private_data;
5833 hist_data_test = data_test->private_data;
5835 if (hist_data->n_vals != hist_data_test->n_vals ||
5836 hist_data->n_fields != hist_data_test->n_fields ||
5837 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
5840 if (!ignore_filter) {
5841 if ((data->filter_str && !data_test->filter_str) ||
5842 (!data->filter_str && data_test->filter_str))
5846 for_each_hist_field(i, hist_data) {
5847 key_field = hist_data->fields[i];
5848 key_field_test = hist_data_test->fields[i];
5850 if (key_field->flags != key_field_test->flags)
5852 if (!compatible_field(key_field->field, key_field_test->field))
5854 if (key_field->offset != key_field_test->offset)
5856 if (key_field->size != key_field_test->size)
5858 if (key_field->is_signed != key_field_test->is_signed)
5860 if (!!key_field->var.name != !!key_field_test->var.name)
5862 if (key_field->var.name &&
5863 strcmp(key_field->var.name, key_field_test->var.name) != 0)
5867 for (i = 0; i < hist_data->n_sort_keys; i++) {
5868 sort_key = &hist_data->sort_keys[i];
5869 sort_key_test = &hist_data_test->sort_keys[i];
5871 if (sort_key->field_idx != sort_key_test->field_idx ||
5872 sort_key->descending != sort_key_test->descending)
5876 if (!ignore_filter && data->filter_str &&
5877 (strcmp(data->filter_str, data_test->filter_str) != 0))
5880 if (!actions_match(hist_data, hist_data_test))
5886 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
5887 struct event_trigger_data *data,
5888 struct trace_event_file *file)
5890 struct hist_trigger_data *hist_data = data->private_data;
5891 struct event_trigger_data *test, *named_data = NULL;
5892 struct trace_array *tr = file->tr;
5895 if (hist_data->attrs->name) {
5896 named_data = find_named_trigger(hist_data->attrs->name);
5898 if (!hist_trigger_match(data, named_data, named_data,
5900 hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name));
5907 if (hist_data->attrs->name && !named_data)
5910 lockdep_assert_held(&event_mutex);
5912 list_for_each_entry(test, &file->triggers, list) {
5913 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5914 if (!hist_trigger_match(data, test, named_data, false))
5916 if (hist_data->attrs->pause)
5917 test->paused = true;
5918 else if (hist_data->attrs->cont)
5919 test->paused = false;
5920 else if (hist_data->attrs->clear)
5923 hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
5930 if (hist_data->attrs->cont || hist_data->attrs->clear) {
5931 hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0);
5936 if (hist_data->attrs->pause)
5937 data->paused = true;
5940 data->private_data = named_data->private_data;
5941 set_named_trigger_data(data, named_data);
5942 data->ops = &event_hist_trigger_named_ops;
5945 if (data->ops->init) {
5946 ret = data->ops->init(data->ops, data);
5951 if (hist_data->enable_timestamps) {
5952 char *clock = hist_data->attrs->clock;
5954 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
5956 hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock));
5960 tracing_set_filter_buffering(file->tr, true);
5964 destroy_hist_data(hist_data);
5971 static int hist_trigger_enable(struct event_trigger_data *data,
5972 struct trace_event_file *file)
5976 list_add_tail_rcu(&data->list, &file->triggers);
5978 update_cond_flag(file);
5980 if (trace_event_trigger_enable_disable(file, 1) < 0) {
5981 list_del_rcu(&data->list);
5982 update_cond_flag(file);
5989 static bool have_hist_trigger_match(struct event_trigger_data *data,
5990 struct trace_event_file *file)
5992 struct hist_trigger_data *hist_data = data->private_data;
5993 struct event_trigger_data *test, *named_data = NULL;
5996 lockdep_assert_held(&event_mutex);
5998 if (hist_data->attrs->name)
5999 named_data = find_named_trigger(hist_data->attrs->name);
6001 list_for_each_entry(test, &file->triggers, list) {
6002 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6003 if (hist_trigger_match(data, test, named_data, false)) {
6013 static bool hist_trigger_check_refs(struct event_trigger_data *data,
6014 struct trace_event_file *file)
6016 struct hist_trigger_data *hist_data = data->private_data;
6017 struct event_trigger_data *test, *named_data = NULL;
6019 lockdep_assert_held(&event_mutex);
6021 if (hist_data->attrs->name)
6022 named_data = find_named_trigger(hist_data->attrs->name);
6024 list_for_each_entry(test, &file->triggers, list) {
6025 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6026 if (!hist_trigger_match(data, test, named_data, false))
6028 hist_data = test->private_data;
6029 if (check_var_refs(hist_data))
6038 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
6039 struct event_trigger_data *data,
6040 struct trace_event_file *file)
6042 struct hist_trigger_data *hist_data = data->private_data;
6043 struct event_trigger_data *test, *named_data = NULL;
6044 bool unregistered = false;
6046 lockdep_assert_held(&event_mutex);
6048 if (hist_data->attrs->name)
6049 named_data = find_named_trigger(hist_data->attrs->name);
6051 list_for_each_entry(test, &file->triggers, list) {
6052 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6053 if (!hist_trigger_match(data, test, named_data, false))
6055 unregistered = true;
6056 list_del_rcu(&test->list);
6057 trace_event_trigger_enable_disable(file, 0);
6058 update_cond_flag(file);
6063 if (unregistered && test->ops->free)
6064 test->ops->free(test->ops, test);
6066 if (hist_data->enable_timestamps) {
6067 if (!hist_data->remove || unregistered)
6068 tracing_set_filter_buffering(file->tr, false);
6072 static bool hist_file_check_refs(struct trace_event_file *file)
6074 struct hist_trigger_data *hist_data;
6075 struct event_trigger_data *test;
6077 lockdep_assert_held(&event_mutex);
6079 list_for_each_entry(test, &file->triggers, list) {
6080 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6081 hist_data = test->private_data;
6082 if (check_var_refs(hist_data))
6090 static void hist_unreg_all(struct trace_event_file *file)
6092 struct event_trigger_data *test, *n;
6093 struct hist_trigger_data *hist_data;
6094 struct synth_event *se;
6095 const char *se_name;
6097 lockdep_assert_held(&event_mutex);
6099 if (hist_file_check_refs(file))
6102 list_for_each_entry_safe(test, n, &file->triggers, list) {
6103 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6104 hist_data = test->private_data;
6105 list_del_rcu(&test->list);
6106 trace_event_trigger_enable_disable(file, 0);
6108 se_name = trace_event_name(file->event_call);
6109 se = find_synth_event(se_name);
6113 update_cond_flag(file);
6114 if (hist_data->enable_timestamps)
6115 tracing_set_filter_buffering(file->tr, false);
6116 if (test->ops->free)
6117 test->ops->free(test->ops, test);
6122 static int event_hist_trigger_func(struct event_command *cmd_ops,
6123 struct trace_event_file *file,
6124 char *glob, char *cmd, char *param)
6126 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
6127 struct event_trigger_data *trigger_data;
6128 struct hist_trigger_attrs *attrs;
6129 struct event_trigger_ops *trigger_ops;
6130 struct hist_trigger_data *hist_data;
6131 struct synth_event *se;
6132 const char *se_name;
6133 bool remove = false;
6134 char *trigger, *p, *start;
6137 lockdep_assert_held(&event_mutex);
6139 if (glob && strlen(glob)) {
6141 last_cmd_set(file, param);
6151 * separate the trigger from the filter (k:v [if filter])
6152 * allowing for whitespace in the trigger
6154 p = trigger = param;
6156 p = strstr(p, "if");
6161 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
6165 if (p >= param + strlen(param) - (sizeof("if") - 1) - 1)
6167 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
6178 param = strstrip(p);
6179 trigger = strstrip(trigger);
6183 * To simplify arithmetic expression parsing, replace occurrences of
6184 * '.sym-offset' modifier with '.symXoffset'
6186 start = strstr(trigger, ".sym-offset");
6189 start = strstr(start + 11, ".sym-offset");
6192 attrs = parse_hist_trigger_attrs(file->tr, trigger);
6194 return PTR_ERR(attrs);
6196 if (attrs->map_bits)
6197 hist_trigger_bits = attrs->map_bits;
6199 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
6200 if (IS_ERR(hist_data)) {
6201 destroy_hist_trigger_attrs(attrs);
6202 return PTR_ERR(hist_data);
6205 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
6207 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
6208 if (!trigger_data) {
6213 trigger_data->count = -1;
6214 trigger_data->ops = trigger_ops;
6215 trigger_data->cmd_ops = cmd_ops;
6217 INIT_LIST_HEAD(&trigger_data->list);
6218 RCU_INIT_POINTER(trigger_data->filter, NULL);
6220 trigger_data->private_data = hist_data;
6222 /* if param is non-empty, it's supposed to be a filter */
6223 if (param && cmd_ops->set_filter) {
6224 ret = cmd_ops->set_filter(param, trigger_data, file);
6230 if (!have_hist_trigger_match(trigger_data, file))
6233 if (hist_trigger_check_refs(trigger_data, file)) {
6238 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
6239 se_name = trace_event_name(file->event_call);
6240 se = find_synth_event(se_name);
6247 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
6249 * The above returns on success the # of triggers registered,
6250 * but if it didn't register any it returns zero. Consider no
6251 * triggers registered a failure too.
6254 if (!(attrs->pause || attrs->cont || attrs->clear))
6260 if (get_named_trigger_data(trigger_data))
6263 if (has_hist_vars(hist_data))
6264 save_hist_vars(hist_data);
6266 ret = create_actions(hist_data);
6270 ret = tracing_map_init(hist_data->map);
6274 ret = hist_trigger_enable(trigger_data, file);
6278 se_name = trace_event_name(file->event_call);
6279 se = find_synth_event(se_name);
6282 /* Just return zero, not the number of registered triggers */
6290 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
6292 if (cmd_ops->set_filter)
6293 cmd_ops->set_filter(NULL, trigger_data, NULL);
6295 remove_hist_vars(hist_data);
6297 kfree(trigger_data);
6299 destroy_hist_data(hist_data);
6303 static struct event_command trigger_hist_cmd = {
6305 .trigger_type = ETT_EVENT_HIST,
6306 .flags = EVENT_CMD_FL_NEEDS_REC,
6307 .func = event_hist_trigger_func,
6308 .reg = hist_register_trigger,
6309 .unreg = hist_unregister_trigger,
6310 .unreg_all = hist_unreg_all,
6311 .get_trigger_ops = event_hist_get_trigger_ops,
6312 .set_filter = set_trigger_filter,
6315 __init int register_trigger_hist_cmd(void)
6319 ret = register_event_command(&trigger_hist_cmd);
6326 hist_enable_trigger(struct event_trigger_data *data,
6327 struct trace_buffer *buffer, void *rec,
6328 struct ring_buffer_event *event)
6330 struct enable_trigger_data *enable_data = data->private_data;
6331 struct event_trigger_data *test;
6333 list_for_each_entry_rcu(test, &enable_data->file->triggers, list,
6334 lockdep_is_held(&event_mutex)) {
6335 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6336 if (enable_data->enable)
6337 test->paused = false;
6339 test->paused = true;
6345 hist_enable_count_trigger(struct event_trigger_data *data,
6346 struct trace_buffer *buffer, void *rec,
6347 struct ring_buffer_event *event)
6352 if (data->count != -1)
6355 hist_enable_trigger(data, buffer, rec, event);
6358 static struct event_trigger_ops hist_enable_trigger_ops = {
6359 .func = hist_enable_trigger,
6360 .print = event_enable_trigger_print,
6361 .init = event_trigger_init,
6362 .free = event_enable_trigger_free,
6365 static struct event_trigger_ops hist_enable_count_trigger_ops = {
6366 .func = hist_enable_count_trigger,
6367 .print = event_enable_trigger_print,
6368 .init = event_trigger_init,
6369 .free = event_enable_trigger_free,
6372 static struct event_trigger_ops hist_disable_trigger_ops = {
6373 .func = hist_enable_trigger,
6374 .print = event_enable_trigger_print,
6375 .init = event_trigger_init,
6376 .free = event_enable_trigger_free,
6379 static struct event_trigger_ops hist_disable_count_trigger_ops = {
6380 .func = hist_enable_count_trigger,
6381 .print = event_enable_trigger_print,
6382 .init = event_trigger_init,
6383 .free = event_enable_trigger_free,
6386 static struct event_trigger_ops *
6387 hist_enable_get_trigger_ops(char *cmd, char *param)
6389 struct event_trigger_ops *ops;
6392 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
6395 ops = param ? &hist_enable_count_trigger_ops :
6396 &hist_enable_trigger_ops;
6398 ops = param ? &hist_disable_count_trigger_ops :
6399 &hist_disable_trigger_ops;
6404 static void hist_enable_unreg_all(struct trace_event_file *file)
6406 struct event_trigger_data *test, *n;
6408 list_for_each_entry_safe(test, n, &file->triggers, list) {
6409 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
6410 list_del_rcu(&test->list);
6411 update_cond_flag(file);
6412 trace_event_trigger_enable_disable(file, 0);
6413 if (test->ops->free)
6414 test->ops->free(test->ops, test);
6419 static struct event_command trigger_hist_enable_cmd = {
6420 .name = ENABLE_HIST_STR,
6421 .trigger_type = ETT_HIST_ENABLE,
6422 .func = event_enable_trigger_func,
6423 .reg = event_enable_register_trigger,
6424 .unreg = event_enable_unregister_trigger,
6425 .unreg_all = hist_enable_unreg_all,
6426 .get_trigger_ops = hist_enable_get_trigger_ops,
6427 .set_filter = set_trigger_filter,
6430 static struct event_command trigger_hist_disable_cmd = {
6431 .name = DISABLE_HIST_STR,
6432 .trigger_type = ETT_HIST_ENABLE,
6433 .func = event_enable_trigger_func,
6434 .reg = event_enable_register_trigger,
6435 .unreg = event_enable_unregister_trigger,
6436 .unreg_all = hist_enable_unreg_all,
6437 .get_trigger_ops = hist_enable_get_trigger_ops,
6438 .set_filter = set_trigger_filter,
6441 static __init void unregister_trigger_hist_enable_disable_cmds(void)
6443 unregister_event_command(&trigger_hist_enable_cmd);
6444 unregister_event_command(&trigger_hist_disable_cmd);
6447 __init int register_trigger_hist_enable_disable_cmds(void)
6451 ret = register_event_command(&trigger_hist_enable_cmd);
6452 if (WARN_ON(ret < 0))
6454 ret = register_event_command(&trigger_hist_disable_cmd);
6455 if (WARN_ON(ret < 0))
6456 unregister_trigger_hist_enable_disable_cmds();