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"
23 #include "trace_dynevent.h"
25 #define SYNTH_SYSTEM "synthetic"
26 #define SYNTH_FIELDS_MAX 32
28 #define STR_VAR_LEN_MAX 32 /* must be multiple of sizeof(u64) */
31 C(NONE, "No error"), \
32 C(DUPLICATE_VAR, "Variable already defined"), \
33 C(VAR_NOT_UNIQUE, "Variable name not unique, need to use fully qualified name (subsys.event.var) for variable"), \
34 C(TOO_MANY_VARS, "Too many variables defined"), \
35 C(MALFORMED_ASSIGNMENT, "Malformed assignment"), \
36 C(NAMED_MISMATCH, "Named hist trigger doesn't match existing named trigger (includes variables)"), \
37 C(TRIGGER_EEXIST, "Hist trigger already exists"), \
38 C(TRIGGER_ENOENT_CLEAR, "Can't clear or continue a nonexistent hist trigger"), \
39 C(SET_CLOCK_FAIL, "Couldn't set trace_clock"), \
40 C(BAD_FIELD_MODIFIER, "Invalid field modifier"), \
41 C(TOO_MANY_SUBEXPR, "Too many subexpressions (3 max)"), \
42 C(TIMESTAMP_MISMATCH, "Timestamp units in expression don't match"), \
43 C(TOO_MANY_FIELD_VARS, "Too many field variables defined"), \
44 C(EVENT_FILE_NOT_FOUND, "Event file not found"), \
45 C(HIST_NOT_FOUND, "Matching event histogram not found"), \
46 C(HIST_CREATE_FAIL, "Couldn't create histogram for field"), \
47 C(SYNTH_VAR_NOT_FOUND, "Couldn't find synthetic variable"), \
48 C(SYNTH_EVENT_NOT_FOUND,"Couldn't find synthetic event"), \
49 C(SYNTH_TYPE_MISMATCH, "Param type doesn't match synthetic event field type"), \
50 C(SYNTH_COUNT_MISMATCH, "Param count doesn't match synthetic event field count"), \
51 C(FIELD_VAR_PARSE_FAIL, "Couldn't parse field variable"), \
52 C(VAR_CREATE_FIND_FAIL, "Couldn't create or find variable"), \
53 C(ONX_NOT_VAR, "For onmax(x) or onchange(x), x must be a variable"), \
54 C(ONX_VAR_NOT_FOUND, "Couldn't find onmax or onchange variable"), \
55 C(ONX_VAR_CREATE_FAIL, "Couldn't create onmax or onchange variable"), \
56 C(FIELD_VAR_CREATE_FAIL,"Couldn't create field variable"), \
57 C(TOO_MANY_PARAMS, "Too many action params"), \
58 C(PARAM_NOT_FOUND, "Couldn't find param"), \
59 C(INVALID_PARAM, "Invalid action param"), \
60 C(ACTION_NOT_FOUND, "No action found"), \
61 C(NO_SAVE_PARAMS, "No params found for save()"), \
62 C(TOO_MANY_SAVE_ACTIONS,"Can't have more than one save() action per hist"), \
63 C(ACTION_MISMATCH, "Handler doesn't support action"), \
64 C(NO_CLOSING_PAREN, "No closing paren found"), \
65 C(SUBSYS_NOT_FOUND, "Missing subsystem"), \
66 C(INVALID_SUBSYS_EVENT, "Invalid subsystem or event name"), \
67 C(INVALID_REF_KEY, "Using variable references in keys not supported"), \
68 C(VAR_NOT_FOUND, "Couldn't find variable"), \
69 C(FIELD_NOT_FOUND, "Couldn't find field"), \
70 C(EMPTY_ASSIGNMENT, "Empty assignment"), \
71 C(INVALID_SORT_MODIFIER,"Invalid sort modifier"), \
72 C(EMPTY_SORT_FIELD, "Empty sort field"), \
73 C(TOO_MANY_SORT_FIELDS, "Too many sort fields (Max = 2)"), \
74 C(INVALID_SORT_FIELD, "Sort field must be a key or a val"),
77 #define C(a, b) HIST_ERR_##a
84 static const char *err_text[] = { ERRORS };
88 typedef u64 (*hist_field_fn_t) (struct hist_field *field,
89 struct tracing_map_elt *elt,
90 struct ring_buffer_event *rbe,
93 #define HIST_FIELD_OPERANDS_MAX 2
94 #define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
95 #define HIST_ACTIONS_MAX 8
101 FIELD_OP_UNARY_MINUS,
105 * A hist_var (histogram variable) contains variable information for
106 * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF
107 * flag set. A hist_var has a variable name e.g. ts0, and is
108 * associated with a given histogram trigger, as specified by
109 * hist_data. The hist_var idx is the unique index assigned to the
110 * variable by the hist trigger's tracing_map. The idx is what is
111 * used to set a variable's value and, by a variable reference, to
116 struct hist_trigger_data *hist_data;
121 struct ftrace_event_field *field;
127 unsigned int is_signed;
129 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
130 struct hist_trigger_data *hist_data;
133 * Variable fields contain variable-specific info in var.
136 enum field_op_id operator;
141 * The name field is used for EXPR and VAR_REF fields. VAR
142 * fields contain the variable name in var.name.
147 * When a histogram trigger is hit, if it has any references
148 * to variables, the values of those variables are collected
149 * into a var_ref_vals array by resolve_var_refs(). The
150 * current value of each variable is read from the tracing_map
151 * using the hist field's hist_var.idx and entered into the
152 * var_ref_idx entry i.e. var_ref_vals[var_ref_idx].
154 unsigned int var_ref_idx;
158 static u64 hist_field_none(struct hist_field *field,
159 struct tracing_map_elt *elt,
160 struct ring_buffer_event *rbe,
166 static u64 hist_field_counter(struct hist_field *field,
167 struct tracing_map_elt *elt,
168 struct ring_buffer_event *rbe,
174 static u64 hist_field_string(struct hist_field *hist_field,
175 struct tracing_map_elt *elt,
176 struct ring_buffer_event *rbe,
179 char *addr = (char *)(event + hist_field->field->offset);
181 return (u64)(unsigned long)addr;
184 static u64 hist_field_dynstring(struct hist_field *hist_field,
185 struct tracing_map_elt *elt,
186 struct ring_buffer_event *rbe,
189 u32 str_item = *(u32 *)(event + hist_field->field->offset);
190 int str_loc = str_item & 0xffff;
191 char *addr = (char *)(event + str_loc);
193 return (u64)(unsigned long)addr;
196 static u64 hist_field_pstring(struct hist_field *hist_field,
197 struct tracing_map_elt *elt,
198 struct ring_buffer_event *rbe,
201 char **addr = (char **)(event + hist_field->field->offset);
203 return (u64)(unsigned long)*addr;
206 static u64 hist_field_log2(struct hist_field *hist_field,
207 struct tracing_map_elt *elt,
208 struct ring_buffer_event *rbe,
211 struct hist_field *operand = hist_field->operands[0];
213 u64 val = operand->fn(operand, elt, rbe, event);
215 return (u64) ilog2(roundup_pow_of_two(val));
218 static u64 hist_field_plus(struct hist_field *hist_field,
219 struct tracing_map_elt *elt,
220 struct ring_buffer_event *rbe,
223 struct hist_field *operand1 = hist_field->operands[0];
224 struct hist_field *operand2 = hist_field->operands[1];
226 u64 val1 = operand1->fn(operand1, elt, rbe, event);
227 u64 val2 = operand2->fn(operand2, elt, rbe, event);
232 static u64 hist_field_minus(struct hist_field *hist_field,
233 struct tracing_map_elt *elt,
234 struct ring_buffer_event *rbe,
237 struct hist_field *operand1 = hist_field->operands[0];
238 struct hist_field *operand2 = hist_field->operands[1];
240 u64 val1 = operand1->fn(operand1, elt, rbe, event);
241 u64 val2 = operand2->fn(operand2, elt, rbe, event);
246 static u64 hist_field_unary_minus(struct hist_field *hist_field,
247 struct tracing_map_elt *elt,
248 struct ring_buffer_event *rbe,
251 struct hist_field *operand = hist_field->operands[0];
253 s64 sval = (s64)operand->fn(operand, elt, rbe, event);
254 u64 val = (u64)-sval;
259 #define DEFINE_HIST_FIELD_FN(type) \
260 static u64 hist_field_##type(struct hist_field *hist_field, \
261 struct tracing_map_elt *elt, \
262 struct ring_buffer_event *rbe, \
265 type *addr = (type *)(event + hist_field->field->offset); \
267 return (u64)(unsigned long)*addr; \
270 DEFINE_HIST_FIELD_FN(s64);
271 DEFINE_HIST_FIELD_FN(u64);
272 DEFINE_HIST_FIELD_FN(s32);
273 DEFINE_HIST_FIELD_FN(u32);
274 DEFINE_HIST_FIELD_FN(s16);
275 DEFINE_HIST_FIELD_FN(u16);
276 DEFINE_HIST_FIELD_FN(s8);
277 DEFINE_HIST_FIELD_FN(u8);
279 #define for_each_hist_field(i, hist_data) \
280 for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
282 #define for_each_hist_val_field(i, hist_data) \
283 for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
285 #define for_each_hist_key_field(i, hist_data) \
286 for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
288 #define HIST_STACKTRACE_DEPTH 16
289 #define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
290 #define HIST_STACKTRACE_SKIP 5
292 #define HITCOUNT_IDX 0
293 #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
295 enum hist_field_flags {
296 HIST_FIELD_FL_HITCOUNT = 1 << 0,
297 HIST_FIELD_FL_KEY = 1 << 1,
298 HIST_FIELD_FL_STRING = 1 << 2,
299 HIST_FIELD_FL_HEX = 1 << 3,
300 HIST_FIELD_FL_SYM = 1 << 4,
301 HIST_FIELD_FL_SYM_OFFSET = 1 << 5,
302 HIST_FIELD_FL_EXECNAME = 1 << 6,
303 HIST_FIELD_FL_SYSCALL = 1 << 7,
304 HIST_FIELD_FL_STACKTRACE = 1 << 8,
305 HIST_FIELD_FL_LOG2 = 1 << 9,
306 HIST_FIELD_FL_TIMESTAMP = 1 << 10,
307 HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11,
308 HIST_FIELD_FL_VAR = 1 << 12,
309 HIST_FIELD_FL_EXPR = 1 << 13,
310 HIST_FIELD_FL_VAR_REF = 1 << 14,
311 HIST_FIELD_FL_CPU = 1 << 15,
312 HIST_FIELD_FL_ALIAS = 1 << 16,
317 char *name[TRACING_MAP_VARS_MAX];
318 char *expr[TRACING_MAP_VARS_MAX];
321 struct hist_trigger_attrs {
331 unsigned int map_bits;
333 char *assignment_str[TRACING_MAP_VARS_MAX];
334 unsigned int n_assignments;
336 char *action_str[HIST_ACTIONS_MAX];
337 unsigned int n_actions;
339 struct var_defs var_defs;
343 struct hist_field *var;
344 struct hist_field *val;
347 struct field_var_hist {
348 struct hist_trigger_data *hist_data;
352 struct hist_trigger_data {
353 struct hist_field *fields[HIST_FIELDS_MAX];
356 unsigned int n_fields;
358 unsigned int key_size;
359 struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX];
360 unsigned int n_sort_keys;
361 struct trace_event_file *event_file;
362 struct hist_trigger_attrs *attrs;
363 struct tracing_map *map;
364 bool enable_timestamps;
366 struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
367 unsigned int n_var_refs;
369 struct action_data *actions[HIST_ACTIONS_MAX];
370 unsigned int n_actions;
372 struct field_var *field_vars[SYNTH_FIELDS_MAX];
373 unsigned int n_field_vars;
374 unsigned int n_field_var_str;
375 struct field_var_hist *field_var_hists[SYNTH_FIELDS_MAX];
376 unsigned int n_field_var_hists;
378 struct field_var *save_vars[SYNTH_FIELDS_MAX];
379 unsigned int n_save_vars;
380 unsigned int n_save_var_str;
383 static int create_synth_event(int argc, const char **argv);
384 static int synth_event_show(struct seq_file *m, struct dyn_event *ev);
385 static int synth_event_release(struct dyn_event *ev);
386 static bool synth_event_is_busy(struct dyn_event *ev);
387 static bool synth_event_match(const char *system, const char *event,
388 int argc, const char **argv, struct dyn_event *ev);
390 static struct dyn_event_operations synth_event_ops = {
391 .create = create_synth_event,
392 .show = synth_event_show,
393 .is_busy = synth_event_is_busy,
394 .free = synth_event_release,
395 .match = synth_event_match,
408 struct dyn_event devent;
411 struct synth_field **fields;
412 unsigned int n_fields;
414 struct trace_event_class class;
415 struct trace_event_call call;
416 struct tracepoint *tp;
420 static bool is_synth_event(struct dyn_event *ev)
422 return ev->ops == &synth_event_ops;
425 static struct synth_event *to_synth_event(struct dyn_event *ev)
427 return container_of(ev, struct synth_event, devent);
430 static bool synth_event_is_busy(struct dyn_event *ev)
432 struct synth_event *event = to_synth_event(ev);
434 return event->ref != 0;
437 static bool synth_event_match(const char *system, const char *event,
438 int argc, const char **argv, struct dyn_event *ev)
440 struct synth_event *sev = to_synth_event(ev);
442 return strcmp(sev->name, event) == 0 &&
443 (!system || strcmp(system, SYNTH_SYSTEM) == 0);
448 typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
449 struct tracing_map_elt *elt, void *rec,
450 struct ring_buffer_event *rbe, void *key,
451 struct action_data *data, u64 *var_ref_vals);
453 typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val);
468 enum handler_id handler;
469 enum action_id action;
473 unsigned int n_params;
474 char *params[SYNTH_FIELDS_MAX];
477 * When a histogram trigger is hit, the values of any
478 * references to variables, including variables being passed
479 * as parameters to synthetic events, are collected into a
480 * var_ref_vals array. This var_ref_idx array is an array of
481 * indices into the var_ref_vals array, one for each synthetic
482 * event param, and is passed to the synthetic event
485 unsigned int var_ref_idx[TRACING_MAP_VARS_MAX];
486 struct synth_event *synth_event;
487 bool use_trace_keyword;
488 char *synth_event_name;
498 * var_str contains the $-unstripped variable
499 * name referenced by var_ref, and used when
500 * printing the action. Because var_ref
501 * creation is deferred to create_actions(),
502 * we need a per-action way to save it until
503 * then, thus var_str.
508 * var_ref refers to the variable being
509 * tracked e.g onmax($var).
511 struct hist_field *var_ref;
514 * track_var contains the 'invisible' tracking
515 * variable created to keep the current
518 struct hist_field *track_var;
520 check_track_val_fn_t check_val;
521 action_fn_t save_data;
530 unsigned int key_len;
532 struct tracing_map_elt elt;
534 struct action_data *action_data;
535 struct hist_trigger_data *hist_data;
538 struct hist_elt_data {
541 char *field_var_str[SYNTH_FIELDS_MAX];
544 struct snapshot_context {
545 struct tracing_map_elt *elt;
549 static void track_data_free(struct track_data *track_data)
551 struct hist_elt_data *elt_data;
556 kfree(track_data->key);
558 elt_data = track_data->elt.private_data;
560 kfree(elt_data->comm);
567 static struct track_data *track_data_alloc(unsigned int key_len,
568 struct action_data *action_data,
569 struct hist_trigger_data *hist_data)
571 struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
572 struct hist_elt_data *elt_data;
575 return ERR_PTR(-ENOMEM);
577 data->key = kzalloc(key_len, GFP_KERNEL);
579 track_data_free(data);
580 return ERR_PTR(-ENOMEM);
583 data->key_len = key_len;
584 data->action_data = action_data;
585 data->hist_data = hist_data;
587 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
589 track_data_free(data);
590 return ERR_PTR(-ENOMEM);
592 data->elt.private_data = elt_data;
594 elt_data->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL);
595 if (!elt_data->comm) {
596 track_data_free(data);
597 return ERR_PTR(-ENOMEM);
603 static char last_cmd[MAX_FILTER_STR_VAL];
604 static char last_cmd_loc[MAX_FILTER_STR_VAL];
606 static int errpos(char *str)
608 return err_pos(last_cmd, str);
611 static void last_cmd_set(struct trace_event_file *file, char *str)
613 const char *system = NULL, *name = NULL;
614 struct trace_event_call *call;
619 strcpy(last_cmd, "hist:");
620 strncat(last_cmd, str, MAX_FILTER_STR_VAL - 1 - sizeof("hist:"));
623 call = file->event_call;
625 system = call->class->system;
627 name = trace_event_name(call);
634 snprintf(last_cmd_loc, MAX_FILTER_STR_VAL, "hist:%s:%s", system, name);
637 static void hist_err(struct trace_array *tr, u8 err_type, u8 err_pos)
639 tracing_log_err(tr, last_cmd_loc, last_cmd, err_text,
643 static void hist_err_clear(void)
646 last_cmd_loc[0] = '\0';
649 struct synth_trace_event {
650 struct trace_entry ent;
654 static int synth_event_define_fields(struct trace_event_call *call)
656 struct synth_trace_event trace;
657 int offset = offsetof(typeof(trace), fields);
658 struct synth_event *event = call->data;
659 unsigned int i, size, n_u64;
664 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
665 size = event->fields[i]->size;
666 is_signed = event->fields[i]->is_signed;
667 type = event->fields[i]->type;
668 name = event->fields[i]->name;
669 ret = trace_define_field(call, type, name, offset, size,
670 is_signed, FILTER_OTHER);
674 event->fields[i]->offset = n_u64;
676 if (event->fields[i]->is_string) {
677 offset += STR_VAR_LEN_MAX;
678 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
680 offset += sizeof(u64);
685 event->n_u64 = n_u64;
690 static bool synth_field_signed(char *type)
692 if (str_has_prefix(type, "u"))
694 if (strcmp(type, "gfp_t") == 0)
700 static int synth_field_is_string(char *type)
702 if (strstr(type, "char[") != NULL)
708 static int synth_field_string_size(char *type)
710 char buf[4], *end, *start;
714 start = strstr(type, "char[");
717 start += sizeof("char[") - 1;
719 end = strchr(type, ']');
720 if (!end || end < start)
727 strncpy(buf, start, len);
730 err = kstrtouint(buf, 0, &size);
734 if (size > STR_VAR_LEN_MAX)
740 static int synth_field_size(char *type)
744 if (strcmp(type, "s64") == 0)
746 else if (strcmp(type, "u64") == 0)
748 else if (strcmp(type, "s32") == 0)
750 else if (strcmp(type, "u32") == 0)
752 else if (strcmp(type, "s16") == 0)
754 else if (strcmp(type, "u16") == 0)
756 else if (strcmp(type, "s8") == 0)
758 else if (strcmp(type, "u8") == 0)
760 else if (strcmp(type, "char") == 0)
762 else if (strcmp(type, "unsigned char") == 0)
763 size = sizeof(unsigned char);
764 else if (strcmp(type, "int") == 0)
766 else if (strcmp(type, "unsigned int") == 0)
767 size = sizeof(unsigned int);
768 else if (strcmp(type, "long") == 0)
770 else if (strcmp(type, "unsigned long") == 0)
771 size = sizeof(unsigned long);
772 else if (strcmp(type, "pid_t") == 0)
773 size = sizeof(pid_t);
774 else if (strcmp(type, "gfp_t") == 0)
775 size = sizeof(gfp_t);
776 else if (synth_field_is_string(type))
777 size = synth_field_string_size(type);
782 static const char *synth_field_fmt(char *type)
784 const char *fmt = "%llu";
786 if (strcmp(type, "s64") == 0)
788 else if (strcmp(type, "u64") == 0)
790 else if (strcmp(type, "s32") == 0)
792 else if (strcmp(type, "u32") == 0)
794 else if (strcmp(type, "s16") == 0)
796 else if (strcmp(type, "u16") == 0)
798 else if (strcmp(type, "s8") == 0)
800 else if (strcmp(type, "u8") == 0)
802 else if (strcmp(type, "char") == 0)
804 else if (strcmp(type, "unsigned char") == 0)
806 else if (strcmp(type, "int") == 0)
808 else if (strcmp(type, "unsigned int") == 0)
810 else if (strcmp(type, "long") == 0)
812 else if (strcmp(type, "unsigned long") == 0)
814 else if (strcmp(type, "pid_t") == 0)
816 else if (strcmp(type, "gfp_t") == 0)
818 else if (synth_field_is_string(type))
824 static enum print_line_t print_synth_event(struct trace_iterator *iter,
826 struct trace_event *event)
828 struct trace_array *tr = iter->tr;
829 struct trace_seq *s = &iter->seq;
830 struct synth_trace_event *entry;
831 struct synth_event *se;
832 unsigned int i, n_u64;
836 entry = (struct synth_trace_event *)iter->ent;
837 se = container_of(event, struct synth_event, call.event);
839 trace_seq_printf(s, "%s: ", se->name);
841 for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
842 if (trace_seq_has_overflowed(s))
845 fmt = synth_field_fmt(se->fields[i]->type);
847 /* parameter types */
848 if (tr && tr->trace_flags & TRACE_ITER_VERBOSE)
849 trace_seq_printf(s, "%s ", fmt);
851 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
853 /* parameter values */
854 if (se->fields[i]->is_string) {
855 trace_seq_printf(s, print_fmt, se->fields[i]->name,
856 (char *)&entry->fields[n_u64],
857 i == se->n_fields - 1 ? "" : " ");
858 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
860 struct trace_print_flags __flags[] = {
861 __def_gfpflag_names, {-1, NULL} };
863 trace_seq_printf(s, print_fmt, se->fields[i]->name,
864 entry->fields[n_u64],
865 i == se->n_fields - 1 ? "" : " ");
867 if (strcmp(se->fields[i]->type, "gfp_t") == 0) {
868 trace_seq_puts(s, " (");
869 trace_print_flags_seq(s, "|",
870 entry->fields[n_u64],
872 trace_seq_putc(s, ')');
878 trace_seq_putc(s, '\n');
880 return trace_handle_return(s);
883 static struct trace_event_functions synth_event_funcs = {
884 .trace = print_synth_event
887 static notrace void trace_event_raw_event_synth(void *__data,
889 unsigned int *var_ref_idx)
891 struct trace_event_file *trace_file = __data;
892 struct synth_trace_event *entry;
893 struct trace_event_buffer fbuffer;
894 struct trace_buffer *buffer;
895 struct synth_event *event;
896 unsigned int i, n_u64, val_idx;
899 event = trace_file->event_call->data;
901 if (trace_trigger_soft_disabled(trace_file))
904 fields_size = event->n_u64 * sizeof(u64);
907 * Avoid ring buffer recursion detection, as this event
908 * is being performed within another event.
910 buffer = trace_file->tr->array_buffer.buffer;
911 ring_buffer_nest_start(buffer);
913 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
914 sizeof(*entry) + fields_size);
918 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
919 val_idx = var_ref_idx[i];
920 if (event->fields[i]->is_string) {
921 char *str_val = (char *)(long)var_ref_vals[val_idx];
922 char *str_field = (char *)&entry->fields[n_u64];
924 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
925 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
927 struct synth_field *field = event->fields[i];
928 u64 val = var_ref_vals[val_idx];
930 switch (field->size) {
932 *(u8 *)&entry->fields[n_u64] = (u8)val;
936 *(u16 *)&entry->fields[n_u64] = (u16)val;
940 *(u32 *)&entry->fields[n_u64] = (u32)val;
944 entry->fields[n_u64] = val;
951 trace_event_buffer_commit(&fbuffer);
953 ring_buffer_nest_end(buffer);
956 static void free_synth_event_print_fmt(struct trace_event_call *call)
959 kfree(call->print_fmt);
960 call->print_fmt = NULL;
964 static int __set_synth_event_print_fmt(struct synth_event *event,
971 /* When len=0, we just calculate the needed length */
972 #define LEN_OR_ZERO (len ? len - pos : 0)
974 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
975 for (i = 0; i < event->n_fields; i++) {
976 fmt = synth_field_fmt(event->fields[i]->type);
977 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
978 event->fields[i]->name, fmt,
979 i == event->n_fields - 1 ? "" : ", ");
981 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
983 for (i = 0; i < event->n_fields; i++) {
984 pos += snprintf(buf + pos, LEN_OR_ZERO,
985 ", REC->%s", event->fields[i]->name);
990 /* return the length of print_fmt */
994 static int set_synth_event_print_fmt(struct trace_event_call *call)
996 struct synth_event *event = call->data;
1000 /* First: called with 0 length to calculate the needed length */
1001 len = __set_synth_event_print_fmt(event, NULL, 0);
1003 print_fmt = kmalloc(len + 1, GFP_KERNEL);
1007 /* Second: actually write the @print_fmt */
1008 __set_synth_event_print_fmt(event, print_fmt, len + 1);
1009 call->print_fmt = print_fmt;
1014 static void free_synth_field(struct synth_field *field)
1021 static struct synth_field *parse_synth_field(int argc, const char **argv,
1024 struct synth_field *field;
1025 const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
1028 if (field_type[0] == ';')
1031 if (!strcmp(field_type, "unsigned")) {
1033 return ERR_PTR(-EINVAL);
1034 prefix = "unsigned ";
1035 field_type = argv[1];
1036 field_name = argv[2];
1039 field_name = argv[1];
1043 field = kzalloc(sizeof(*field), GFP_KERNEL);
1045 return ERR_PTR(-ENOMEM);
1047 len = strlen(field_name);
1048 array = strchr(field_name, '[');
1050 len -= strlen(array);
1051 else if (field_name[len - 1] == ';')
1054 field->name = kmemdup_nul(field_name, len, GFP_KERNEL);
1060 if (field_type[0] == ';')
1062 len = strlen(field_type) + 1;
1064 len += strlen(array);
1066 len += strlen(prefix);
1068 field->type = kzalloc(len, GFP_KERNEL);
1074 strcat(field->type, prefix);
1075 strcat(field->type, field_type);
1077 strcat(field->type, array);
1078 if (field->type[len - 1] == ';')
1079 field->type[len - 1] = '\0';
1082 field->size = synth_field_size(field->type);
1088 if (synth_field_is_string(field->type))
1089 field->is_string = true;
1091 field->is_signed = synth_field_signed(field->type);
1096 free_synth_field(field);
1097 field = ERR_PTR(ret);
1101 static void free_synth_tracepoint(struct tracepoint *tp)
1110 static struct tracepoint *alloc_synth_tracepoint(char *name)
1112 struct tracepoint *tp;
1114 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
1116 return ERR_PTR(-ENOMEM);
1118 tp->name = kstrdup(name, GFP_KERNEL);
1121 return ERR_PTR(-ENOMEM);
1127 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
1128 unsigned int *var_ref_idx);
1130 static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
1131 unsigned int *var_ref_idx)
1133 struct tracepoint *tp = event->tp;
1135 if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
1136 struct tracepoint_func *probe_func_ptr;
1137 synth_probe_func_t probe_func;
1140 if (!(cpu_online(raw_smp_processor_id())))
1143 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
1144 if (probe_func_ptr) {
1146 probe_func = probe_func_ptr->func;
1147 __data = probe_func_ptr->data;
1148 probe_func(__data, var_ref_vals, var_ref_idx);
1149 } while ((++probe_func_ptr)->func);
1154 static struct synth_event *find_synth_event(const char *name)
1156 struct dyn_event *pos;
1157 struct synth_event *event;
1159 for_each_dyn_event(pos) {
1160 if (!is_synth_event(pos))
1162 event = to_synth_event(pos);
1163 if (strcmp(event->name, name) == 0)
1170 static struct trace_event_fields synth_event_fields_array[] = {
1171 { .type = TRACE_FUNCTION_TYPE,
1172 .define_fields = synth_event_define_fields },
1176 static int register_synth_event(struct synth_event *event)
1178 struct trace_event_call *call = &event->call;
1181 event->call.class = &event->class;
1182 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
1183 if (!event->class.system) {
1188 event->tp = alloc_synth_tracepoint(event->name);
1189 if (IS_ERR(event->tp)) {
1190 ret = PTR_ERR(event->tp);
1195 INIT_LIST_HEAD(&call->class->fields);
1196 call->event.funcs = &synth_event_funcs;
1197 call->class->fields_array = synth_event_fields_array;
1199 ret = register_trace_event(&call->event);
1204 call->flags = TRACE_EVENT_FL_TRACEPOINT;
1205 call->class->reg = trace_event_reg;
1206 call->class->probe = trace_event_raw_event_synth;
1208 call->tp = event->tp;
1210 ret = trace_add_event_call(call);
1212 pr_warn("Failed to register synthetic event: %s\n",
1213 trace_event_name(call));
1217 ret = set_synth_event_print_fmt(call);
1219 trace_remove_event_call(call);
1225 unregister_trace_event(&call->event);
1229 static int unregister_synth_event(struct synth_event *event)
1231 struct trace_event_call *call = &event->call;
1234 ret = trace_remove_event_call(call);
1239 static void free_synth_event(struct synth_event *event)
1246 for (i = 0; i < event->n_fields; i++)
1247 free_synth_field(event->fields[i]);
1249 kfree(event->fields);
1251 kfree(event->class.system);
1252 free_synth_tracepoint(event->tp);
1253 free_synth_event_print_fmt(&event->call);
1257 static struct synth_event *alloc_synth_event(const char *name, int n_fields,
1258 struct synth_field **fields)
1260 struct synth_event *event;
1263 event = kzalloc(sizeof(*event), GFP_KERNEL);
1265 event = ERR_PTR(-ENOMEM);
1269 event->name = kstrdup(name, GFP_KERNEL);
1272 event = ERR_PTR(-ENOMEM);
1276 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
1277 if (!event->fields) {
1278 free_synth_event(event);
1279 event = ERR_PTR(-ENOMEM);
1283 dyn_event_init(&event->devent, &synth_event_ops);
1285 for (i = 0; i < n_fields; i++)
1286 event->fields[i] = fields[i];
1288 event->n_fields = n_fields;
1293 static void action_trace(struct hist_trigger_data *hist_data,
1294 struct tracing_map_elt *elt, void *rec,
1295 struct ring_buffer_event *rbe, void *key,
1296 struct action_data *data, u64 *var_ref_vals)
1298 struct synth_event *event = data->synth_event;
1300 trace_synth(event, var_ref_vals, data->var_ref_idx);
1303 struct hist_var_data {
1304 struct list_head list;
1305 struct hist_trigger_data *hist_data;
1308 static int synth_event_check_arg_fn(void *data)
1310 struct dynevent_arg_pair *arg_pair = data;
1313 size = synth_field_size((char *)arg_pair->lhs);
1315 return size ? 0 : -EINVAL;
1319 * synth_event_add_field - Add a new field to a synthetic event cmd
1320 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1321 * @type: The type of the new field to add
1322 * @name: The name of the new field to add
1324 * Add a new field to a synthetic event cmd object. Field ordering is in
1325 * the same order the fields are added.
1327 * See synth_field_size() for available types. If field_name contains
1328 * [n] the field is considered to be an array.
1330 * Return: 0 if successful, error otherwise.
1332 int synth_event_add_field(struct dynevent_cmd *cmd, const char *type,
1335 struct dynevent_arg_pair arg_pair;
1338 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1344 dynevent_arg_pair_init(&arg_pair, 0, ';');
1346 arg_pair.lhs = type;
1347 arg_pair.rhs = name;
1349 ret = dynevent_arg_pair_add(cmd, &arg_pair, synth_event_check_arg_fn);
1353 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
1358 EXPORT_SYMBOL_GPL(synth_event_add_field);
1361 * synth_event_add_field_str - Add a new field to a synthetic event cmd
1362 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1363 * @type_name: The type and name of the new field to add, as a single string
1365 * Add a new field to a synthetic event cmd object, as a single
1366 * string. The @type_name string is expected to be of the form 'type
1367 * name', which will be appended by ';'. No sanity checking is done -
1368 * what's passed in is assumed to already be well-formed. Field
1369 * ordering is in the same order the fields are added.
1371 * See synth_field_size() for available types. If field_name contains
1372 * [n] the field is considered to be an array.
1374 * Return: 0 if successful, error otherwise.
1376 int synth_event_add_field_str(struct dynevent_cmd *cmd, const char *type_name)
1378 struct dynevent_arg arg;
1381 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1387 dynevent_arg_init(&arg, ';');
1389 arg.str = type_name;
1391 ret = dynevent_arg_add(cmd, &arg, NULL);
1395 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
1400 EXPORT_SYMBOL_GPL(synth_event_add_field_str);
1403 * synth_event_add_fields - Add multiple fields to a synthetic event cmd
1404 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1405 * @fields: An array of type/name field descriptions
1406 * @n_fields: The number of field descriptions contained in the fields array
1408 * Add a new set of fields to a synthetic event cmd object. The event
1409 * fields that will be defined for the event should be passed in as an
1410 * array of struct synth_field_desc, and the number of elements in the
1411 * array passed in as n_fields. Field ordering will retain the
1412 * ordering given in the fields array.
1414 * See synth_field_size() for available types. If field_name contains
1415 * [n] the field is considered to be an array.
1417 * Return: 0 if successful, error otherwise.
1419 int synth_event_add_fields(struct dynevent_cmd *cmd,
1420 struct synth_field_desc *fields,
1421 unsigned int n_fields)
1426 for (i = 0; i < n_fields; i++) {
1427 if (fields[i].type == NULL || fields[i].name == NULL) {
1432 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1439 EXPORT_SYMBOL_GPL(synth_event_add_fields);
1442 * __synth_event_gen_cmd_start - Start a synthetic event command from arg list
1443 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1444 * @name: The name of the synthetic event
1445 * @mod: The module creating the event, NULL if not created from a module
1446 * @args: Variable number of arg (pairs), one pair for each field
1448 * NOTE: Users normally won't want to call this function directly, but
1449 * rather use the synth_event_gen_cmd_start() wrapper, which
1450 * automatically adds a NULL to the end of the arg list. If this
1451 * function is used directly, make sure the last arg in the variable
1454 * Generate a synthetic event command to be executed by
1455 * synth_event_gen_cmd_end(). This function can be used to generate
1456 * the complete command or only the first part of it; in the latter
1457 * case, synth_event_add_field(), synth_event_add_field_str(), or
1458 * synth_event_add_fields() can be used to add more fields following
1461 * There should be an even number variable args, each pair consisting
1462 * of a type followed by a field name.
1464 * See synth_field_size() for available types. If field_name contains
1465 * [n] the field is considered to be an array.
1467 * Return: 0 if successful, error otherwise.
1469 int __synth_event_gen_cmd_start(struct dynevent_cmd *cmd, const char *name,
1470 struct module *mod, ...)
1472 struct dynevent_arg arg;
1476 cmd->event_name = name;
1477 cmd->private_data = mod;
1479 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1482 dynevent_arg_init(&arg, 0);
1484 ret = dynevent_arg_add(cmd, &arg, NULL);
1488 va_start(args, mod);
1490 const char *type, *name;
1492 type = va_arg(args, const char *);
1495 name = va_arg(args, const char *);
1499 if (++cmd->n_fields > SYNTH_FIELDS_MAX) {
1504 ret = synth_event_add_field(cmd, type, name);
1512 EXPORT_SYMBOL_GPL(__synth_event_gen_cmd_start);
1515 * synth_event_gen_cmd_array_start - Start synthetic event command from an array
1516 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1517 * @name: The name of the synthetic event
1518 * @fields: An array of type/name field descriptions
1519 * @n_fields: The number of field descriptions contained in the fields array
1521 * Generate a synthetic event command to be executed by
1522 * synth_event_gen_cmd_end(). This function can be used to generate
1523 * the complete command or only the first part of it; in the latter
1524 * case, synth_event_add_field(), synth_event_add_field_str(), or
1525 * synth_event_add_fields() can be used to add more fields following
1528 * The event fields that will be defined for the event should be
1529 * passed in as an array of struct synth_field_desc, and the number of
1530 * elements in the array passed in as n_fields. Field ordering will
1531 * retain the ordering given in the fields array.
1533 * See synth_field_size() for available types. If field_name contains
1534 * [n] the field is considered to be an array.
1536 * Return: 0 if successful, error otherwise.
1538 int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd, const char *name,
1540 struct synth_field_desc *fields,
1541 unsigned int n_fields)
1543 struct dynevent_arg arg;
1547 cmd->event_name = name;
1548 cmd->private_data = mod;
1550 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1553 if (n_fields > SYNTH_FIELDS_MAX)
1556 dynevent_arg_init(&arg, 0);
1558 ret = dynevent_arg_add(cmd, &arg, NULL);
1562 for (i = 0; i < n_fields; i++) {
1563 if (fields[i].type == NULL || fields[i].name == NULL)
1566 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1573 EXPORT_SYMBOL_GPL(synth_event_gen_cmd_array_start);
1575 static int __create_synth_event(int argc, const char *name, const char **argv)
1577 struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1578 struct synth_event *event = NULL;
1579 int i, consumed = 0, n_fields = 0, ret = 0;
1583 * - Add synthetic event: <event_name> field[;field] ...
1584 * - Remove synthetic event: !<event_name> field[;field] ...
1585 * where 'field' = type field_name
1588 if (name[0] == '\0' || argc < 1)
1591 mutex_lock(&event_mutex);
1593 event = find_synth_event(name);
1599 for (i = 0; i < argc - 1; i++) {
1600 if (strcmp(argv[i], ";") == 0)
1602 if (n_fields == SYNTH_FIELDS_MAX) {
1607 field = parse_synth_field(argc - i, &argv[i], &consumed);
1608 if (IS_ERR(field)) {
1609 ret = PTR_ERR(field);
1612 fields[n_fields++] = field;
1616 if (i < argc && strcmp(argv[i], ";") != 0) {
1621 event = alloc_synth_event(name, n_fields, fields);
1622 if (IS_ERR(event)) {
1623 ret = PTR_ERR(event);
1627 ret = register_synth_event(event);
1629 dyn_event_add(&event->devent);
1631 free_synth_event(event);
1633 mutex_unlock(&event_mutex);
1637 for (i = 0; i < n_fields; i++)
1638 free_synth_field(fields[i]);
1644 * synth_event_create - Create a new synthetic event
1645 * @name: The name of the new sythetic event
1646 * @fields: An array of type/name field descriptions
1647 * @n_fields: The number of field descriptions contained in the fields array
1648 * @mod: The module creating the event, NULL if not created from a module
1650 * Create a new synthetic event with the given name under the
1651 * trace/events/synthetic/ directory. The event fields that will be
1652 * defined for the event should be passed in as an array of struct
1653 * synth_field_desc, and the number elements in the array passed in as
1654 * n_fields. Field ordering will retain the ordering given in the
1657 * If the new synthetic event is being created from a module, the mod
1658 * param must be non-NULL. This will ensure that the trace buffer
1659 * won't contain unreadable events.
1661 * The new synth event should be deleted using synth_event_delete()
1662 * function. The new synthetic event can be generated from modules or
1663 * other kernel code using trace_synth_event() and related functions.
1665 * Return: 0 if successful, error otherwise.
1667 int synth_event_create(const char *name, struct synth_field_desc *fields,
1668 unsigned int n_fields, struct module *mod)
1670 struct dynevent_cmd cmd;
1674 buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
1678 synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
1680 ret = synth_event_gen_cmd_array_start(&cmd, name, mod,
1685 ret = synth_event_gen_cmd_end(&cmd);
1691 EXPORT_SYMBOL_GPL(synth_event_create);
1693 static int destroy_synth_event(struct synth_event *se)
1700 ret = unregister_synth_event(se);
1702 dyn_event_remove(&se->devent);
1703 free_synth_event(se);
1711 * synth_event_delete - Delete a synthetic event
1712 * @event_name: The name of the new sythetic event
1714 * Delete a synthetic event that was created with synth_event_create().
1716 * Return: 0 if successful, error otherwise.
1718 int synth_event_delete(const char *event_name)
1720 struct synth_event *se = NULL;
1721 struct module *mod = NULL;
1724 mutex_lock(&event_mutex);
1725 se = find_synth_event(event_name);
1728 ret = destroy_synth_event(se);
1730 mutex_unlock(&event_mutex);
1733 mutex_lock(&trace_types_lock);
1735 * It is safest to reset the ring buffer if the module
1736 * being unloaded registered any events that were
1737 * used. The only worry is if a new module gets
1738 * loaded, and takes on the same id as the events of
1739 * this module. When printing out the buffer, traced
1740 * events left over from this module may be passed to
1741 * the new module events and unexpected results may
1744 tracing_reset_all_online_cpus();
1745 mutex_unlock(&trace_types_lock);
1750 EXPORT_SYMBOL_GPL(synth_event_delete);
1752 static int create_or_delete_synth_event(int argc, char **argv)
1754 const char *name = argv[0];
1757 /* trace_run_command() ensures argc != 0 */
1758 if (name[0] == '!') {
1759 ret = synth_event_delete(name + 1);
1763 ret = __create_synth_event(argc - 1, name, (const char **)argv + 1);
1764 return ret == -ECANCELED ? -EINVAL : ret;
1767 static int synth_event_run_command(struct dynevent_cmd *cmd)
1769 struct synth_event *se;
1772 ret = trace_run_command(cmd->seq.buffer, create_or_delete_synth_event);
1776 se = find_synth_event(cmd->event_name);
1780 se->mod = cmd->private_data;
1786 * synth_event_cmd_init - Initialize a synthetic event command object
1787 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1788 * @buf: A pointer to the buffer used to build the command
1789 * @maxlen: The length of the buffer passed in @buf
1791 * Initialize a synthetic event command object. Use this before
1792 * calling any of the other dyenvent_cmd functions.
1794 void synth_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
1796 dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_SYNTH,
1797 synth_event_run_command);
1799 EXPORT_SYMBOL_GPL(synth_event_cmd_init);
1802 __synth_event_trace_start(struct trace_event_file *file,
1803 struct synth_event_trace_state *trace_state)
1805 int entry_size, fields_size = 0;
1809 * Normal event tracing doesn't get called at all unless the
1810 * ENABLED bit is set (which attaches the probe thus allowing
1811 * this code to be called, etc). Because this is called
1812 * directly by the user, we don't have that but we still need
1813 * to honor not logging when disabled. For the the iterated
1814 * trace case, we save the enabed state upon start and just
1815 * ignore the following data calls.
1817 if (!(file->flags & EVENT_FILE_FL_ENABLED) ||
1818 trace_trigger_soft_disabled(file)) {
1819 trace_state->disabled = true;
1824 trace_state->event = file->event_call->data;
1826 fields_size = trace_state->event->n_u64 * sizeof(u64);
1829 * Avoid ring buffer recursion detection, as this event
1830 * is being performed within another event.
1832 trace_state->buffer = file->tr->array_buffer.buffer;
1833 ring_buffer_nest_start(trace_state->buffer);
1835 entry_size = sizeof(*trace_state->entry) + fields_size;
1836 trace_state->entry = trace_event_buffer_reserve(&trace_state->fbuffer,
1839 if (!trace_state->entry) {
1840 ring_buffer_nest_end(trace_state->buffer);
1848 __synth_event_trace_end(struct synth_event_trace_state *trace_state)
1850 trace_event_buffer_commit(&trace_state->fbuffer);
1852 ring_buffer_nest_end(trace_state->buffer);
1856 * synth_event_trace - Trace a synthetic event
1857 * @file: The trace_event_file representing the synthetic event
1858 * @n_vals: The number of values in vals
1859 * @args: Variable number of args containing the event values
1861 * Trace a synthetic event using the values passed in the variable
1864 * The argument list should be a list 'n_vals' u64 values. The number
1865 * of vals must match the number of field in the synthetic event, and
1866 * must be in the same order as the synthetic event fields.
1868 * All vals should be cast to u64, and string vals are just pointers
1869 * to strings, cast to u64. Strings will be copied into space
1870 * reserved in the event for the string, using these pointers.
1872 * Return: 0 on success, err otherwise.
1874 int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
1876 struct synth_event_trace_state state;
1877 unsigned int i, n_u64;
1881 ret = __synth_event_trace_start(file, &state);
1884 ret = 0; /* just disabled, not really an error */
1888 va_start(args, n_vals);
1889 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1892 val = va_arg(args, u64);
1894 if (state.event->fields[i]->is_string) {
1895 char *str_val = (char *)(long)val;
1896 char *str_field = (char *)&state.entry->fields[n_u64];
1898 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
1899 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
1901 state.entry->fields[n_u64] = val;
1907 __synth_event_trace_end(&state);
1911 EXPORT_SYMBOL_GPL(synth_event_trace);
1914 * synth_event_trace_array - Trace a synthetic event from an array
1915 * @file: The trace_event_file representing the synthetic event
1916 * @vals: Array of values
1917 * @n_vals: The number of values in vals
1919 * Trace a synthetic event using the values passed in as 'vals'.
1921 * The 'vals' array is just an array of 'n_vals' u64. The number of
1922 * vals must match the number of field in the synthetic event, and
1923 * must be in the same order as the synthetic event fields.
1925 * All vals should be cast to u64, and string vals are just pointers
1926 * to strings, cast to u64. Strings will be copied into space
1927 * reserved in the event for the string, using these pointers.
1929 * Return: 0 on success, err otherwise.
1931 int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
1932 unsigned int n_vals)
1934 struct synth_event_trace_state state;
1935 unsigned int i, n_u64;
1938 ret = __synth_event_trace_start(file, &state);
1941 ret = 0; /* just disabled, not really an error */
1945 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1946 if (state.event->fields[i]->is_string) {
1947 char *str_val = (char *)(long)vals[i];
1948 char *str_field = (char *)&state.entry->fields[n_u64];
1950 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
1951 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
1953 state.entry->fields[n_u64] = vals[i];
1958 __synth_event_trace_end(&state);
1962 EXPORT_SYMBOL_GPL(synth_event_trace_array);
1965 * synth_event_trace_start - Start piecewise synthetic event trace
1966 * @file: The trace_event_file representing the synthetic event
1967 * @trace_state: A pointer to object tracking the piecewise trace state
1969 * Start the trace of a synthetic event field-by-field rather than all
1972 * This function 'opens' an event trace, which means space is reserved
1973 * for the event in the trace buffer, after which the event's
1974 * individual field values can be set through either
1975 * synth_event_add_next_val() or synth_event_add_val().
1977 * A pointer to a trace_state object is passed in, which will keep
1978 * track of the current event trace state until the event trace is
1979 * closed (and the event finally traced) using
1980 * synth_event_trace_end().
1982 * Note that synth_event_trace_end() must be called after all values
1983 * have been added for each event trace, regardless of whether adding
1984 * all field values succeeded or not.
1986 * Note also that for a given event trace, all fields must be added
1987 * using either synth_event_add_next_val() or synth_event_add_val()
1988 * but not both together or interleaved.
1990 * Return: 0 on success, err otherwise.
1992 int synth_event_trace_start(struct trace_event_file *file,
1993 struct synth_event_trace_state *trace_state)
2000 memset(trace_state, '\0', sizeof(*trace_state));
2002 ret = __synth_event_trace_start(file, trace_state);
2004 ret = 0; /* just disabled, not really an error */
2008 EXPORT_SYMBOL_GPL(synth_event_trace_start);
2010 static int __synth_event_add_val(const char *field_name, u64 val,
2011 struct synth_event_trace_state *trace_state)
2013 struct synth_field *field = NULL;
2014 struct synth_trace_event *entry;
2015 struct synth_event *event;
2023 /* can't mix add_next_synth_val() with add_synth_val() */
2025 if (trace_state->add_next) {
2029 trace_state->add_name = true;
2031 if (trace_state->add_name) {
2035 trace_state->add_next = true;
2038 if (trace_state->disabled)
2041 event = trace_state->event;
2042 if (trace_state->add_name) {
2043 for (i = 0; i < event->n_fields; i++) {
2044 field = event->fields[i];
2045 if (strcmp(field->name, field_name) == 0)
2053 if (trace_state->cur_field >= event->n_fields) {
2057 field = event->fields[trace_state->cur_field++];
2060 entry = trace_state->entry;
2061 if (field->is_string) {
2062 char *str_val = (char *)(long)val;
2070 str_field = (char *)&entry->fields[field->offset];
2071 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
2073 entry->fields[field->offset] = val;
2079 * synth_event_add_next_val - Add the next field's value to an open synth trace
2080 * @val: The value to set the next field to
2081 * @trace_state: A pointer to object tracking the piecewise trace state
2083 * Set the value of the next field in an event that's been opened by
2084 * synth_event_trace_start().
2086 * The val param should be the value cast to u64. If the value points
2087 * to a string, the val param should be a char * cast to u64.
2089 * This function assumes all the fields in an event are to be set one
2090 * after another - successive calls to this function are made, one for
2091 * each field, in the order of the fields in the event, until all
2092 * fields have been set. If you'd rather set each field individually
2093 * without regard to ordering, synth_event_add_val() can be used
2096 * Note however that synth_event_add_next_val() and
2097 * synth_event_add_val() can't be intermixed for a given event trace -
2098 * one or the other but not both can be used at the same time.
2100 * Note also that synth_event_trace_end() must be called after all
2101 * values have been added for each event trace, regardless of whether
2102 * adding all field values succeeded or not.
2104 * Return: 0 on success, err otherwise.
2106 int synth_event_add_next_val(u64 val,
2107 struct synth_event_trace_state *trace_state)
2109 return __synth_event_add_val(NULL, val, trace_state);
2111 EXPORT_SYMBOL_GPL(synth_event_add_next_val);
2114 * synth_event_add_val - Add a named field's value to an open synth trace
2115 * @field_name: The name of the synthetic event field value to set
2116 * @val: The value to set the next field to
2117 * @trace_state: A pointer to object tracking the piecewise trace state
2119 * Set the value of the named field in an event that's been opened by
2120 * synth_event_trace_start().
2122 * The val param should be the value cast to u64. If the value points
2123 * to a string, the val param should be a char * cast to u64.
2125 * This function looks up the field name, and if found, sets the field
2126 * to the specified value. This lookup makes this function more
2127 * expensive than synth_event_add_next_val(), so use that or the
2128 * none-piecewise synth_event_trace() instead if efficiency is more
2131 * Note however that synth_event_add_next_val() and
2132 * synth_event_add_val() can't be intermixed for a given event trace -
2133 * one or the other but not both can be used at the same time.
2135 * Note also that synth_event_trace_end() must be called after all
2136 * values have been added for each event trace, regardless of whether
2137 * adding all field values succeeded or not.
2139 * Return: 0 on success, err otherwise.
2141 int synth_event_add_val(const char *field_name, u64 val,
2142 struct synth_event_trace_state *trace_state)
2144 return __synth_event_add_val(field_name, val, trace_state);
2146 EXPORT_SYMBOL_GPL(synth_event_add_val);
2149 * synth_event_trace_end - End piecewise synthetic event trace
2150 * @trace_state: A pointer to object tracking the piecewise trace state
2152 * End the trace of a synthetic event opened by
2153 * synth_event_trace__start().
2155 * This function 'closes' an event trace, which basically means that
2156 * it commits the reserved event and cleans up other loose ends.
2158 * A pointer to a trace_state object is passed in, which will keep
2159 * track of the current event trace state opened with
2160 * synth_event_trace_start().
2162 * Note that this function must be called after all values have been
2163 * added for each event trace, regardless of whether adding all field
2164 * values succeeded or not.
2166 * Return: 0 on success, err otherwise.
2168 int synth_event_trace_end(struct synth_event_trace_state *trace_state)
2173 __synth_event_trace_end(trace_state);
2177 EXPORT_SYMBOL_GPL(synth_event_trace_end);
2179 static int create_synth_event(int argc, const char **argv)
2181 const char *name = argv[0];
2184 if (name[0] != 's' || name[1] != ':')
2188 /* This interface accepts group name prefix */
2189 if (strchr(name, '/')) {
2190 len = str_has_prefix(name, SYNTH_SYSTEM "/");
2195 return __create_synth_event(argc - 1, name, argv + 1);
2198 static int synth_event_release(struct dyn_event *ev)
2200 struct synth_event *event = to_synth_event(ev);
2206 ret = unregister_synth_event(event);
2210 dyn_event_remove(ev);
2211 free_synth_event(event);
2215 static int __synth_event_show(struct seq_file *m, struct synth_event *event)
2217 struct synth_field *field;
2220 seq_printf(m, "%s\t", event->name);
2222 for (i = 0; i < event->n_fields; i++) {
2223 field = event->fields[i];
2225 /* parameter values */
2226 seq_printf(m, "%s %s%s", field->type, field->name,
2227 i == event->n_fields - 1 ? "" : "; ");
2235 static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
2237 struct synth_event *event = to_synth_event(ev);
2239 seq_printf(m, "s:%s/", event->class.system);
2241 return __synth_event_show(m, event);
2244 static int synth_events_seq_show(struct seq_file *m, void *v)
2246 struct dyn_event *ev = v;
2248 if (!is_synth_event(ev))
2251 return __synth_event_show(m, to_synth_event(ev));
2254 static const struct seq_operations synth_events_seq_op = {
2255 .start = dyn_event_seq_start,
2256 .next = dyn_event_seq_next,
2257 .stop = dyn_event_seq_stop,
2258 .show = synth_events_seq_show,
2261 static int synth_events_open(struct inode *inode, struct file *file)
2265 ret = security_locked_down(LOCKDOWN_TRACEFS);
2269 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
2270 ret = dyn_events_release_all(&synth_event_ops);
2275 return seq_open(file, &synth_events_seq_op);
2278 static ssize_t synth_events_write(struct file *file,
2279 const char __user *buffer,
2280 size_t count, loff_t *ppos)
2282 return trace_parse_run_command(file, buffer, count, ppos,
2283 create_or_delete_synth_event);
2286 static const struct file_operations synth_events_fops = {
2287 .open = synth_events_open,
2288 .write = synth_events_write,
2290 .llseek = seq_lseek,
2291 .release = seq_release,
2294 static u64 hist_field_timestamp(struct hist_field *hist_field,
2295 struct tracing_map_elt *elt,
2296 struct ring_buffer_event *rbe,
2299 struct hist_trigger_data *hist_data = hist_field->hist_data;
2300 struct trace_array *tr = hist_data->event_file->tr;
2302 u64 ts = ring_buffer_event_time_stamp(rbe);
2304 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
2310 static u64 hist_field_cpu(struct hist_field *hist_field,
2311 struct tracing_map_elt *elt,
2312 struct ring_buffer_event *rbe,
2315 int cpu = smp_processor_id();
2321 * check_field_for_var_ref - Check if a VAR_REF field references a variable
2322 * @hist_field: The VAR_REF field to check
2323 * @var_data: The hist trigger that owns the variable
2324 * @var_idx: The trigger variable identifier
2326 * Check the given VAR_REF field to see whether or not it references
2327 * the given variable associated with the given trigger.
2329 * Return: The VAR_REF field if it does reference the variable, NULL if not
2331 static struct hist_field *
2332 check_field_for_var_ref(struct hist_field *hist_field,
2333 struct hist_trigger_data *var_data,
2334 unsigned int var_idx)
2336 WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
2338 if (hist_field && hist_field->var.idx == var_idx &&
2339 hist_field->var.hist_data == var_data)
2346 * find_var_ref - Check if a trigger has a reference to a trigger variable
2347 * @hist_data: The hist trigger that might have a reference to the variable
2348 * @var_data: The hist trigger that owns the variable
2349 * @var_idx: The trigger variable identifier
2351 * Check the list of var_refs[] on the first hist trigger to see
2352 * whether any of them are references to the variable on the second
2355 * Return: The VAR_REF field referencing the variable if so, NULL if not
2357 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
2358 struct hist_trigger_data *var_data,
2359 unsigned int var_idx)
2361 struct hist_field *hist_field;
2364 for (i = 0; i < hist_data->n_var_refs; i++) {
2365 hist_field = hist_data->var_refs[i];
2366 if (check_field_for_var_ref(hist_field, var_data, var_idx))
2374 * find_any_var_ref - Check if there is a reference to a given trigger variable
2375 * @hist_data: The hist trigger
2376 * @var_idx: The trigger variable identifier
2378 * Check to see whether the given variable is currently referenced by
2379 * any other trigger.
2381 * The trigger the variable is defined on is explicitly excluded - the
2382 * assumption being that a self-reference doesn't prevent a trigger
2383 * from being removed.
2385 * Return: The VAR_REF field referencing the variable if so, NULL if not
2387 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
2388 unsigned int var_idx)
2390 struct trace_array *tr = hist_data->event_file->tr;
2391 struct hist_field *found = NULL;
2392 struct hist_var_data *var_data;
2394 list_for_each_entry(var_data, &tr->hist_vars, list) {
2395 if (var_data->hist_data == hist_data)
2397 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
2406 * check_var_refs - Check if there is a reference to any of trigger's variables
2407 * @hist_data: The hist trigger
2409 * A trigger can define one or more variables. If any one of them is
2410 * currently referenced by any other trigger, this function will
2413 * Typically used to determine whether or not a trigger can be removed
2414 * - if there are any references to a trigger's variables, it cannot.
2416 * Return: True if there is a reference to any of trigger's variables
2418 static bool check_var_refs(struct hist_trigger_data *hist_data)
2420 struct hist_field *field;
2424 for_each_hist_field(i, hist_data) {
2425 field = hist_data->fields[i];
2426 if (field && field->flags & HIST_FIELD_FL_VAR) {
2427 if (find_any_var_ref(hist_data, field->var.idx)) {
2437 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
2439 struct trace_array *tr = hist_data->event_file->tr;
2440 struct hist_var_data *var_data, *found = NULL;
2442 list_for_each_entry(var_data, &tr->hist_vars, list) {
2443 if (var_data->hist_data == hist_data) {
2452 static bool field_has_hist_vars(struct hist_field *hist_field,
2463 if (hist_field->flags & HIST_FIELD_FL_VAR ||
2464 hist_field->flags & HIST_FIELD_FL_VAR_REF)
2467 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
2468 struct hist_field *operand;
2470 operand = hist_field->operands[i];
2471 if (field_has_hist_vars(operand, level + 1))
2478 static bool has_hist_vars(struct hist_trigger_data *hist_data)
2480 struct hist_field *hist_field;
2483 for_each_hist_field(i, hist_data) {
2484 hist_field = hist_data->fields[i];
2485 if (field_has_hist_vars(hist_field, 0))
2492 static int save_hist_vars(struct hist_trigger_data *hist_data)
2494 struct trace_array *tr = hist_data->event_file->tr;
2495 struct hist_var_data *var_data;
2497 var_data = find_hist_vars(hist_data);
2501 if (tracing_check_open_get_tr(tr))
2504 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
2506 trace_array_put(tr);
2510 var_data->hist_data = hist_data;
2511 list_add(&var_data->list, &tr->hist_vars);
2516 static void remove_hist_vars(struct hist_trigger_data *hist_data)
2518 struct trace_array *tr = hist_data->event_file->tr;
2519 struct hist_var_data *var_data;
2521 var_data = find_hist_vars(hist_data);
2525 if (WARN_ON(check_var_refs(hist_data)))
2528 list_del(&var_data->list);
2532 trace_array_put(tr);
2535 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
2536 const char *var_name)
2538 struct hist_field *hist_field, *found = NULL;
2541 for_each_hist_field(i, hist_data) {
2542 hist_field = hist_data->fields[i];
2543 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
2544 strcmp(hist_field->var.name, var_name) == 0) {
2553 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
2554 struct trace_event_file *file,
2555 const char *var_name)
2557 struct hist_trigger_data *test_data;
2558 struct event_trigger_data *test;
2559 struct hist_field *hist_field;
2561 lockdep_assert_held(&event_mutex);
2563 hist_field = find_var_field(hist_data, var_name);
2567 list_for_each_entry(test, &file->triggers, list) {
2568 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2569 test_data = test->private_data;
2570 hist_field = find_var_field(test_data, var_name);
2579 static struct trace_event_file *find_var_file(struct trace_array *tr,
2584 struct hist_trigger_data *var_hist_data;
2585 struct hist_var_data *var_data;
2586 struct trace_event_file *file, *found = NULL;
2589 return find_event_file(tr, system, event_name);
2591 list_for_each_entry(var_data, &tr->hist_vars, list) {
2592 var_hist_data = var_data->hist_data;
2593 file = var_hist_data->event_file;
2597 if (find_var_field(var_hist_data, var_name)) {
2599 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name));
2610 static struct hist_field *find_file_var(struct trace_event_file *file,
2611 const char *var_name)
2613 struct hist_trigger_data *test_data;
2614 struct event_trigger_data *test;
2615 struct hist_field *hist_field;
2617 lockdep_assert_held(&event_mutex);
2619 list_for_each_entry(test, &file->triggers, list) {
2620 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2621 test_data = test->private_data;
2622 hist_field = find_var_field(test_data, var_name);
2631 static struct hist_field *
2632 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
2634 struct trace_array *tr = hist_data->event_file->tr;
2635 struct hist_field *hist_field, *found = NULL;
2636 struct trace_event_file *file;
2639 for (i = 0; i < hist_data->n_actions; i++) {
2640 struct action_data *data = hist_data->actions[i];
2642 if (data->handler == HANDLER_ONMATCH) {
2643 char *system = data->match_data.event_system;
2644 char *event_name = data->match_data.event;
2646 file = find_var_file(tr, system, event_name, var_name);
2649 hist_field = find_file_var(file, var_name);
2652 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE,
2654 return ERR_PTR(-EINVAL);
2664 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
2669 struct trace_array *tr = hist_data->event_file->tr;
2670 struct hist_field *hist_field = NULL;
2671 struct trace_event_file *file;
2673 if (!system || !event_name) {
2674 hist_field = find_match_var(hist_data, var_name);
2675 if (IS_ERR(hist_field))
2681 file = find_var_file(tr, system, event_name, var_name);
2685 hist_field = find_file_var(file, var_name);
2690 static u64 hist_field_var_ref(struct hist_field *hist_field,
2691 struct tracing_map_elt *elt,
2692 struct ring_buffer_event *rbe,
2695 struct hist_elt_data *elt_data;
2698 if (WARN_ON_ONCE(!elt))
2701 elt_data = elt->private_data;
2702 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
2707 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
2708 u64 *var_ref_vals, bool self)
2710 struct hist_trigger_data *var_data;
2711 struct tracing_map_elt *var_elt;
2712 struct hist_field *hist_field;
2713 unsigned int i, var_idx;
2714 bool resolved = true;
2717 for (i = 0; i < hist_data->n_var_refs; i++) {
2718 hist_field = hist_data->var_refs[i];
2719 var_idx = hist_field->var.idx;
2720 var_data = hist_field->var.hist_data;
2722 if (var_data == NULL) {
2727 if ((self && var_data != hist_data) ||
2728 (!self && var_data == hist_data))
2731 var_elt = tracing_map_lookup(var_data->map, key);
2737 if (!tracing_map_var_set(var_elt, var_idx)) {
2742 if (self || !hist_field->read_once)
2743 var_val = tracing_map_read_var(var_elt, var_idx);
2745 var_val = tracing_map_read_var_once(var_elt, var_idx);
2747 var_ref_vals[i] = var_val;
2753 static const char *hist_field_name(struct hist_field *field,
2756 const char *field_name = "";
2762 field_name = field->field->name;
2763 else if (field->flags & HIST_FIELD_FL_LOG2 ||
2764 field->flags & HIST_FIELD_FL_ALIAS)
2765 field_name = hist_field_name(field->operands[0], ++level);
2766 else if (field->flags & HIST_FIELD_FL_CPU)
2768 else if (field->flags & HIST_FIELD_FL_EXPR ||
2769 field->flags & HIST_FIELD_FL_VAR_REF) {
2770 if (field->system) {
2771 static char full_name[MAX_FILTER_STR_VAL];
2773 strcat(full_name, field->system);
2774 strcat(full_name, ".");
2775 strcat(full_name, field->event_name);
2776 strcat(full_name, ".");
2777 strcat(full_name, field->name);
2778 field_name = full_name;
2780 field_name = field->name;
2781 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
2782 field_name = "common_timestamp";
2784 if (field_name == NULL)
2790 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
2792 hist_field_fn_t fn = NULL;
2794 switch (field_size) {
2796 if (field_is_signed)
2797 fn = hist_field_s64;
2799 fn = hist_field_u64;
2802 if (field_is_signed)
2803 fn = hist_field_s32;
2805 fn = hist_field_u32;
2808 if (field_is_signed)
2809 fn = hist_field_s16;
2811 fn = hist_field_u16;
2814 if (field_is_signed)
2824 static int parse_map_size(char *str)
2826 unsigned long size, map_bits;
2829 ret = kstrtoul(str, 0, &size);
2833 map_bits = ilog2(roundup_pow_of_two(size));
2834 if (map_bits < TRACING_MAP_BITS_MIN ||
2835 map_bits > TRACING_MAP_BITS_MAX)
2843 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
2850 for (i = 0; i < attrs->n_assignments; i++)
2851 kfree(attrs->assignment_str[i]);
2853 for (i = 0; i < attrs->n_actions; i++)
2854 kfree(attrs->action_str[i]);
2857 kfree(attrs->sort_key_str);
2858 kfree(attrs->keys_str);
2859 kfree(attrs->vals_str);
2860 kfree(attrs->clock);
2864 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
2868 if (attrs->n_actions >= HIST_ACTIONS_MAX)
2871 if ((str_has_prefix(str, "onmatch(")) ||
2872 (str_has_prefix(str, "onmax(")) ||
2873 (str_has_prefix(str, "onchange("))) {
2874 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
2875 if (!attrs->action_str[attrs->n_actions]) {
2885 static int parse_assignment(struct trace_array *tr,
2886 char *str, struct hist_trigger_attrs *attrs)
2890 if ((len = str_has_prefix(str, "key=")) ||
2891 (len = str_has_prefix(str, "keys="))) {
2892 attrs->keys_str = kstrdup(str + len, GFP_KERNEL);
2893 if (!attrs->keys_str) {
2897 } else if ((len = str_has_prefix(str, "val=")) ||
2898 (len = str_has_prefix(str, "vals=")) ||
2899 (len = str_has_prefix(str, "values="))) {
2900 attrs->vals_str = kstrdup(str + len, GFP_KERNEL);
2901 if (!attrs->vals_str) {
2905 } else if ((len = str_has_prefix(str, "sort="))) {
2906 attrs->sort_key_str = kstrdup(str + len, GFP_KERNEL);
2907 if (!attrs->sort_key_str) {
2911 } else if (str_has_prefix(str, "name=")) {
2912 attrs->name = kstrdup(str, GFP_KERNEL);
2917 } else if ((len = str_has_prefix(str, "clock="))) {
2920 str = strstrip(str);
2921 attrs->clock = kstrdup(str, GFP_KERNEL);
2922 if (!attrs->clock) {
2926 } else if ((len = str_has_prefix(str, "size="))) {
2927 int map_bits = parse_map_size(str + len);
2933 attrs->map_bits = map_bits;
2937 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
2938 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str));
2943 assignment = kstrdup(str, GFP_KERNEL);
2949 attrs->assignment_str[attrs->n_assignments++] = assignment;
2955 static struct hist_trigger_attrs *
2956 parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str)
2958 struct hist_trigger_attrs *attrs;
2961 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
2963 return ERR_PTR(-ENOMEM);
2965 while (trigger_str) {
2966 char *str = strsep(&trigger_str, ":");
2969 rhs = strchr(str, '=');
2971 if (!strlen(++rhs)) {
2973 hist_err(tr, HIST_ERR_EMPTY_ASSIGNMENT, errpos(str));
2976 ret = parse_assignment(tr, str, attrs);
2979 } else if (strcmp(str, "pause") == 0)
2980 attrs->pause = true;
2981 else if ((strcmp(str, "cont") == 0) ||
2982 (strcmp(str, "continue") == 0))
2984 else if (strcmp(str, "clear") == 0)
2985 attrs->clear = true;
2987 ret = parse_action(str, attrs);
2993 if (!attrs->keys_str) {
2998 if (!attrs->clock) {
2999 attrs->clock = kstrdup("global", GFP_KERNEL);
3000 if (!attrs->clock) {
3008 destroy_hist_trigger_attrs(attrs);
3010 return ERR_PTR(ret);
3013 static inline void save_comm(char *comm, struct task_struct *task)
3016 strcpy(comm, "<idle>");
3020 if (WARN_ON_ONCE(task->pid < 0)) {
3021 strcpy(comm, "<XXX>");
3025 strncpy(comm, task->comm, TASK_COMM_LEN);
3028 static void hist_elt_data_free(struct hist_elt_data *elt_data)
3032 for (i = 0; i < SYNTH_FIELDS_MAX; i++)
3033 kfree(elt_data->field_var_str[i]);
3035 kfree(elt_data->comm);
3039 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
3041 struct hist_elt_data *elt_data = elt->private_data;
3043 hist_elt_data_free(elt_data);
3046 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
3048 struct hist_trigger_data *hist_data = elt->map->private_data;
3049 unsigned int size = TASK_COMM_LEN;
3050 struct hist_elt_data *elt_data;
3051 struct hist_field *key_field;
3052 unsigned int i, n_str;
3054 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
3058 for_each_hist_key_field(i, hist_data) {
3059 key_field = hist_data->fields[i];
3061 if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
3062 elt_data->comm = kzalloc(size, GFP_KERNEL);
3063 if (!elt_data->comm) {
3071 n_str = hist_data->n_field_var_str + hist_data->n_save_var_str;
3073 size = STR_VAR_LEN_MAX;
3075 for (i = 0; i < n_str; i++) {
3076 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
3077 if (!elt_data->field_var_str[i]) {
3078 hist_elt_data_free(elt_data);
3083 elt->private_data = elt_data;
3088 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
3090 struct hist_elt_data *elt_data = elt->private_data;
3093 save_comm(elt_data->comm, current);
3096 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
3097 .elt_alloc = hist_trigger_elt_data_alloc,
3098 .elt_free = hist_trigger_elt_data_free,
3099 .elt_init = hist_trigger_elt_data_init,
3102 static const char *get_hist_field_flags(struct hist_field *hist_field)
3104 const char *flags_str = NULL;
3106 if (hist_field->flags & HIST_FIELD_FL_HEX)
3108 else if (hist_field->flags & HIST_FIELD_FL_SYM)
3110 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
3111 flags_str = "sym-offset";
3112 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
3113 flags_str = "execname";
3114 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
3115 flags_str = "syscall";
3116 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
3118 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
3119 flags_str = "usecs";
3124 static void expr_field_str(struct hist_field *field, char *expr)
3126 if (field->flags & HIST_FIELD_FL_VAR_REF)
3129 strcat(expr, hist_field_name(field, 0));
3131 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
3132 const char *flags_str = get_hist_field_flags(field);
3136 strcat(expr, flags_str);
3141 static char *expr_str(struct hist_field *field, unsigned int level)
3148 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3152 if (!field->operands[0]) {
3153 expr_field_str(field, expr);
3157 if (field->operator == FIELD_OP_UNARY_MINUS) {
3161 subexpr = expr_str(field->operands[0], ++level);
3166 strcat(expr, subexpr);
3174 expr_field_str(field->operands[0], expr);
3176 switch (field->operator) {
3177 case FIELD_OP_MINUS:
3188 expr_field_str(field->operands[1], expr);
3193 static int contains_operator(char *str)
3195 enum field_op_id field_op = FIELD_OP_NONE;
3198 op = strpbrk(str, "+-");
3200 return FIELD_OP_NONE;
3205 field_op = FIELD_OP_UNARY_MINUS;
3207 field_op = FIELD_OP_MINUS;
3210 field_op = FIELD_OP_PLUS;
3219 static void get_hist_field(struct hist_field *hist_field)
3224 static void __destroy_hist_field(struct hist_field *hist_field)
3226 if (--hist_field->ref > 1)
3229 kfree(hist_field->var.name);
3230 kfree(hist_field->name);
3231 kfree(hist_field->type);
3236 static void destroy_hist_field(struct hist_field *hist_field,
3247 if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
3248 return; /* var refs will be destroyed separately */
3250 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
3251 destroy_hist_field(hist_field->operands[i], level + 1);
3253 __destroy_hist_field(hist_field);
3256 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
3257 struct ftrace_event_field *field,
3258 unsigned long flags,
3261 struct hist_field *hist_field;
3263 if (field && is_function_field(field))
3266 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3270 hist_field->ref = 1;
3272 hist_field->hist_data = hist_data;
3274 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
3275 goto out; /* caller will populate */
3277 if (flags & HIST_FIELD_FL_VAR_REF) {
3278 hist_field->fn = hist_field_var_ref;
3282 if (flags & HIST_FIELD_FL_HITCOUNT) {
3283 hist_field->fn = hist_field_counter;
3284 hist_field->size = sizeof(u64);
3285 hist_field->type = kstrdup("u64", GFP_KERNEL);
3286 if (!hist_field->type)
3291 if (flags & HIST_FIELD_FL_STACKTRACE) {
3292 hist_field->fn = hist_field_none;
3296 if (flags & HIST_FIELD_FL_LOG2) {
3297 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2;
3298 hist_field->fn = hist_field_log2;
3299 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
3300 hist_field->size = hist_field->operands[0]->size;
3301 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL);
3302 if (!hist_field->type)
3307 if (flags & HIST_FIELD_FL_TIMESTAMP) {
3308 hist_field->fn = hist_field_timestamp;
3309 hist_field->size = sizeof(u64);
3310 hist_field->type = kstrdup("u64", GFP_KERNEL);
3311 if (!hist_field->type)
3316 if (flags & HIST_FIELD_FL_CPU) {
3317 hist_field->fn = hist_field_cpu;
3318 hist_field->size = sizeof(int);
3319 hist_field->type = kstrdup("unsigned int", GFP_KERNEL);
3320 if (!hist_field->type)
3325 if (WARN_ON_ONCE(!field))
3328 if (is_string_field(field)) {
3329 flags |= HIST_FIELD_FL_STRING;
3331 hist_field->size = MAX_FILTER_STR_VAL;
3332 hist_field->type = kstrdup(field->type, GFP_KERNEL);
3333 if (!hist_field->type)
3336 if (field->filter_type == FILTER_STATIC_STRING)
3337 hist_field->fn = hist_field_string;
3338 else if (field->filter_type == FILTER_DYN_STRING)
3339 hist_field->fn = hist_field_dynstring;
3341 hist_field->fn = hist_field_pstring;
3343 hist_field->size = field->size;
3344 hist_field->is_signed = field->is_signed;
3345 hist_field->type = kstrdup(field->type, GFP_KERNEL);
3346 if (!hist_field->type)
3349 hist_field->fn = select_value_fn(field->size,
3351 if (!hist_field->fn) {
3352 destroy_hist_field(hist_field, 0);
3357 hist_field->field = field;
3358 hist_field->flags = flags;
3361 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
3362 if (!hist_field->var.name)
3368 destroy_hist_field(hist_field, 0);
3372 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
3376 for (i = 0; i < HIST_FIELDS_MAX; i++) {
3377 if (hist_data->fields[i]) {
3378 destroy_hist_field(hist_data->fields[i], 0);
3379 hist_data->fields[i] = NULL;
3383 for (i = 0; i < hist_data->n_var_refs; i++) {
3384 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
3385 __destroy_hist_field(hist_data->var_refs[i]);
3386 hist_data->var_refs[i] = NULL;
3390 static int init_var_ref(struct hist_field *ref_field,
3391 struct hist_field *var_field,
3392 char *system, char *event_name)
3396 ref_field->var.idx = var_field->var.idx;
3397 ref_field->var.hist_data = var_field->hist_data;
3398 ref_field->size = var_field->size;
3399 ref_field->is_signed = var_field->is_signed;
3400 ref_field->flags |= var_field->flags &
3401 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
3404 ref_field->system = kstrdup(system, GFP_KERNEL);
3405 if (!ref_field->system)
3410 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
3411 if (!ref_field->event_name) {
3417 if (var_field->var.name) {
3418 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
3419 if (!ref_field->name) {
3423 } else if (var_field->name) {
3424 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
3425 if (!ref_field->name) {
3431 ref_field->type = kstrdup(var_field->type, GFP_KERNEL);
3432 if (!ref_field->type) {
3439 kfree(ref_field->system);
3440 kfree(ref_field->event_name);
3441 kfree(ref_field->name);
3446 static int find_var_ref_idx(struct hist_trigger_data *hist_data,
3447 struct hist_field *var_field)
3449 struct hist_field *ref_field;
3452 for (i = 0; i < hist_data->n_var_refs; i++) {
3453 ref_field = hist_data->var_refs[i];
3454 if (ref_field->var.idx == var_field->var.idx &&
3455 ref_field->var.hist_data == var_field->hist_data)
3463 * create_var_ref - Create a variable reference and attach it to trigger
3464 * @hist_data: The trigger that will be referencing the variable
3465 * @var_field: The VAR field to create a reference to
3466 * @system: The optional system string
3467 * @event_name: The optional event_name string
3469 * Given a variable hist_field, create a VAR_REF hist_field that
3470 * represents a reference to it.
3472 * This function also adds the reference to the trigger that
3473 * now references the variable.
3475 * Return: The VAR_REF field if successful, NULL if not
3477 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
3478 struct hist_field *var_field,
3479 char *system, char *event_name)
3481 unsigned long flags = HIST_FIELD_FL_VAR_REF;
3482 struct hist_field *ref_field;
3485 /* Check if the variable already exists */
3486 for (i = 0; i < hist_data->n_var_refs; i++) {
3487 ref_field = hist_data->var_refs[i];
3488 if (ref_field->var.idx == var_field->var.idx &&
3489 ref_field->var.hist_data == var_field->hist_data) {
3490 get_hist_field(ref_field);
3495 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
3497 if (init_var_ref(ref_field, var_field, system, event_name)) {
3498 destroy_hist_field(ref_field, 0);
3502 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
3503 ref_field->var_ref_idx = hist_data->n_var_refs++;
3509 static bool is_var_ref(char *var_name)
3511 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
3517 static char *field_name_from_var(struct hist_trigger_data *hist_data,
3523 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
3524 name = hist_data->attrs->var_defs.name[i];
3526 if (strcmp(var_name, name) == 0) {
3527 field = hist_data->attrs->var_defs.expr[i];
3528 if (contains_operator(field) || is_var_ref(field))
3537 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
3538 char *system, char *event_name,
3541 struct trace_event_call *call;
3543 if (system && event_name) {
3544 call = hist_data->event_file->event_call;
3546 if (strcmp(system, call->class->system) != 0)
3549 if (strcmp(event_name, trace_event_name(call)) != 0)
3553 if (!!system != !!event_name)
3556 if (!is_var_ref(var_name))
3561 return field_name_from_var(hist_data, var_name);
3564 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
3565 char *system, char *event_name,
3568 struct hist_field *var_field = NULL, *ref_field = NULL;
3569 struct trace_array *tr = hist_data->event_file->tr;
3571 if (!is_var_ref(var_name))
3576 var_field = find_event_var(hist_data, system, event_name, var_name);
3578 ref_field = create_var_ref(hist_data, var_field,
3579 system, event_name);
3582 hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name));
3587 static struct ftrace_event_field *
3588 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
3589 char *field_str, unsigned long *flags)
3591 struct ftrace_event_field *field = NULL;
3592 char *field_name, *modifier, *str;
3593 struct trace_array *tr = file->tr;
3595 modifier = str = kstrdup(field_str, GFP_KERNEL);
3597 return ERR_PTR(-ENOMEM);
3599 field_name = strsep(&modifier, ".");
3601 if (strcmp(modifier, "hex") == 0)
3602 *flags |= HIST_FIELD_FL_HEX;
3603 else if (strcmp(modifier, "sym") == 0)
3604 *flags |= HIST_FIELD_FL_SYM;
3605 else if (strcmp(modifier, "sym-offset") == 0)
3606 *flags |= HIST_FIELD_FL_SYM_OFFSET;
3607 else if ((strcmp(modifier, "execname") == 0) &&
3608 (strcmp(field_name, "common_pid") == 0))
3609 *flags |= HIST_FIELD_FL_EXECNAME;
3610 else if (strcmp(modifier, "syscall") == 0)
3611 *flags |= HIST_FIELD_FL_SYSCALL;
3612 else if (strcmp(modifier, "log2") == 0)
3613 *flags |= HIST_FIELD_FL_LOG2;
3614 else if (strcmp(modifier, "usecs") == 0)
3615 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
3617 hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier));
3618 field = ERR_PTR(-EINVAL);
3623 if (strcmp(field_name, "common_timestamp") == 0) {
3624 *flags |= HIST_FIELD_FL_TIMESTAMP;
3625 hist_data->enable_timestamps = true;
3626 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
3627 hist_data->attrs->ts_in_usecs = true;
3628 } else if (strcmp(field_name, "cpu") == 0)
3629 *flags |= HIST_FIELD_FL_CPU;
3631 field = trace_find_event_field(file->event_call, field_name);
3632 if (!field || !field->size) {
3633 hist_err(tr, HIST_ERR_FIELD_NOT_FOUND, errpos(field_name));
3634 field = ERR_PTR(-EINVAL);
3644 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
3645 struct hist_field *var_ref,
3648 struct hist_field *alias = NULL;
3649 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
3651 alias = create_hist_field(hist_data, NULL, flags, var_name);
3655 alias->fn = var_ref->fn;
3656 alias->operands[0] = var_ref;
3658 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
3659 destroy_hist_field(alias, 0);
3663 alias->var_ref_idx = var_ref->var_ref_idx;
3668 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
3669 struct trace_event_file *file, char *str,
3670 unsigned long *flags, char *var_name)
3672 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
3673 struct ftrace_event_field *field = NULL;
3674 struct hist_field *hist_field = NULL;
3677 s = strchr(str, '.');
3679 s = strchr(++s, '.');
3681 ref_system = strsep(&str, ".");
3686 ref_event = strsep(&str, ".");
3695 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
3697 hist_field = parse_var_ref(hist_data, ref_system,
3698 ref_event, ref_var);
3701 hist_field = create_alias(hist_data, hist_field, var_name);
3712 field = parse_field(hist_data, file, str, flags);
3713 if (IS_ERR(field)) {
3714 ret = PTR_ERR(field);
3718 hist_field = create_hist_field(hist_data, field, *flags, var_name);
3726 return ERR_PTR(ret);
3729 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
3730 struct trace_event_file *file,
3731 char *str, unsigned long flags,
3732 char *var_name, unsigned int level);
3734 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
3735 struct trace_event_file *file,
3736 char *str, unsigned long flags,
3737 char *var_name, unsigned int level)
3739 struct hist_field *operand1, *expr = NULL;
3740 unsigned long operand_flags;
3744 /* we support only -(xxx) i.e. explicit parens required */
3747 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
3752 str++; /* skip leading '-' */
3754 s = strchr(str, '(');
3762 s = strrchr(str, ')');
3766 ret = -EINVAL; /* no closing ')' */
3770 flags |= HIST_FIELD_FL_EXPR;
3771 expr = create_hist_field(hist_data, NULL, flags, var_name);
3778 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
3779 if (IS_ERR(operand1)) {
3780 ret = PTR_ERR(operand1);
3784 expr->flags |= operand1->flags &
3785 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
3786 expr->fn = hist_field_unary_minus;
3787 expr->operands[0] = operand1;
3788 expr->operator = FIELD_OP_UNARY_MINUS;
3789 expr->name = expr_str(expr, 0);
3790 expr->type = kstrdup(operand1->type, GFP_KERNEL);
3798 destroy_hist_field(expr, 0);
3799 return ERR_PTR(ret);
3802 static int check_expr_operands(struct trace_array *tr,
3803 struct hist_field *operand1,
3804 struct hist_field *operand2)
3806 unsigned long operand1_flags = operand1->flags;
3807 unsigned long operand2_flags = operand2->flags;
3809 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
3810 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
3811 struct hist_field *var;
3813 var = find_var_field(operand1->var.hist_data, operand1->name);
3816 operand1_flags = var->flags;
3819 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
3820 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
3821 struct hist_field *var;
3823 var = find_var_field(operand2->var.hist_data, operand2->name);
3826 operand2_flags = var->flags;
3829 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
3830 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
3831 hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0);
3838 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
3839 struct trace_event_file *file,
3840 char *str, unsigned long flags,
3841 char *var_name, unsigned int level)
3843 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
3844 unsigned long operand_flags;
3845 int field_op, ret = -EINVAL;
3846 char *sep, *operand1_str;
3849 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
3850 return ERR_PTR(-EINVAL);
3853 field_op = contains_operator(str);
3855 if (field_op == FIELD_OP_NONE)
3856 return parse_atom(hist_data, file, str, &flags, var_name);
3858 if (field_op == FIELD_OP_UNARY_MINUS)
3859 return parse_unary(hist_data, file, str, flags, var_name, ++level);
3862 case FIELD_OP_MINUS:
3872 operand1_str = strsep(&str, sep);
3873 if (!operand1_str || !str)
3877 operand1 = parse_atom(hist_data, file, operand1_str,
3878 &operand_flags, NULL);
3879 if (IS_ERR(operand1)) {
3880 ret = PTR_ERR(operand1);
3885 /* rest of string could be another expression e.g. b+c in a+b+c */
3887 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
3888 if (IS_ERR(operand2)) {
3889 ret = PTR_ERR(operand2);
3894 ret = check_expr_operands(file->tr, operand1, operand2);
3898 flags |= HIST_FIELD_FL_EXPR;
3900 flags |= operand1->flags &
3901 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
3903 expr = create_hist_field(hist_data, NULL, flags, var_name);
3909 operand1->read_once = true;
3910 operand2->read_once = true;
3912 expr->operands[0] = operand1;
3913 expr->operands[1] = operand2;
3914 expr->operator = field_op;
3915 expr->name = expr_str(expr, 0);
3916 expr->type = kstrdup(operand1->type, GFP_KERNEL);
3923 case FIELD_OP_MINUS:
3924 expr->fn = hist_field_minus;
3927 expr->fn = hist_field_plus;
3936 destroy_hist_field(operand1, 0);
3937 destroy_hist_field(operand2, 0);
3938 destroy_hist_field(expr, 0);
3940 return ERR_PTR(ret);
3943 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
3944 struct trace_event_file *file)
3946 struct event_trigger_data *test;
3948 lockdep_assert_held(&event_mutex);
3950 list_for_each_entry(test, &file->triggers, list) {
3951 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
3952 if (test->private_data == hist_data)
3953 return test->filter_str;
3960 static struct event_command trigger_hist_cmd;
3961 static int event_hist_trigger_func(struct event_command *cmd_ops,
3962 struct trace_event_file *file,
3963 char *glob, char *cmd, char *param);
3965 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
3966 struct hist_trigger_data *hist_data,
3967 unsigned int n_keys)
3969 struct hist_field *target_hist_field, *hist_field;
3970 unsigned int n, i, j;
3972 if (hist_data->n_fields - hist_data->n_vals != n_keys)
3975 i = hist_data->n_vals;
3976 j = target_hist_data->n_vals;
3978 for (n = 0; n < n_keys; n++) {
3979 hist_field = hist_data->fields[i + n];
3980 target_hist_field = target_hist_data->fields[j + n];
3982 if (strcmp(hist_field->type, target_hist_field->type) != 0)
3984 if (hist_field->size != target_hist_field->size)
3986 if (hist_field->is_signed != target_hist_field->is_signed)
3993 static struct hist_trigger_data *
3994 find_compatible_hist(struct hist_trigger_data *target_hist_data,
3995 struct trace_event_file *file)
3997 struct hist_trigger_data *hist_data;
3998 struct event_trigger_data *test;
3999 unsigned int n_keys;
4001 lockdep_assert_held(&event_mutex);
4003 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
4005 list_for_each_entry(test, &file->triggers, list) {
4006 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
4007 hist_data = test->private_data;
4009 if (compatible_keys(target_hist_data, hist_data, n_keys))
4017 static struct trace_event_file *event_file(struct trace_array *tr,
4018 char *system, char *event_name)
4020 struct trace_event_file *file;
4022 file = __find_event_file(tr, system, event_name);
4024 return ERR_PTR(-EINVAL);
4029 static struct hist_field *
4030 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
4031 char *system, char *event_name, char *field_name)
4033 struct hist_field *event_var;
4034 char *synthetic_name;
4036 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
4037 if (!synthetic_name)
4038 return ERR_PTR(-ENOMEM);
4040 strcpy(synthetic_name, "synthetic_");
4041 strcat(synthetic_name, field_name);
4043 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
4045 kfree(synthetic_name);
4051 * create_field_var_hist - Automatically create a histogram and var for a field
4052 * @target_hist_data: The target hist trigger
4053 * @subsys_name: Optional subsystem name
4054 * @event_name: Optional event name
4055 * @field_name: The name of the field (and the resulting variable)
4057 * Hist trigger actions fetch data from variables, not directly from
4058 * events. However, for convenience, users are allowed to directly
4059 * specify an event field in an action, which will be automatically
4060 * converted into a variable on their behalf.
4062 * If a user specifies a field on an event that isn't the event the
4063 * histogram currently being defined (the target event histogram), the
4064 * only way that can be accomplished is if a new hist trigger is
4065 * created and the field variable defined on that.
4067 * This function creates a new histogram compatible with the target
4068 * event (meaning a histogram with the same key as the target
4069 * histogram), and creates a variable for the specified field, but
4070 * with 'synthetic_' prepended to the variable name in order to avoid
4071 * collision with normal field variables.
4073 * Return: The variable created for the field.
4075 static struct hist_field *
4076 create_field_var_hist(struct hist_trigger_data *target_hist_data,
4077 char *subsys_name, char *event_name, char *field_name)
4079 struct trace_array *tr = target_hist_data->event_file->tr;
4080 struct hist_field *event_var = ERR_PTR(-EINVAL);
4081 struct hist_trigger_data *hist_data;
4082 unsigned int i, n, first = true;
4083 struct field_var_hist *var_hist;
4084 struct trace_event_file *file;
4085 struct hist_field *key_field;
4090 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
4091 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
4092 return ERR_PTR(-EINVAL);
4095 file = event_file(tr, subsys_name, event_name);
4098 hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name));
4099 ret = PTR_ERR(file);
4100 return ERR_PTR(ret);
4104 * Look for a histogram compatible with target. We'll use the
4105 * found histogram specification to create a new matching
4106 * histogram with our variable on it. target_hist_data is not
4107 * yet a registered histogram so we can't use that.
4109 hist_data = find_compatible_hist(target_hist_data, file);
4111 hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name));
4112 return ERR_PTR(-EINVAL);
4115 /* See if a synthetic field variable has already been created */
4116 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
4117 event_name, field_name);
4118 if (!IS_ERR_OR_NULL(event_var))
4121 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
4123 return ERR_PTR(-ENOMEM);
4125 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
4128 return ERR_PTR(-ENOMEM);
4131 /* Use the same keys as the compatible histogram */
4132 strcat(cmd, "keys=");
4134 for_each_hist_key_field(i, hist_data) {
4135 key_field = hist_data->fields[i];
4138 strcat(cmd, key_field->field->name);
4142 /* Create the synthetic field variable specification */
4143 strcat(cmd, ":synthetic_");
4144 strcat(cmd, field_name);
4146 strcat(cmd, field_name);
4148 /* Use the same filter as the compatible histogram */
4149 saved_filter = find_trigger_filter(hist_data, file);
4151 strcat(cmd, " if ");
4152 strcat(cmd, saved_filter);
4155 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
4156 if (!var_hist->cmd) {
4159 return ERR_PTR(-ENOMEM);
4162 /* Save the compatible histogram information */
4163 var_hist->hist_data = hist_data;
4165 /* Create the new histogram with our variable */
4166 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
4170 kfree(var_hist->cmd);
4172 hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name));
4173 return ERR_PTR(ret);
4178 /* If we can't find the variable, something went wrong */
4179 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
4180 event_name, field_name);
4181 if (IS_ERR_OR_NULL(event_var)) {
4182 kfree(var_hist->cmd);
4184 hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
4185 return ERR_PTR(-EINVAL);
4188 n = target_hist_data->n_field_var_hists;
4189 target_hist_data->field_var_hists[n] = var_hist;
4190 target_hist_data->n_field_var_hists++;
4195 static struct hist_field *
4196 find_target_event_var(struct hist_trigger_data *hist_data,
4197 char *subsys_name, char *event_name, char *var_name)
4199 struct trace_event_file *file = hist_data->event_file;
4200 struct hist_field *hist_field = NULL;
4203 struct trace_event_call *call;
4208 call = file->event_call;
4210 if (strcmp(subsys_name, call->class->system) != 0)
4213 if (strcmp(event_name, trace_event_name(call)) != 0)
4217 hist_field = find_var_field(hist_data, var_name);
4222 static inline void __update_field_vars(struct tracing_map_elt *elt,
4223 struct ring_buffer_event *rbe,
4225 struct field_var **field_vars,
4226 unsigned int n_field_vars,
4227 unsigned int field_var_str_start)
4229 struct hist_elt_data *elt_data = elt->private_data;
4230 unsigned int i, j, var_idx;
4233 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
4234 struct field_var *field_var = field_vars[i];
4235 struct hist_field *var = field_var->var;
4236 struct hist_field *val = field_var->val;
4238 var_val = val->fn(val, elt, rbe, rec);
4239 var_idx = var->var.idx;
4241 if (val->flags & HIST_FIELD_FL_STRING) {
4242 char *str = elt_data->field_var_str[j++];
4243 char *val_str = (char *)(uintptr_t)var_val;
4245 strscpy(str, val_str, STR_VAR_LEN_MAX);
4246 var_val = (u64)(uintptr_t)str;
4248 tracing_map_set_var(elt, var_idx, var_val);
4252 static void update_field_vars(struct hist_trigger_data *hist_data,
4253 struct tracing_map_elt *elt,
4254 struct ring_buffer_event *rbe,
4257 __update_field_vars(elt, rbe, rec, hist_data->field_vars,
4258 hist_data->n_field_vars, 0);
4261 static void save_track_data_vars(struct hist_trigger_data *hist_data,
4262 struct tracing_map_elt *elt, void *rec,
4263 struct ring_buffer_event *rbe, void *key,
4264 struct action_data *data, u64 *var_ref_vals)
4266 __update_field_vars(elt, rbe, rec, hist_data->save_vars,
4267 hist_data->n_save_vars, hist_data->n_field_var_str);
4270 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
4271 struct trace_event_file *file,
4272 char *name, int size, const char *type)
4274 struct hist_field *var;
4277 if (find_var(hist_data, file, name) && !hist_data->remove) {
4278 var = ERR_PTR(-EINVAL);
4282 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
4284 var = ERR_PTR(-ENOMEM);
4288 idx = tracing_map_add_var(hist_data->map);
4291 var = ERR_PTR(-EINVAL);
4295 var->flags = HIST_FIELD_FL_VAR;
4297 var->var.hist_data = var->hist_data = hist_data;
4299 var->var.name = kstrdup(name, GFP_KERNEL);
4300 var->type = kstrdup(type, GFP_KERNEL);
4301 if (!var->var.name || !var->type) {
4302 kfree(var->var.name);
4305 var = ERR_PTR(-ENOMEM);
4311 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
4312 struct trace_event_file *file,
4315 struct hist_field *val = NULL, *var = NULL;
4316 unsigned long flags = HIST_FIELD_FL_VAR;
4317 struct trace_array *tr = file->tr;
4318 struct field_var *field_var;
4321 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
4322 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
4327 val = parse_atom(hist_data, file, field_name, &flags, NULL);
4329 hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name));
4334 var = create_var(hist_data, file, field_name, val->size, val->type);
4336 hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name));
4342 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
4350 field_var->var = var;
4351 field_var->val = val;
4355 field_var = ERR_PTR(ret);
4360 * create_target_field_var - Automatically create a variable for a field
4361 * @target_hist_data: The target hist trigger
4362 * @subsys_name: Optional subsystem name
4363 * @event_name: Optional event name
4364 * @var_name: The name of the field (and the resulting variable)
4366 * Hist trigger actions fetch data from variables, not directly from
4367 * events. However, for convenience, users are allowed to directly
4368 * specify an event field in an action, which will be automatically
4369 * converted into a variable on their behalf.
4371 * This function creates a field variable with the name var_name on
4372 * the hist trigger currently being defined on the target event. If
4373 * subsys_name and event_name are specified, this function simply
4374 * verifies that they do in fact match the target event subsystem and
4377 * Return: The variable created for the field.
4379 static struct field_var *
4380 create_target_field_var(struct hist_trigger_data *target_hist_data,
4381 char *subsys_name, char *event_name, char *var_name)
4383 struct trace_event_file *file = target_hist_data->event_file;
4386 struct trace_event_call *call;
4391 call = file->event_call;
4393 if (strcmp(subsys_name, call->class->system) != 0)
4396 if (strcmp(event_name, trace_event_name(call)) != 0)
4400 return create_field_var(target_hist_data, file, var_name);
4403 static bool check_track_val_max(u64 track_val, u64 var_val)
4405 if (var_val <= track_val)
4411 static bool check_track_val_changed(u64 track_val, u64 var_val)
4413 if (var_val == track_val)
4419 static u64 get_track_val(struct hist_trigger_data *hist_data,
4420 struct tracing_map_elt *elt,
4421 struct action_data *data)
4423 unsigned int track_var_idx = data->track_data.track_var->var.idx;
4426 track_val = tracing_map_read_var(elt, track_var_idx);
4431 static void save_track_val(struct hist_trigger_data *hist_data,
4432 struct tracing_map_elt *elt,
4433 struct action_data *data, u64 var_val)
4435 unsigned int track_var_idx = data->track_data.track_var->var.idx;
4437 tracing_map_set_var(elt, track_var_idx, var_val);
4440 static void save_track_data(struct hist_trigger_data *hist_data,
4441 struct tracing_map_elt *elt, void *rec,
4442 struct ring_buffer_event *rbe, void *key,
4443 struct action_data *data, u64 *var_ref_vals)
4445 if (data->track_data.save_data)
4446 data->track_data.save_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
4449 static bool check_track_val(struct tracing_map_elt *elt,
4450 struct action_data *data,
4453 struct hist_trigger_data *hist_data;
4456 hist_data = data->track_data.track_var->hist_data;
4457 track_val = get_track_val(hist_data, elt, data);
4459 return data->track_data.check_val(track_val, var_val);
4462 #ifdef CONFIG_TRACER_SNAPSHOT
4463 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
4465 /* called with tr->max_lock held */
4466 struct track_data *track_data = tr->cond_snapshot->cond_data;
4467 struct hist_elt_data *elt_data, *track_elt_data;
4468 struct snapshot_context *context = cond_data;
4469 struct action_data *action;
4475 action = track_data->action_data;
4477 track_val = get_track_val(track_data->hist_data, context->elt,
4478 track_data->action_data);
4480 if (!action->track_data.check_val(track_data->track_val, track_val))
4483 track_data->track_val = track_val;
4484 memcpy(track_data->key, context->key, track_data->key_len);
4486 elt_data = context->elt->private_data;
4487 track_elt_data = track_data->elt.private_data;
4489 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
4491 track_data->updated = true;
4496 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
4497 struct tracing_map_elt *elt, void *rec,
4498 struct ring_buffer_event *rbe, void *key,
4499 struct action_data *data,
4502 struct trace_event_file *file = hist_data->event_file;
4503 struct snapshot_context context;
4508 tracing_snapshot_cond(file->tr, &context);
4511 static void hist_trigger_print_key(struct seq_file *m,
4512 struct hist_trigger_data *hist_data,
4514 struct tracing_map_elt *elt);
4516 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
4520 if (!hist_data->n_actions)
4523 for (i = 0; i < hist_data->n_actions; i++) {
4524 struct action_data *data = hist_data->actions[i];
4526 if (data->action == ACTION_SNAPSHOT)
4533 static void track_data_snapshot_print(struct seq_file *m,
4534 struct hist_trigger_data *hist_data)
4536 struct trace_event_file *file = hist_data->event_file;
4537 struct track_data *track_data;
4538 struct action_data *action;
4540 track_data = tracing_cond_snapshot_data(file->tr);
4544 if (!track_data->updated)
4547 action = snapshot_action(hist_data);
4551 seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n");
4552 seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
4553 action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
4554 action->track_data.var_str, track_data->track_val);
4556 seq_puts(m, "\ttriggered by event with key: ");
4557 hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
4561 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
4565 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
4566 struct tracing_map_elt *elt, void *rec,
4567 struct ring_buffer_event *rbe, void *key,
4568 struct action_data *data,
4569 u64 *var_ref_vals) {}
4570 static void track_data_snapshot_print(struct seq_file *m,
4571 struct hist_trigger_data *hist_data) {}
4572 #endif /* CONFIG_TRACER_SNAPSHOT */
4574 static void track_data_print(struct seq_file *m,
4575 struct hist_trigger_data *hist_data,
4576 struct tracing_map_elt *elt,
4577 struct action_data *data)
4579 u64 track_val = get_track_val(hist_data, elt, data);
4580 unsigned int i, save_var_idx;
4582 if (data->handler == HANDLER_ONMAX)
4583 seq_printf(m, "\n\tmax: %10llu", track_val);
4584 else if (data->handler == HANDLER_ONCHANGE)
4585 seq_printf(m, "\n\tchanged: %10llu", track_val);
4587 if (data->action == ACTION_SNAPSHOT)
4590 for (i = 0; i < hist_data->n_save_vars; i++) {
4591 struct hist_field *save_val = hist_data->save_vars[i]->val;
4592 struct hist_field *save_var = hist_data->save_vars[i]->var;
4595 save_var_idx = save_var->var.idx;
4597 val = tracing_map_read_var(elt, save_var_idx);
4599 if (save_val->flags & HIST_FIELD_FL_STRING) {
4600 seq_printf(m, " %s: %-32s", save_var->var.name,
4601 (char *)(uintptr_t)(val));
4603 seq_printf(m, " %s: %10llu", save_var->var.name, val);
4607 static void ontrack_action(struct hist_trigger_data *hist_data,
4608 struct tracing_map_elt *elt, void *rec,
4609 struct ring_buffer_event *rbe, void *key,
4610 struct action_data *data, u64 *var_ref_vals)
4612 u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
4614 if (check_track_val(elt, data, var_val)) {
4615 save_track_val(hist_data, elt, data, var_val);
4616 save_track_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
4620 static void action_data_destroy(struct action_data *data)
4624 lockdep_assert_held(&event_mutex);
4626 kfree(data->action_name);
4628 for (i = 0; i < data->n_params; i++)
4629 kfree(data->params[i]);
4631 if (data->synth_event)
4632 data->synth_event->ref--;
4634 kfree(data->synth_event_name);
4639 static void track_data_destroy(struct hist_trigger_data *hist_data,
4640 struct action_data *data)
4642 struct trace_event_file *file = hist_data->event_file;
4644 destroy_hist_field(data->track_data.track_var, 0);
4646 if (data->action == ACTION_SNAPSHOT) {
4647 struct track_data *track_data;
4649 track_data = tracing_cond_snapshot_data(file->tr);
4650 if (track_data && track_data->hist_data == hist_data) {
4651 tracing_snapshot_cond_disable(file->tr);
4652 track_data_free(track_data);
4656 kfree(data->track_data.var_str);
4658 action_data_destroy(data);
4661 static int action_create(struct hist_trigger_data *hist_data,
4662 struct action_data *data);
4664 static int track_data_create(struct hist_trigger_data *hist_data,
4665 struct action_data *data)
4667 struct hist_field *var_field, *ref_field, *track_var = NULL;
4668 struct trace_event_file *file = hist_data->event_file;
4669 struct trace_array *tr = file->tr;
4670 char *track_data_var_str;
4673 track_data_var_str = data->track_data.var_str;
4674 if (track_data_var_str[0] != '$') {
4675 hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str));
4678 track_data_var_str++;
4680 var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
4682 hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str));
4686 ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
4690 data->track_data.var_ref = ref_field;
4692 if (data->handler == HANDLER_ONMAX)
4693 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
4694 if (IS_ERR(track_var)) {
4695 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
4696 ret = PTR_ERR(track_var);
4700 if (data->handler == HANDLER_ONCHANGE)
4701 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
4702 if (IS_ERR(track_var)) {
4703 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
4704 ret = PTR_ERR(track_var);
4707 data->track_data.track_var = track_var;
4709 ret = action_create(hist_data, data);
4714 static int parse_action_params(struct trace_array *tr, char *params,
4715 struct action_data *data)
4717 char *param, *saved_param;
4718 bool first_param = true;
4722 if (data->n_params >= SYNTH_FIELDS_MAX) {
4723 hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0);
4727 param = strsep(¶ms, ",");
4729 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0);
4734 param = strstrip(param);
4735 if (strlen(param) < 2) {
4736 hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param));
4741 saved_param = kstrdup(param, GFP_KERNEL);
4747 if (first_param && data->use_trace_keyword) {
4748 data->synth_event_name = saved_param;
4749 first_param = false;
4752 first_param = false;
4754 data->params[data->n_params++] = saved_param;
4760 static int action_parse(struct trace_array *tr, char *str, struct action_data *data,
4761 enum handler_id handler)
4768 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
4773 action_name = strsep(&str, "(");
4774 if (!action_name || !str) {
4775 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
4780 if (str_has_prefix(action_name, "save")) {
4781 char *params = strsep(&str, ")");
4784 hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0);
4789 ret = parse_action_params(tr, params, data);
4793 if (handler == HANDLER_ONMAX)
4794 data->track_data.check_val = check_track_val_max;
4795 else if (handler == HANDLER_ONCHANGE)
4796 data->track_data.check_val = check_track_val_changed;
4798 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
4803 data->track_data.save_data = save_track_data_vars;
4804 data->fn = ontrack_action;
4805 data->action = ACTION_SAVE;
4806 } else if (str_has_prefix(action_name, "snapshot")) {
4807 char *params = strsep(&str, ")");
4810 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params));
4815 if (handler == HANDLER_ONMAX)
4816 data->track_data.check_val = check_track_val_max;
4817 else if (handler == HANDLER_ONCHANGE)
4818 data->track_data.check_val = check_track_val_changed;
4820 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
4825 data->track_data.save_data = save_track_data_snapshot;
4826 data->fn = ontrack_action;
4827 data->action = ACTION_SNAPSHOT;
4829 char *params = strsep(&str, ")");
4831 if (str_has_prefix(action_name, "trace"))
4832 data->use_trace_keyword = true;
4835 ret = parse_action_params(tr, params, data);
4840 if (handler == HANDLER_ONMAX)
4841 data->track_data.check_val = check_track_val_max;
4842 else if (handler == HANDLER_ONCHANGE)
4843 data->track_data.check_val = check_track_val_changed;
4845 if (handler != HANDLER_ONMATCH) {
4846 data->track_data.save_data = action_trace;
4847 data->fn = ontrack_action;
4849 data->fn = action_trace;
4851 data->action = ACTION_TRACE;
4854 data->action_name = kstrdup(action_name, GFP_KERNEL);
4855 if (!data->action_name) {
4860 data->handler = handler;
4865 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
4866 char *str, enum handler_id handler)
4868 struct action_data *data;
4872 data = kzalloc(sizeof(*data), GFP_KERNEL);
4874 return ERR_PTR(-ENOMEM);
4876 var_str = strsep(&str, ")");
4877 if (!var_str || !str) {
4882 data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
4883 if (!data->track_data.var_str) {
4888 ret = action_parse(hist_data->event_file->tr, str, data, handler);
4894 track_data_destroy(hist_data, data);
4895 data = ERR_PTR(ret);
4899 static void onmatch_destroy(struct action_data *data)
4901 kfree(data->match_data.event);
4902 kfree(data->match_data.event_system);
4904 action_data_destroy(data);
4907 static void destroy_field_var(struct field_var *field_var)
4912 destroy_hist_field(field_var->var, 0);
4913 destroy_hist_field(field_var->val, 0);
4918 static void destroy_field_vars(struct hist_trigger_data *hist_data)
4922 for (i = 0; i < hist_data->n_field_vars; i++)
4923 destroy_field_var(hist_data->field_vars[i]);
4926 static void save_field_var(struct hist_trigger_data *hist_data,
4927 struct field_var *field_var)
4929 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
4931 if (field_var->val->flags & HIST_FIELD_FL_STRING)
4932 hist_data->n_field_var_str++;
4936 static int check_synth_field(struct synth_event *event,
4937 struct hist_field *hist_field,
4938 unsigned int field_pos)
4940 struct synth_field *field;
4942 if (field_pos >= event->n_fields)
4945 field = event->fields[field_pos];
4947 if (strcmp(field->type, hist_field->type) != 0) {
4948 if (field->size != hist_field->size ||
4949 field->is_signed != hist_field->is_signed)
4956 static struct hist_field *
4957 trace_action_find_var(struct hist_trigger_data *hist_data,
4958 struct action_data *data,
4959 char *system, char *event, char *var)
4961 struct trace_array *tr = hist_data->event_file->tr;
4962 struct hist_field *hist_field;
4964 var++; /* skip '$' */
4966 hist_field = find_target_event_var(hist_data, system, event, var);
4968 if (!system && data->handler == HANDLER_ONMATCH) {
4969 system = data->match_data.event_system;
4970 event = data->match_data.event;
4973 hist_field = find_event_var(hist_data, system, event, var);
4977 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var));
4982 static struct hist_field *
4983 trace_action_create_field_var(struct hist_trigger_data *hist_data,
4984 struct action_data *data, char *system,
4985 char *event, char *var)
4987 struct hist_field *hist_field = NULL;
4988 struct field_var *field_var;
4991 * First try to create a field var on the target event (the
4992 * currently being defined). This will create a variable for
4993 * unqualified fields on the target event, or if qualified,
4994 * target fields that have qualified names matching the target.
4996 field_var = create_target_field_var(hist_data, system, event, var);
4998 if (field_var && !IS_ERR(field_var)) {
4999 save_field_var(hist_data, field_var);
5000 hist_field = field_var->var;
5004 * If no explicit system.event is specfied, default to
5005 * looking for fields on the onmatch(system.event.xxx)
5008 if (!system && data->handler == HANDLER_ONMATCH) {
5009 system = data->match_data.event_system;
5010 event = data->match_data.event;
5014 * At this point, we're looking at a field on another
5015 * event. Because we can't modify a hist trigger on
5016 * another event to add a variable for a field, we need
5017 * to create a new trigger on that event and create the
5018 * variable at the same time.
5020 hist_field = create_field_var_hist(hist_data, system, event, var);
5021 if (IS_ERR(hist_field))
5027 destroy_field_var(field_var);
5032 static int trace_action_create(struct hist_trigger_data *hist_data,
5033 struct action_data *data)
5035 struct trace_array *tr = hist_data->event_file->tr;
5036 char *event_name, *param, *system = NULL;
5037 struct hist_field *hist_field, *var_ref;
5039 unsigned int field_pos = 0;
5040 struct synth_event *event;
5041 char *synth_event_name;
5042 int var_ref_idx, ret = 0;
5044 lockdep_assert_held(&event_mutex);
5046 if (data->use_trace_keyword)
5047 synth_event_name = data->synth_event_name;
5049 synth_event_name = data->action_name;
5051 event = find_synth_event(synth_event_name);
5053 hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name));
5059 for (i = 0; i < data->n_params; i++) {
5062 p = param = kstrdup(data->params[i], GFP_KERNEL);
5068 system = strsep(¶m, ".");
5070 param = (char *)system;
5071 system = event_name = NULL;
5073 event_name = strsep(¶m, ".");
5081 if (param[0] == '$')
5082 hist_field = trace_action_find_var(hist_data, data,
5086 hist_field = trace_action_create_field_var(hist_data,
5098 if (check_synth_field(event, hist_field, field_pos) == 0) {
5099 var_ref = create_var_ref(hist_data, hist_field,
5100 system, event_name);
5107 var_ref_idx = find_var_ref_idx(hist_data, var_ref);
5108 if (WARN_ON(var_ref_idx < 0)) {
5113 data->var_ref_idx[i] = var_ref_idx;
5120 hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param));
5126 if (field_pos != event->n_fields) {
5127 hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name));
5132 data->synth_event = event;
5141 static int action_create(struct hist_trigger_data *hist_data,
5142 struct action_data *data)
5144 struct trace_event_file *file = hist_data->event_file;
5145 struct trace_array *tr = file->tr;
5146 struct track_data *track_data;
5147 struct field_var *field_var;
5152 if (data->action == ACTION_TRACE)
5153 return trace_action_create(hist_data, data);
5155 if (data->action == ACTION_SNAPSHOT) {
5156 track_data = track_data_alloc(hist_data->key_size, data, hist_data);
5157 if (IS_ERR(track_data)) {
5158 ret = PTR_ERR(track_data);
5162 ret = tracing_snapshot_cond_enable(file->tr, track_data,
5163 cond_snapshot_update);
5165 track_data_free(track_data);
5170 if (data->action == ACTION_SAVE) {
5171 if (hist_data->n_save_vars) {
5173 hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0);
5177 for (i = 0; i < data->n_params; i++) {
5178 param = kstrdup(data->params[i], GFP_KERNEL);
5184 field_var = create_target_field_var(hist_data, NULL, NULL, param);
5185 if (IS_ERR(field_var)) {
5186 hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL,
5188 ret = PTR_ERR(field_var);
5193 hist_data->save_vars[hist_data->n_save_vars++] = field_var;
5194 if (field_var->val->flags & HIST_FIELD_FL_STRING)
5195 hist_data->n_save_var_str++;
5203 static int onmatch_create(struct hist_trigger_data *hist_data,
5204 struct action_data *data)
5206 return action_create(hist_data, data);
5209 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
5211 char *match_event, *match_event_system;
5212 struct action_data *data;
5215 data = kzalloc(sizeof(*data), GFP_KERNEL);
5217 return ERR_PTR(-ENOMEM);
5219 match_event = strsep(&str, ")");
5220 if (!match_event || !str) {
5221 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event));
5225 match_event_system = strsep(&match_event, ".");
5227 hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system));
5231 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
5232 hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event));
5236 data->match_data.event = kstrdup(match_event, GFP_KERNEL);
5237 if (!data->match_data.event) {
5242 data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
5243 if (!data->match_data.event_system) {
5248 ret = action_parse(tr, str, data, HANDLER_ONMATCH);
5254 onmatch_destroy(data);
5255 data = ERR_PTR(ret);
5259 static int create_hitcount_val(struct hist_trigger_data *hist_data)
5261 hist_data->fields[HITCOUNT_IDX] =
5262 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
5263 if (!hist_data->fields[HITCOUNT_IDX])
5266 hist_data->n_vals++;
5267 hist_data->n_fields++;
5269 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
5275 static int __create_val_field(struct hist_trigger_data *hist_data,
5276 unsigned int val_idx,
5277 struct trace_event_file *file,
5278 char *var_name, char *field_str,
5279 unsigned long flags)
5281 struct hist_field *hist_field;
5284 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
5285 if (IS_ERR(hist_field)) {
5286 ret = PTR_ERR(hist_field);
5290 hist_data->fields[val_idx] = hist_field;
5292 ++hist_data->n_vals;
5293 ++hist_data->n_fields;
5295 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
5301 static int create_val_field(struct hist_trigger_data *hist_data,
5302 unsigned int val_idx,
5303 struct trace_event_file *file,
5306 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
5309 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
5312 static int create_var_field(struct hist_trigger_data *hist_data,
5313 unsigned int val_idx,
5314 struct trace_event_file *file,
5315 char *var_name, char *expr_str)
5317 struct trace_array *tr = hist_data->event_file->tr;
5318 unsigned long flags = 0;
5320 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
5323 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
5324 hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name));
5328 flags |= HIST_FIELD_FL_VAR;
5329 hist_data->n_vars++;
5330 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
5333 return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
5336 static int create_val_fields(struct hist_trigger_data *hist_data,
5337 struct trace_event_file *file)
5339 char *fields_str, *field_str;
5340 unsigned int i, j = 1;
5343 ret = create_hitcount_val(hist_data);
5347 fields_str = hist_data->attrs->vals_str;
5351 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
5352 j < TRACING_MAP_VALS_MAX; i++) {
5353 field_str = strsep(&fields_str, ",");
5357 if (strcmp(field_str, "hitcount") == 0)
5360 ret = create_val_field(hist_data, j++, file, field_str);
5365 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
5371 static int create_key_field(struct hist_trigger_data *hist_data,
5372 unsigned int key_idx,
5373 unsigned int key_offset,
5374 struct trace_event_file *file,
5377 struct trace_array *tr = hist_data->event_file->tr;
5378 struct hist_field *hist_field = NULL;
5379 unsigned long flags = 0;
5380 unsigned int key_size;
5383 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
5386 flags |= HIST_FIELD_FL_KEY;
5388 if (strcmp(field_str, "stacktrace") == 0) {
5389 flags |= HIST_FIELD_FL_STACKTRACE;
5390 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
5391 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
5393 hist_field = parse_expr(hist_data, file, field_str, flags,
5395 if (IS_ERR(hist_field)) {
5396 ret = PTR_ERR(hist_field);
5400 if (field_has_hist_vars(hist_field, 0)) {
5401 hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str));
5402 destroy_hist_field(hist_field, 0);
5407 key_size = hist_field->size;
5410 hist_data->fields[key_idx] = hist_field;
5412 key_size = ALIGN(key_size, sizeof(u64));
5413 hist_data->fields[key_idx]->size = key_size;
5414 hist_data->fields[key_idx]->offset = key_offset;
5416 hist_data->key_size += key_size;
5418 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
5423 hist_data->n_keys++;
5424 hist_data->n_fields++;
5426 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
5434 static int create_key_fields(struct hist_trigger_data *hist_data,
5435 struct trace_event_file *file)
5437 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
5438 char *fields_str, *field_str;
5441 fields_str = hist_data->attrs->keys_str;
5445 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
5446 field_str = strsep(&fields_str, ",");
5449 ret = create_key_field(hist_data, i, key_offset,
5464 static int create_var_fields(struct hist_trigger_data *hist_data,
5465 struct trace_event_file *file)
5467 unsigned int i, j = hist_data->n_vals;
5470 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
5472 for (i = 0; i < n_vars; i++) {
5473 char *var_name = hist_data->attrs->var_defs.name[i];
5474 char *expr = hist_data->attrs->var_defs.expr[i];
5476 ret = create_var_field(hist_data, j++, file, var_name, expr);
5484 static void free_var_defs(struct hist_trigger_data *hist_data)
5488 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
5489 kfree(hist_data->attrs->var_defs.name[i]);
5490 kfree(hist_data->attrs->var_defs.expr[i]);
5493 hist_data->attrs->var_defs.n_vars = 0;
5496 static int parse_var_defs(struct hist_trigger_data *hist_data)
5498 struct trace_array *tr = hist_data->event_file->tr;
5499 char *s, *str, *var_name, *field_str;
5500 unsigned int i, j, n_vars = 0;
5503 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
5504 str = hist_data->attrs->assignment_str[i];
5505 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
5506 field_str = strsep(&str, ",");
5510 var_name = strsep(&field_str, "=");
5511 if (!var_name || !field_str) {
5512 hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT,
5518 if (n_vars == TRACING_MAP_VARS_MAX) {
5519 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name));
5524 s = kstrdup(var_name, GFP_KERNEL);
5529 hist_data->attrs->var_defs.name[n_vars] = s;
5531 s = kstrdup(field_str, GFP_KERNEL);
5533 kfree(hist_data->attrs->var_defs.name[n_vars]);
5537 hist_data->attrs->var_defs.expr[n_vars++] = s;
5539 hist_data->attrs->var_defs.n_vars = n_vars;
5545 free_var_defs(hist_data);
5550 static int create_hist_fields(struct hist_trigger_data *hist_data,
5551 struct trace_event_file *file)
5555 ret = parse_var_defs(hist_data);
5559 ret = create_val_fields(hist_data, file);
5563 ret = create_var_fields(hist_data, file);
5567 ret = create_key_fields(hist_data, file);
5571 free_var_defs(hist_data);
5576 static int is_descending(struct trace_array *tr, const char *str)
5581 if (strcmp(str, "descending") == 0)
5584 if (strcmp(str, "ascending") == 0)
5587 hist_err(tr, HIST_ERR_INVALID_SORT_MODIFIER, errpos((char *)str));
5592 static int create_sort_keys(struct hist_trigger_data *hist_data)
5594 struct trace_array *tr = hist_data->event_file->tr;
5595 char *fields_str = hist_data->attrs->sort_key_str;
5596 struct tracing_map_sort_key *sort_key;
5597 int descending, ret = 0;
5598 unsigned int i, j, k;
5600 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
5605 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
5606 struct hist_field *hist_field;
5607 char *field_str, *field_name;
5608 const char *test_name;
5610 sort_key = &hist_data->sort_keys[i];
5612 field_str = strsep(&fields_str, ",");
5618 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
5622 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
5623 hist_err(tr, HIST_ERR_TOO_MANY_SORT_FIELDS, errpos("sort="));
5628 field_name = strsep(&field_str, ".");
5629 if (!field_name || !*field_name) {
5631 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
5635 if (strcmp(field_name, "hitcount") == 0) {
5636 descending = is_descending(tr, field_str);
5637 if (descending < 0) {
5641 sort_key->descending = descending;
5645 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
5648 hist_field = hist_data->fields[j];
5649 if (hist_field->flags & HIST_FIELD_FL_VAR)
5654 test_name = hist_field_name(hist_field, 0);
5656 if (strcmp(field_name, test_name) == 0) {
5657 sort_key->field_idx = idx;
5658 descending = is_descending(tr, field_str);
5659 if (descending < 0) {
5663 sort_key->descending = descending;
5667 if (j == hist_data->n_fields) {
5669 hist_err(tr, HIST_ERR_INVALID_SORT_FIELD, errpos(field_name));
5674 hist_data->n_sort_keys = i;
5679 static void destroy_actions(struct hist_trigger_data *hist_data)
5683 for (i = 0; i < hist_data->n_actions; i++) {
5684 struct action_data *data = hist_data->actions[i];
5686 if (data->handler == HANDLER_ONMATCH)
5687 onmatch_destroy(data);
5688 else if (data->handler == HANDLER_ONMAX ||
5689 data->handler == HANDLER_ONCHANGE)
5690 track_data_destroy(hist_data, data);
5696 static int parse_actions(struct hist_trigger_data *hist_data)
5698 struct trace_array *tr = hist_data->event_file->tr;
5699 struct action_data *data;
5705 for (i = 0; i < hist_data->attrs->n_actions; i++) {
5706 str = hist_data->attrs->action_str[i];
5708 if ((len = str_has_prefix(str, "onmatch("))) {
5709 char *action_str = str + len;
5711 data = onmatch_parse(tr, action_str);
5713 ret = PTR_ERR(data);
5716 } else if ((len = str_has_prefix(str, "onmax("))) {
5717 char *action_str = str + len;
5719 data = track_data_parse(hist_data, action_str,
5722 ret = PTR_ERR(data);
5725 } else if ((len = str_has_prefix(str, "onchange("))) {
5726 char *action_str = str + len;
5728 data = track_data_parse(hist_data, action_str,
5731 ret = PTR_ERR(data);
5739 hist_data->actions[hist_data->n_actions++] = data;
5745 static int create_actions(struct hist_trigger_data *hist_data)
5747 struct action_data *data;
5751 for (i = 0; i < hist_data->attrs->n_actions; i++) {
5752 data = hist_data->actions[i];
5754 if (data->handler == HANDLER_ONMATCH) {
5755 ret = onmatch_create(hist_data, data);
5758 } else if (data->handler == HANDLER_ONMAX ||
5759 data->handler == HANDLER_ONCHANGE) {
5760 ret = track_data_create(hist_data, data);
5772 static void print_actions(struct seq_file *m,
5773 struct hist_trigger_data *hist_data,
5774 struct tracing_map_elt *elt)
5778 for (i = 0; i < hist_data->n_actions; i++) {
5779 struct action_data *data = hist_data->actions[i];
5781 if (data->action == ACTION_SNAPSHOT)
5784 if (data->handler == HANDLER_ONMAX ||
5785 data->handler == HANDLER_ONCHANGE)
5786 track_data_print(m, hist_data, elt, data);
5790 static void print_action_spec(struct seq_file *m,
5791 struct hist_trigger_data *hist_data,
5792 struct action_data *data)
5796 if (data->action == ACTION_SAVE) {
5797 for (i = 0; i < hist_data->n_save_vars; i++) {
5798 seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
5799 if (i < hist_data->n_save_vars - 1)
5802 } else if (data->action == ACTION_TRACE) {
5803 if (data->use_trace_keyword)
5804 seq_printf(m, "%s", data->synth_event_name);
5805 for (i = 0; i < data->n_params; i++) {
5806 if (i || data->use_trace_keyword)
5808 seq_printf(m, "%s", data->params[i]);
5813 static void print_track_data_spec(struct seq_file *m,
5814 struct hist_trigger_data *hist_data,
5815 struct action_data *data)
5817 if (data->handler == HANDLER_ONMAX)
5818 seq_puts(m, ":onmax(");
5819 else if (data->handler == HANDLER_ONCHANGE)
5820 seq_puts(m, ":onchange(");
5821 seq_printf(m, "%s", data->track_data.var_str);
5822 seq_printf(m, ").%s(", data->action_name);
5824 print_action_spec(m, hist_data, data);
5829 static void print_onmatch_spec(struct seq_file *m,
5830 struct hist_trigger_data *hist_data,
5831 struct action_data *data)
5833 seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
5834 data->match_data.event);
5836 seq_printf(m, "%s(", data->action_name);
5838 print_action_spec(m, hist_data, data);
5843 static bool actions_match(struct hist_trigger_data *hist_data,
5844 struct hist_trigger_data *hist_data_test)
5848 if (hist_data->n_actions != hist_data_test->n_actions)
5851 for (i = 0; i < hist_data->n_actions; i++) {
5852 struct action_data *data = hist_data->actions[i];
5853 struct action_data *data_test = hist_data_test->actions[i];
5854 char *action_name, *action_name_test;
5856 if (data->handler != data_test->handler)
5858 if (data->action != data_test->action)
5861 if (data->n_params != data_test->n_params)
5864 for (j = 0; j < data->n_params; j++) {
5865 if (strcmp(data->params[j], data_test->params[j]) != 0)
5869 if (data->use_trace_keyword)
5870 action_name = data->synth_event_name;
5872 action_name = data->action_name;
5874 if (data_test->use_trace_keyword)
5875 action_name_test = data_test->synth_event_name;
5877 action_name_test = data_test->action_name;
5879 if (strcmp(action_name, action_name_test) != 0)
5882 if (data->handler == HANDLER_ONMATCH) {
5883 if (strcmp(data->match_data.event_system,
5884 data_test->match_data.event_system) != 0)
5886 if (strcmp(data->match_data.event,
5887 data_test->match_data.event) != 0)
5889 } else if (data->handler == HANDLER_ONMAX ||
5890 data->handler == HANDLER_ONCHANGE) {
5891 if (strcmp(data->track_data.var_str,
5892 data_test->track_data.var_str) != 0)
5901 static void print_actions_spec(struct seq_file *m,
5902 struct hist_trigger_data *hist_data)
5906 for (i = 0; i < hist_data->n_actions; i++) {
5907 struct action_data *data = hist_data->actions[i];
5909 if (data->handler == HANDLER_ONMATCH)
5910 print_onmatch_spec(m, hist_data, data);
5911 else if (data->handler == HANDLER_ONMAX ||
5912 data->handler == HANDLER_ONCHANGE)
5913 print_track_data_spec(m, hist_data, data);
5917 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
5921 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5922 kfree(hist_data->field_var_hists[i]->cmd);
5923 kfree(hist_data->field_var_hists[i]);
5927 static void destroy_hist_data(struct hist_trigger_data *hist_data)
5932 destroy_hist_trigger_attrs(hist_data->attrs);
5933 destroy_hist_fields(hist_data);
5934 tracing_map_destroy(hist_data->map);
5936 destroy_actions(hist_data);
5937 destroy_field_vars(hist_data);
5938 destroy_field_var_hists(hist_data);
5943 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
5945 struct tracing_map *map = hist_data->map;
5946 struct ftrace_event_field *field;
5947 struct hist_field *hist_field;
5950 for_each_hist_field(i, hist_data) {
5951 hist_field = hist_data->fields[i];
5952 if (hist_field->flags & HIST_FIELD_FL_KEY) {
5953 tracing_map_cmp_fn_t cmp_fn;
5955 field = hist_field->field;
5957 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
5958 cmp_fn = tracing_map_cmp_none;
5960 cmp_fn = tracing_map_cmp_num(hist_field->size,
5961 hist_field->is_signed);
5962 else if (is_string_field(field))
5963 cmp_fn = tracing_map_cmp_string;
5965 cmp_fn = tracing_map_cmp_num(field->size,
5967 idx = tracing_map_add_key_field(map,
5970 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
5971 idx = tracing_map_add_sum_field(map);
5976 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5977 idx = tracing_map_add_var(map);
5980 hist_field->var.idx = idx;
5981 hist_field->var.hist_data = hist_data;
5988 static struct hist_trigger_data *
5989 create_hist_data(unsigned int map_bits,
5990 struct hist_trigger_attrs *attrs,
5991 struct trace_event_file *file,
5994 const struct tracing_map_ops *map_ops = NULL;
5995 struct hist_trigger_data *hist_data;
5998 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
6000 return ERR_PTR(-ENOMEM);
6002 hist_data->attrs = attrs;
6003 hist_data->remove = remove;
6004 hist_data->event_file = file;
6006 ret = parse_actions(hist_data);
6010 ret = create_hist_fields(hist_data, file);
6014 ret = create_sort_keys(hist_data);
6018 map_ops = &hist_trigger_elt_data_ops;
6020 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
6021 map_ops, hist_data);
6022 if (IS_ERR(hist_data->map)) {
6023 ret = PTR_ERR(hist_data->map);
6024 hist_data->map = NULL;
6028 ret = create_tracing_map_fields(hist_data);
6034 hist_data->attrs = NULL;
6036 destroy_hist_data(hist_data);
6038 hist_data = ERR_PTR(ret);
6043 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
6044 struct tracing_map_elt *elt, void *rec,
6045 struct ring_buffer_event *rbe,
6048 struct hist_elt_data *elt_data;
6049 struct hist_field *hist_field;
6050 unsigned int i, var_idx;
6053 elt_data = elt->private_data;
6054 elt_data->var_ref_vals = var_ref_vals;
6056 for_each_hist_val_field(i, hist_data) {
6057 hist_field = hist_data->fields[i];
6058 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
6059 if (hist_field->flags & HIST_FIELD_FL_VAR) {
6060 var_idx = hist_field->var.idx;
6061 tracing_map_set_var(elt, var_idx, hist_val);
6064 tracing_map_update_sum(elt, i, hist_val);
6067 for_each_hist_key_field(i, hist_data) {
6068 hist_field = hist_data->fields[i];
6069 if (hist_field->flags & HIST_FIELD_FL_VAR) {
6070 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
6071 var_idx = hist_field->var.idx;
6072 tracing_map_set_var(elt, var_idx, hist_val);
6076 update_field_vars(hist_data, elt, rbe, rec);
6079 static inline void add_to_key(char *compound_key, void *key,
6080 struct hist_field *key_field, void *rec)
6082 size_t size = key_field->size;
6084 if (key_field->flags & HIST_FIELD_FL_STRING) {
6085 struct ftrace_event_field *field;
6087 field = key_field->field;
6088 if (field->filter_type == FILTER_DYN_STRING)
6089 size = *(u32 *)(rec + field->offset) >> 16;
6090 else if (field->filter_type == FILTER_PTR_STRING)
6092 else if (field->filter_type == FILTER_STATIC_STRING)
6095 /* ensure NULL-termination */
6096 if (size > key_field->size - 1)
6097 size = key_field->size - 1;
6099 strncpy(compound_key + key_field->offset, (char *)key, size);
6101 memcpy(compound_key + key_field->offset, key, size);
6105 hist_trigger_actions(struct hist_trigger_data *hist_data,
6106 struct tracing_map_elt *elt, void *rec,
6107 struct ring_buffer_event *rbe, void *key,
6110 struct action_data *data;
6113 for (i = 0; i < hist_data->n_actions; i++) {
6114 data = hist_data->actions[i];
6115 data->fn(hist_data, elt, rec, rbe, key, data, var_ref_vals);
6119 static void event_hist_trigger(struct event_trigger_data *data, void *rec,
6120 struct ring_buffer_event *rbe)
6122 struct hist_trigger_data *hist_data = data->private_data;
6123 bool use_compound_key = (hist_data->n_keys > 1);
6124 unsigned long entries[HIST_STACKTRACE_DEPTH];
6125 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
6126 char compound_key[HIST_KEY_SIZE_MAX];
6127 struct tracing_map_elt *elt = NULL;
6128 struct hist_field *key_field;
6133 memset(compound_key, 0, hist_data->key_size);
6135 for_each_hist_key_field(i, hist_data) {
6136 key_field = hist_data->fields[i];
6138 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
6139 memset(entries, 0, HIST_STACKTRACE_SIZE);
6140 stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
6141 HIST_STACKTRACE_SKIP);
6144 field_contents = key_field->fn(key_field, elt, rbe, rec);
6145 if (key_field->flags & HIST_FIELD_FL_STRING) {
6146 key = (void *)(unsigned long)field_contents;
6147 use_compound_key = true;
6149 key = (void *)&field_contents;
6152 if (use_compound_key)
6153 add_to_key(compound_key, key, key_field, rec);
6156 if (use_compound_key)
6159 if (hist_data->n_var_refs &&
6160 !resolve_var_refs(hist_data, key, var_ref_vals, false))
6163 elt = tracing_map_insert(hist_data->map, key);
6167 hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals);
6169 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
6170 hist_trigger_actions(hist_data, elt, rec, rbe, key, var_ref_vals);
6173 static void hist_trigger_stacktrace_print(struct seq_file *m,
6174 unsigned long *stacktrace_entries,
6175 unsigned int max_entries)
6177 char str[KSYM_SYMBOL_LEN];
6178 unsigned int spaces = 8;
6181 for (i = 0; i < max_entries; i++) {
6182 if (!stacktrace_entries[i])
6185 seq_printf(m, "%*c", 1 + spaces, ' ');
6186 sprint_symbol(str, stacktrace_entries[i]);
6187 seq_printf(m, "%s\n", str);
6191 static void hist_trigger_print_key(struct seq_file *m,
6192 struct hist_trigger_data *hist_data,
6194 struct tracing_map_elt *elt)
6196 struct hist_field *key_field;
6197 char str[KSYM_SYMBOL_LEN];
6198 bool multiline = false;
6199 const char *field_name;
6205 for_each_hist_key_field(i, hist_data) {
6206 key_field = hist_data->fields[i];
6208 if (i > hist_data->n_vals)
6211 field_name = hist_field_name(key_field, 0);
6213 if (key_field->flags & HIST_FIELD_FL_HEX) {
6214 uval = *(u64 *)(key + key_field->offset);
6215 seq_printf(m, "%s: %llx", field_name, uval);
6216 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
6217 uval = *(u64 *)(key + key_field->offset);
6218 sprint_symbol_no_offset(str, uval);
6219 seq_printf(m, "%s: [%llx] %-45s", field_name,
6221 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
6222 uval = *(u64 *)(key + key_field->offset);
6223 sprint_symbol(str, uval);
6224 seq_printf(m, "%s: [%llx] %-55s", field_name,
6226 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
6227 struct hist_elt_data *elt_data = elt->private_data;
6230 if (WARN_ON_ONCE(!elt_data))
6233 comm = elt_data->comm;
6235 uval = *(u64 *)(key + key_field->offset);
6236 seq_printf(m, "%s: %-16s[%10llu]", field_name,
6238 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
6239 const char *syscall_name;
6241 uval = *(u64 *)(key + key_field->offset);
6242 syscall_name = get_syscall_name(uval);
6244 syscall_name = "unknown_syscall";
6246 seq_printf(m, "%s: %-30s[%3llu]", field_name,
6247 syscall_name, uval);
6248 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
6249 seq_puts(m, "stacktrace:\n");
6250 hist_trigger_stacktrace_print(m,
6251 key + key_field->offset,
6252 HIST_STACKTRACE_DEPTH);
6254 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
6255 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
6256 *(u64 *)(key + key_field->offset));
6257 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
6258 seq_printf(m, "%s: %-50s", field_name,
6259 (char *)(key + key_field->offset));
6261 uval = *(u64 *)(key + key_field->offset);
6262 seq_printf(m, "%s: %10llu", field_name, uval);
6272 static void hist_trigger_entry_print(struct seq_file *m,
6273 struct hist_trigger_data *hist_data,
6275 struct tracing_map_elt *elt)
6277 const char *field_name;
6280 hist_trigger_print_key(m, hist_data, key, elt);
6282 seq_printf(m, " hitcount: %10llu",
6283 tracing_map_read_sum(elt, HITCOUNT_IDX));
6285 for (i = 1; i < hist_data->n_vals; i++) {
6286 field_name = hist_field_name(hist_data->fields[i], 0);
6288 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
6289 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
6292 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
6293 seq_printf(m, " %s: %10llx", field_name,
6294 tracing_map_read_sum(elt, i));
6296 seq_printf(m, " %s: %10llu", field_name,
6297 tracing_map_read_sum(elt, i));
6301 print_actions(m, hist_data, elt);
6306 static int print_entries(struct seq_file *m,
6307 struct hist_trigger_data *hist_data)
6309 struct tracing_map_sort_entry **sort_entries = NULL;
6310 struct tracing_map *map = hist_data->map;
6313 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
6314 hist_data->n_sort_keys,
6319 for (i = 0; i < n_entries; i++)
6320 hist_trigger_entry_print(m, hist_data,
6321 sort_entries[i]->key,
6322 sort_entries[i]->elt);
6324 tracing_map_destroy_sort_entries(sort_entries, n_entries);
6329 static void hist_trigger_show(struct seq_file *m,
6330 struct event_trigger_data *data, int n)
6332 struct hist_trigger_data *hist_data;
6336 seq_puts(m, "\n\n");
6338 seq_puts(m, "# event histogram\n#\n# trigger info: ");
6339 data->ops->print(m, data->ops, data);
6340 seq_puts(m, "#\n\n");
6342 hist_data = data->private_data;
6343 n_entries = print_entries(m, hist_data);
6347 track_data_snapshot_print(m, hist_data);
6349 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
6350 (u64)atomic64_read(&hist_data->map->hits),
6351 n_entries, (u64)atomic64_read(&hist_data->map->drops));
6354 static int hist_show(struct seq_file *m, void *v)
6356 struct event_trigger_data *data;
6357 struct trace_event_file *event_file;
6360 mutex_lock(&event_mutex);
6362 event_file = event_file_data(m->private);
6363 if (unlikely(!event_file)) {
6368 list_for_each_entry(data, &event_file->triggers, list) {
6369 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
6370 hist_trigger_show(m, data, n++);
6374 mutex_unlock(&event_mutex);
6379 static int event_hist_open(struct inode *inode, struct file *file)
6383 ret = security_locked_down(LOCKDOWN_TRACEFS);
6387 return single_open(file, hist_show, file);
6390 const struct file_operations event_hist_fops = {
6391 .open = event_hist_open,
6393 .llseek = seq_lseek,
6394 .release = single_release,
6397 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
6399 const char *field_name = hist_field_name(hist_field, 0);
6401 if (hist_field->var.name)
6402 seq_printf(m, "%s=", hist_field->var.name);
6404 if (hist_field->flags & HIST_FIELD_FL_CPU)
6406 else if (field_name) {
6407 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
6408 hist_field->flags & HIST_FIELD_FL_ALIAS)
6410 seq_printf(m, "%s", field_name);
6411 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
6412 seq_puts(m, "common_timestamp");
6414 if (hist_field->flags) {
6415 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
6416 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
6417 const char *flags = get_hist_field_flags(hist_field);
6420 seq_printf(m, ".%s", flags);
6425 static int event_hist_trigger_print(struct seq_file *m,
6426 struct event_trigger_ops *ops,
6427 struct event_trigger_data *data)
6429 struct hist_trigger_data *hist_data = data->private_data;
6430 struct hist_field *field;
6431 bool have_var = false;
6434 seq_puts(m, "hist:");
6437 seq_printf(m, "%s:", data->name);
6439 seq_puts(m, "keys=");
6441 for_each_hist_key_field(i, hist_data) {
6442 field = hist_data->fields[i];
6444 if (i > hist_data->n_vals)
6447 if (field->flags & HIST_FIELD_FL_STACKTRACE)
6448 seq_puts(m, "stacktrace");
6450 hist_field_print(m, field);
6453 seq_puts(m, ":vals=");
6455 for_each_hist_val_field(i, hist_data) {
6456 field = hist_data->fields[i];
6457 if (field->flags & HIST_FIELD_FL_VAR) {
6462 if (i == HITCOUNT_IDX)
6463 seq_puts(m, "hitcount");
6466 hist_field_print(m, field);
6475 for_each_hist_val_field(i, hist_data) {
6476 field = hist_data->fields[i];
6478 if (field->flags & HIST_FIELD_FL_VAR) {
6481 hist_field_print(m, field);
6486 seq_puts(m, ":sort=");
6488 for (i = 0; i < hist_data->n_sort_keys; i++) {
6489 struct tracing_map_sort_key *sort_key;
6490 unsigned int idx, first_key_idx;
6493 first_key_idx = hist_data->n_vals - hist_data->n_vars;
6495 sort_key = &hist_data->sort_keys[i];
6496 idx = sort_key->field_idx;
6498 if (WARN_ON(idx >= HIST_FIELDS_MAX))
6504 if (idx == HITCOUNT_IDX)
6505 seq_puts(m, "hitcount");
6507 if (idx >= first_key_idx)
6508 idx += hist_data->n_vars;
6509 hist_field_print(m, hist_data->fields[idx]);
6512 if (sort_key->descending)
6513 seq_puts(m, ".descending");
6515 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
6516 if (hist_data->enable_timestamps)
6517 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
6519 print_actions_spec(m, hist_data);
6521 if (data->filter_str)
6522 seq_printf(m, " if %s", data->filter_str);
6525 seq_puts(m, " [paused]");
6527 seq_puts(m, " [active]");
6534 static int event_hist_trigger_init(struct event_trigger_ops *ops,
6535 struct event_trigger_data *data)
6537 struct hist_trigger_data *hist_data = data->private_data;
6539 if (!data->ref && hist_data->attrs->name)
6540 save_named_trigger(hist_data->attrs->name, data);
6547 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
6549 struct trace_event_file *file;
6554 for (i = 0; i < hist_data->n_field_var_hists; i++) {
6555 file = hist_data->field_var_hists[i]->hist_data->event_file;
6556 cmd = hist_data->field_var_hists[i]->cmd;
6557 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
6558 "!hist", "hist", cmd);
6562 static void event_hist_trigger_free(struct event_trigger_ops *ops,
6563 struct event_trigger_data *data)
6565 struct hist_trigger_data *hist_data = data->private_data;
6567 if (WARN_ON_ONCE(data->ref <= 0))
6573 del_named_trigger(data);
6575 trigger_data_free(data);
6577 remove_hist_vars(hist_data);
6579 unregister_field_var_hists(hist_data);
6581 destroy_hist_data(hist_data);
6585 static struct event_trigger_ops event_hist_trigger_ops = {
6586 .func = event_hist_trigger,
6587 .print = event_hist_trigger_print,
6588 .init = event_hist_trigger_init,
6589 .free = event_hist_trigger_free,
6592 static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
6593 struct event_trigger_data *data)
6597 save_named_trigger(data->named_data->name, data);
6599 event_hist_trigger_init(ops, data->named_data);
6604 static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
6605 struct event_trigger_data *data)
6607 if (WARN_ON_ONCE(data->ref <= 0))
6610 event_hist_trigger_free(ops, data->named_data);
6614 del_named_trigger(data);
6615 trigger_data_free(data);
6619 static struct event_trigger_ops event_hist_trigger_named_ops = {
6620 .func = event_hist_trigger,
6621 .print = event_hist_trigger_print,
6622 .init = event_hist_trigger_named_init,
6623 .free = event_hist_trigger_named_free,
6626 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
6629 return &event_hist_trigger_ops;
6632 static void hist_clear(struct event_trigger_data *data)
6634 struct hist_trigger_data *hist_data = data->private_data;
6637 pause_named_trigger(data);
6639 tracepoint_synchronize_unregister();
6641 tracing_map_clear(hist_data->map);
6644 unpause_named_trigger(data);
6647 static bool compatible_field(struct ftrace_event_field *field,
6648 struct ftrace_event_field *test_field)
6650 if (field == test_field)
6652 if (field == NULL || test_field == NULL)
6654 if (strcmp(field->name, test_field->name) != 0)
6656 if (strcmp(field->type, test_field->type) != 0)
6658 if (field->size != test_field->size)
6660 if (field->is_signed != test_field->is_signed)
6666 static bool hist_trigger_match(struct event_trigger_data *data,
6667 struct event_trigger_data *data_test,
6668 struct event_trigger_data *named_data,
6671 struct tracing_map_sort_key *sort_key, *sort_key_test;
6672 struct hist_trigger_data *hist_data, *hist_data_test;
6673 struct hist_field *key_field, *key_field_test;
6676 if (named_data && (named_data != data_test) &&
6677 (named_data != data_test->named_data))
6680 if (!named_data && is_named_trigger(data_test))
6683 hist_data = data->private_data;
6684 hist_data_test = data_test->private_data;
6686 if (hist_data->n_vals != hist_data_test->n_vals ||
6687 hist_data->n_fields != hist_data_test->n_fields ||
6688 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
6691 if (!ignore_filter) {
6692 if ((data->filter_str && !data_test->filter_str) ||
6693 (!data->filter_str && data_test->filter_str))
6697 for_each_hist_field(i, hist_data) {
6698 key_field = hist_data->fields[i];
6699 key_field_test = hist_data_test->fields[i];
6701 if (key_field->flags != key_field_test->flags)
6703 if (!compatible_field(key_field->field, key_field_test->field))
6705 if (key_field->offset != key_field_test->offset)
6707 if (key_field->size != key_field_test->size)
6709 if (key_field->is_signed != key_field_test->is_signed)
6711 if (!!key_field->var.name != !!key_field_test->var.name)
6713 if (key_field->var.name &&
6714 strcmp(key_field->var.name, key_field_test->var.name) != 0)
6718 for (i = 0; i < hist_data->n_sort_keys; i++) {
6719 sort_key = &hist_data->sort_keys[i];
6720 sort_key_test = &hist_data_test->sort_keys[i];
6722 if (sort_key->field_idx != sort_key_test->field_idx ||
6723 sort_key->descending != sort_key_test->descending)
6727 if (!ignore_filter && data->filter_str &&
6728 (strcmp(data->filter_str, data_test->filter_str) != 0))
6731 if (!actions_match(hist_data, hist_data_test))
6737 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
6738 struct event_trigger_data *data,
6739 struct trace_event_file *file)
6741 struct hist_trigger_data *hist_data = data->private_data;
6742 struct event_trigger_data *test, *named_data = NULL;
6743 struct trace_array *tr = file->tr;
6746 if (hist_data->attrs->name) {
6747 named_data = find_named_trigger(hist_data->attrs->name);
6749 if (!hist_trigger_match(data, named_data, named_data,
6751 hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name));
6758 if (hist_data->attrs->name && !named_data)
6761 lockdep_assert_held(&event_mutex);
6763 list_for_each_entry(test, &file->triggers, list) {
6764 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6765 if (!hist_trigger_match(data, test, named_data, false))
6767 if (hist_data->attrs->pause)
6768 test->paused = true;
6769 else if (hist_data->attrs->cont)
6770 test->paused = false;
6771 else if (hist_data->attrs->clear)
6774 hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
6781 if (hist_data->attrs->cont || hist_data->attrs->clear) {
6782 hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0);
6787 if (hist_data->attrs->pause)
6788 data->paused = true;
6791 data->private_data = named_data->private_data;
6792 set_named_trigger_data(data, named_data);
6793 data->ops = &event_hist_trigger_named_ops;
6796 if (data->ops->init) {
6797 ret = data->ops->init(data->ops, data);
6802 if (hist_data->enable_timestamps) {
6803 char *clock = hist_data->attrs->clock;
6805 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
6807 hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock));
6811 tracing_set_time_stamp_abs(file->tr, true);
6815 destroy_hist_data(hist_data);
6822 static int hist_trigger_enable(struct event_trigger_data *data,
6823 struct trace_event_file *file)
6827 list_add_tail_rcu(&data->list, &file->triggers);
6829 update_cond_flag(file);
6831 if (trace_event_trigger_enable_disable(file, 1) < 0) {
6832 list_del_rcu(&data->list);
6833 update_cond_flag(file);
6840 static bool have_hist_trigger_match(struct event_trigger_data *data,
6841 struct trace_event_file *file)
6843 struct hist_trigger_data *hist_data = data->private_data;
6844 struct event_trigger_data *test, *named_data = NULL;
6847 lockdep_assert_held(&event_mutex);
6849 if (hist_data->attrs->name)
6850 named_data = find_named_trigger(hist_data->attrs->name);
6852 list_for_each_entry(test, &file->triggers, list) {
6853 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6854 if (hist_trigger_match(data, test, named_data, false)) {
6864 static bool hist_trigger_check_refs(struct event_trigger_data *data,
6865 struct trace_event_file *file)
6867 struct hist_trigger_data *hist_data = data->private_data;
6868 struct event_trigger_data *test, *named_data = NULL;
6870 lockdep_assert_held(&event_mutex);
6872 if (hist_data->attrs->name)
6873 named_data = find_named_trigger(hist_data->attrs->name);
6875 list_for_each_entry(test, &file->triggers, list) {
6876 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6877 if (!hist_trigger_match(data, test, named_data, false))
6879 hist_data = test->private_data;
6880 if (check_var_refs(hist_data))
6889 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
6890 struct event_trigger_data *data,
6891 struct trace_event_file *file)
6893 struct hist_trigger_data *hist_data = data->private_data;
6894 struct event_trigger_data *test, *named_data = NULL;
6895 bool unregistered = false;
6897 lockdep_assert_held(&event_mutex);
6899 if (hist_data->attrs->name)
6900 named_data = find_named_trigger(hist_data->attrs->name);
6902 list_for_each_entry(test, &file->triggers, list) {
6903 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6904 if (!hist_trigger_match(data, test, named_data, false))
6906 unregistered = true;
6907 list_del_rcu(&test->list);
6908 trace_event_trigger_enable_disable(file, 0);
6909 update_cond_flag(file);
6914 if (unregistered && test->ops->free)
6915 test->ops->free(test->ops, test);
6917 if (hist_data->enable_timestamps) {
6918 if (!hist_data->remove || unregistered)
6919 tracing_set_time_stamp_abs(file->tr, false);
6923 static bool hist_file_check_refs(struct trace_event_file *file)
6925 struct hist_trigger_data *hist_data;
6926 struct event_trigger_data *test;
6928 lockdep_assert_held(&event_mutex);
6930 list_for_each_entry(test, &file->triggers, list) {
6931 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6932 hist_data = test->private_data;
6933 if (check_var_refs(hist_data))
6941 static void hist_unreg_all(struct trace_event_file *file)
6943 struct event_trigger_data *test, *n;
6944 struct hist_trigger_data *hist_data;
6945 struct synth_event *se;
6946 const char *se_name;
6948 lockdep_assert_held(&event_mutex);
6950 if (hist_file_check_refs(file))
6953 list_for_each_entry_safe(test, n, &file->triggers, list) {
6954 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6955 hist_data = test->private_data;
6956 list_del_rcu(&test->list);
6957 trace_event_trigger_enable_disable(file, 0);
6959 se_name = trace_event_name(file->event_call);
6960 se = find_synth_event(se_name);
6964 update_cond_flag(file);
6965 if (hist_data->enable_timestamps)
6966 tracing_set_time_stamp_abs(file->tr, false);
6967 if (test->ops->free)
6968 test->ops->free(test->ops, test);
6973 static int event_hist_trigger_func(struct event_command *cmd_ops,
6974 struct trace_event_file *file,
6975 char *glob, char *cmd, char *param)
6977 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
6978 struct event_trigger_data *trigger_data;
6979 struct hist_trigger_attrs *attrs;
6980 struct event_trigger_ops *trigger_ops;
6981 struct hist_trigger_data *hist_data;
6982 struct synth_event *se;
6983 const char *se_name;
6984 bool remove = false;
6988 lockdep_assert_held(&event_mutex);
6990 if (glob && strlen(glob)) {
6992 last_cmd_set(file, param);
7002 * separate the trigger from the filter (k:v [if filter])
7003 * allowing for whitespace in the trigger
7005 p = trigger = param;
7007 p = strstr(p, "if");
7012 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
7016 if (p >= param + strlen(param) - (sizeof("if") - 1) - 1)
7018 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
7029 param = strstrip(p);
7030 trigger = strstrip(trigger);
7033 attrs = parse_hist_trigger_attrs(file->tr, trigger);
7035 return PTR_ERR(attrs);
7037 if (attrs->map_bits)
7038 hist_trigger_bits = attrs->map_bits;
7040 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
7041 if (IS_ERR(hist_data)) {
7042 destroy_hist_trigger_attrs(attrs);
7043 return PTR_ERR(hist_data);
7046 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
7048 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
7049 if (!trigger_data) {
7054 trigger_data->count = -1;
7055 trigger_data->ops = trigger_ops;
7056 trigger_data->cmd_ops = cmd_ops;
7058 INIT_LIST_HEAD(&trigger_data->list);
7059 RCU_INIT_POINTER(trigger_data->filter, NULL);
7061 trigger_data->private_data = hist_data;
7063 /* if param is non-empty, it's supposed to be a filter */
7064 if (param && cmd_ops->set_filter) {
7065 ret = cmd_ops->set_filter(param, trigger_data, file);
7071 if (!have_hist_trigger_match(trigger_data, file))
7074 if (hist_trigger_check_refs(trigger_data, file)) {
7079 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
7080 se_name = trace_event_name(file->event_call);
7081 se = find_synth_event(se_name);
7088 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
7090 * The above returns on success the # of triggers registered,
7091 * but if it didn't register any it returns zero. Consider no
7092 * triggers registered a failure too.
7095 if (!(attrs->pause || attrs->cont || attrs->clear))
7101 if (get_named_trigger_data(trigger_data))
7104 if (has_hist_vars(hist_data))
7105 save_hist_vars(hist_data);
7107 ret = create_actions(hist_data);
7111 ret = tracing_map_init(hist_data->map);
7115 ret = hist_trigger_enable(trigger_data, file);
7119 se_name = trace_event_name(file->event_call);
7120 se = find_synth_event(se_name);
7123 /* Just return zero, not the number of registered triggers */
7131 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
7133 if (cmd_ops->set_filter)
7134 cmd_ops->set_filter(NULL, trigger_data, NULL);
7136 remove_hist_vars(hist_data);
7138 kfree(trigger_data);
7140 destroy_hist_data(hist_data);
7144 static struct event_command trigger_hist_cmd = {
7146 .trigger_type = ETT_EVENT_HIST,
7147 .flags = EVENT_CMD_FL_NEEDS_REC,
7148 .func = event_hist_trigger_func,
7149 .reg = hist_register_trigger,
7150 .unreg = hist_unregister_trigger,
7151 .unreg_all = hist_unreg_all,
7152 .get_trigger_ops = event_hist_get_trigger_ops,
7153 .set_filter = set_trigger_filter,
7156 __init int register_trigger_hist_cmd(void)
7160 ret = register_event_command(&trigger_hist_cmd);
7167 hist_enable_trigger(struct event_trigger_data *data, void *rec,
7168 struct ring_buffer_event *event)
7170 struct enable_trigger_data *enable_data = data->private_data;
7171 struct event_trigger_data *test;
7173 list_for_each_entry_rcu(test, &enable_data->file->triggers, list,
7174 lockdep_is_held(&event_mutex)) {
7175 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
7176 if (enable_data->enable)
7177 test->paused = false;
7179 test->paused = true;
7185 hist_enable_count_trigger(struct event_trigger_data *data, void *rec,
7186 struct ring_buffer_event *event)
7191 if (data->count != -1)
7194 hist_enable_trigger(data, rec, event);
7197 static struct event_trigger_ops hist_enable_trigger_ops = {
7198 .func = hist_enable_trigger,
7199 .print = event_enable_trigger_print,
7200 .init = event_trigger_init,
7201 .free = event_enable_trigger_free,
7204 static struct event_trigger_ops hist_enable_count_trigger_ops = {
7205 .func = hist_enable_count_trigger,
7206 .print = event_enable_trigger_print,
7207 .init = event_trigger_init,
7208 .free = event_enable_trigger_free,
7211 static struct event_trigger_ops hist_disable_trigger_ops = {
7212 .func = hist_enable_trigger,
7213 .print = event_enable_trigger_print,
7214 .init = event_trigger_init,
7215 .free = event_enable_trigger_free,
7218 static struct event_trigger_ops hist_disable_count_trigger_ops = {
7219 .func = hist_enable_count_trigger,
7220 .print = event_enable_trigger_print,
7221 .init = event_trigger_init,
7222 .free = event_enable_trigger_free,
7225 static struct event_trigger_ops *
7226 hist_enable_get_trigger_ops(char *cmd, char *param)
7228 struct event_trigger_ops *ops;
7231 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
7234 ops = param ? &hist_enable_count_trigger_ops :
7235 &hist_enable_trigger_ops;
7237 ops = param ? &hist_disable_count_trigger_ops :
7238 &hist_disable_trigger_ops;
7243 static void hist_enable_unreg_all(struct trace_event_file *file)
7245 struct event_trigger_data *test, *n;
7247 list_for_each_entry_safe(test, n, &file->triggers, list) {
7248 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
7249 list_del_rcu(&test->list);
7250 update_cond_flag(file);
7251 trace_event_trigger_enable_disable(file, 0);
7252 if (test->ops->free)
7253 test->ops->free(test->ops, test);
7258 static struct event_command trigger_hist_enable_cmd = {
7259 .name = ENABLE_HIST_STR,
7260 .trigger_type = ETT_HIST_ENABLE,
7261 .func = event_enable_trigger_func,
7262 .reg = event_enable_register_trigger,
7263 .unreg = event_enable_unregister_trigger,
7264 .unreg_all = hist_enable_unreg_all,
7265 .get_trigger_ops = hist_enable_get_trigger_ops,
7266 .set_filter = set_trigger_filter,
7269 static struct event_command trigger_hist_disable_cmd = {
7270 .name = DISABLE_HIST_STR,
7271 .trigger_type = ETT_HIST_ENABLE,
7272 .func = event_enable_trigger_func,
7273 .reg = event_enable_register_trigger,
7274 .unreg = event_enable_unregister_trigger,
7275 .unreg_all = hist_enable_unreg_all,
7276 .get_trigger_ops = hist_enable_get_trigger_ops,
7277 .set_filter = set_trigger_filter,
7280 static __init void unregister_trigger_hist_enable_disable_cmds(void)
7282 unregister_event_command(&trigger_hist_enable_cmd);
7283 unregister_event_command(&trigger_hist_disable_cmd);
7286 __init int register_trigger_hist_enable_disable_cmds(void)
7290 ret = register_event_command(&trigger_hist_enable_cmd);
7291 if (WARN_ON(ret < 0))
7293 ret = register_event_command(&trigger_hist_disable_cmd);
7294 if (WARN_ON(ret < 0))
7295 unregister_trigger_hist_enable_disable_cmds();
7300 static __init int trace_events_hist_init(void)
7302 struct dentry *entry = NULL;
7303 struct dentry *d_tracer;
7306 err = dyn_event_register(&synth_event_ops);
7308 pr_warn("Could not register synth_event_ops\n");
7312 d_tracer = tracing_init_dentry();
7313 if (IS_ERR(d_tracer)) {
7314 err = PTR_ERR(d_tracer);
7318 entry = tracefs_create_file("synthetic_events", 0644, d_tracer,
7319 NULL, &synth_events_fops);
7327 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
7332 fs_initcall(trace_events_hist_init);