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;
126 unsigned int is_signed;
128 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
129 struct hist_trigger_data *hist_data;
132 * Variable fields contain variable-specific info in var.
135 enum field_op_id operator;
140 * The name field is used for EXPR and VAR_REF fields. VAR
141 * fields contain the variable name in var.name.
146 * When a histogram trigger is hit, if it has any references
147 * to variables, the values of those variables are collected
148 * into a var_ref_vals array by resolve_var_refs(). The
149 * current value of each variable is read from the tracing_map
150 * using the hist field's hist_var.idx and entered into the
151 * var_ref_idx entry i.e. var_ref_vals[var_ref_idx].
153 unsigned int var_ref_idx;
157 static u64 hist_field_none(struct hist_field *field,
158 struct tracing_map_elt *elt,
159 struct ring_buffer_event *rbe,
165 static u64 hist_field_counter(struct hist_field *field,
166 struct tracing_map_elt *elt,
167 struct ring_buffer_event *rbe,
173 static u64 hist_field_string(struct hist_field *hist_field,
174 struct tracing_map_elt *elt,
175 struct ring_buffer_event *rbe,
178 char *addr = (char *)(event + hist_field->field->offset);
180 return (u64)(unsigned long)addr;
183 static u64 hist_field_dynstring(struct hist_field *hist_field,
184 struct tracing_map_elt *elt,
185 struct ring_buffer_event *rbe,
188 u32 str_item = *(u32 *)(event + hist_field->field->offset);
189 int str_loc = str_item & 0xffff;
190 char *addr = (char *)(event + str_loc);
192 return (u64)(unsigned long)addr;
195 static u64 hist_field_pstring(struct hist_field *hist_field,
196 struct tracing_map_elt *elt,
197 struct ring_buffer_event *rbe,
200 char **addr = (char **)(event + hist_field->field->offset);
202 return (u64)(unsigned long)*addr;
205 static u64 hist_field_log2(struct hist_field *hist_field,
206 struct tracing_map_elt *elt,
207 struct ring_buffer_event *rbe,
210 struct hist_field *operand = hist_field->operands[0];
212 u64 val = operand->fn(operand, elt, rbe, event);
214 return (u64) ilog2(roundup_pow_of_two(val));
217 static u64 hist_field_plus(struct hist_field *hist_field,
218 struct tracing_map_elt *elt,
219 struct ring_buffer_event *rbe,
222 struct hist_field *operand1 = hist_field->operands[0];
223 struct hist_field *operand2 = hist_field->operands[1];
225 u64 val1 = operand1->fn(operand1, elt, rbe, event);
226 u64 val2 = operand2->fn(operand2, elt, rbe, event);
231 static u64 hist_field_minus(struct hist_field *hist_field,
232 struct tracing_map_elt *elt,
233 struct ring_buffer_event *rbe,
236 struct hist_field *operand1 = hist_field->operands[0];
237 struct hist_field *operand2 = hist_field->operands[1];
239 u64 val1 = operand1->fn(operand1, elt, rbe, event);
240 u64 val2 = operand2->fn(operand2, elt, rbe, event);
245 static u64 hist_field_unary_minus(struct hist_field *hist_field,
246 struct tracing_map_elt *elt,
247 struct ring_buffer_event *rbe,
250 struct hist_field *operand = hist_field->operands[0];
252 s64 sval = (s64)operand->fn(operand, elt, rbe, event);
253 u64 val = (u64)-sval;
258 #define DEFINE_HIST_FIELD_FN(type) \
259 static u64 hist_field_##type(struct hist_field *hist_field, \
260 struct tracing_map_elt *elt, \
261 struct ring_buffer_event *rbe, \
264 type *addr = (type *)(event + hist_field->field->offset); \
266 return (u64)(unsigned long)*addr; \
269 DEFINE_HIST_FIELD_FN(s64);
270 DEFINE_HIST_FIELD_FN(u64);
271 DEFINE_HIST_FIELD_FN(s32);
272 DEFINE_HIST_FIELD_FN(u32);
273 DEFINE_HIST_FIELD_FN(s16);
274 DEFINE_HIST_FIELD_FN(u16);
275 DEFINE_HIST_FIELD_FN(s8);
276 DEFINE_HIST_FIELD_FN(u8);
278 #define for_each_hist_field(i, hist_data) \
279 for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
281 #define for_each_hist_val_field(i, hist_data) \
282 for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
284 #define for_each_hist_key_field(i, hist_data) \
285 for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
287 #define HIST_STACKTRACE_DEPTH 16
288 #define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
289 #define HIST_STACKTRACE_SKIP 5
291 #define HITCOUNT_IDX 0
292 #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
294 enum hist_field_flags {
295 HIST_FIELD_FL_HITCOUNT = 1 << 0,
296 HIST_FIELD_FL_KEY = 1 << 1,
297 HIST_FIELD_FL_STRING = 1 << 2,
298 HIST_FIELD_FL_HEX = 1 << 3,
299 HIST_FIELD_FL_SYM = 1 << 4,
300 HIST_FIELD_FL_SYM_OFFSET = 1 << 5,
301 HIST_FIELD_FL_EXECNAME = 1 << 6,
302 HIST_FIELD_FL_SYSCALL = 1 << 7,
303 HIST_FIELD_FL_STACKTRACE = 1 << 8,
304 HIST_FIELD_FL_LOG2 = 1 << 9,
305 HIST_FIELD_FL_TIMESTAMP = 1 << 10,
306 HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11,
307 HIST_FIELD_FL_VAR = 1 << 12,
308 HIST_FIELD_FL_EXPR = 1 << 13,
309 HIST_FIELD_FL_VAR_REF = 1 << 14,
310 HIST_FIELD_FL_CPU = 1 << 15,
311 HIST_FIELD_FL_ALIAS = 1 << 16,
316 char *name[TRACING_MAP_VARS_MAX];
317 char *expr[TRACING_MAP_VARS_MAX];
320 struct hist_trigger_attrs {
330 unsigned int map_bits;
332 char *assignment_str[TRACING_MAP_VARS_MAX];
333 unsigned int n_assignments;
335 char *action_str[HIST_ACTIONS_MAX];
336 unsigned int n_actions;
338 struct var_defs var_defs;
342 struct hist_field *var;
343 struct hist_field *val;
346 struct field_var_hist {
347 struct hist_trigger_data *hist_data;
351 struct hist_trigger_data {
352 struct hist_field *fields[HIST_FIELDS_MAX];
355 unsigned int n_fields;
357 unsigned int key_size;
358 struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX];
359 unsigned int n_sort_keys;
360 struct trace_event_file *event_file;
361 struct hist_trigger_attrs *attrs;
362 struct tracing_map *map;
363 bool enable_timestamps;
365 struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
366 unsigned int n_var_refs;
368 struct action_data *actions[HIST_ACTIONS_MAX];
369 unsigned int n_actions;
371 struct field_var *field_vars[SYNTH_FIELDS_MAX];
372 unsigned int n_field_vars;
373 unsigned int n_field_var_str;
374 struct field_var_hist *field_var_hists[SYNTH_FIELDS_MAX];
375 unsigned int n_field_var_hists;
377 struct field_var *save_vars[SYNTH_FIELDS_MAX];
378 unsigned int n_save_vars;
379 unsigned int n_save_var_str;
382 static int create_synth_event(int argc, const char **argv);
383 static int synth_event_show(struct seq_file *m, struct dyn_event *ev);
384 static int synth_event_release(struct dyn_event *ev);
385 static bool synth_event_is_busy(struct dyn_event *ev);
386 static bool synth_event_match(const char *system, const char *event,
387 int argc, const char **argv, struct dyn_event *ev);
389 static struct dyn_event_operations synth_event_ops = {
390 .create = create_synth_event,
391 .show = synth_event_show,
392 .is_busy = synth_event_is_busy,
393 .free = synth_event_release,
394 .match = synth_event_match,
407 struct dyn_event devent;
410 struct synth_field **fields;
411 unsigned int n_fields;
413 struct trace_event_class class;
414 struct trace_event_call call;
415 struct tracepoint *tp;
419 static bool is_synth_event(struct dyn_event *ev)
421 return ev->ops == &synth_event_ops;
424 static struct synth_event *to_synth_event(struct dyn_event *ev)
426 return container_of(ev, struct synth_event, devent);
429 static bool synth_event_is_busy(struct dyn_event *ev)
431 struct synth_event *event = to_synth_event(ev);
433 return event->ref != 0;
436 static bool synth_event_match(const char *system, const char *event,
437 int argc, const char **argv, struct dyn_event *ev)
439 struct synth_event *sev = to_synth_event(ev);
441 return strcmp(sev->name, event) == 0 &&
442 (!system || strcmp(system, SYNTH_SYSTEM) == 0);
447 typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
448 struct tracing_map_elt *elt, void *rec,
449 struct ring_buffer_event *rbe, void *key,
450 struct action_data *data, u64 *var_ref_vals);
452 typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val);
467 enum handler_id handler;
468 enum action_id action;
472 unsigned int n_params;
473 char *params[SYNTH_FIELDS_MAX];
476 * When a histogram trigger is hit, the values of any
477 * references to variables, including variables being passed
478 * as parameters to synthetic events, are collected into a
479 * var_ref_vals array. This var_ref_idx array is an array of
480 * indices into the var_ref_vals array, one for each synthetic
481 * event param, and is passed to the synthetic event
484 unsigned int var_ref_idx[TRACING_MAP_VARS_MAX];
485 struct synth_event *synth_event;
486 bool use_trace_keyword;
487 char *synth_event_name;
497 * var_str contains the $-unstripped variable
498 * name referenced by var_ref, and used when
499 * printing the action. Because var_ref
500 * creation is deferred to create_actions(),
501 * we need a per-action way to save it until
502 * then, thus var_str.
507 * var_ref refers to the variable being
508 * tracked e.g onmax($var).
510 struct hist_field *var_ref;
513 * track_var contains the 'invisible' tracking
514 * variable created to keep the current
517 struct hist_field *track_var;
519 check_track_val_fn_t check_val;
520 action_fn_t save_data;
529 unsigned int key_len;
531 struct tracing_map_elt elt;
533 struct action_data *action_data;
534 struct hist_trigger_data *hist_data;
537 struct hist_elt_data {
540 char *field_var_str[SYNTH_FIELDS_MAX];
543 struct snapshot_context {
544 struct tracing_map_elt *elt;
548 static void track_data_free(struct track_data *track_data)
550 struct hist_elt_data *elt_data;
555 kfree(track_data->key);
557 elt_data = track_data->elt.private_data;
559 kfree(elt_data->comm);
566 static struct track_data *track_data_alloc(unsigned int key_len,
567 struct action_data *action_data,
568 struct hist_trigger_data *hist_data)
570 struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
571 struct hist_elt_data *elt_data;
574 return ERR_PTR(-ENOMEM);
576 data->key = kzalloc(key_len, GFP_KERNEL);
578 track_data_free(data);
579 return ERR_PTR(-ENOMEM);
582 data->key_len = key_len;
583 data->action_data = action_data;
584 data->hist_data = hist_data;
586 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
588 track_data_free(data);
589 return ERR_PTR(-ENOMEM);
591 data->elt.private_data = elt_data;
593 elt_data->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL);
594 if (!elt_data->comm) {
595 track_data_free(data);
596 return ERR_PTR(-ENOMEM);
602 static char last_cmd[MAX_FILTER_STR_VAL];
603 static char last_cmd_loc[MAX_FILTER_STR_VAL];
605 static int errpos(char *str)
607 return err_pos(last_cmd, str);
610 static void last_cmd_set(struct trace_event_file *file, char *str)
612 const char *system = NULL, *name = NULL;
613 struct trace_event_call *call;
618 strcpy(last_cmd, "hist:");
619 strncat(last_cmd, str, MAX_FILTER_STR_VAL - 1 - sizeof("hist:"));
622 call = file->event_call;
624 system = call->class->system;
626 name = trace_event_name(call);
633 snprintf(last_cmd_loc, MAX_FILTER_STR_VAL, "hist:%s:%s", system, name);
636 static void hist_err(struct trace_array *tr, u8 err_type, u8 err_pos)
638 tracing_log_err(tr, last_cmd_loc, last_cmd, err_text,
642 static void hist_err_clear(void)
645 last_cmd_loc[0] = '\0';
648 struct synth_trace_event {
649 struct trace_entry ent;
653 static int synth_event_define_fields(struct trace_event_call *call)
655 struct synth_trace_event trace;
656 int offset = offsetof(typeof(trace), fields);
657 struct synth_event *event = call->data;
658 unsigned int i, size, n_u64;
663 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
664 size = event->fields[i]->size;
665 is_signed = event->fields[i]->is_signed;
666 type = event->fields[i]->type;
667 name = event->fields[i]->name;
668 ret = trace_define_field(call, type, name, offset, size,
669 is_signed, FILTER_OTHER);
673 event->fields[i]->offset = n_u64;
675 if (event->fields[i]->is_string) {
676 offset += STR_VAR_LEN_MAX;
677 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
679 offset += sizeof(u64);
684 event->n_u64 = n_u64;
689 static bool synth_field_signed(char *type)
691 if (str_has_prefix(type, "u"))
693 if (strcmp(type, "gfp_t") == 0)
699 static int synth_field_is_string(char *type)
701 if (strstr(type, "char[") != NULL)
707 static int synth_field_string_size(char *type)
709 char buf[4], *end, *start;
713 start = strstr(type, "char[");
716 start += sizeof("char[") - 1;
718 end = strchr(type, ']');
719 if (!end || end < start)
726 strncpy(buf, start, len);
729 err = kstrtouint(buf, 0, &size);
733 if (size > STR_VAR_LEN_MAX)
739 static int synth_field_size(char *type)
743 if (strcmp(type, "s64") == 0)
745 else if (strcmp(type, "u64") == 0)
747 else if (strcmp(type, "s32") == 0)
749 else if (strcmp(type, "u32") == 0)
751 else if (strcmp(type, "s16") == 0)
753 else if (strcmp(type, "u16") == 0)
755 else if (strcmp(type, "s8") == 0)
757 else if (strcmp(type, "u8") == 0)
759 else if (strcmp(type, "char") == 0)
761 else if (strcmp(type, "unsigned char") == 0)
762 size = sizeof(unsigned char);
763 else if (strcmp(type, "int") == 0)
765 else if (strcmp(type, "unsigned int") == 0)
766 size = sizeof(unsigned int);
767 else if (strcmp(type, "long") == 0)
769 else if (strcmp(type, "unsigned long") == 0)
770 size = sizeof(unsigned long);
771 else if (strcmp(type, "pid_t") == 0)
772 size = sizeof(pid_t);
773 else if (strcmp(type, "gfp_t") == 0)
774 size = sizeof(gfp_t);
775 else if (synth_field_is_string(type))
776 size = synth_field_string_size(type);
781 static const char *synth_field_fmt(char *type)
783 const char *fmt = "%llu";
785 if (strcmp(type, "s64") == 0)
787 else if (strcmp(type, "u64") == 0)
789 else if (strcmp(type, "s32") == 0)
791 else if (strcmp(type, "u32") == 0)
793 else if (strcmp(type, "s16") == 0)
795 else if (strcmp(type, "u16") == 0)
797 else if (strcmp(type, "s8") == 0)
799 else if (strcmp(type, "u8") == 0)
801 else if (strcmp(type, "char") == 0)
803 else if (strcmp(type, "unsigned char") == 0)
805 else if (strcmp(type, "int") == 0)
807 else if (strcmp(type, "unsigned int") == 0)
809 else if (strcmp(type, "long") == 0)
811 else if (strcmp(type, "unsigned long") == 0)
813 else if (strcmp(type, "pid_t") == 0)
815 else if (strcmp(type, "gfp_t") == 0)
817 else if (synth_field_is_string(type))
823 static enum print_line_t print_synth_event(struct trace_iterator *iter,
825 struct trace_event *event)
827 struct trace_array *tr = iter->tr;
828 struct trace_seq *s = &iter->seq;
829 struct synth_trace_event *entry;
830 struct synth_event *se;
831 unsigned int i, n_u64;
835 entry = (struct synth_trace_event *)iter->ent;
836 se = container_of(event, struct synth_event, call.event);
838 trace_seq_printf(s, "%s: ", se->name);
840 for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
841 if (trace_seq_has_overflowed(s))
844 fmt = synth_field_fmt(se->fields[i]->type);
846 /* parameter types */
847 if (tr && tr->trace_flags & TRACE_ITER_VERBOSE)
848 trace_seq_printf(s, "%s ", fmt);
850 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
852 /* parameter values */
853 if (se->fields[i]->is_string) {
854 trace_seq_printf(s, print_fmt, se->fields[i]->name,
855 (char *)&entry->fields[n_u64],
856 i == se->n_fields - 1 ? "" : " ");
857 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
859 struct trace_print_flags __flags[] = {
860 __def_gfpflag_names, {-1, NULL} };
862 trace_seq_printf(s, print_fmt, se->fields[i]->name,
863 entry->fields[n_u64],
864 i == se->n_fields - 1 ? "" : " ");
866 if (strcmp(se->fields[i]->type, "gfp_t") == 0) {
867 trace_seq_puts(s, " (");
868 trace_print_flags_seq(s, "|",
869 entry->fields[n_u64],
871 trace_seq_putc(s, ')');
877 trace_seq_putc(s, '\n');
879 return trace_handle_return(s);
882 static struct trace_event_functions synth_event_funcs = {
883 .trace = print_synth_event
886 static notrace void trace_event_raw_event_synth(void *__data,
888 unsigned int *var_ref_idx)
890 struct trace_event_file *trace_file = __data;
891 struct synth_trace_event *entry;
892 struct trace_event_buffer fbuffer;
893 struct trace_buffer *buffer;
894 struct synth_event *event;
895 unsigned int i, n_u64, val_idx;
898 event = trace_file->event_call->data;
900 if (trace_trigger_soft_disabled(trace_file))
903 fields_size = event->n_u64 * sizeof(u64);
906 * Avoid ring buffer recursion detection, as this event
907 * is being performed within another event.
909 buffer = trace_file->tr->array_buffer.buffer;
910 ring_buffer_nest_start(buffer);
912 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
913 sizeof(*entry) + fields_size);
917 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
918 val_idx = var_ref_idx[i];
919 if (event->fields[i]->is_string) {
920 char *str_val = (char *)(long)var_ref_vals[val_idx];
921 char *str_field = (char *)&entry->fields[n_u64];
923 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
924 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
926 struct synth_field *field = event->fields[i];
927 u64 val = var_ref_vals[val_idx];
929 switch (field->size) {
931 *(u8 *)&entry->fields[n_u64] = (u8)val;
935 *(u16 *)&entry->fields[n_u64] = (u16)val;
939 *(u32 *)&entry->fields[n_u64] = (u32)val;
943 entry->fields[n_u64] = val;
950 trace_event_buffer_commit(&fbuffer);
952 ring_buffer_nest_end(buffer);
955 static void free_synth_event_print_fmt(struct trace_event_call *call)
958 kfree(call->print_fmt);
959 call->print_fmt = NULL;
963 static int __set_synth_event_print_fmt(struct synth_event *event,
970 /* When len=0, we just calculate the needed length */
971 #define LEN_OR_ZERO (len ? len - pos : 0)
973 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
974 for (i = 0; i < event->n_fields; i++) {
975 fmt = synth_field_fmt(event->fields[i]->type);
976 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
977 event->fields[i]->name, fmt,
978 i == event->n_fields - 1 ? "" : ", ");
980 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
982 for (i = 0; i < event->n_fields; i++) {
983 pos += snprintf(buf + pos, LEN_OR_ZERO,
984 ", REC->%s", event->fields[i]->name);
989 /* return the length of print_fmt */
993 static int set_synth_event_print_fmt(struct trace_event_call *call)
995 struct synth_event *event = call->data;
999 /* First: called with 0 length to calculate the needed length */
1000 len = __set_synth_event_print_fmt(event, NULL, 0);
1002 print_fmt = kmalloc(len + 1, GFP_KERNEL);
1006 /* Second: actually write the @print_fmt */
1007 __set_synth_event_print_fmt(event, print_fmt, len + 1);
1008 call->print_fmt = print_fmt;
1013 static void free_synth_field(struct synth_field *field)
1020 static struct synth_field *parse_synth_field(int argc, const char **argv,
1023 struct synth_field *field;
1024 const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
1027 if (field_type[0] == ';')
1030 if (!strcmp(field_type, "unsigned")) {
1032 return ERR_PTR(-EINVAL);
1033 prefix = "unsigned ";
1034 field_type = argv[1];
1035 field_name = argv[2];
1038 field_name = argv[1];
1042 field = kzalloc(sizeof(*field), GFP_KERNEL);
1044 return ERR_PTR(-ENOMEM);
1046 len = strlen(field_name);
1047 array = strchr(field_name, '[');
1049 len -= strlen(array);
1050 else if (field_name[len - 1] == ';')
1053 field->name = kmemdup_nul(field_name, len, GFP_KERNEL);
1059 if (field_type[0] == ';')
1061 len = strlen(field_type) + 1;
1063 len += strlen(array);
1065 len += strlen(prefix);
1067 field->type = kzalloc(len, GFP_KERNEL);
1073 strcat(field->type, prefix);
1074 strcat(field->type, field_type);
1076 strcat(field->type, array);
1077 if (field->type[len - 1] == ';')
1078 field->type[len - 1] = '\0';
1081 field->size = synth_field_size(field->type);
1087 if (synth_field_is_string(field->type))
1088 field->is_string = true;
1090 field->is_signed = synth_field_signed(field->type);
1095 free_synth_field(field);
1096 field = ERR_PTR(ret);
1100 static void free_synth_tracepoint(struct tracepoint *tp)
1109 static struct tracepoint *alloc_synth_tracepoint(char *name)
1111 struct tracepoint *tp;
1113 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
1115 return ERR_PTR(-ENOMEM);
1117 tp->name = kstrdup(name, GFP_KERNEL);
1120 return ERR_PTR(-ENOMEM);
1126 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
1127 unsigned int *var_ref_idx);
1129 static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
1130 unsigned int *var_ref_idx)
1132 struct tracepoint *tp = event->tp;
1134 if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
1135 struct tracepoint_func *probe_func_ptr;
1136 synth_probe_func_t probe_func;
1139 if (!(cpu_online(raw_smp_processor_id())))
1142 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
1143 if (probe_func_ptr) {
1145 probe_func = probe_func_ptr->func;
1146 __data = probe_func_ptr->data;
1147 probe_func(__data, var_ref_vals, var_ref_idx);
1148 } while ((++probe_func_ptr)->func);
1153 static struct synth_event *find_synth_event(const char *name)
1155 struct dyn_event *pos;
1156 struct synth_event *event;
1158 for_each_dyn_event(pos) {
1159 if (!is_synth_event(pos))
1161 event = to_synth_event(pos);
1162 if (strcmp(event->name, name) == 0)
1169 static int register_synth_event(struct synth_event *event)
1171 struct trace_event_call *call = &event->call;
1174 event->call.class = &event->class;
1175 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
1176 if (!event->class.system) {
1181 event->tp = alloc_synth_tracepoint(event->name);
1182 if (IS_ERR(event->tp)) {
1183 ret = PTR_ERR(event->tp);
1188 INIT_LIST_HEAD(&call->class->fields);
1189 call->event.funcs = &synth_event_funcs;
1190 call->class->define_fields = synth_event_define_fields;
1192 ret = register_trace_event(&call->event);
1197 call->flags = TRACE_EVENT_FL_TRACEPOINT;
1198 call->class->reg = trace_event_reg;
1199 call->class->probe = trace_event_raw_event_synth;
1201 call->tp = event->tp;
1203 ret = trace_add_event_call(call);
1205 pr_warn("Failed to register synthetic event: %s\n",
1206 trace_event_name(call));
1210 ret = set_synth_event_print_fmt(call);
1212 trace_remove_event_call(call);
1218 unregister_trace_event(&call->event);
1222 static int unregister_synth_event(struct synth_event *event)
1224 struct trace_event_call *call = &event->call;
1227 ret = trace_remove_event_call(call);
1232 static void free_synth_event(struct synth_event *event)
1239 for (i = 0; i < event->n_fields; i++)
1240 free_synth_field(event->fields[i]);
1242 kfree(event->fields);
1244 kfree(event->class.system);
1245 free_synth_tracepoint(event->tp);
1246 free_synth_event_print_fmt(&event->call);
1250 static struct synth_event *alloc_synth_event(const char *name, int n_fields,
1251 struct synth_field **fields)
1253 struct synth_event *event;
1256 event = kzalloc(sizeof(*event), GFP_KERNEL);
1258 event = ERR_PTR(-ENOMEM);
1262 event->name = kstrdup(name, GFP_KERNEL);
1265 event = ERR_PTR(-ENOMEM);
1269 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
1270 if (!event->fields) {
1271 free_synth_event(event);
1272 event = ERR_PTR(-ENOMEM);
1276 dyn_event_init(&event->devent, &synth_event_ops);
1278 for (i = 0; i < n_fields; i++)
1279 event->fields[i] = fields[i];
1281 event->n_fields = n_fields;
1286 static void action_trace(struct hist_trigger_data *hist_data,
1287 struct tracing_map_elt *elt, void *rec,
1288 struct ring_buffer_event *rbe, void *key,
1289 struct action_data *data, u64 *var_ref_vals)
1291 struct synth_event *event = data->synth_event;
1293 trace_synth(event, var_ref_vals, data->var_ref_idx);
1296 struct hist_var_data {
1297 struct list_head list;
1298 struct hist_trigger_data *hist_data;
1301 static int synth_event_check_arg_fn(void *data)
1303 struct dynevent_arg_pair *arg_pair = data;
1306 size = synth_field_size((char *)arg_pair->lhs);
1308 return size ? 0 : -EINVAL;
1312 * synth_event_add_field - Add a new field to a synthetic event cmd
1313 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1314 * @type: The type of the new field to add
1315 * @name: The name of the new field to add
1317 * Add a new field to a synthetic event cmd object. Field ordering is in
1318 * the same order the fields are added.
1320 * See synth_field_size() for available types. If field_name contains
1321 * [n] the field is considered to be an array.
1323 * Return: 0 if successful, error otherwise.
1325 int synth_event_add_field(struct dynevent_cmd *cmd, const char *type,
1328 struct dynevent_arg_pair arg_pair;
1331 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1337 dynevent_arg_pair_init(&arg_pair, 0, ';');
1339 arg_pair.lhs = type;
1340 arg_pair.rhs = name;
1342 ret = dynevent_arg_pair_add(cmd, &arg_pair, synth_event_check_arg_fn);
1346 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
1351 EXPORT_SYMBOL_GPL(synth_event_add_field);
1354 * synth_event_add_field_str - Add a new field to a synthetic event cmd
1355 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1356 * @type_name: The type and name of the new field to add, as a single string
1358 * Add a new field to a synthetic event cmd object, as a single
1359 * string. The @type_name string is expected to be of the form 'type
1360 * name', which will be appended by ';'. No sanity checking is done -
1361 * what's passed in is assumed to already be well-formed. Field
1362 * ordering is in the same order the fields are added.
1364 * See synth_field_size() for available types. If field_name contains
1365 * [n] the field is considered to be an array.
1367 * Return: 0 if successful, error otherwise.
1369 int synth_event_add_field_str(struct dynevent_cmd *cmd, const char *type_name)
1371 struct dynevent_arg arg;
1374 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1380 dynevent_arg_init(&arg, ';');
1382 arg.str = type_name;
1384 ret = dynevent_arg_add(cmd, &arg, NULL);
1388 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
1393 EXPORT_SYMBOL_GPL(synth_event_add_field_str);
1396 * synth_event_add_fields - Add multiple fields to a synthetic event cmd
1397 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1398 * @fields: An array of type/name field descriptions
1399 * @n_fields: The number of field descriptions contained in the fields array
1401 * Add a new set of fields to a synthetic event cmd object. The event
1402 * fields that will be defined for the event should be passed in as an
1403 * array of struct synth_field_desc, and the number of elements in the
1404 * array passed in as n_fields. Field ordering will retain the
1405 * ordering given in the fields array.
1407 * See synth_field_size() for available types. If field_name contains
1408 * [n] the field is considered to be an array.
1410 * Return: 0 if successful, error otherwise.
1412 int synth_event_add_fields(struct dynevent_cmd *cmd,
1413 struct synth_field_desc *fields,
1414 unsigned int n_fields)
1419 for (i = 0; i < n_fields; i++) {
1420 if (fields[i].type == NULL || fields[i].name == NULL) {
1425 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1432 EXPORT_SYMBOL_GPL(synth_event_add_fields);
1435 * __synth_event_gen_cmd_start - Start a synthetic event command from arg list
1436 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1437 * @name: The name of the synthetic event
1438 * @mod: The module creating the event, NULL if not created from a module
1439 * @args: Variable number of arg (pairs), one pair for each field
1441 * NOTE: Users normally won't want to call this function directly, but
1442 * rather use the synth_event_gen_cmd_start() wrapper, which
1443 * automatically adds a NULL to the end of the arg list. If this
1444 * function is used directly, make sure the last arg in the variable
1447 * Generate a synthetic event command to be executed by
1448 * synth_event_gen_cmd_end(). This function can be used to generate
1449 * the complete command or only the first part of it; in the latter
1450 * case, synth_event_add_field(), synth_event_add_field_str(), or
1451 * synth_event_add_fields() can be used to add more fields following
1454 * There should be an even number variable args, each pair consisting
1455 * of a type followed by a field name.
1457 * See synth_field_size() for available types. If field_name contains
1458 * [n] the field is considered to be an array.
1460 * Return: 0 if successful, error otherwise.
1462 int __synth_event_gen_cmd_start(struct dynevent_cmd *cmd, const char *name,
1463 struct module *mod, ...)
1465 struct dynevent_arg arg;
1469 cmd->event_name = name;
1470 cmd->private_data = mod;
1472 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1475 dynevent_arg_init(&arg, 0);
1477 ret = dynevent_arg_add(cmd, &arg, NULL);
1481 va_start(args, mod);
1483 const char *type, *name;
1485 type = va_arg(args, const char *);
1488 name = va_arg(args, const char *);
1492 if (++cmd->n_fields > SYNTH_FIELDS_MAX) {
1497 ret = synth_event_add_field(cmd, type, name);
1505 EXPORT_SYMBOL_GPL(__synth_event_gen_cmd_start);
1508 * synth_event_gen_cmd_array_start - Start synthetic event command from an array
1509 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1510 * @name: The name of the synthetic event
1511 * @fields: An array of type/name field descriptions
1512 * @n_fields: The number of field descriptions contained in the fields array
1514 * Generate a synthetic event command to be executed by
1515 * synth_event_gen_cmd_end(). This function can be used to generate
1516 * the complete command or only the first part of it; in the latter
1517 * case, synth_event_add_field(), synth_event_add_field_str(), or
1518 * synth_event_add_fields() can be used to add more fields following
1521 * The event fields that will be defined for the event should be
1522 * passed in as an array of struct synth_field_desc, and the number of
1523 * elements in the array passed in as n_fields. Field ordering will
1524 * retain the ordering given in the fields array.
1526 * See synth_field_size() for available types. If field_name contains
1527 * [n] the field is considered to be an array.
1529 * Return: 0 if successful, error otherwise.
1531 int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd, const char *name,
1533 struct synth_field_desc *fields,
1534 unsigned int n_fields)
1536 struct dynevent_arg arg;
1540 cmd->event_name = name;
1541 cmd->private_data = mod;
1543 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1546 if (n_fields > SYNTH_FIELDS_MAX)
1549 dynevent_arg_init(&arg, 0);
1551 ret = dynevent_arg_add(cmd, &arg, NULL);
1555 for (i = 0; i < n_fields; i++) {
1556 if (fields[i].type == NULL || fields[i].name == NULL)
1559 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1566 EXPORT_SYMBOL_GPL(synth_event_gen_cmd_array_start);
1568 static int __create_synth_event(int argc, const char *name, const char **argv)
1570 struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1571 struct synth_event *event = NULL;
1572 int i, consumed = 0, n_fields = 0, ret = 0;
1576 * - Add synthetic event: <event_name> field[;field] ...
1577 * - Remove synthetic event: !<event_name> field[;field] ...
1578 * where 'field' = type field_name
1581 if (name[0] == '\0' || argc < 1)
1584 mutex_lock(&event_mutex);
1586 event = find_synth_event(name);
1592 for (i = 0; i < argc - 1; i++) {
1593 if (strcmp(argv[i], ";") == 0)
1595 if (n_fields == SYNTH_FIELDS_MAX) {
1600 field = parse_synth_field(argc - i, &argv[i], &consumed);
1601 if (IS_ERR(field)) {
1602 ret = PTR_ERR(field);
1605 fields[n_fields++] = field;
1609 if (i < argc && strcmp(argv[i], ";") != 0) {
1614 event = alloc_synth_event(name, n_fields, fields);
1615 if (IS_ERR(event)) {
1616 ret = PTR_ERR(event);
1620 ret = register_synth_event(event);
1622 dyn_event_add(&event->devent);
1624 free_synth_event(event);
1626 mutex_unlock(&event_mutex);
1630 for (i = 0; i < n_fields; i++)
1631 free_synth_field(fields[i]);
1637 * synth_event_create - Create a new synthetic event
1638 * @name: The name of the new sythetic event
1639 * @fields: An array of type/name field descriptions
1640 * @n_fields: The number of field descriptions contained in the fields array
1641 * @mod: The module creating the event, NULL if not created from a module
1643 * Create a new synthetic event with the given name under the
1644 * trace/events/synthetic/ directory. The event fields that will be
1645 * defined for the event should be passed in as an array of struct
1646 * synth_field_desc, and the number elements in the array passed in as
1647 * n_fields. Field ordering will retain the ordering given in the
1650 * If the new synthetic event is being created from a module, the mod
1651 * param must be non-NULL. This will ensure that the trace buffer
1652 * won't contain unreadable events.
1654 * The new synth event should be deleted using synth_event_delete()
1655 * function. The new synthetic event can be generated from modules or
1656 * other kernel code using trace_synth_event() and related functions.
1658 * Return: 0 if successful, error otherwise.
1660 int synth_event_create(const char *name, struct synth_field_desc *fields,
1661 unsigned int n_fields, struct module *mod)
1663 struct dynevent_cmd cmd;
1667 buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
1671 synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
1673 ret = synth_event_gen_cmd_array_start(&cmd, name, mod,
1678 ret = synth_event_gen_cmd_end(&cmd);
1684 EXPORT_SYMBOL_GPL(synth_event_create);
1686 static int destroy_synth_event(struct synth_event *se)
1693 ret = unregister_synth_event(se);
1695 dyn_event_remove(&se->devent);
1696 free_synth_event(se);
1704 * synth_event_delete - Delete a synthetic event
1705 * @event_name: The name of the new sythetic event
1707 * Delete a synthetic event that was created with synth_event_create().
1709 * Return: 0 if successful, error otherwise.
1711 int synth_event_delete(const char *event_name)
1713 struct synth_event *se = NULL;
1714 struct module *mod = NULL;
1717 mutex_lock(&event_mutex);
1718 se = find_synth_event(event_name);
1721 ret = destroy_synth_event(se);
1723 mutex_unlock(&event_mutex);
1726 mutex_lock(&trace_types_lock);
1728 * It is safest to reset the ring buffer if the module
1729 * being unloaded registered any events that were
1730 * used. The only worry is if a new module gets
1731 * loaded, and takes on the same id as the events of
1732 * this module. When printing out the buffer, traced
1733 * events left over from this module may be passed to
1734 * the new module events and unexpected results may
1737 tracing_reset_all_online_cpus();
1738 mutex_unlock(&trace_types_lock);
1743 EXPORT_SYMBOL_GPL(synth_event_delete);
1745 static int create_or_delete_synth_event(int argc, char **argv)
1747 const char *name = argv[0];
1750 /* trace_run_command() ensures argc != 0 */
1751 if (name[0] == '!') {
1752 ret = synth_event_delete(name + 1);
1756 ret = __create_synth_event(argc - 1, name, (const char **)argv + 1);
1757 return ret == -ECANCELED ? -EINVAL : ret;
1760 static int synth_event_run_command(struct dynevent_cmd *cmd)
1762 struct synth_event *se;
1765 ret = trace_run_command(cmd->seq.buffer, create_or_delete_synth_event);
1769 se = find_synth_event(cmd->event_name);
1773 se->mod = cmd->private_data;
1779 * synth_event_cmd_init - Initialize a synthetic event command object
1780 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1781 * @buf: A pointer to the buffer used to build the command
1782 * @maxlen: The length of the buffer passed in @buf
1784 * Initialize a synthetic event command object. Use this before
1785 * calling any of the other dyenvent_cmd functions.
1787 void synth_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
1789 dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_SYNTH,
1790 synth_event_run_command);
1792 EXPORT_SYMBOL_GPL(synth_event_cmd_init);
1795 * synth_event_trace - Trace a synthetic event
1796 * @file: The trace_event_file representing the synthetic event
1797 * @n_vals: The number of values in vals
1798 * @args: Variable number of args containing the event values
1800 * Trace a synthetic event using the values passed in the variable
1803 * The argument list should be a list 'n_vals' u64 values. The number
1804 * of vals must match the number of field in the synthetic event, and
1805 * must be in the same order as the synthetic event fields.
1807 * All vals should be cast to u64, and string vals are just pointers
1808 * to strings, cast to u64. Strings will be copied into space
1809 * reserved in the event for the string, using these pointers.
1811 * Return: 0 on success, err otherwise.
1813 int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
1815 struct trace_event_buffer fbuffer;
1816 struct synth_trace_event *entry;
1817 struct trace_buffer *buffer;
1818 struct synth_event *event;
1819 unsigned int i, n_u64;
1820 int fields_size = 0;
1825 * Normal event generation doesn't get called at all unless
1826 * the ENABLED bit is set (which attaches the probe thus
1827 * allowing this code to be called, etc). Because this is
1828 * called directly by the user, we don't have that but we
1829 * still need to honor not logging when disabled.
1831 if (!(file->flags & EVENT_FILE_FL_ENABLED))
1834 event = file->event_call->data;
1836 if (n_vals != event->n_fields)
1839 if (trace_trigger_soft_disabled(file))
1842 fields_size = event->n_u64 * sizeof(u64);
1845 * Avoid ring buffer recursion detection, as this event
1846 * is being performed within another event.
1848 buffer = file->tr->array_buffer.buffer;
1849 ring_buffer_nest_start(buffer);
1851 entry = trace_event_buffer_reserve(&fbuffer, file,
1852 sizeof(*entry) + fields_size);
1858 va_start(args, n_vals);
1859 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
1862 val = va_arg(args, u64);
1864 if (event->fields[i]->is_string) {
1865 char *str_val = (char *)(long)val;
1866 char *str_field = (char *)&entry->fields[n_u64];
1868 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
1869 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
1871 entry->fields[n_u64] = val;
1877 trace_event_buffer_commit(&fbuffer);
1879 ring_buffer_nest_end(buffer);
1883 EXPORT_SYMBOL_GPL(synth_event_trace);
1886 * synth_event_trace_array - Trace a synthetic event from an array
1887 * @file: The trace_event_file representing the synthetic event
1888 * @vals: Array of values
1889 * @n_vals: The number of values in vals
1891 * Trace a synthetic event using the values passed in as 'vals'.
1893 * The 'vals' array is just an array of 'n_vals' u64. The number of
1894 * vals must match the number of field in the synthetic event, and
1895 * must be in the same order as the synthetic event fields.
1897 * All vals should be cast to u64, and string vals are just pointers
1898 * to strings, cast to u64. Strings will be copied into space
1899 * reserved in the event for the string, using these pointers.
1901 * Return: 0 on success, err otherwise.
1903 int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
1904 unsigned int n_vals)
1906 struct trace_event_buffer fbuffer;
1907 struct synth_trace_event *entry;
1908 struct trace_buffer *buffer;
1909 struct synth_event *event;
1910 unsigned int i, n_u64;
1911 int fields_size = 0;
1915 * Normal event generation doesn't get called at all unless
1916 * the ENABLED bit is set (which attaches the probe thus
1917 * allowing this code to be called, etc). Because this is
1918 * called directly by the user, we don't have that but we
1919 * still need to honor not logging when disabled.
1921 if (!(file->flags & EVENT_FILE_FL_ENABLED))
1924 event = file->event_call->data;
1926 if (n_vals != event->n_fields)
1929 if (trace_trigger_soft_disabled(file))
1932 fields_size = event->n_u64 * sizeof(u64);
1935 * Avoid ring buffer recursion detection, as this event
1936 * is being performed within another event.
1938 buffer = file->tr->array_buffer.buffer;
1939 ring_buffer_nest_start(buffer);
1941 entry = trace_event_buffer_reserve(&fbuffer, file,
1942 sizeof(*entry) + fields_size);
1948 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
1949 if (event->fields[i]->is_string) {
1950 char *str_val = (char *)(long)vals[i];
1951 char *str_field = (char *)&entry->fields[n_u64];
1953 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
1954 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
1956 entry->fields[n_u64] = vals[i];
1961 trace_event_buffer_commit(&fbuffer);
1963 ring_buffer_nest_end(buffer);
1967 EXPORT_SYMBOL_GPL(synth_event_trace_array);
1970 * synth_event_trace_start - Start piecewise synthetic event trace
1971 * @file: The trace_event_file representing the synthetic event
1972 * @trace_state: A pointer to object tracking the piecewise trace state
1974 * Start the trace of a synthetic event field-by-field rather than all
1977 * This function 'opens' an event trace, which means space is reserved
1978 * for the event in the trace buffer, after which the event's
1979 * individual field values can be set through either
1980 * synth_event_add_next_val() or synth_event_add_val().
1982 * A pointer to a trace_state object is passed in, which will keep
1983 * track of the current event trace state until the event trace is
1984 * closed (and the event finally traced) using
1985 * synth_event_trace_end().
1987 * Note that synth_event_trace_end() must be called after all values
1988 * have been added for each event trace, regardless of whether adding
1989 * all field values succeeded or not.
1991 * Note also that for a given event trace, all fields must be added
1992 * using either synth_event_add_next_val() or synth_event_add_val()
1993 * but not both together or interleaved.
1995 * Return: 0 on success, err otherwise.
1997 int synth_event_trace_start(struct trace_event_file *file,
1998 struct synth_event_trace_state *trace_state)
2000 struct synth_trace_event *entry;
2001 int fields_size = 0;
2009 memset(trace_state, '\0', sizeof(*trace_state));
2012 * Normal event tracing doesn't get called at all unless the
2013 * ENABLED bit is set (which attaches the probe thus allowing
2014 * this code to be called, etc). Because this is called
2015 * directly by the user, we don't have that but we still need
2016 * to honor not logging when disabled. For the the iterated
2017 * trace case, we save the enabed state upon start and just
2018 * ignore the following data calls.
2020 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
2021 trace_state->enabled = false;
2025 trace_state->enabled = true;
2027 trace_state->event = file->event_call->data;
2029 if (trace_trigger_soft_disabled(file)) {
2034 fields_size = trace_state->event->n_u64 * sizeof(u64);
2037 * Avoid ring buffer recursion detection, as this event
2038 * is being performed within another event.
2040 trace_state->buffer = file->tr->array_buffer.buffer;
2041 ring_buffer_nest_start(trace_state->buffer);
2043 entry = trace_event_buffer_reserve(&trace_state->fbuffer, file,
2044 sizeof(*entry) + fields_size);
2050 trace_state->entry = entry;
2054 EXPORT_SYMBOL_GPL(synth_event_trace_start);
2056 static int __synth_event_add_val(const char *field_name, u64 val,
2057 struct synth_event_trace_state *trace_state)
2059 struct synth_field *field = NULL;
2060 struct synth_trace_event *entry;
2061 struct synth_event *event;
2069 /* can't mix add_next_synth_val() with add_synth_val() */
2071 if (trace_state->add_next) {
2075 trace_state->add_name = true;
2077 if (trace_state->add_name) {
2081 trace_state->add_next = true;
2084 if (!trace_state->enabled)
2087 event = trace_state->event;
2088 if (trace_state->add_name) {
2089 for (i = 0; i < event->n_fields; i++) {
2090 field = event->fields[i];
2091 if (strcmp(field->name, field_name) == 0)
2099 if (trace_state->cur_field >= event->n_fields) {
2103 field = event->fields[trace_state->cur_field++];
2106 entry = trace_state->entry;
2107 if (field->is_string) {
2108 char *str_val = (char *)(long)val;
2116 str_field = (char *)&entry->fields[field->offset];
2117 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
2119 entry->fields[field->offset] = val;
2125 * synth_event_add_next_val - Add the next field's value to an open synth trace
2126 * @val: The value to set the next field to
2127 * @trace_state: A pointer to object tracking the piecewise trace state
2129 * Set the value of the next field in an event that's been opened by
2130 * synth_event_trace_start().
2132 * The val param should be the value cast to u64. If the value points
2133 * to a string, the val param should be a char * cast to u64.
2135 * This function assumes all the fields in an event are to be set one
2136 * after another - successive calls to this function are made, one for
2137 * each field, in the order of the fields in the event, until all
2138 * fields have been set. If you'd rather set each field individually
2139 * without regard to ordering, synth_event_add_val() can be used
2142 * Note however that synth_event_add_next_val() and
2143 * synth_event_add_val() can't be intermixed for a given event trace -
2144 * one or the other but not both can be used at the same time.
2146 * Note also that synth_event_trace_end() must be called after all
2147 * values have been added for each event trace, regardless of whether
2148 * adding all field values succeeded or not.
2150 * Return: 0 on success, err otherwise.
2152 int synth_event_add_next_val(u64 val,
2153 struct synth_event_trace_state *trace_state)
2155 return __synth_event_add_val(NULL, val, trace_state);
2157 EXPORT_SYMBOL_GPL(synth_event_add_next_val);
2160 * synth_event_add_val - Add a named field's value to an open synth trace
2161 * @field_name: The name of the synthetic event field value to set
2162 * @val: The value to set the next field to
2163 * @trace_state: A pointer to object tracking the piecewise trace state
2165 * Set the value of the named field in an event that's been opened by
2166 * synth_event_trace_start().
2168 * The val param should be the value cast to u64. If the value points
2169 * to a string, the val param should be a char * cast to u64.
2171 * This function looks up the field name, and if found, sets the field
2172 * to the specified value. This lookup makes this function more
2173 * expensive than synth_event_add_next_val(), so use that or the
2174 * none-piecewise synth_event_trace() instead if efficiency is more
2177 * Note however that synth_event_add_next_val() and
2178 * synth_event_add_val() can't be intermixed for a given event trace -
2179 * one or the other but not both can be used at the same time.
2181 * Note also that synth_event_trace_end() must be called after all
2182 * values have been added for each event trace, regardless of whether
2183 * adding all field values succeeded or not.
2185 * Return: 0 on success, err otherwise.
2187 int synth_event_add_val(const char *field_name, u64 val,
2188 struct synth_event_trace_state *trace_state)
2190 return __synth_event_add_val(field_name, val, trace_state);
2192 EXPORT_SYMBOL_GPL(synth_event_add_val);
2195 * synth_event_trace_end - End piecewise synthetic event trace
2196 * @trace_state: A pointer to object tracking the piecewise trace state
2198 * End the trace of a synthetic event opened by
2199 * synth_event_trace__start().
2201 * This function 'closes' an event trace, which basically means that
2202 * it commits the reserved event and cleans up other loose ends.
2204 * A pointer to a trace_state object is passed in, which will keep
2205 * track of the current event trace state opened with
2206 * synth_event_trace_start().
2208 * Note that this function must be called after all values have been
2209 * added for each event trace, regardless of whether adding all field
2210 * values succeeded or not.
2212 * Return: 0 on success, err otherwise.
2214 int synth_event_trace_end(struct synth_event_trace_state *trace_state)
2219 trace_event_buffer_commit(&trace_state->fbuffer);
2221 ring_buffer_nest_end(trace_state->buffer);
2225 EXPORT_SYMBOL_GPL(synth_event_trace_end);
2227 static int create_synth_event(int argc, const char **argv)
2229 const char *name = argv[0];
2232 if (name[0] != 's' || name[1] != ':')
2236 /* This interface accepts group name prefix */
2237 if (strchr(name, '/')) {
2238 len = str_has_prefix(name, SYNTH_SYSTEM "/");
2243 return __create_synth_event(argc - 1, name, argv + 1);
2246 static int synth_event_release(struct dyn_event *ev)
2248 struct synth_event *event = to_synth_event(ev);
2254 ret = unregister_synth_event(event);
2258 dyn_event_remove(ev);
2259 free_synth_event(event);
2263 static int __synth_event_show(struct seq_file *m, struct synth_event *event)
2265 struct synth_field *field;
2268 seq_printf(m, "%s\t", event->name);
2270 for (i = 0; i < event->n_fields; i++) {
2271 field = event->fields[i];
2273 /* parameter values */
2274 seq_printf(m, "%s %s%s", field->type, field->name,
2275 i == event->n_fields - 1 ? "" : "; ");
2283 static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
2285 struct synth_event *event = to_synth_event(ev);
2287 seq_printf(m, "s:%s/", event->class.system);
2289 return __synth_event_show(m, event);
2292 static int synth_events_seq_show(struct seq_file *m, void *v)
2294 struct dyn_event *ev = v;
2296 if (!is_synth_event(ev))
2299 return __synth_event_show(m, to_synth_event(ev));
2302 static const struct seq_operations synth_events_seq_op = {
2303 .start = dyn_event_seq_start,
2304 .next = dyn_event_seq_next,
2305 .stop = dyn_event_seq_stop,
2306 .show = synth_events_seq_show,
2309 static int synth_events_open(struct inode *inode, struct file *file)
2313 ret = security_locked_down(LOCKDOWN_TRACEFS);
2317 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
2318 ret = dyn_events_release_all(&synth_event_ops);
2323 return seq_open(file, &synth_events_seq_op);
2326 static ssize_t synth_events_write(struct file *file,
2327 const char __user *buffer,
2328 size_t count, loff_t *ppos)
2330 return trace_parse_run_command(file, buffer, count, ppos,
2331 create_or_delete_synth_event);
2334 static const struct file_operations synth_events_fops = {
2335 .open = synth_events_open,
2336 .write = synth_events_write,
2338 .llseek = seq_lseek,
2339 .release = seq_release,
2342 static u64 hist_field_timestamp(struct hist_field *hist_field,
2343 struct tracing_map_elt *elt,
2344 struct ring_buffer_event *rbe,
2347 struct hist_trigger_data *hist_data = hist_field->hist_data;
2348 struct trace_array *tr = hist_data->event_file->tr;
2350 u64 ts = ring_buffer_event_time_stamp(rbe);
2352 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
2358 static u64 hist_field_cpu(struct hist_field *hist_field,
2359 struct tracing_map_elt *elt,
2360 struct ring_buffer_event *rbe,
2363 int cpu = smp_processor_id();
2369 * check_field_for_var_ref - Check if a VAR_REF field references a variable
2370 * @hist_field: The VAR_REF field to check
2371 * @var_data: The hist trigger that owns the variable
2372 * @var_idx: The trigger variable identifier
2374 * Check the given VAR_REF field to see whether or not it references
2375 * the given variable associated with the given trigger.
2377 * Return: The VAR_REF field if it does reference the variable, NULL if not
2379 static struct hist_field *
2380 check_field_for_var_ref(struct hist_field *hist_field,
2381 struct hist_trigger_data *var_data,
2382 unsigned int var_idx)
2384 WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
2386 if (hist_field && hist_field->var.idx == var_idx &&
2387 hist_field->var.hist_data == var_data)
2394 * find_var_ref - Check if a trigger has a reference to a trigger variable
2395 * @hist_data: The hist trigger that might have a reference to the variable
2396 * @var_data: The hist trigger that owns the variable
2397 * @var_idx: The trigger variable identifier
2399 * Check the list of var_refs[] on the first hist trigger to see
2400 * whether any of them are references to the variable on the second
2403 * Return: The VAR_REF field referencing the variable if so, NULL if not
2405 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
2406 struct hist_trigger_data *var_data,
2407 unsigned int var_idx)
2409 struct hist_field *hist_field;
2412 for (i = 0; i < hist_data->n_var_refs; i++) {
2413 hist_field = hist_data->var_refs[i];
2414 if (check_field_for_var_ref(hist_field, var_data, var_idx))
2422 * find_any_var_ref - Check if there is a reference to a given trigger variable
2423 * @hist_data: The hist trigger
2424 * @var_idx: The trigger variable identifier
2426 * Check to see whether the given variable is currently referenced by
2427 * any other trigger.
2429 * The trigger the variable is defined on is explicitly excluded - the
2430 * assumption being that a self-reference doesn't prevent a trigger
2431 * from being removed.
2433 * Return: The VAR_REF field referencing the variable if so, NULL if not
2435 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
2436 unsigned int var_idx)
2438 struct trace_array *tr = hist_data->event_file->tr;
2439 struct hist_field *found = NULL;
2440 struct hist_var_data *var_data;
2442 list_for_each_entry(var_data, &tr->hist_vars, list) {
2443 if (var_data->hist_data == hist_data)
2445 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
2454 * check_var_refs - Check if there is a reference to any of trigger's variables
2455 * @hist_data: The hist trigger
2457 * A trigger can define one or more variables. If any one of them is
2458 * currently referenced by any other trigger, this function will
2461 * Typically used to determine whether or not a trigger can be removed
2462 * - if there are any references to a trigger's variables, it cannot.
2464 * Return: True if there is a reference to any of trigger's variables
2466 static bool check_var_refs(struct hist_trigger_data *hist_data)
2468 struct hist_field *field;
2472 for_each_hist_field(i, hist_data) {
2473 field = hist_data->fields[i];
2474 if (field && field->flags & HIST_FIELD_FL_VAR) {
2475 if (find_any_var_ref(hist_data, field->var.idx)) {
2485 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
2487 struct trace_array *tr = hist_data->event_file->tr;
2488 struct hist_var_data *var_data, *found = NULL;
2490 list_for_each_entry(var_data, &tr->hist_vars, list) {
2491 if (var_data->hist_data == hist_data) {
2500 static bool field_has_hist_vars(struct hist_field *hist_field,
2511 if (hist_field->flags & HIST_FIELD_FL_VAR ||
2512 hist_field->flags & HIST_FIELD_FL_VAR_REF)
2515 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
2516 struct hist_field *operand;
2518 operand = hist_field->operands[i];
2519 if (field_has_hist_vars(operand, level + 1))
2526 static bool has_hist_vars(struct hist_trigger_data *hist_data)
2528 struct hist_field *hist_field;
2531 for_each_hist_field(i, hist_data) {
2532 hist_field = hist_data->fields[i];
2533 if (field_has_hist_vars(hist_field, 0))
2540 static int save_hist_vars(struct hist_trigger_data *hist_data)
2542 struct trace_array *tr = hist_data->event_file->tr;
2543 struct hist_var_data *var_data;
2545 var_data = find_hist_vars(hist_data);
2549 if (tracing_check_open_get_tr(tr))
2552 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
2554 trace_array_put(tr);
2558 var_data->hist_data = hist_data;
2559 list_add(&var_data->list, &tr->hist_vars);
2564 static void remove_hist_vars(struct hist_trigger_data *hist_data)
2566 struct trace_array *tr = hist_data->event_file->tr;
2567 struct hist_var_data *var_data;
2569 var_data = find_hist_vars(hist_data);
2573 if (WARN_ON(check_var_refs(hist_data)))
2576 list_del(&var_data->list);
2580 trace_array_put(tr);
2583 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
2584 const char *var_name)
2586 struct hist_field *hist_field, *found = NULL;
2589 for_each_hist_field(i, hist_data) {
2590 hist_field = hist_data->fields[i];
2591 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
2592 strcmp(hist_field->var.name, var_name) == 0) {
2601 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
2602 struct trace_event_file *file,
2603 const char *var_name)
2605 struct hist_trigger_data *test_data;
2606 struct event_trigger_data *test;
2607 struct hist_field *hist_field;
2609 lockdep_assert_held(&event_mutex);
2611 hist_field = find_var_field(hist_data, var_name);
2615 list_for_each_entry(test, &file->triggers, list) {
2616 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2617 test_data = test->private_data;
2618 hist_field = find_var_field(test_data, var_name);
2627 static struct trace_event_file *find_var_file(struct trace_array *tr,
2632 struct hist_trigger_data *var_hist_data;
2633 struct hist_var_data *var_data;
2634 struct trace_event_file *file, *found = NULL;
2637 return find_event_file(tr, system, event_name);
2639 list_for_each_entry(var_data, &tr->hist_vars, list) {
2640 var_hist_data = var_data->hist_data;
2641 file = var_hist_data->event_file;
2645 if (find_var_field(var_hist_data, var_name)) {
2647 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name));
2658 static struct hist_field *find_file_var(struct trace_event_file *file,
2659 const char *var_name)
2661 struct hist_trigger_data *test_data;
2662 struct event_trigger_data *test;
2663 struct hist_field *hist_field;
2665 lockdep_assert_held(&event_mutex);
2667 list_for_each_entry(test, &file->triggers, list) {
2668 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2669 test_data = test->private_data;
2670 hist_field = find_var_field(test_data, var_name);
2679 static struct hist_field *
2680 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
2682 struct trace_array *tr = hist_data->event_file->tr;
2683 struct hist_field *hist_field, *found = NULL;
2684 struct trace_event_file *file;
2687 for (i = 0; i < hist_data->n_actions; i++) {
2688 struct action_data *data = hist_data->actions[i];
2690 if (data->handler == HANDLER_ONMATCH) {
2691 char *system = data->match_data.event_system;
2692 char *event_name = data->match_data.event;
2694 file = find_var_file(tr, system, event_name, var_name);
2697 hist_field = find_file_var(file, var_name);
2700 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE,
2702 return ERR_PTR(-EINVAL);
2712 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
2717 struct trace_array *tr = hist_data->event_file->tr;
2718 struct hist_field *hist_field = NULL;
2719 struct trace_event_file *file;
2721 if (!system || !event_name) {
2722 hist_field = find_match_var(hist_data, var_name);
2723 if (IS_ERR(hist_field))
2729 file = find_var_file(tr, system, event_name, var_name);
2733 hist_field = find_file_var(file, var_name);
2738 static u64 hist_field_var_ref(struct hist_field *hist_field,
2739 struct tracing_map_elt *elt,
2740 struct ring_buffer_event *rbe,
2743 struct hist_elt_data *elt_data;
2746 if (WARN_ON_ONCE(!elt))
2749 elt_data = elt->private_data;
2750 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
2755 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
2756 u64 *var_ref_vals, bool self)
2758 struct hist_trigger_data *var_data;
2759 struct tracing_map_elt *var_elt;
2760 struct hist_field *hist_field;
2761 unsigned int i, var_idx;
2762 bool resolved = true;
2765 for (i = 0; i < hist_data->n_var_refs; i++) {
2766 hist_field = hist_data->var_refs[i];
2767 var_idx = hist_field->var.idx;
2768 var_data = hist_field->var.hist_data;
2770 if (var_data == NULL) {
2775 if ((self && var_data != hist_data) ||
2776 (!self && var_data == hist_data))
2779 var_elt = tracing_map_lookup(var_data->map, key);
2785 if (!tracing_map_var_set(var_elt, var_idx)) {
2790 if (self || !hist_field->read_once)
2791 var_val = tracing_map_read_var(var_elt, var_idx);
2793 var_val = tracing_map_read_var_once(var_elt, var_idx);
2795 var_ref_vals[i] = var_val;
2801 static const char *hist_field_name(struct hist_field *field,
2804 const char *field_name = "";
2810 field_name = field->field->name;
2811 else if (field->flags & HIST_FIELD_FL_LOG2 ||
2812 field->flags & HIST_FIELD_FL_ALIAS)
2813 field_name = hist_field_name(field->operands[0], ++level);
2814 else if (field->flags & HIST_FIELD_FL_CPU)
2816 else if (field->flags & HIST_FIELD_FL_EXPR ||
2817 field->flags & HIST_FIELD_FL_VAR_REF) {
2818 if (field->system) {
2819 static char full_name[MAX_FILTER_STR_VAL];
2821 strcat(full_name, field->system);
2822 strcat(full_name, ".");
2823 strcat(full_name, field->event_name);
2824 strcat(full_name, ".");
2825 strcat(full_name, field->name);
2826 field_name = full_name;
2828 field_name = field->name;
2829 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
2830 field_name = "common_timestamp";
2832 if (field_name == NULL)
2838 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
2840 hist_field_fn_t fn = NULL;
2842 switch (field_size) {
2844 if (field_is_signed)
2845 fn = hist_field_s64;
2847 fn = hist_field_u64;
2850 if (field_is_signed)
2851 fn = hist_field_s32;
2853 fn = hist_field_u32;
2856 if (field_is_signed)
2857 fn = hist_field_s16;
2859 fn = hist_field_u16;
2862 if (field_is_signed)
2872 static int parse_map_size(char *str)
2874 unsigned long size, map_bits;
2877 ret = kstrtoul(str, 0, &size);
2881 map_bits = ilog2(roundup_pow_of_two(size));
2882 if (map_bits < TRACING_MAP_BITS_MIN ||
2883 map_bits > TRACING_MAP_BITS_MAX)
2891 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
2898 for (i = 0; i < attrs->n_assignments; i++)
2899 kfree(attrs->assignment_str[i]);
2901 for (i = 0; i < attrs->n_actions; i++)
2902 kfree(attrs->action_str[i]);
2905 kfree(attrs->sort_key_str);
2906 kfree(attrs->keys_str);
2907 kfree(attrs->vals_str);
2908 kfree(attrs->clock);
2912 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
2916 if (attrs->n_actions >= HIST_ACTIONS_MAX)
2919 if ((str_has_prefix(str, "onmatch(")) ||
2920 (str_has_prefix(str, "onmax(")) ||
2921 (str_has_prefix(str, "onchange("))) {
2922 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
2923 if (!attrs->action_str[attrs->n_actions]) {
2933 static int parse_assignment(struct trace_array *tr,
2934 char *str, struct hist_trigger_attrs *attrs)
2938 if ((len = str_has_prefix(str, "key=")) ||
2939 (len = str_has_prefix(str, "keys="))) {
2940 attrs->keys_str = kstrdup(str + len, GFP_KERNEL);
2941 if (!attrs->keys_str) {
2945 } else if ((len = str_has_prefix(str, "val=")) ||
2946 (len = str_has_prefix(str, "vals=")) ||
2947 (len = str_has_prefix(str, "values="))) {
2948 attrs->vals_str = kstrdup(str + len, GFP_KERNEL);
2949 if (!attrs->vals_str) {
2953 } else if ((len = str_has_prefix(str, "sort="))) {
2954 attrs->sort_key_str = kstrdup(str + len, GFP_KERNEL);
2955 if (!attrs->sort_key_str) {
2959 } else if (str_has_prefix(str, "name=")) {
2960 attrs->name = kstrdup(str, GFP_KERNEL);
2965 } else if ((len = str_has_prefix(str, "clock="))) {
2968 str = strstrip(str);
2969 attrs->clock = kstrdup(str, GFP_KERNEL);
2970 if (!attrs->clock) {
2974 } else if ((len = str_has_prefix(str, "size="))) {
2975 int map_bits = parse_map_size(str + len);
2981 attrs->map_bits = map_bits;
2985 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
2986 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str));
2991 assignment = kstrdup(str, GFP_KERNEL);
2997 attrs->assignment_str[attrs->n_assignments++] = assignment;
3003 static struct hist_trigger_attrs *
3004 parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str)
3006 struct hist_trigger_attrs *attrs;
3009 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
3011 return ERR_PTR(-ENOMEM);
3013 while (trigger_str) {
3014 char *str = strsep(&trigger_str, ":");
3017 rhs = strchr(str, '=');
3019 if (!strlen(++rhs)) {
3021 hist_err(tr, HIST_ERR_EMPTY_ASSIGNMENT, errpos(str));
3024 ret = parse_assignment(tr, str, attrs);
3027 } else if (strcmp(str, "pause") == 0)
3028 attrs->pause = true;
3029 else if ((strcmp(str, "cont") == 0) ||
3030 (strcmp(str, "continue") == 0))
3032 else if (strcmp(str, "clear") == 0)
3033 attrs->clear = true;
3035 ret = parse_action(str, attrs);
3041 if (!attrs->keys_str) {
3046 if (!attrs->clock) {
3047 attrs->clock = kstrdup("global", GFP_KERNEL);
3048 if (!attrs->clock) {
3056 destroy_hist_trigger_attrs(attrs);
3058 return ERR_PTR(ret);
3061 static inline void save_comm(char *comm, struct task_struct *task)
3064 strcpy(comm, "<idle>");
3068 if (WARN_ON_ONCE(task->pid < 0)) {
3069 strcpy(comm, "<XXX>");
3073 strncpy(comm, task->comm, TASK_COMM_LEN);
3076 static void hist_elt_data_free(struct hist_elt_data *elt_data)
3080 for (i = 0; i < SYNTH_FIELDS_MAX; i++)
3081 kfree(elt_data->field_var_str[i]);
3083 kfree(elt_data->comm);
3087 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
3089 struct hist_elt_data *elt_data = elt->private_data;
3091 hist_elt_data_free(elt_data);
3094 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
3096 struct hist_trigger_data *hist_data = elt->map->private_data;
3097 unsigned int size = TASK_COMM_LEN;
3098 struct hist_elt_data *elt_data;
3099 struct hist_field *key_field;
3100 unsigned int i, n_str;
3102 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
3106 for_each_hist_key_field(i, hist_data) {
3107 key_field = hist_data->fields[i];
3109 if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
3110 elt_data->comm = kzalloc(size, GFP_KERNEL);
3111 if (!elt_data->comm) {
3119 n_str = hist_data->n_field_var_str + hist_data->n_save_var_str;
3121 size = STR_VAR_LEN_MAX;
3123 for (i = 0; i < n_str; i++) {
3124 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
3125 if (!elt_data->field_var_str[i]) {
3126 hist_elt_data_free(elt_data);
3131 elt->private_data = elt_data;
3136 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
3138 struct hist_elt_data *elt_data = elt->private_data;
3141 save_comm(elt_data->comm, current);
3144 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
3145 .elt_alloc = hist_trigger_elt_data_alloc,
3146 .elt_free = hist_trigger_elt_data_free,
3147 .elt_init = hist_trigger_elt_data_init,
3150 static const char *get_hist_field_flags(struct hist_field *hist_field)
3152 const char *flags_str = NULL;
3154 if (hist_field->flags & HIST_FIELD_FL_HEX)
3156 else if (hist_field->flags & HIST_FIELD_FL_SYM)
3158 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
3159 flags_str = "sym-offset";
3160 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
3161 flags_str = "execname";
3162 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
3163 flags_str = "syscall";
3164 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
3166 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
3167 flags_str = "usecs";
3172 static void expr_field_str(struct hist_field *field, char *expr)
3174 if (field->flags & HIST_FIELD_FL_VAR_REF)
3177 strcat(expr, hist_field_name(field, 0));
3179 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
3180 const char *flags_str = get_hist_field_flags(field);
3184 strcat(expr, flags_str);
3189 static char *expr_str(struct hist_field *field, unsigned int level)
3196 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3200 if (!field->operands[0]) {
3201 expr_field_str(field, expr);
3205 if (field->operator == FIELD_OP_UNARY_MINUS) {
3209 subexpr = expr_str(field->operands[0], ++level);
3214 strcat(expr, subexpr);
3222 expr_field_str(field->operands[0], expr);
3224 switch (field->operator) {
3225 case FIELD_OP_MINUS:
3236 expr_field_str(field->operands[1], expr);
3241 static int contains_operator(char *str)
3243 enum field_op_id field_op = FIELD_OP_NONE;
3246 op = strpbrk(str, "+-");
3248 return FIELD_OP_NONE;
3253 field_op = FIELD_OP_UNARY_MINUS;
3255 field_op = FIELD_OP_MINUS;
3258 field_op = FIELD_OP_PLUS;
3267 static void __destroy_hist_field(struct hist_field *hist_field)
3269 kfree(hist_field->var.name);
3270 kfree(hist_field->name);
3271 kfree(hist_field->type);
3276 static void destroy_hist_field(struct hist_field *hist_field,
3287 if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
3288 return; /* var refs will be destroyed separately */
3290 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
3291 destroy_hist_field(hist_field->operands[i], level + 1);
3293 __destroy_hist_field(hist_field);
3296 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
3297 struct ftrace_event_field *field,
3298 unsigned long flags,
3301 struct hist_field *hist_field;
3303 if (field && is_function_field(field))
3306 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3310 hist_field->hist_data = hist_data;
3312 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
3313 goto out; /* caller will populate */
3315 if (flags & HIST_FIELD_FL_VAR_REF) {
3316 hist_field->fn = hist_field_var_ref;
3320 if (flags & HIST_FIELD_FL_HITCOUNT) {
3321 hist_field->fn = hist_field_counter;
3322 hist_field->size = sizeof(u64);
3323 hist_field->type = kstrdup("u64", GFP_KERNEL);
3324 if (!hist_field->type)
3329 if (flags & HIST_FIELD_FL_STACKTRACE) {
3330 hist_field->fn = hist_field_none;
3334 if (flags & HIST_FIELD_FL_LOG2) {
3335 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2;
3336 hist_field->fn = hist_field_log2;
3337 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
3338 hist_field->size = hist_field->operands[0]->size;
3339 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL);
3340 if (!hist_field->type)
3345 if (flags & HIST_FIELD_FL_TIMESTAMP) {
3346 hist_field->fn = hist_field_timestamp;
3347 hist_field->size = sizeof(u64);
3348 hist_field->type = kstrdup("u64", GFP_KERNEL);
3349 if (!hist_field->type)
3354 if (flags & HIST_FIELD_FL_CPU) {
3355 hist_field->fn = hist_field_cpu;
3356 hist_field->size = sizeof(int);
3357 hist_field->type = kstrdup("unsigned int", GFP_KERNEL);
3358 if (!hist_field->type)
3363 if (WARN_ON_ONCE(!field))
3366 if (is_string_field(field)) {
3367 flags |= HIST_FIELD_FL_STRING;
3369 hist_field->size = MAX_FILTER_STR_VAL;
3370 hist_field->type = kstrdup(field->type, GFP_KERNEL);
3371 if (!hist_field->type)
3374 if (field->filter_type == FILTER_STATIC_STRING)
3375 hist_field->fn = hist_field_string;
3376 else if (field->filter_type == FILTER_DYN_STRING)
3377 hist_field->fn = hist_field_dynstring;
3379 hist_field->fn = hist_field_pstring;
3381 hist_field->size = field->size;
3382 hist_field->is_signed = field->is_signed;
3383 hist_field->type = kstrdup(field->type, GFP_KERNEL);
3384 if (!hist_field->type)
3387 hist_field->fn = select_value_fn(field->size,
3389 if (!hist_field->fn) {
3390 destroy_hist_field(hist_field, 0);
3395 hist_field->field = field;
3396 hist_field->flags = flags;
3399 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
3400 if (!hist_field->var.name)
3406 destroy_hist_field(hist_field, 0);
3410 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
3414 for (i = 0; i < HIST_FIELDS_MAX; i++) {
3415 if (hist_data->fields[i]) {
3416 destroy_hist_field(hist_data->fields[i], 0);
3417 hist_data->fields[i] = NULL;
3421 for (i = 0; i < hist_data->n_var_refs; i++) {
3422 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
3423 __destroy_hist_field(hist_data->var_refs[i]);
3424 hist_data->var_refs[i] = NULL;
3428 static int init_var_ref(struct hist_field *ref_field,
3429 struct hist_field *var_field,
3430 char *system, char *event_name)
3434 ref_field->var.idx = var_field->var.idx;
3435 ref_field->var.hist_data = var_field->hist_data;
3436 ref_field->size = var_field->size;
3437 ref_field->is_signed = var_field->is_signed;
3438 ref_field->flags |= var_field->flags &
3439 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
3442 ref_field->system = kstrdup(system, GFP_KERNEL);
3443 if (!ref_field->system)
3448 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
3449 if (!ref_field->event_name) {
3455 if (var_field->var.name) {
3456 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
3457 if (!ref_field->name) {
3461 } else if (var_field->name) {
3462 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
3463 if (!ref_field->name) {
3469 ref_field->type = kstrdup(var_field->type, GFP_KERNEL);
3470 if (!ref_field->type) {
3477 kfree(ref_field->system);
3478 kfree(ref_field->event_name);
3479 kfree(ref_field->name);
3484 static int find_var_ref_idx(struct hist_trigger_data *hist_data,
3485 struct hist_field *var_field)
3487 struct hist_field *ref_field;
3490 for (i = 0; i < hist_data->n_var_refs; i++) {
3491 ref_field = hist_data->var_refs[i];
3492 if (ref_field->var.idx == var_field->var.idx &&
3493 ref_field->var.hist_data == var_field->hist_data)
3501 * create_var_ref - Create a variable reference and attach it to trigger
3502 * @hist_data: The trigger that will be referencing the variable
3503 * @var_field: The VAR field to create a reference to
3504 * @system: The optional system string
3505 * @event_name: The optional event_name string
3507 * Given a variable hist_field, create a VAR_REF hist_field that
3508 * represents a reference to it.
3510 * This function also adds the reference to the trigger that
3511 * now references the variable.
3513 * Return: The VAR_REF field if successful, NULL if not
3515 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
3516 struct hist_field *var_field,
3517 char *system, char *event_name)
3519 unsigned long flags = HIST_FIELD_FL_VAR_REF;
3520 struct hist_field *ref_field;
3522 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
3524 if (init_var_ref(ref_field, var_field, system, event_name)) {
3525 destroy_hist_field(ref_field, 0);
3529 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
3530 ref_field->var_ref_idx = hist_data->n_var_refs++;
3536 static bool is_var_ref(char *var_name)
3538 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
3544 static char *field_name_from_var(struct hist_trigger_data *hist_data,
3550 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
3551 name = hist_data->attrs->var_defs.name[i];
3553 if (strcmp(var_name, name) == 0) {
3554 field = hist_data->attrs->var_defs.expr[i];
3555 if (contains_operator(field) || is_var_ref(field))
3564 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
3565 char *system, char *event_name,
3568 struct trace_event_call *call;
3570 if (system && event_name) {
3571 call = hist_data->event_file->event_call;
3573 if (strcmp(system, call->class->system) != 0)
3576 if (strcmp(event_name, trace_event_name(call)) != 0)
3580 if (!!system != !!event_name)
3583 if (!is_var_ref(var_name))
3588 return field_name_from_var(hist_data, var_name);
3591 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
3592 char *system, char *event_name,
3595 struct hist_field *var_field = NULL, *ref_field = NULL;
3596 struct trace_array *tr = hist_data->event_file->tr;
3598 if (!is_var_ref(var_name))
3603 var_field = find_event_var(hist_data, system, event_name, var_name);
3605 ref_field = create_var_ref(hist_data, var_field,
3606 system, event_name);
3609 hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name));
3614 static struct ftrace_event_field *
3615 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
3616 char *field_str, unsigned long *flags)
3618 struct ftrace_event_field *field = NULL;
3619 char *field_name, *modifier, *str;
3620 struct trace_array *tr = file->tr;
3622 modifier = str = kstrdup(field_str, GFP_KERNEL);
3624 return ERR_PTR(-ENOMEM);
3626 field_name = strsep(&modifier, ".");
3628 if (strcmp(modifier, "hex") == 0)
3629 *flags |= HIST_FIELD_FL_HEX;
3630 else if (strcmp(modifier, "sym") == 0)
3631 *flags |= HIST_FIELD_FL_SYM;
3632 else if (strcmp(modifier, "sym-offset") == 0)
3633 *flags |= HIST_FIELD_FL_SYM_OFFSET;
3634 else if ((strcmp(modifier, "execname") == 0) &&
3635 (strcmp(field_name, "common_pid") == 0))
3636 *flags |= HIST_FIELD_FL_EXECNAME;
3637 else if (strcmp(modifier, "syscall") == 0)
3638 *flags |= HIST_FIELD_FL_SYSCALL;
3639 else if (strcmp(modifier, "log2") == 0)
3640 *flags |= HIST_FIELD_FL_LOG2;
3641 else if (strcmp(modifier, "usecs") == 0)
3642 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
3644 hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier));
3645 field = ERR_PTR(-EINVAL);
3650 if (strcmp(field_name, "common_timestamp") == 0) {
3651 *flags |= HIST_FIELD_FL_TIMESTAMP;
3652 hist_data->enable_timestamps = true;
3653 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
3654 hist_data->attrs->ts_in_usecs = true;
3655 } else if (strcmp(field_name, "cpu") == 0)
3656 *flags |= HIST_FIELD_FL_CPU;
3658 field = trace_find_event_field(file->event_call, field_name);
3659 if (!field || !field->size) {
3660 hist_err(tr, HIST_ERR_FIELD_NOT_FOUND, errpos(field_name));
3661 field = ERR_PTR(-EINVAL);
3671 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
3672 struct hist_field *var_ref,
3675 struct hist_field *alias = NULL;
3676 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
3678 alias = create_hist_field(hist_data, NULL, flags, var_name);
3682 alias->fn = var_ref->fn;
3683 alias->operands[0] = var_ref;
3685 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
3686 destroy_hist_field(alias, 0);
3690 alias->var_ref_idx = var_ref->var_ref_idx;
3695 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
3696 struct trace_event_file *file, char *str,
3697 unsigned long *flags, char *var_name)
3699 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
3700 struct ftrace_event_field *field = NULL;
3701 struct hist_field *hist_field = NULL;
3704 s = strchr(str, '.');
3706 s = strchr(++s, '.');
3708 ref_system = strsep(&str, ".");
3713 ref_event = strsep(&str, ".");
3722 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
3724 hist_field = parse_var_ref(hist_data, ref_system,
3725 ref_event, ref_var);
3728 hist_field = create_alias(hist_data, hist_field, var_name);
3739 field = parse_field(hist_data, file, str, flags);
3740 if (IS_ERR(field)) {
3741 ret = PTR_ERR(field);
3745 hist_field = create_hist_field(hist_data, field, *flags, var_name);
3753 return ERR_PTR(ret);
3756 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
3757 struct trace_event_file *file,
3758 char *str, unsigned long flags,
3759 char *var_name, unsigned int level);
3761 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
3762 struct trace_event_file *file,
3763 char *str, unsigned long flags,
3764 char *var_name, unsigned int level)
3766 struct hist_field *operand1, *expr = NULL;
3767 unsigned long operand_flags;
3771 /* we support only -(xxx) i.e. explicit parens required */
3774 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
3779 str++; /* skip leading '-' */
3781 s = strchr(str, '(');
3789 s = strrchr(str, ')');
3793 ret = -EINVAL; /* no closing ')' */
3797 flags |= HIST_FIELD_FL_EXPR;
3798 expr = create_hist_field(hist_data, NULL, flags, var_name);
3805 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
3806 if (IS_ERR(operand1)) {
3807 ret = PTR_ERR(operand1);
3811 expr->flags |= operand1->flags &
3812 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
3813 expr->fn = hist_field_unary_minus;
3814 expr->operands[0] = operand1;
3815 expr->operator = FIELD_OP_UNARY_MINUS;
3816 expr->name = expr_str(expr, 0);
3817 expr->type = kstrdup(operand1->type, GFP_KERNEL);
3825 destroy_hist_field(expr, 0);
3826 return ERR_PTR(ret);
3829 static int check_expr_operands(struct trace_array *tr,
3830 struct hist_field *operand1,
3831 struct hist_field *operand2)
3833 unsigned long operand1_flags = operand1->flags;
3834 unsigned long operand2_flags = operand2->flags;
3836 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
3837 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
3838 struct hist_field *var;
3840 var = find_var_field(operand1->var.hist_data, operand1->name);
3843 operand1_flags = var->flags;
3846 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
3847 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
3848 struct hist_field *var;
3850 var = find_var_field(operand2->var.hist_data, operand2->name);
3853 operand2_flags = var->flags;
3856 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
3857 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
3858 hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0);
3865 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
3866 struct trace_event_file *file,
3867 char *str, unsigned long flags,
3868 char *var_name, unsigned int level)
3870 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
3871 unsigned long operand_flags;
3872 int field_op, ret = -EINVAL;
3873 char *sep, *operand1_str;
3876 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
3877 return ERR_PTR(-EINVAL);
3880 field_op = contains_operator(str);
3882 if (field_op == FIELD_OP_NONE)
3883 return parse_atom(hist_data, file, str, &flags, var_name);
3885 if (field_op == FIELD_OP_UNARY_MINUS)
3886 return parse_unary(hist_data, file, str, flags, var_name, ++level);
3889 case FIELD_OP_MINUS:
3899 operand1_str = strsep(&str, sep);
3900 if (!operand1_str || !str)
3904 operand1 = parse_atom(hist_data, file, operand1_str,
3905 &operand_flags, NULL);
3906 if (IS_ERR(operand1)) {
3907 ret = PTR_ERR(operand1);
3912 /* rest of string could be another expression e.g. b+c in a+b+c */
3914 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
3915 if (IS_ERR(operand2)) {
3916 ret = PTR_ERR(operand2);
3921 ret = check_expr_operands(file->tr, operand1, operand2);
3925 flags |= HIST_FIELD_FL_EXPR;
3927 flags |= operand1->flags &
3928 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
3930 expr = create_hist_field(hist_data, NULL, flags, var_name);
3936 operand1->read_once = true;
3937 operand2->read_once = true;
3939 expr->operands[0] = operand1;
3940 expr->operands[1] = operand2;
3941 expr->operator = field_op;
3942 expr->name = expr_str(expr, 0);
3943 expr->type = kstrdup(operand1->type, GFP_KERNEL);
3950 case FIELD_OP_MINUS:
3951 expr->fn = hist_field_minus;
3954 expr->fn = hist_field_plus;
3963 destroy_hist_field(operand1, 0);
3964 destroy_hist_field(operand2, 0);
3965 destroy_hist_field(expr, 0);
3967 return ERR_PTR(ret);
3970 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
3971 struct trace_event_file *file)
3973 struct event_trigger_data *test;
3975 lockdep_assert_held(&event_mutex);
3977 list_for_each_entry(test, &file->triggers, list) {
3978 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
3979 if (test->private_data == hist_data)
3980 return test->filter_str;
3987 static struct event_command trigger_hist_cmd;
3988 static int event_hist_trigger_func(struct event_command *cmd_ops,
3989 struct trace_event_file *file,
3990 char *glob, char *cmd, char *param);
3992 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
3993 struct hist_trigger_data *hist_data,
3994 unsigned int n_keys)
3996 struct hist_field *target_hist_field, *hist_field;
3997 unsigned int n, i, j;
3999 if (hist_data->n_fields - hist_data->n_vals != n_keys)
4002 i = hist_data->n_vals;
4003 j = target_hist_data->n_vals;
4005 for (n = 0; n < n_keys; n++) {
4006 hist_field = hist_data->fields[i + n];
4007 target_hist_field = target_hist_data->fields[j + n];
4009 if (strcmp(hist_field->type, target_hist_field->type) != 0)
4011 if (hist_field->size != target_hist_field->size)
4013 if (hist_field->is_signed != target_hist_field->is_signed)
4020 static struct hist_trigger_data *
4021 find_compatible_hist(struct hist_trigger_data *target_hist_data,
4022 struct trace_event_file *file)
4024 struct hist_trigger_data *hist_data;
4025 struct event_trigger_data *test;
4026 unsigned int n_keys;
4028 lockdep_assert_held(&event_mutex);
4030 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
4032 list_for_each_entry(test, &file->triggers, list) {
4033 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
4034 hist_data = test->private_data;
4036 if (compatible_keys(target_hist_data, hist_data, n_keys))
4044 static struct trace_event_file *event_file(struct trace_array *tr,
4045 char *system, char *event_name)
4047 struct trace_event_file *file;
4049 file = __find_event_file(tr, system, event_name);
4051 return ERR_PTR(-EINVAL);
4056 static struct hist_field *
4057 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
4058 char *system, char *event_name, char *field_name)
4060 struct hist_field *event_var;
4061 char *synthetic_name;
4063 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
4064 if (!synthetic_name)
4065 return ERR_PTR(-ENOMEM);
4067 strcpy(synthetic_name, "synthetic_");
4068 strcat(synthetic_name, field_name);
4070 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
4072 kfree(synthetic_name);
4078 * create_field_var_hist - Automatically create a histogram and var for a field
4079 * @target_hist_data: The target hist trigger
4080 * @subsys_name: Optional subsystem name
4081 * @event_name: Optional event name
4082 * @field_name: The name of the field (and the resulting variable)
4084 * Hist trigger actions fetch data from variables, not directly from
4085 * events. However, for convenience, users are allowed to directly
4086 * specify an event field in an action, which will be automatically
4087 * converted into a variable on their behalf.
4089 * If a user specifies a field on an event that isn't the event the
4090 * histogram currently being defined (the target event histogram), the
4091 * only way that can be accomplished is if a new hist trigger is
4092 * created and the field variable defined on that.
4094 * This function creates a new histogram compatible with the target
4095 * event (meaning a histogram with the same key as the target
4096 * histogram), and creates a variable for the specified field, but
4097 * with 'synthetic_' prepended to the variable name in order to avoid
4098 * collision with normal field variables.
4100 * Return: The variable created for the field.
4102 static struct hist_field *
4103 create_field_var_hist(struct hist_trigger_data *target_hist_data,
4104 char *subsys_name, char *event_name, char *field_name)
4106 struct trace_array *tr = target_hist_data->event_file->tr;
4107 struct hist_field *event_var = ERR_PTR(-EINVAL);
4108 struct hist_trigger_data *hist_data;
4109 unsigned int i, n, first = true;
4110 struct field_var_hist *var_hist;
4111 struct trace_event_file *file;
4112 struct hist_field *key_field;
4117 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
4118 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
4119 return ERR_PTR(-EINVAL);
4122 file = event_file(tr, subsys_name, event_name);
4125 hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name));
4126 ret = PTR_ERR(file);
4127 return ERR_PTR(ret);
4131 * Look for a histogram compatible with target. We'll use the
4132 * found histogram specification to create a new matching
4133 * histogram with our variable on it. target_hist_data is not
4134 * yet a registered histogram so we can't use that.
4136 hist_data = find_compatible_hist(target_hist_data, file);
4138 hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name));
4139 return ERR_PTR(-EINVAL);
4142 /* See if a synthetic field variable has already been created */
4143 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
4144 event_name, field_name);
4145 if (!IS_ERR_OR_NULL(event_var))
4148 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
4150 return ERR_PTR(-ENOMEM);
4152 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
4155 return ERR_PTR(-ENOMEM);
4158 /* Use the same keys as the compatible histogram */
4159 strcat(cmd, "keys=");
4161 for_each_hist_key_field(i, hist_data) {
4162 key_field = hist_data->fields[i];
4165 strcat(cmd, key_field->field->name);
4169 /* Create the synthetic field variable specification */
4170 strcat(cmd, ":synthetic_");
4171 strcat(cmd, field_name);
4173 strcat(cmd, field_name);
4175 /* Use the same filter as the compatible histogram */
4176 saved_filter = find_trigger_filter(hist_data, file);
4178 strcat(cmd, " if ");
4179 strcat(cmd, saved_filter);
4182 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
4183 if (!var_hist->cmd) {
4186 return ERR_PTR(-ENOMEM);
4189 /* Save the compatible histogram information */
4190 var_hist->hist_data = hist_data;
4192 /* Create the new histogram with our variable */
4193 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
4197 kfree(var_hist->cmd);
4199 hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name));
4200 return ERR_PTR(ret);
4205 /* If we can't find the variable, something went wrong */
4206 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
4207 event_name, field_name);
4208 if (IS_ERR_OR_NULL(event_var)) {
4209 kfree(var_hist->cmd);
4211 hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
4212 return ERR_PTR(-EINVAL);
4215 n = target_hist_data->n_field_var_hists;
4216 target_hist_data->field_var_hists[n] = var_hist;
4217 target_hist_data->n_field_var_hists++;
4222 static struct hist_field *
4223 find_target_event_var(struct hist_trigger_data *hist_data,
4224 char *subsys_name, char *event_name, char *var_name)
4226 struct trace_event_file *file = hist_data->event_file;
4227 struct hist_field *hist_field = NULL;
4230 struct trace_event_call *call;
4235 call = file->event_call;
4237 if (strcmp(subsys_name, call->class->system) != 0)
4240 if (strcmp(event_name, trace_event_name(call)) != 0)
4244 hist_field = find_var_field(hist_data, var_name);
4249 static inline void __update_field_vars(struct tracing_map_elt *elt,
4250 struct ring_buffer_event *rbe,
4252 struct field_var **field_vars,
4253 unsigned int n_field_vars,
4254 unsigned int field_var_str_start)
4256 struct hist_elt_data *elt_data = elt->private_data;
4257 unsigned int i, j, var_idx;
4260 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
4261 struct field_var *field_var = field_vars[i];
4262 struct hist_field *var = field_var->var;
4263 struct hist_field *val = field_var->val;
4265 var_val = val->fn(val, elt, rbe, rec);
4266 var_idx = var->var.idx;
4268 if (val->flags & HIST_FIELD_FL_STRING) {
4269 char *str = elt_data->field_var_str[j++];
4270 char *val_str = (char *)(uintptr_t)var_val;
4272 strscpy(str, val_str, STR_VAR_LEN_MAX);
4273 var_val = (u64)(uintptr_t)str;
4275 tracing_map_set_var(elt, var_idx, var_val);
4279 static void update_field_vars(struct hist_trigger_data *hist_data,
4280 struct tracing_map_elt *elt,
4281 struct ring_buffer_event *rbe,
4284 __update_field_vars(elt, rbe, rec, hist_data->field_vars,
4285 hist_data->n_field_vars, 0);
4288 static void save_track_data_vars(struct hist_trigger_data *hist_data,
4289 struct tracing_map_elt *elt, void *rec,
4290 struct ring_buffer_event *rbe, void *key,
4291 struct action_data *data, u64 *var_ref_vals)
4293 __update_field_vars(elt, rbe, rec, hist_data->save_vars,
4294 hist_data->n_save_vars, hist_data->n_field_var_str);
4297 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
4298 struct trace_event_file *file,
4299 char *name, int size, const char *type)
4301 struct hist_field *var;
4304 if (find_var(hist_data, file, name) && !hist_data->remove) {
4305 var = ERR_PTR(-EINVAL);
4309 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
4311 var = ERR_PTR(-ENOMEM);
4315 idx = tracing_map_add_var(hist_data->map);
4318 var = ERR_PTR(-EINVAL);
4322 var->flags = HIST_FIELD_FL_VAR;
4324 var->var.hist_data = var->hist_data = hist_data;
4326 var->var.name = kstrdup(name, GFP_KERNEL);
4327 var->type = kstrdup(type, GFP_KERNEL);
4328 if (!var->var.name || !var->type) {
4329 kfree(var->var.name);
4332 var = ERR_PTR(-ENOMEM);
4338 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
4339 struct trace_event_file *file,
4342 struct hist_field *val = NULL, *var = NULL;
4343 unsigned long flags = HIST_FIELD_FL_VAR;
4344 struct trace_array *tr = file->tr;
4345 struct field_var *field_var;
4348 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
4349 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
4354 val = parse_atom(hist_data, file, field_name, &flags, NULL);
4356 hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name));
4361 var = create_var(hist_data, file, field_name, val->size, val->type);
4363 hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name));
4369 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
4377 field_var->var = var;
4378 field_var->val = val;
4382 field_var = ERR_PTR(ret);
4387 * create_target_field_var - Automatically create a variable for a field
4388 * @target_hist_data: The target hist trigger
4389 * @subsys_name: Optional subsystem name
4390 * @event_name: Optional event name
4391 * @var_name: The name of the field (and the resulting variable)
4393 * Hist trigger actions fetch data from variables, not directly from
4394 * events. However, for convenience, users are allowed to directly
4395 * specify an event field in an action, which will be automatically
4396 * converted into a variable on their behalf.
4398 * This function creates a field variable with the name var_name on
4399 * the hist trigger currently being defined on the target event. If
4400 * subsys_name and event_name are specified, this function simply
4401 * verifies that they do in fact match the target event subsystem and
4404 * Return: The variable created for the field.
4406 static struct field_var *
4407 create_target_field_var(struct hist_trigger_data *target_hist_data,
4408 char *subsys_name, char *event_name, char *var_name)
4410 struct trace_event_file *file = target_hist_data->event_file;
4413 struct trace_event_call *call;
4418 call = file->event_call;
4420 if (strcmp(subsys_name, call->class->system) != 0)
4423 if (strcmp(event_name, trace_event_name(call)) != 0)
4427 return create_field_var(target_hist_data, file, var_name);
4430 static bool check_track_val_max(u64 track_val, u64 var_val)
4432 if (var_val <= track_val)
4438 static bool check_track_val_changed(u64 track_val, u64 var_val)
4440 if (var_val == track_val)
4446 static u64 get_track_val(struct hist_trigger_data *hist_data,
4447 struct tracing_map_elt *elt,
4448 struct action_data *data)
4450 unsigned int track_var_idx = data->track_data.track_var->var.idx;
4453 track_val = tracing_map_read_var(elt, track_var_idx);
4458 static void save_track_val(struct hist_trigger_data *hist_data,
4459 struct tracing_map_elt *elt,
4460 struct action_data *data, u64 var_val)
4462 unsigned int track_var_idx = data->track_data.track_var->var.idx;
4464 tracing_map_set_var(elt, track_var_idx, var_val);
4467 static void save_track_data(struct hist_trigger_data *hist_data,
4468 struct tracing_map_elt *elt, void *rec,
4469 struct ring_buffer_event *rbe, void *key,
4470 struct action_data *data, u64 *var_ref_vals)
4472 if (data->track_data.save_data)
4473 data->track_data.save_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
4476 static bool check_track_val(struct tracing_map_elt *elt,
4477 struct action_data *data,
4480 struct hist_trigger_data *hist_data;
4483 hist_data = data->track_data.track_var->hist_data;
4484 track_val = get_track_val(hist_data, elt, data);
4486 return data->track_data.check_val(track_val, var_val);
4489 #ifdef CONFIG_TRACER_SNAPSHOT
4490 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
4492 /* called with tr->max_lock held */
4493 struct track_data *track_data = tr->cond_snapshot->cond_data;
4494 struct hist_elt_data *elt_data, *track_elt_data;
4495 struct snapshot_context *context = cond_data;
4496 struct action_data *action;
4502 action = track_data->action_data;
4504 track_val = get_track_val(track_data->hist_data, context->elt,
4505 track_data->action_data);
4507 if (!action->track_data.check_val(track_data->track_val, track_val))
4510 track_data->track_val = track_val;
4511 memcpy(track_data->key, context->key, track_data->key_len);
4513 elt_data = context->elt->private_data;
4514 track_elt_data = track_data->elt.private_data;
4516 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
4518 track_data->updated = true;
4523 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
4524 struct tracing_map_elt *elt, void *rec,
4525 struct ring_buffer_event *rbe, void *key,
4526 struct action_data *data,
4529 struct trace_event_file *file = hist_data->event_file;
4530 struct snapshot_context context;
4535 tracing_snapshot_cond(file->tr, &context);
4538 static void hist_trigger_print_key(struct seq_file *m,
4539 struct hist_trigger_data *hist_data,
4541 struct tracing_map_elt *elt);
4543 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
4547 if (!hist_data->n_actions)
4550 for (i = 0; i < hist_data->n_actions; i++) {
4551 struct action_data *data = hist_data->actions[i];
4553 if (data->action == ACTION_SNAPSHOT)
4560 static void track_data_snapshot_print(struct seq_file *m,
4561 struct hist_trigger_data *hist_data)
4563 struct trace_event_file *file = hist_data->event_file;
4564 struct track_data *track_data;
4565 struct action_data *action;
4567 track_data = tracing_cond_snapshot_data(file->tr);
4571 if (!track_data->updated)
4574 action = snapshot_action(hist_data);
4578 seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n");
4579 seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
4580 action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
4581 action->track_data.var_str, track_data->track_val);
4583 seq_puts(m, "\ttriggered by event with key: ");
4584 hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
4588 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
4592 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
4593 struct tracing_map_elt *elt, void *rec,
4594 struct ring_buffer_event *rbe, void *key,
4595 struct action_data *data,
4596 u64 *var_ref_vals) {}
4597 static void track_data_snapshot_print(struct seq_file *m,
4598 struct hist_trigger_data *hist_data) {}
4599 #endif /* CONFIG_TRACER_SNAPSHOT */
4601 static void track_data_print(struct seq_file *m,
4602 struct hist_trigger_data *hist_data,
4603 struct tracing_map_elt *elt,
4604 struct action_data *data)
4606 u64 track_val = get_track_val(hist_data, elt, data);
4607 unsigned int i, save_var_idx;
4609 if (data->handler == HANDLER_ONMAX)
4610 seq_printf(m, "\n\tmax: %10llu", track_val);
4611 else if (data->handler == HANDLER_ONCHANGE)
4612 seq_printf(m, "\n\tchanged: %10llu", track_val);
4614 if (data->action == ACTION_SNAPSHOT)
4617 for (i = 0; i < hist_data->n_save_vars; i++) {
4618 struct hist_field *save_val = hist_data->save_vars[i]->val;
4619 struct hist_field *save_var = hist_data->save_vars[i]->var;
4622 save_var_idx = save_var->var.idx;
4624 val = tracing_map_read_var(elt, save_var_idx);
4626 if (save_val->flags & HIST_FIELD_FL_STRING) {
4627 seq_printf(m, " %s: %-32s", save_var->var.name,
4628 (char *)(uintptr_t)(val));
4630 seq_printf(m, " %s: %10llu", save_var->var.name, val);
4634 static void ontrack_action(struct hist_trigger_data *hist_data,
4635 struct tracing_map_elt *elt, void *rec,
4636 struct ring_buffer_event *rbe, void *key,
4637 struct action_data *data, u64 *var_ref_vals)
4639 u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
4641 if (check_track_val(elt, data, var_val)) {
4642 save_track_val(hist_data, elt, data, var_val);
4643 save_track_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
4647 static void action_data_destroy(struct action_data *data)
4651 lockdep_assert_held(&event_mutex);
4653 kfree(data->action_name);
4655 for (i = 0; i < data->n_params; i++)
4656 kfree(data->params[i]);
4658 if (data->synth_event)
4659 data->synth_event->ref--;
4661 kfree(data->synth_event_name);
4666 static void track_data_destroy(struct hist_trigger_data *hist_data,
4667 struct action_data *data)
4669 struct trace_event_file *file = hist_data->event_file;
4671 destroy_hist_field(data->track_data.track_var, 0);
4673 if (data->action == ACTION_SNAPSHOT) {
4674 struct track_data *track_data;
4676 track_data = tracing_cond_snapshot_data(file->tr);
4677 if (track_data && track_data->hist_data == hist_data) {
4678 tracing_snapshot_cond_disable(file->tr);
4679 track_data_free(track_data);
4683 kfree(data->track_data.var_str);
4685 action_data_destroy(data);
4688 static int action_create(struct hist_trigger_data *hist_data,
4689 struct action_data *data);
4691 static int track_data_create(struct hist_trigger_data *hist_data,
4692 struct action_data *data)
4694 struct hist_field *var_field, *ref_field, *track_var = NULL;
4695 struct trace_event_file *file = hist_data->event_file;
4696 struct trace_array *tr = file->tr;
4697 char *track_data_var_str;
4700 track_data_var_str = data->track_data.var_str;
4701 if (track_data_var_str[0] != '$') {
4702 hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str));
4705 track_data_var_str++;
4707 var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
4709 hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str));
4713 ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
4717 data->track_data.var_ref = ref_field;
4719 if (data->handler == HANDLER_ONMAX)
4720 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
4721 if (IS_ERR(track_var)) {
4722 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
4723 ret = PTR_ERR(track_var);
4727 if (data->handler == HANDLER_ONCHANGE)
4728 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
4729 if (IS_ERR(track_var)) {
4730 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
4731 ret = PTR_ERR(track_var);
4734 data->track_data.track_var = track_var;
4736 ret = action_create(hist_data, data);
4741 static int parse_action_params(struct trace_array *tr, char *params,
4742 struct action_data *data)
4744 char *param, *saved_param;
4745 bool first_param = true;
4749 if (data->n_params >= SYNTH_FIELDS_MAX) {
4750 hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0);
4754 param = strsep(¶ms, ",");
4756 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0);
4761 param = strstrip(param);
4762 if (strlen(param) < 2) {
4763 hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param));
4768 saved_param = kstrdup(param, GFP_KERNEL);
4774 if (first_param && data->use_trace_keyword) {
4775 data->synth_event_name = saved_param;
4776 first_param = false;
4779 first_param = false;
4781 data->params[data->n_params++] = saved_param;
4787 static int action_parse(struct trace_array *tr, char *str, struct action_data *data,
4788 enum handler_id handler)
4795 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
4800 action_name = strsep(&str, "(");
4801 if (!action_name || !str) {
4802 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
4807 if (str_has_prefix(action_name, "save")) {
4808 char *params = strsep(&str, ")");
4811 hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0);
4816 ret = parse_action_params(tr, params, data);
4820 if (handler == HANDLER_ONMAX)
4821 data->track_data.check_val = check_track_val_max;
4822 else if (handler == HANDLER_ONCHANGE)
4823 data->track_data.check_val = check_track_val_changed;
4825 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
4830 data->track_data.save_data = save_track_data_vars;
4831 data->fn = ontrack_action;
4832 data->action = ACTION_SAVE;
4833 } else if (str_has_prefix(action_name, "snapshot")) {
4834 char *params = strsep(&str, ")");
4837 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params));
4842 if (handler == HANDLER_ONMAX)
4843 data->track_data.check_val = check_track_val_max;
4844 else if (handler == HANDLER_ONCHANGE)
4845 data->track_data.check_val = check_track_val_changed;
4847 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
4852 data->track_data.save_data = save_track_data_snapshot;
4853 data->fn = ontrack_action;
4854 data->action = ACTION_SNAPSHOT;
4856 char *params = strsep(&str, ")");
4858 if (str_has_prefix(action_name, "trace"))
4859 data->use_trace_keyword = true;
4862 ret = parse_action_params(tr, params, data);
4867 if (handler == HANDLER_ONMAX)
4868 data->track_data.check_val = check_track_val_max;
4869 else if (handler == HANDLER_ONCHANGE)
4870 data->track_data.check_val = check_track_val_changed;
4872 if (handler != HANDLER_ONMATCH) {
4873 data->track_data.save_data = action_trace;
4874 data->fn = ontrack_action;
4876 data->fn = action_trace;
4878 data->action = ACTION_TRACE;
4881 data->action_name = kstrdup(action_name, GFP_KERNEL);
4882 if (!data->action_name) {
4887 data->handler = handler;
4892 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
4893 char *str, enum handler_id handler)
4895 struct action_data *data;
4899 data = kzalloc(sizeof(*data), GFP_KERNEL);
4901 return ERR_PTR(-ENOMEM);
4903 var_str = strsep(&str, ")");
4904 if (!var_str || !str) {
4909 data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
4910 if (!data->track_data.var_str) {
4915 ret = action_parse(hist_data->event_file->tr, str, data, handler);
4921 track_data_destroy(hist_data, data);
4922 data = ERR_PTR(ret);
4926 static void onmatch_destroy(struct action_data *data)
4928 kfree(data->match_data.event);
4929 kfree(data->match_data.event_system);
4931 action_data_destroy(data);
4934 static void destroy_field_var(struct field_var *field_var)
4939 destroy_hist_field(field_var->var, 0);
4940 destroy_hist_field(field_var->val, 0);
4945 static void destroy_field_vars(struct hist_trigger_data *hist_data)
4949 for (i = 0; i < hist_data->n_field_vars; i++)
4950 destroy_field_var(hist_data->field_vars[i]);
4953 static void save_field_var(struct hist_trigger_data *hist_data,
4954 struct field_var *field_var)
4956 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
4958 if (field_var->val->flags & HIST_FIELD_FL_STRING)
4959 hist_data->n_field_var_str++;
4963 static int check_synth_field(struct synth_event *event,
4964 struct hist_field *hist_field,
4965 unsigned int field_pos)
4967 struct synth_field *field;
4969 if (field_pos >= event->n_fields)
4972 field = event->fields[field_pos];
4974 if (strcmp(field->type, hist_field->type) != 0) {
4975 if (field->size != hist_field->size ||
4976 field->is_signed != hist_field->is_signed)
4983 static struct hist_field *
4984 trace_action_find_var(struct hist_trigger_data *hist_data,
4985 struct action_data *data,
4986 char *system, char *event, char *var)
4988 struct trace_array *tr = hist_data->event_file->tr;
4989 struct hist_field *hist_field;
4991 var++; /* skip '$' */
4993 hist_field = find_target_event_var(hist_data, system, event, var);
4995 if (!system && data->handler == HANDLER_ONMATCH) {
4996 system = data->match_data.event_system;
4997 event = data->match_data.event;
5000 hist_field = find_event_var(hist_data, system, event, var);
5004 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var));
5009 static struct hist_field *
5010 trace_action_create_field_var(struct hist_trigger_data *hist_data,
5011 struct action_data *data, char *system,
5012 char *event, char *var)
5014 struct hist_field *hist_field = NULL;
5015 struct field_var *field_var;
5018 * First try to create a field var on the target event (the
5019 * currently being defined). This will create a variable for
5020 * unqualified fields on the target event, or if qualified,
5021 * target fields that have qualified names matching the target.
5023 field_var = create_target_field_var(hist_data, system, event, var);
5025 if (field_var && !IS_ERR(field_var)) {
5026 save_field_var(hist_data, field_var);
5027 hist_field = field_var->var;
5031 * If no explicit system.event is specfied, default to
5032 * looking for fields on the onmatch(system.event.xxx)
5035 if (!system && data->handler == HANDLER_ONMATCH) {
5036 system = data->match_data.event_system;
5037 event = data->match_data.event;
5041 * At this point, we're looking at a field on another
5042 * event. Because we can't modify a hist trigger on
5043 * another event to add a variable for a field, we need
5044 * to create a new trigger on that event and create the
5045 * variable at the same time.
5047 hist_field = create_field_var_hist(hist_data, system, event, var);
5048 if (IS_ERR(hist_field))
5054 destroy_field_var(field_var);
5059 static int trace_action_create(struct hist_trigger_data *hist_data,
5060 struct action_data *data)
5062 struct trace_array *tr = hist_data->event_file->tr;
5063 char *event_name, *param, *system = NULL;
5064 struct hist_field *hist_field, *var_ref;
5066 unsigned int field_pos = 0;
5067 struct synth_event *event;
5068 char *synth_event_name;
5069 int var_ref_idx, ret = 0;
5071 lockdep_assert_held(&event_mutex);
5073 if (data->use_trace_keyword)
5074 synth_event_name = data->synth_event_name;
5076 synth_event_name = data->action_name;
5078 event = find_synth_event(synth_event_name);
5080 hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name));
5086 for (i = 0; i < data->n_params; i++) {
5089 p = param = kstrdup(data->params[i], GFP_KERNEL);
5095 system = strsep(¶m, ".");
5097 param = (char *)system;
5098 system = event_name = NULL;
5100 event_name = strsep(¶m, ".");
5108 if (param[0] == '$')
5109 hist_field = trace_action_find_var(hist_data, data,
5113 hist_field = trace_action_create_field_var(hist_data,
5125 if (check_synth_field(event, hist_field, field_pos) == 0) {
5126 var_ref = create_var_ref(hist_data, hist_field,
5127 system, event_name);
5134 var_ref_idx = find_var_ref_idx(hist_data, var_ref);
5135 if (WARN_ON(var_ref_idx < 0)) {
5140 data->var_ref_idx[i] = var_ref_idx;
5147 hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param));
5153 if (field_pos != event->n_fields) {
5154 hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name));
5159 data->synth_event = event;
5168 static int action_create(struct hist_trigger_data *hist_data,
5169 struct action_data *data)
5171 struct trace_event_file *file = hist_data->event_file;
5172 struct trace_array *tr = file->tr;
5173 struct track_data *track_data;
5174 struct field_var *field_var;
5179 if (data->action == ACTION_TRACE)
5180 return trace_action_create(hist_data, data);
5182 if (data->action == ACTION_SNAPSHOT) {
5183 track_data = track_data_alloc(hist_data->key_size, data, hist_data);
5184 if (IS_ERR(track_data)) {
5185 ret = PTR_ERR(track_data);
5189 ret = tracing_snapshot_cond_enable(file->tr, track_data,
5190 cond_snapshot_update);
5192 track_data_free(track_data);
5197 if (data->action == ACTION_SAVE) {
5198 if (hist_data->n_save_vars) {
5200 hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0);
5204 for (i = 0; i < data->n_params; i++) {
5205 param = kstrdup(data->params[i], GFP_KERNEL);
5211 field_var = create_target_field_var(hist_data, NULL, NULL, param);
5212 if (IS_ERR(field_var)) {
5213 hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL,
5215 ret = PTR_ERR(field_var);
5220 hist_data->save_vars[hist_data->n_save_vars++] = field_var;
5221 if (field_var->val->flags & HIST_FIELD_FL_STRING)
5222 hist_data->n_save_var_str++;
5230 static int onmatch_create(struct hist_trigger_data *hist_data,
5231 struct action_data *data)
5233 return action_create(hist_data, data);
5236 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
5238 char *match_event, *match_event_system;
5239 struct action_data *data;
5242 data = kzalloc(sizeof(*data), GFP_KERNEL);
5244 return ERR_PTR(-ENOMEM);
5246 match_event = strsep(&str, ")");
5247 if (!match_event || !str) {
5248 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event));
5252 match_event_system = strsep(&match_event, ".");
5254 hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system));
5258 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
5259 hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event));
5263 data->match_data.event = kstrdup(match_event, GFP_KERNEL);
5264 if (!data->match_data.event) {
5269 data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
5270 if (!data->match_data.event_system) {
5275 ret = action_parse(tr, str, data, HANDLER_ONMATCH);
5281 onmatch_destroy(data);
5282 data = ERR_PTR(ret);
5286 static int create_hitcount_val(struct hist_trigger_data *hist_data)
5288 hist_data->fields[HITCOUNT_IDX] =
5289 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
5290 if (!hist_data->fields[HITCOUNT_IDX])
5293 hist_data->n_vals++;
5294 hist_data->n_fields++;
5296 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
5302 static int __create_val_field(struct hist_trigger_data *hist_data,
5303 unsigned int val_idx,
5304 struct trace_event_file *file,
5305 char *var_name, char *field_str,
5306 unsigned long flags)
5308 struct hist_field *hist_field;
5311 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
5312 if (IS_ERR(hist_field)) {
5313 ret = PTR_ERR(hist_field);
5317 hist_data->fields[val_idx] = hist_field;
5319 ++hist_data->n_vals;
5320 ++hist_data->n_fields;
5322 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
5328 static int create_val_field(struct hist_trigger_data *hist_data,
5329 unsigned int val_idx,
5330 struct trace_event_file *file,
5333 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
5336 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
5339 static int create_var_field(struct hist_trigger_data *hist_data,
5340 unsigned int val_idx,
5341 struct trace_event_file *file,
5342 char *var_name, char *expr_str)
5344 struct trace_array *tr = hist_data->event_file->tr;
5345 unsigned long flags = 0;
5347 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
5350 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
5351 hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name));
5355 flags |= HIST_FIELD_FL_VAR;
5356 hist_data->n_vars++;
5357 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
5360 return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
5363 static int create_val_fields(struct hist_trigger_data *hist_data,
5364 struct trace_event_file *file)
5366 char *fields_str, *field_str;
5367 unsigned int i, j = 1;
5370 ret = create_hitcount_val(hist_data);
5374 fields_str = hist_data->attrs->vals_str;
5378 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
5379 j < TRACING_MAP_VALS_MAX; i++) {
5380 field_str = strsep(&fields_str, ",");
5384 if (strcmp(field_str, "hitcount") == 0)
5387 ret = create_val_field(hist_data, j++, file, field_str);
5392 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
5398 static int create_key_field(struct hist_trigger_data *hist_data,
5399 unsigned int key_idx,
5400 unsigned int key_offset,
5401 struct trace_event_file *file,
5404 struct trace_array *tr = hist_data->event_file->tr;
5405 struct hist_field *hist_field = NULL;
5406 unsigned long flags = 0;
5407 unsigned int key_size;
5410 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
5413 flags |= HIST_FIELD_FL_KEY;
5415 if (strcmp(field_str, "stacktrace") == 0) {
5416 flags |= HIST_FIELD_FL_STACKTRACE;
5417 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
5418 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
5420 hist_field = parse_expr(hist_data, file, field_str, flags,
5422 if (IS_ERR(hist_field)) {
5423 ret = PTR_ERR(hist_field);
5427 if (field_has_hist_vars(hist_field, 0)) {
5428 hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str));
5429 destroy_hist_field(hist_field, 0);
5434 key_size = hist_field->size;
5437 hist_data->fields[key_idx] = hist_field;
5439 key_size = ALIGN(key_size, sizeof(u64));
5440 hist_data->fields[key_idx]->size = key_size;
5441 hist_data->fields[key_idx]->offset = key_offset;
5443 hist_data->key_size += key_size;
5445 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
5450 hist_data->n_keys++;
5451 hist_data->n_fields++;
5453 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
5461 static int create_key_fields(struct hist_trigger_data *hist_data,
5462 struct trace_event_file *file)
5464 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
5465 char *fields_str, *field_str;
5468 fields_str = hist_data->attrs->keys_str;
5472 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
5473 field_str = strsep(&fields_str, ",");
5476 ret = create_key_field(hist_data, i, key_offset,
5491 static int create_var_fields(struct hist_trigger_data *hist_data,
5492 struct trace_event_file *file)
5494 unsigned int i, j = hist_data->n_vals;
5497 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
5499 for (i = 0; i < n_vars; i++) {
5500 char *var_name = hist_data->attrs->var_defs.name[i];
5501 char *expr = hist_data->attrs->var_defs.expr[i];
5503 ret = create_var_field(hist_data, j++, file, var_name, expr);
5511 static void free_var_defs(struct hist_trigger_data *hist_data)
5515 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
5516 kfree(hist_data->attrs->var_defs.name[i]);
5517 kfree(hist_data->attrs->var_defs.expr[i]);
5520 hist_data->attrs->var_defs.n_vars = 0;
5523 static int parse_var_defs(struct hist_trigger_data *hist_data)
5525 struct trace_array *tr = hist_data->event_file->tr;
5526 char *s, *str, *var_name, *field_str;
5527 unsigned int i, j, n_vars = 0;
5530 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
5531 str = hist_data->attrs->assignment_str[i];
5532 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
5533 field_str = strsep(&str, ",");
5537 var_name = strsep(&field_str, "=");
5538 if (!var_name || !field_str) {
5539 hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT,
5545 if (n_vars == TRACING_MAP_VARS_MAX) {
5546 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name));
5551 s = kstrdup(var_name, GFP_KERNEL);
5556 hist_data->attrs->var_defs.name[n_vars] = s;
5558 s = kstrdup(field_str, GFP_KERNEL);
5560 kfree(hist_data->attrs->var_defs.name[n_vars]);
5564 hist_data->attrs->var_defs.expr[n_vars++] = s;
5566 hist_data->attrs->var_defs.n_vars = n_vars;
5572 free_var_defs(hist_data);
5577 static int create_hist_fields(struct hist_trigger_data *hist_data,
5578 struct trace_event_file *file)
5582 ret = parse_var_defs(hist_data);
5586 ret = create_val_fields(hist_data, file);
5590 ret = create_var_fields(hist_data, file);
5594 ret = create_key_fields(hist_data, file);
5598 free_var_defs(hist_data);
5603 static int is_descending(struct trace_array *tr, const char *str)
5608 if (strcmp(str, "descending") == 0)
5611 if (strcmp(str, "ascending") == 0)
5614 hist_err(tr, HIST_ERR_INVALID_SORT_MODIFIER, errpos((char *)str));
5619 static int create_sort_keys(struct hist_trigger_data *hist_data)
5621 struct trace_array *tr = hist_data->event_file->tr;
5622 char *fields_str = hist_data->attrs->sort_key_str;
5623 struct tracing_map_sort_key *sort_key;
5624 int descending, ret = 0;
5625 unsigned int i, j, k;
5627 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
5632 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
5633 struct hist_field *hist_field;
5634 char *field_str, *field_name;
5635 const char *test_name;
5637 sort_key = &hist_data->sort_keys[i];
5639 field_str = strsep(&fields_str, ",");
5645 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
5649 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
5650 hist_err(tr, HIST_ERR_TOO_MANY_SORT_FIELDS, errpos("sort="));
5655 field_name = strsep(&field_str, ".");
5656 if (!field_name || !*field_name) {
5658 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
5662 if (strcmp(field_name, "hitcount") == 0) {
5663 descending = is_descending(tr, field_str);
5664 if (descending < 0) {
5668 sort_key->descending = descending;
5672 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
5675 hist_field = hist_data->fields[j];
5676 if (hist_field->flags & HIST_FIELD_FL_VAR)
5681 test_name = hist_field_name(hist_field, 0);
5683 if (strcmp(field_name, test_name) == 0) {
5684 sort_key->field_idx = idx;
5685 descending = is_descending(tr, field_str);
5686 if (descending < 0) {
5690 sort_key->descending = descending;
5694 if (j == hist_data->n_fields) {
5696 hist_err(tr, HIST_ERR_INVALID_SORT_FIELD, errpos(field_name));
5701 hist_data->n_sort_keys = i;
5706 static void destroy_actions(struct hist_trigger_data *hist_data)
5710 for (i = 0; i < hist_data->n_actions; i++) {
5711 struct action_data *data = hist_data->actions[i];
5713 if (data->handler == HANDLER_ONMATCH)
5714 onmatch_destroy(data);
5715 else if (data->handler == HANDLER_ONMAX ||
5716 data->handler == HANDLER_ONCHANGE)
5717 track_data_destroy(hist_data, data);
5723 static int parse_actions(struct hist_trigger_data *hist_data)
5725 struct trace_array *tr = hist_data->event_file->tr;
5726 struct action_data *data;
5732 for (i = 0; i < hist_data->attrs->n_actions; i++) {
5733 str = hist_data->attrs->action_str[i];
5735 if ((len = str_has_prefix(str, "onmatch("))) {
5736 char *action_str = str + len;
5738 data = onmatch_parse(tr, action_str);
5740 ret = PTR_ERR(data);
5743 } else if ((len = str_has_prefix(str, "onmax("))) {
5744 char *action_str = str + len;
5746 data = track_data_parse(hist_data, action_str,
5749 ret = PTR_ERR(data);
5752 } else if ((len = str_has_prefix(str, "onchange("))) {
5753 char *action_str = str + len;
5755 data = track_data_parse(hist_data, action_str,
5758 ret = PTR_ERR(data);
5766 hist_data->actions[hist_data->n_actions++] = data;
5772 static int create_actions(struct hist_trigger_data *hist_data)
5774 struct action_data *data;
5778 for (i = 0; i < hist_data->attrs->n_actions; i++) {
5779 data = hist_data->actions[i];
5781 if (data->handler == HANDLER_ONMATCH) {
5782 ret = onmatch_create(hist_data, data);
5785 } else if (data->handler == HANDLER_ONMAX ||
5786 data->handler == HANDLER_ONCHANGE) {
5787 ret = track_data_create(hist_data, data);
5799 static void print_actions(struct seq_file *m,
5800 struct hist_trigger_data *hist_data,
5801 struct tracing_map_elt *elt)
5805 for (i = 0; i < hist_data->n_actions; i++) {
5806 struct action_data *data = hist_data->actions[i];
5808 if (data->action == ACTION_SNAPSHOT)
5811 if (data->handler == HANDLER_ONMAX ||
5812 data->handler == HANDLER_ONCHANGE)
5813 track_data_print(m, hist_data, elt, data);
5817 static void print_action_spec(struct seq_file *m,
5818 struct hist_trigger_data *hist_data,
5819 struct action_data *data)
5823 if (data->action == ACTION_SAVE) {
5824 for (i = 0; i < hist_data->n_save_vars; i++) {
5825 seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
5826 if (i < hist_data->n_save_vars - 1)
5829 } else if (data->action == ACTION_TRACE) {
5830 if (data->use_trace_keyword)
5831 seq_printf(m, "%s", data->synth_event_name);
5832 for (i = 0; i < data->n_params; i++) {
5833 if (i || data->use_trace_keyword)
5835 seq_printf(m, "%s", data->params[i]);
5840 static void print_track_data_spec(struct seq_file *m,
5841 struct hist_trigger_data *hist_data,
5842 struct action_data *data)
5844 if (data->handler == HANDLER_ONMAX)
5845 seq_puts(m, ":onmax(");
5846 else if (data->handler == HANDLER_ONCHANGE)
5847 seq_puts(m, ":onchange(");
5848 seq_printf(m, "%s", data->track_data.var_str);
5849 seq_printf(m, ").%s(", data->action_name);
5851 print_action_spec(m, hist_data, data);
5856 static void print_onmatch_spec(struct seq_file *m,
5857 struct hist_trigger_data *hist_data,
5858 struct action_data *data)
5860 seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
5861 data->match_data.event);
5863 seq_printf(m, "%s(", data->action_name);
5865 print_action_spec(m, hist_data, data);
5870 static bool actions_match(struct hist_trigger_data *hist_data,
5871 struct hist_trigger_data *hist_data_test)
5875 if (hist_data->n_actions != hist_data_test->n_actions)
5878 for (i = 0; i < hist_data->n_actions; i++) {
5879 struct action_data *data = hist_data->actions[i];
5880 struct action_data *data_test = hist_data_test->actions[i];
5881 char *action_name, *action_name_test;
5883 if (data->handler != data_test->handler)
5885 if (data->action != data_test->action)
5888 if (data->n_params != data_test->n_params)
5891 for (j = 0; j < data->n_params; j++) {
5892 if (strcmp(data->params[j], data_test->params[j]) != 0)
5896 if (data->use_trace_keyword)
5897 action_name = data->synth_event_name;
5899 action_name = data->action_name;
5901 if (data_test->use_trace_keyword)
5902 action_name_test = data_test->synth_event_name;
5904 action_name_test = data_test->action_name;
5906 if (strcmp(action_name, action_name_test) != 0)
5909 if (data->handler == HANDLER_ONMATCH) {
5910 if (strcmp(data->match_data.event_system,
5911 data_test->match_data.event_system) != 0)
5913 if (strcmp(data->match_data.event,
5914 data_test->match_data.event) != 0)
5916 } else if (data->handler == HANDLER_ONMAX ||
5917 data->handler == HANDLER_ONCHANGE) {
5918 if (strcmp(data->track_data.var_str,
5919 data_test->track_data.var_str) != 0)
5928 static void print_actions_spec(struct seq_file *m,
5929 struct hist_trigger_data *hist_data)
5933 for (i = 0; i < hist_data->n_actions; i++) {
5934 struct action_data *data = hist_data->actions[i];
5936 if (data->handler == HANDLER_ONMATCH)
5937 print_onmatch_spec(m, hist_data, data);
5938 else if (data->handler == HANDLER_ONMAX ||
5939 data->handler == HANDLER_ONCHANGE)
5940 print_track_data_spec(m, hist_data, data);
5944 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
5948 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5949 kfree(hist_data->field_var_hists[i]->cmd);
5950 kfree(hist_data->field_var_hists[i]);
5954 static void destroy_hist_data(struct hist_trigger_data *hist_data)
5959 destroy_hist_trigger_attrs(hist_data->attrs);
5960 destroy_hist_fields(hist_data);
5961 tracing_map_destroy(hist_data->map);
5963 destroy_actions(hist_data);
5964 destroy_field_vars(hist_data);
5965 destroy_field_var_hists(hist_data);
5970 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
5972 struct tracing_map *map = hist_data->map;
5973 struct ftrace_event_field *field;
5974 struct hist_field *hist_field;
5977 for_each_hist_field(i, hist_data) {
5978 hist_field = hist_data->fields[i];
5979 if (hist_field->flags & HIST_FIELD_FL_KEY) {
5980 tracing_map_cmp_fn_t cmp_fn;
5982 field = hist_field->field;
5984 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
5985 cmp_fn = tracing_map_cmp_none;
5987 cmp_fn = tracing_map_cmp_num(hist_field->size,
5988 hist_field->is_signed);
5989 else if (is_string_field(field))
5990 cmp_fn = tracing_map_cmp_string;
5992 cmp_fn = tracing_map_cmp_num(field->size,
5994 idx = tracing_map_add_key_field(map,
5997 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
5998 idx = tracing_map_add_sum_field(map);
6003 if (hist_field->flags & HIST_FIELD_FL_VAR) {
6004 idx = tracing_map_add_var(map);
6007 hist_field->var.idx = idx;
6008 hist_field->var.hist_data = hist_data;
6015 static struct hist_trigger_data *
6016 create_hist_data(unsigned int map_bits,
6017 struct hist_trigger_attrs *attrs,
6018 struct trace_event_file *file,
6021 const struct tracing_map_ops *map_ops = NULL;
6022 struct hist_trigger_data *hist_data;
6025 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
6027 return ERR_PTR(-ENOMEM);
6029 hist_data->attrs = attrs;
6030 hist_data->remove = remove;
6031 hist_data->event_file = file;
6033 ret = parse_actions(hist_data);
6037 ret = create_hist_fields(hist_data, file);
6041 ret = create_sort_keys(hist_data);
6045 map_ops = &hist_trigger_elt_data_ops;
6047 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
6048 map_ops, hist_data);
6049 if (IS_ERR(hist_data->map)) {
6050 ret = PTR_ERR(hist_data->map);
6051 hist_data->map = NULL;
6055 ret = create_tracing_map_fields(hist_data);
6061 hist_data->attrs = NULL;
6063 destroy_hist_data(hist_data);
6065 hist_data = ERR_PTR(ret);
6070 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
6071 struct tracing_map_elt *elt, void *rec,
6072 struct ring_buffer_event *rbe,
6075 struct hist_elt_data *elt_data;
6076 struct hist_field *hist_field;
6077 unsigned int i, var_idx;
6080 elt_data = elt->private_data;
6081 elt_data->var_ref_vals = var_ref_vals;
6083 for_each_hist_val_field(i, hist_data) {
6084 hist_field = hist_data->fields[i];
6085 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
6086 if (hist_field->flags & HIST_FIELD_FL_VAR) {
6087 var_idx = hist_field->var.idx;
6088 tracing_map_set_var(elt, var_idx, hist_val);
6091 tracing_map_update_sum(elt, i, hist_val);
6094 for_each_hist_key_field(i, hist_data) {
6095 hist_field = hist_data->fields[i];
6096 if (hist_field->flags & HIST_FIELD_FL_VAR) {
6097 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
6098 var_idx = hist_field->var.idx;
6099 tracing_map_set_var(elt, var_idx, hist_val);
6103 update_field_vars(hist_data, elt, rbe, rec);
6106 static inline void add_to_key(char *compound_key, void *key,
6107 struct hist_field *key_field, void *rec)
6109 size_t size = key_field->size;
6111 if (key_field->flags & HIST_FIELD_FL_STRING) {
6112 struct ftrace_event_field *field;
6114 field = key_field->field;
6115 if (field->filter_type == FILTER_DYN_STRING)
6116 size = *(u32 *)(rec + field->offset) >> 16;
6117 else if (field->filter_type == FILTER_PTR_STRING)
6119 else if (field->filter_type == FILTER_STATIC_STRING)
6122 /* ensure NULL-termination */
6123 if (size > key_field->size - 1)
6124 size = key_field->size - 1;
6126 strncpy(compound_key + key_field->offset, (char *)key, size);
6128 memcpy(compound_key + key_field->offset, key, size);
6132 hist_trigger_actions(struct hist_trigger_data *hist_data,
6133 struct tracing_map_elt *elt, void *rec,
6134 struct ring_buffer_event *rbe, void *key,
6137 struct action_data *data;
6140 for (i = 0; i < hist_data->n_actions; i++) {
6141 data = hist_data->actions[i];
6142 data->fn(hist_data, elt, rec, rbe, key, data, var_ref_vals);
6146 static void event_hist_trigger(struct event_trigger_data *data, void *rec,
6147 struct ring_buffer_event *rbe)
6149 struct hist_trigger_data *hist_data = data->private_data;
6150 bool use_compound_key = (hist_data->n_keys > 1);
6151 unsigned long entries[HIST_STACKTRACE_DEPTH];
6152 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
6153 char compound_key[HIST_KEY_SIZE_MAX];
6154 struct tracing_map_elt *elt = NULL;
6155 struct hist_field *key_field;
6160 memset(compound_key, 0, hist_data->key_size);
6162 for_each_hist_key_field(i, hist_data) {
6163 key_field = hist_data->fields[i];
6165 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
6166 memset(entries, 0, HIST_STACKTRACE_SIZE);
6167 stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
6168 HIST_STACKTRACE_SKIP);
6171 field_contents = key_field->fn(key_field, elt, rbe, rec);
6172 if (key_field->flags & HIST_FIELD_FL_STRING) {
6173 key = (void *)(unsigned long)field_contents;
6174 use_compound_key = true;
6176 key = (void *)&field_contents;
6179 if (use_compound_key)
6180 add_to_key(compound_key, key, key_field, rec);
6183 if (use_compound_key)
6186 if (hist_data->n_var_refs &&
6187 !resolve_var_refs(hist_data, key, var_ref_vals, false))
6190 elt = tracing_map_insert(hist_data->map, key);
6194 hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals);
6196 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
6197 hist_trigger_actions(hist_data, elt, rec, rbe, key, var_ref_vals);
6200 static void hist_trigger_stacktrace_print(struct seq_file *m,
6201 unsigned long *stacktrace_entries,
6202 unsigned int max_entries)
6204 char str[KSYM_SYMBOL_LEN];
6205 unsigned int spaces = 8;
6208 for (i = 0; i < max_entries; i++) {
6209 if (!stacktrace_entries[i])
6212 seq_printf(m, "%*c", 1 + spaces, ' ');
6213 sprint_symbol(str, stacktrace_entries[i]);
6214 seq_printf(m, "%s\n", str);
6218 static void hist_trigger_print_key(struct seq_file *m,
6219 struct hist_trigger_data *hist_data,
6221 struct tracing_map_elt *elt)
6223 struct hist_field *key_field;
6224 char str[KSYM_SYMBOL_LEN];
6225 bool multiline = false;
6226 const char *field_name;
6232 for_each_hist_key_field(i, hist_data) {
6233 key_field = hist_data->fields[i];
6235 if (i > hist_data->n_vals)
6238 field_name = hist_field_name(key_field, 0);
6240 if (key_field->flags & HIST_FIELD_FL_HEX) {
6241 uval = *(u64 *)(key + key_field->offset);
6242 seq_printf(m, "%s: %llx", field_name, uval);
6243 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
6244 uval = *(u64 *)(key + key_field->offset);
6245 sprint_symbol_no_offset(str, uval);
6246 seq_printf(m, "%s: [%llx] %-45s", field_name,
6248 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
6249 uval = *(u64 *)(key + key_field->offset);
6250 sprint_symbol(str, uval);
6251 seq_printf(m, "%s: [%llx] %-55s", field_name,
6253 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
6254 struct hist_elt_data *elt_data = elt->private_data;
6257 if (WARN_ON_ONCE(!elt_data))
6260 comm = elt_data->comm;
6262 uval = *(u64 *)(key + key_field->offset);
6263 seq_printf(m, "%s: %-16s[%10llu]", field_name,
6265 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
6266 const char *syscall_name;
6268 uval = *(u64 *)(key + key_field->offset);
6269 syscall_name = get_syscall_name(uval);
6271 syscall_name = "unknown_syscall";
6273 seq_printf(m, "%s: %-30s[%3llu]", field_name,
6274 syscall_name, uval);
6275 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
6276 seq_puts(m, "stacktrace:\n");
6277 hist_trigger_stacktrace_print(m,
6278 key + key_field->offset,
6279 HIST_STACKTRACE_DEPTH);
6281 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
6282 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
6283 *(u64 *)(key + key_field->offset));
6284 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
6285 seq_printf(m, "%s: %-50s", field_name,
6286 (char *)(key + key_field->offset));
6288 uval = *(u64 *)(key + key_field->offset);
6289 seq_printf(m, "%s: %10llu", field_name, uval);
6299 static void hist_trigger_entry_print(struct seq_file *m,
6300 struct hist_trigger_data *hist_data,
6302 struct tracing_map_elt *elt)
6304 const char *field_name;
6307 hist_trigger_print_key(m, hist_data, key, elt);
6309 seq_printf(m, " hitcount: %10llu",
6310 tracing_map_read_sum(elt, HITCOUNT_IDX));
6312 for (i = 1; i < hist_data->n_vals; i++) {
6313 field_name = hist_field_name(hist_data->fields[i], 0);
6315 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
6316 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
6319 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
6320 seq_printf(m, " %s: %10llx", field_name,
6321 tracing_map_read_sum(elt, i));
6323 seq_printf(m, " %s: %10llu", field_name,
6324 tracing_map_read_sum(elt, i));
6328 print_actions(m, hist_data, elt);
6333 static int print_entries(struct seq_file *m,
6334 struct hist_trigger_data *hist_data)
6336 struct tracing_map_sort_entry **sort_entries = NULL;
6337 struct tracing_map *map = hist_data->map;
6340 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
6341 hist_data->n_sort_keys,
6346 for (i = 0; i < n_entries; i++)
6347 hist_trigger_entry_print(m, hist_data,
6348 sort_entries[i]->key,
6349 sort_entries[i]->elt);
6351 tracing_map_destroy_sort_entries(sort_entries, n_entries);
6356 static void hist_trigger_show(struct seq_file *m,
6357 struct event_trigger_data *data, int n)
6359 struct hist_trigger_data *hist_data;
6363 seq_puts(m, "\n\n");
6365 seq_puts(m, "# event histogram\n#\n# trigger info: ");
6366 data->ops->print(m, data->ops, data);
6367 seq_puts(m, "#\n\n");
6369 hist_data = data->private_data;
6370 n_entries = print_entries(m, hist_data);
6374 track_data_snapshot_print(m, hist_data);
6376 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
6377 (u64)atomic64_read(&hist_data->map->hits),
6378 n_entries, (u64)atomic64_read(&hist_data->map->drops));
6381 static int hist_show(struct seq_file *m, void *v)
6383 struct event_trigger_data *data;
6384 struct trace_event_file *event_file;
6387 mutex_lock(&event_mutex);
6389 event_file = event_file_data(m->private);
6390 if (unlikely(!event_file)) {
6395 list_for_each_entry(data, &event_file->triggers, list) {
6396 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
6397 hist_trigger_show(m, data, n++);
6401 mutex_unlock(&event_mutex);
6406 static int event_hist_open(struct inode *inode, struct file *file)
6410 ret = security_locked_down(LOCKDOWN_TRACEFS);
6414 return single_open(file, hist_show, file);
6417 const struct file_operations event_hist_fops = {
6418 .open = event_hist_open,
6420 .llseek = seq_lseek,
6421 .release = single_release,
6424 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
6426 const char *field_name = hist_field_name(hist_field, 0);
6428 if (hist_field->var.name)
6429 seq_printf(m, "%s=", hist_field->var.name);
6431 if (hist_field->flags & HIST_FIELD_FL_CPU)
6433 else if (field_name) {
6434 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
6435 hist_field->flags & HIST_FIELD_FL_ALIAS)
6437 seq_printf(m, "%s", field_name);
6438 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
6439 seq_puts(m, "common_timestamp");
6441 if (hist_field->flags) {
6442 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
6443 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
6444 const char *flags = get_hist_field_flags(hist_field);
6447 seq_printf(m, ".%s", flags);
6452 static int event_hist_trigger_print(struct seq_file *m,
6453 struct event_trigger_ops *ops,
6454 struct event_trigger_data *data)
6456 struct hist_trigger_data *hist_data = data->private_data;
6457 struct hist_field *field;
6458 bool have_var = false;
6461 seq_puts(m, "hist:");
6464 seq_printf(m, "%s:", data->name);
6466 seq_puts(m, "keys=");
6468 for_each_hist_key_field(i, hist_data) {
6469 field = hist_data->fields[i];
6471 if (i > hist_data->n_vals)
6474 if (field->flags & HIST_FIELD_FL_STACKTRACE)
6475 seq_puts(m, "stacktrace");
6477 hist_field_print(m, field);
6480 seq_puts(m, ":vals=");
6482 for_each_hist_val_field(i, hist_data) {
6483 field = hist_data->fields[i];
6484 if (field->flags & HIST_FIELD_FL_VAR) {
6489 if (i == HITCOUNT_IDX)
6490 seq_puts(m, "hitcount");
6493 hist_field_print(m, field);
6502 for_each_hist_val_field(i, hist_data) {
6503 field = hist_data->fields[i];
6505 if (field->flags & HIST_FIELD_FL_VAR) {
6508 hist_field_print(m, field);
6513 seq_puts(m, ":sort=");
6515 for (i = 0; i < hist_data->n_sort_keys; i++) {
6516 struct tracing_map_sort_key *sort_key;
6517 unsigned int idx, first_key_idx;
6520 first_key_idx = hist_data->n_vals - hist_data->n_vars;
6522 sort_key = &hist_data->sort_keys[i];
6523 idx = sort_key->field_idx;
6525 if (WARN_ON(idx >= HIST_FIELDS_MAX))
6531 if (idx == HITCOUNT_IDX)
6532 seq_puts(m, "hitcount");
6534 if (idx >= first_key_idx)
6535 idx += hist_data->n_vars;
6536 hist_field_print(m, hist_data->fields[idx]);
6539 if (sort_key->descending)
6540 seq_puts(m, ".descending");
6542 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
6543 if (hist_data->enable_timestamps)
6544 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
6546 print_actions_spec(m, hist_data);
6548 if (data->filter_str)
6549 seq_printf(m, " if %s", data->filter_str);
6552 seq_puts(m, " [paused]");
6554 seq_puts(m, " [active]");
6561 static int event_hist_trigger_init(struct event_trigger_ops *ops,
6562 struct event_trigger_data *data)
6564 struct hist_trigger_data *hist_data = data->private_data;
6566 if (!data->ref && hist_data->attrs->name)
6567 save_named_trigger(hist_data->attrs->name, data);
6574 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
6576 struct trace_event_file *file;
6581 for (i = 0; i < hist_data->n_field_var_hists; i++) {
6582 file = hist_data->field_var_hists[i]->hist_data->event_file;
6583 cmd = hist_data->field_var_hists[i]->cmd;
6584 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
6585 "!hist", "hist", cmd);
6589 static void event_hist_trigger_free(struct event_trigger_ops *ops,
6590 struct event_trigger_data *data)
6592 struct hist_trigger_data *hist_data = data->private_data;
6594 if (WARN_ON_ONCE(data->ref <= 0))
6600 del_named_trigger(data);
6602 trigger_data_free(data);
6604 remove_hist_vars(hist_data);
6606 unregister_field_var_hists(hist_data);
6608 destroy_hist_data(hist_data);
6612 static struct event_trigger_ops event_hist_trigger_ops = {
6613 .func = event_hist_trigger,
6614 .print = event_hist_trigger_print,
6615 .init = event_hist_trigger_init,
6616 .free = event_hist_trigger_free,
6619 static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
6620 struct event_trigger_data *data)
6624 save_named_trigger(data->named_data->name, data);
6626 event_hist_trigger_init(ops, data->named_data);
6631 static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
6632 struct event_trigger_data *data)
6634 if (WARN_ON_ONCE(data->ref <= 0))
6637 event_hist_trigger_free(ops, data->named_data);
6641 del_named_trigger(data);
6642 trigger_data_free(data);
6646 static struct event_trigger_ops event_hist_trigger_named_ops = {
6647 .func = event_hist_trigger,
6648 .print = event_hist_trigger_print,
6649 .init = event_hist_trigger_named_init,
6650 .free = event_hist_trigger_named_free,
6653 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
6656 return &event_hist_trigger_ops;
6659 static void hist_clear(struct event_trigger_data *data)
6661 struct hist_trigger_data *hist_data = data->private_data;
6664 pause_named_trigger(data);
6666 tracepoint_synchronize_unregister();
6668 tracing_map_clear(hist_data->map);
6671 unpause_named_trigger(data);
6674 static bool compatible_field(struct ftrace_event_field *field,
6675 struct ftrace_event_field *test_field)
6677 if (field == test_field)
6679 if (field == NULL || test_field == NULL)
6681 if (strcmp(field->name, test_field->name) != 0)
6683 if (strcmp(field->type, test_field->type) != 0)
6685 if (field->size != test_field->size)
6687 if (field->is_signed != test_field->is_signed)
6693 static bool hist_trigger_match(struct event_trigger_data *data,
6694 struct event_trigger_data *data_test,
6695 struct event_trigger_data *named_data,
6698 struct tracing_map_sort_key *sort_key, *sort_key_test;
6699 struct hist_trigger_data *hist_data, *hist_data_test;
6700 struct hist_field *key_field, *key_field_test;
6703 if (named_data && (named_data != data_test) &&
6704 (named_data != data_test->named_data))
6707 if (!named_data && is_named_trigger(data_test))
6710 hist_data = data->private_data;
6711 hist_data_test = data_test->private_data;
6713 if (hist_data->n_vals != hist_data_test->n_vals ||
6714 hist_data->n_fields != hist_data_test->n_fields ||
6715 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
6718 if (!ignore_filter) {
6719 if ((data->filter_str && !data_test->filter_str) ||
6720 (!data->filter_str && data_test->filter_str))
6724 for_each_hist_field(i, hist_data) {
6725 key_field = hist_data->fields[i];
6726 key_field_test = hist_data_test->fields[i];
6728 if (key_field->flags != key_field_test->flags)
6730 if (!compatible_field(key_field->field, key_field_test->field))
6732 if (key_field->offset != key_field_test->offset)
6734 if (key_field->size != key_field_test->size)
6736 if (key_field->is_signed != key_field_test->is_signed)
6738 if (!!key_field->var.name != !!key_field_test->var.name)
6740 if (key_field->var.name &&
6741 strcmp(key_field->var.name, key_field_test->var.name) != 0)
6745 for (i = 0; i < hist_data->n_sort_keys; i++) {
6746 sort_key = &hist_data->sort_keys[i];
6747 sort_key_test = &hist_data_test->sort_keys[i];
6749 if (sort_key->field_idx != sort_key_test->field_idx ||
6750 sort_key->descending != sort_key_test->descending)
6754 if (!ignore_filter && data->filter_str &&
6755 (strcmp(data->filter_str, data_test->filter_str) != 0))
6758 if (!actions_match(hist_data, hist_data_test))
6764 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
6765 struct event_trigger_data *data,
6766 struct trace_event_file *file)
6768 struct hist_trigger_data *hist_data = data->private_data;
6769 struct event_trigger_data *test, *named_data = NULL;
6770 struct trace_array *tr = file->tr;
6773 if (hist_data->attrs->name) {
6774 named_data = find_named_trigger(hist_data->attrs->name);
6776 if (!hist_trigger_match(data, named_data, named_data,
6778 hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name));
6785 if (hist_data->attrs->name && !named_data)
6788 lockdep_assert_held(&event_mutex);
6790 list_for_each_entry(test, &file->triggers, list) {
6791 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6792 if (!hist_trigger_match(data, test, named_data, false))
6794 if (hist_data->attrs->pause)
6795 test->paused = true;
6796 else if (hist_data->attrs->cont)
6797 test->paused = false;
6798 else if (hist_data->attrs->clear)
6801 hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
6808 if (hist_data->attrs->cont || hist_data->attrs->clear) {
6809 hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0);
6814 if (hist_data->attrs->pause)
6815 data->paused = true;
6818 data->private_data = named_data->private_data;
6819 set_named_trigger_data(data, named_data);
6820 data->ops = &event_hist_trigger_named_ops;
6823 if (data->ops->init) {
6824 ret = data->ops->init(data->ops, data);
6829 if (hist_data->enable_timestamps) {
6830 char *clock = hist_data->attrs->clock;
6832 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
6834 hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock));
6838 tracing_set_time_stamp_abs(file->tr, true);
6842 destroy_hist_data(hist_data);
6849 static int hist_trigger_enable(struct event_trigger_data *data,
6850 struct trace_event_file *file)
6854 list_add_tail_rcu(&data->list, &file->triggers);
6856 update_cond_flag(file);
6858 if (trace_event_trigger_enable_disable(file, 1) < 0) {
6859 list_del_rcu(&data->list);
6860 update_cond_flag(file);
6867 static bool have_hist_trigger_match(struct event_trigger_data *data,
6868 struct trace_event_file *file)
6870 struct hist_trigger_data *hist_data = data->private_data;
6871 struct event_trigger_data *test, *named_data = NULL;
6874 lockdep_assert_held(&event_mutex);
6876 if (hist_data->attrs->name)
6877 named_data = find_named_trigger(hist_data->attrs->name);
6879 list_for_each_entry(test, &file->triggers, list) {
6880 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6881 if (hist_trigger_match(data, test, named_data, false)) {
6891 static bool hist_trigger_check_refs(struct event_trigger_data *data,
6892 struct trace_event_file *file)
6894 struct hist_trigger_data *hist_data = data->private_data;
6895 struct event_trigger_data *test, *named_data = NULL;
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 hist_data = test->private_data;
6907 if (check_var_refs(hist_data))
6916 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
6917 struct event_trigger_data *data,
6918 struct trace_event_file *file)
6920 struct hist_trigger_data *hist_data = data->private_data;
6921 struct event_trigger_data *test, *named_data = NULL;
6922 bool unregistered = false;
6924 lockdep_assert_held(&event_mutex);
6926 if (hist_data->attrs->name)
6927 named_data = find_named_trigger(hist_data->attrs->name);
6929 list_for_each_entry(test, &file->triggers, list) {
6930 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6931 if (!hist_trigger_match(data, test, named_data, false))
6933 unregistered = true;
6934 list_del_rcu(&test->list);
6935 trace_event_trigger_enable_disable(file, 0);
6936 update_cond_flag(file);
6941 if (unregistered && test->ops->free)
6942 test->ops->free(test->ops, test);
6944 if (hist_data->enable_timestamps) {
6945 if (!hist_data->remove || unregistered)
6946 tracing_set_time_stamp_abs(file->tr, false);
6950 static bool hist_file_check_refs(struct trace_event_file *file)
6952 struct hist_trigger_data *hist_data;
6953 struct event_trigger_data *test;
6955 lockdep_assert_held(&event_mutex);
6957 list_for_each_entry(test, &file->triggers, list) {
6958 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6959 hist_data = test->private_data;
6960 if (check_var_refs(hist_data))
6968 static void hist_unreg_all(struct trace_event_file *file)
6970 struct event_trigger_data *test, *n;
6971 struct hist_trigger_data *hist_data;
6972 struct synth_event *se;
6973 const char *se_name;
6975 lockdep_assert_held(&event_mutex);
6977 if (hist_file_check_refs(file))
6980 list_for_each_entry_safe(test, n, &file->triggers, list) {
6981 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6982 hist_data = test->private_data;
6983 list_del_rcu(&test->list);
6984 trace_event_trigger_enable_disable(file, 0);
6986 se_name = trace_event_name(file->event_call);
6987 se = find_synth_event(se_name);
6991 update_cond_flag(file);
6992 if (hist_data->enable_timestamps)
6993 tracing_set_time_stamp_abs(file->tr, false);
6994 if (test->ops->free)
6995 test->ops->free(test->ops, test);
7000 static int event_hist_trigger_func(struct event_command *cmd_ops,
7001 struct trace_event_file *file,
7002 char *glob, char *cmd, char *param)
7004 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
7005 struct event_trigger_data *trigger_data;
7006 struct hist_trigger_attrs *attrs;
7007 struct event_trigger_ops *trigger_ops;
7008 struct hist_trigger_data *hist_data;
7009 struct synth_event *se;
7010 const char *se_name;
7011 bool remove = false;
7015 lockdep_assert_held(&event_mutex);
7017 if (glob && strlen(glob)) {
7019 last_cmd_set(file, param);
7029 * separate the trigger from the filter (k:v [if filter])
7030 * allowing for whitespace in the trigger
7032 p = trigger = param;
7034 p = strstr(p, "if");
7039 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
7043 if (p >= param + strlen(param) - (sizeof("if") - 1) - 1)
7045 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
7056 param = strstrip(p);
7057 trigger = strstrip(trigger);
7060 attrs = parse_hist_trigger_attrs(file->tr, trigger);
7062 return PTR_ERR(attrs);
7064 if (attrs->map_bits)
7065 hist_trigger_bits = attrs->map_bits;
7067 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
7068 if (IS_ERR(hist_data)) {
7069 destroy_hist_trigger_attrs(attrs);
7070 return PTR_ERR(hist_data);
7073 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
7075 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
7076 if (!trigger_data) {
7081 trigger_data->count = -1;
7082 trigger_data->ops = trigger_ops;
7083 trigger_data->cmd_ops = cmd_ops;
7085 INIT_LIST_HEAD(&trigger_data->list);
7086 RCU_INIT_POINTER(trigger_data->filter, NULL);
7088 trigger_data->private_data = hist_data;
7090 /* if param is non-empty, it's supposed to be a filter */
7091 if (param && cmd_ops->set_filter) {
7092 ret = cmd_ops->set_filter(param, trigger_data, file);
7098 if (!have_hist_trigger_match(trigger_data, file))
7101 if (hist_trigger_check_refs(trigger_data, file)) {
7106 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
7107 se_name = trace_event_name(file->event_call);
7108 se = find_synth_event(se_name);
7115 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
7117 * The above returns on success the # of triggers registered,
7118 * but if it didn't register any it returns zero. Consider no
7119 * triggers registered a failure too.
7122 if (!(attrs->pause || attrs->cont || attrs->clear))
7128 if (get_named_trigger_data(trigger_data))
7131 if (has_hist_vars(hist_data))
7132 save_hist_vars(hist_data);
7134 ret = create_actions(hist_data);
7138 ret = tracing_map_init(hist_data->map);
7142 ret = hist_trigger_enable(trigger_data, file);
7146 se_name = trace_event_name(file->event_call);
7147 se = find_synth_event(se_name);
7150 /* Just return zero, not the number of registered triggers */
7158 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
7160 if (cmd_ops->set_filter)
7161 cmd_ops->set_filter(NULL, trigger_data, NULL);
7163 remove_hist_vars(hist_data);
7165 kfree(trigger_data);
7167 destroy_hist_data(hist_data);
7171 static struct event_command trigger_hist_cmd = {
7173 .trigger_type = ETT_EVENT_HIST,
7174 .flags = EVENT_CMD_FL_NEEDS_REC,
7175 .func = event_hist_trigger_func,
7176 .reg = hist_register_trigger,
7177 .unreg = hist_unregister_trigger,
7178 .unreg_all = hist_unreg_all,
7179 .get_trigger_ops = event_hist_get_trigger_ops,
7180 .set_filter = set_trigger_filter,
7183 __init int register_trigger_hist_cmd(void)
7187 ret = register_event_command(&trigger_hist_cmd);
7194 hist_enable_trigger(struct event_trigger_data *data, void *rec,
7195 struct ring_buffer_event *event)
7197 struct enable_trigger_data *enable_data = data->private_data;
7198 struct event_trigger_data *test;
7200 list_for_each_entry_rcu(test, &enable_data->file->triggers, list,
7201 lockdep_is_held(&event_mutex)) {
7202 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
7203 if (enable_data->enable)
7204 test->paused = false;
7206 test->paused = true;
7212 hist_enable_count_trigger(struct event_trigger_data *data, void *rec,
7213 struct ring_buffer_event *event)
7218 if (data->count != -1)
7221 hist_enable_trigger(data, rec, event);
7224 static struct event_trigger_ops hist_enable_trigger_ops = {
7225 .func = hist_enable_trigger,
7226 .print = event_enable_trigger_print,
7227 .init = event_trigger_init,
7228 .free = event_enable_trigger_free,
7231 static struct event_trigger_ops hist_enable_count_trigger_ops = {
7232 .func = hist_enable_count_trigger,
7233 .print = event_enable_trigger_print,
7234 .init = event_trigger_init,
7235 .free = event_enable_trigger_free,
7238 static struct event_trigger_ops hist_disable_trigger_ops = {
7239 .func = hist_enable_trigger,
7240 .print = event_enable_trigger_print,
7241 .init = event_trigger_init,
7242 .free = event_enable_trigger_free,
7245 static struct event_trigger_ops hist_disable_count_trigger_ops = {
7246 .func = hist_enable_count_trigger,
7247 .print = event_enable_trigger_print,
7248 .init = event_trigger_init,
7249 .free = event_enable_trigger_free,
7252 static struct event_trigger_ops *
7253 hist_enable_get_trigger_ops(char *cmd, char *param)
7255 struct event_trigger_ops *ops;
7258 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
7261 ops = param ? &hist_enable_count_trigger_ops :
7262 &hist_enable_trigger_ops;
7264 ops = param ? &hist_disable_count_trigger_ops :
7265 &hist_disable_trigger_ops;
7270 static void hist_enable_unreg_all(struct trace_event_file *file)
7272 struct event_trigger_data *test, *n;
7274 list_for_each_entry_safe(test, n, &file->triggers, list) {
7275 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
7276 list_del_rcu(&test->list);
7277 update_cond_flag(file);
7278 trace_event_trigger_enable_disable(file, 0);
7279 if (test->ops->free)
7280 test->ops->free(test->ops, test);
7285 static struct event_command trigger_hist_enable_cmd = {
7286 .name = ENABLE_HIST_STR,
7287 .trigger_type = ETT_HIST_ENABLE,
7288 .func = event_enable_trigger_func,
7289 .reg = event_enable_register_trigger,
7290 .unreg = event_enable_unregister_trigger,
7291 .unreg_all = hist_enable_unreg_all,
7292 .get_trigger_ops = hist_enable_get_trigger_ops,
7293 .set_filter = set_trigger_filter,
7296 static struct event_command trigger_hist_disable_cmd = {
7297 .name = DISABLE_HIST_STR,
7298 .trigger_type = ETT_HIST_ENABLE,
7299 .func = event_enable_trigger_func,
7300 .reg = event_enable_register_trigger,
7301 .unreg = event_enable_unregister_trigger,
7302 .unreg_all = hist_enable_unreg_all,
7303 .get_trigger_ops = hist_enable_get_trigger_ops,
7304 .set_filter = set_trigger_filter,
7307 static __init void unregister_trigger_hist_enable_disable_cmds(void)
7309 unregister_event_command(&trigger_hist_enable_cmd);
7310 unregister_event_command(&trigger_hist_disable_cmd);
7313 __init int register_trigger_hist_enable_disable_cmds(void)
7317 ret = register_event_command(&trigger_hist_enable_cmd);
7318 if (WARN_ON(ret < 0))
7320 ret = register_event_command(&trigger_hist_disable_cmd);
7321 if (WARN_ON(ret < 0))
7322 unregister_trigger_hist_enable_disable_cmds();
7327 static __init int trace_events_hist_init(void)
7329 struct dentry *entry = NULL;
7330 struct dentry *d_tracer;
7333 err = dyn_event_register(&synth_event_ops);
7335 pr_warn("Could not register synth_event_ops\n");
7339 d_tracer = tracing_init_dentry();
7340 if (IS_ERR(d_tracer)) {
7341 err = PTR_ERR(d_tracer);
7345 entry = tracefs_create_file("synthetic_events", 0644, d_tracer,
7346 NULL, &synth_events_fops);
7354 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
7359 fs_initcall(trace_events_hist_init);