]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - kernel/trace/trace_events_hist.c
99a02168599ba69e4485e4c6c26ebff3620384e1
[mirror_ubuntu-jammy-kernel.git] / kernel / trace / trace_events_hist.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * trace_events_hist - trace event hist triggers
4 *
5 * Copyright (C) 2015 Tom Zanussi <tom.zanussi@linux.intel.com>
6 */
7
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>
16
17 /* for gfp flag names */
18 #include <linux/trace_events.h>
19 #include <trace/events/mmflags.h>
20
21 #include "tracing_map.h"
22 #include "trace.h"
23 #include "trace_dynevent.h"
24
25 #define SYNTH_SYSTEM "synthetic"
26 #define SYNTH_FIELDS_MAX 32
27
28 #define STR_VAR_LEN_MAX 32 /* must be multiple of sizeof(u64) */
29
30 #define ERRORS \
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"),
75
76 #undef C
77 #define C(a, b) HIST_ERR_##a
78
79 enum { ERRORS };
80
81 #undef C
82 #define C(a, b) b
83
84 static const char *err_text[] = { ERRORS };
85
86 struct hist_field;
87
88 typedef u64 (*hist_field_fn_t) (struct hist_field *field,
89 struct tracing_map_elt *elt,
90 struct ring_buffer_event *rbe,
91 void *event);
92
93 #define HIST_FIELD_OPERANDS_MAX 2
94 #define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
95 #define HIST_ACTIONS_MAX 8
96
97 enum field_op_id {
98 FIELD_OP_NONE,
99 FIELD_OP_PLUS,
100 FIELD_OP_MINUS,
101 FIELD_OP_UNARY_MINUS,
102 };
103
104 /*
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
112 * retrieve it.
113 */
114 struct hist_var {
115 char *name;
116 struct hist_trigger_data *hist_data;
117 unsigned int idx;
118 };
119
120 struct hist_field {
121 struct ftrace_event_field *field;
122 unsigned long flags;
123 hist_field_fn_t fn;
124 unsigned int size;
125 unsigned int offset;
126 unsigned int is_signed;
127 const char *type;
128 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
129 struct hist_trigger_data *hist_data;
130
131 /*
132 * Variable fields contain variable-specific info in var.
133 */
134 struct hist_var var;
135 enum field_op_id operator;
136 char *system;
137 char *event_name;
138
139 /*
140 * The name field is used for EXPR and VAR_REF fields. VAR
141 * fields contain the variable name in var.name.
142 */
143 char *name;
144
145 /*
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].
152 */
153 unsigned int var_ref_idx;
154 bool read_once;
155 };
156
157 static u64 hist_field_none(struct hist_field *field,
158 struct tracing_map_elt *elt,
159 struct ring_buffer_event *rbe,
160 void *event)
161 {
162 return 0;
163 }
164
165 static u64 hist_field_counter(struct hist_field *field,
166 struct tracing_map_elt *elt,
167 struct ring_buffer_event *rbe,
168 void *event)
169 {
170 return 1;
171 }
172
173 static u64 hist_field_string(struct hist_field *hist_field,
174 struct tracing_map_elt *elt,
175 struct ring_buffer_event *rbe,
176 void *event)
177 {
178 char *addr = (char *)(event + hist_field->field->offset);
179
180 return (u64)(unsigned long)addr;
181 }
182
183 static u64 hist_field_dynstring(struct hist_field *hist_field,
184 struct tracing_map_elt *elt,
185 struct ring_buffer_event *rbe,
186 void *event)
187 {
188 u32 str_item = *(u32 *)(event + hist_field->field->offset);
189 int str_loc = str_item & 0xffff;
190 char *addr = (char *)(event + str_loc);
191
192 return (u64)(unsigned long)addr;
193 }
194
195 static u64 hist_field_pstring(struct hist_field *hist_field,
196 struct tracing_map_elt *elt,
197 struct ring_buffer_event *rbe,
198 void *event)
199 {
200 char **addr = (char **)(event + hist_field->field->offset);
201
202 return (u64)(unsigned long)*addr;
203 }
204
205 static u64 hist_field_log2(struct hist_field *hist_field,
206 struct tracing_map_elt *elt,
207 struct ring_buffer_event *rbe,
208 void *event)
209 {
210 struct hist_field *operand = hist_field->operands[0];
211
212 u64 val = operand->fn(operand, elt, rbe, event);
213
214 return (u64) ilog2(roundup_pow_of_two(val));
215 }
216
217 static u64 hist_field_plus(struct hist_field *hist_field,
218 struct tracing_map_elt *elt,
219 struct ring_buffer_event *rbe,
220 void *event)
221 {
222 struct hist_field *operand1 = hist_field->operands[0];
223 struct hist_field *operand2 = hist_field->operands[1];
224
225 u64 val1 = operand1->fn(operand1, elt, rbe, event);
226 u64 val2 = operand2->fn(operand2, elt, rbe, event);
227
228 return val1 + val2;
229 }
230
231 static u64 hist_field_minus(struct hist_field *hist_field,
232 struct tracing_map_elt *elt,
233 struct ring_buffer_event *rbe,
234 void *event)
235 {
236 struct hist_field *operand1 = hist_field->operands[0];
237 struct hist_field *operand2 = hist_field->operands[1];
238
239 u64 val1 = operand1->fn(operand1, elt, rbe, event);
240 u64 val2 = operand2->fn(operand2, elt, rbe, event);
241
242 return val1 - val2;
243 }
244
245 static u64 hist_field_unary_minus(struct hist_field *hist_field,
246 struct tracing_map_elt *elt,
247 struct ring_buffer_event *rbe,
248 void *event)
249 {
250 struct hist_field *operand = hist_field->operands[0];
251
252 s64 sval = (s64)operand->fn(operand, elt, rbe, event);
253 u64 val = (u64)-sval;
254
255 return val;
256 }
257
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, \
262 void *event) \
263 { \
264 type *addr = (type *)(event + hist_field->field->offset); \
265 \
266 return (u64)(unsigned long)*addr; \
267 }
268
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);
277
278 #define for_each_hist_field(i, hist_data) \
279 for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
280
281 #define for_each_hist_val_field(i, hist_data) \
282 for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
283
284 #define for_each_hist_key_field(i, hist_data) \
285 for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
286
287 #define HIST_STACKTRACE_DEPTH 16
288 #define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
289 #define HIST_STACKTRACE_SKIP 5
290
291 #define HITCOUNT_IDX 0
292 #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
293
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,
312 };
313
314 struct var_defs {
315 unsigned int n_vars;
316 char *name[TRACING_MAP_VARS_MAX];
317 char *expr[TRACING_MAP_VARS_MAX];
318 };
319
320 struct hist_trigger_attrs {
321 char *keys_str;
322 char *vals_str;
323 char *sort_key_str;
324 char *name;
325 char *clock;
326 bool pause;
327 bool cont;
328 bool clear;
329 bool ts_in_usecs;
330 unsigned int map_bits;
331
332 char *assignment_str[TRACING_MAP_VARS_MAX];
333 unsigned int n_assignments;
334
335 char *action_str[HIST_ACTIONS_MAX];
336 unsigned int n_actions;
337
338 struct var_defs var_defs;
339 };
340
341 struct field_var {
342 struct hist_field *var;
343 struct hist_field *val;
344 };
345
346 struct field_var_hist {
347 struct hist_trigger_data *hist_data;
348 char *cmd;
349 };
350
351 struct hist_trigger_data {
352 struct hist_field *fields[HIST_FIELDS_MAX];
353 unsigned int n_vals;
354 unsigned int n_keys;
355 unsigned int n_fields;
356 unsigned int n_vars;
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;
364 bool remove;
365 struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
366 unsigned int n_var_refs;
367
368 struct action_data *actions[HIST_ACTIONS_MAX];
369 unsigned int n_actions;
370
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;
376
377 struct field_var *save_vars[SYNTH_FIELDS_MAX];
378 unsigned int n_save_vars;
379 unsigned int n_save_var_str;
380 };
381
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);
388
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,
395 };
396
397 struct synth_field {
398 char *type;
399 char *name;
400 size_t size;
401 unsigned int offset;
402 bool is_signed;
403 bool is_string;
404 };
405
406 struct synth_event {
407 struct dyn_event devent;
408 int ref;
409 char *name;
410 struct synth_field **fields;
411 unsigned int n_fields;
412 unsigned int n_u64;
413 struct trace_event_class class;
414 struct trace_event_call call;
415 struct tracepoint *tp;
416 struct module *mod;
417 };
418
419 static bool is_synth_event(struct dyn_event *ev)
420 {
421 return ev->ops == &synth_event_ops;
422 }
423
424 static struct synth_event *to_synth_event(struct dyn_event *ev)
425 {
426 return container_of(ev, struct synth_event, devent);
427 }
428
429 static bool synth_event_is_busy(struct dyn_event *ev)
430 {
431 struct synth_event *event = to_synth_event(ev);
432
433 return event->ref != 0;
434 }
435
436 static bool synth_event_match(const char *system, const char *event,
437 int argc, const char **argv, struct dyn_event *ev)
438 {
439 struct synth_event *sev = to_synth_event(ev);
440
441 return strcmp(sev->name, event) == 0 &&
442 (!system || strcmp(system, SYNTH_SYSTEM) == 0);
443 }
444
445 struct action_data;
446
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);
451
452 typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val);
453
454 enum handler_id {
455 HANDLER_ONMATCH = 1,
456 HANDLER_ONMAX,
457 HANDLER_ONCHANGE,
458 };
459
460 enum action_id {
461 ACTION_SAVE = 1,
462 ACTION_TRACE,
463 ACTION_SNAPSHOT,
464 };
465
466 struct action_data {
467 enum handler_id handler;
468 enum action_id action;
469 char *action_name;
470 action_fn_t fn;
471
472 unsigned int n_params;
473 char *params[SYNTH_FIELDS_MAX];
474
475 /*
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
482 * invocation.
483 */
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;
488
489 union {
490 struct {
491 char *event;
492 char *event_system;
493 } match_data;
494
495 struct {
496 /*
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.
503 */
504 char *var_str;
505
506 /*
507 * var_ref refers to the variable being
508 * tracked e.g onmax($var).
509 */
510 struct hist_field *var_ref;
511
512 /*
513 * track_var contains the 'invisible' tracking
514 * variable created to keep the current
515 * e.g. max value.
516 */
517 struct hist_field *track_var;
518
519 check_track_val_fn_t check_val;
520 action_fn_t save_data;
521 } track_data;
522 };
523 };
524
525 struct track_data {
526 u64 track_val;
527 bool updated;
528
529 unsigned int key_len;
530 void *key;
531 struct tracing_map_elt elt;
532
533 struct action_data *action_data;
534 struct hist_trigger_data *hist_data;
535 };
536
537 struct hist_elt_data {
538 char *comm;
539 u64 *var_ref_vals;
540 char *field_var_str[SYNTH_FIELDS_MAX];
541 };
542
543 struct snapshot_context {
544 struct tracing_map_elt *elt;
545 void *key;
546 };
547
548 static void track_data_free(struct track_data *track_data)
549 {
550 struct hist_elt_data *elt_data;
551
552 if (!track_data)
553 return;
554
555 kfree(track_data->key);
556
557 elt_data = track_data->elt.private_data;
558 if (elt_data) {
559 kfree(elt_data->comm);
560 kfree(elt_data);
561 }
562
563 kfree(track_data);
564 }
565
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)
569 {
570 struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
571 struct hist_elt_data *elt_data;
572
573 if (!data)
574 return ERR_PTR(-ENOMEM);
575
576 data->key = kzalloc(key_len, GFP_KERNEL);
577 if (!data->key) {
578 track_data_free(data);
579 return ERR_PTR(-ENOMEM);
580 }
581
582 data->key_len = key_len;
583 data->action_data = action_data;
584 data->hist_data = hist_data;
585
586 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
587 if (!elt_data) {
588 track_data_free(data);
589 return ERR_PTR(-ENOMEM);
590 }
591 data->elt.private_data = elt_data;
592
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);
597 }
598
599 return data;
600 }
601
602 static char last_cmd[MAX_FILTER_STR_VAL];
603 static char last_cmd_loc[MAX_FILTER_STR_VAL];
604
605 static int errpos(char *str)
606 {
607 return err_pos(last_cmd, str);
608 }
609
610 static void last_cmd_set(struct trace_event_file *file, char *str)
611 {
612 const char *system = NULL, *name = NULL;
613 struct trace_event_call *call;
614
615 if (!str)
616 return;
617
618 strcpy(last_cmd, "hist:");
619 strncat(last_cmd, str, MAX_FILTER_STR_VAL - 1 - sizeof("hist:"));
620
621 if (file) {
622 call = file->event_call;
623
624 system = call->class->system;
625 if (system) {
626 name = trace_event_name(call);
627 if (!name)
628 system = NULL;
629 }
630 }
631
632 if (system)
633 snprintf(last_cmd_loc, MAX_FILTER_STR_VAL, "hist:%s:%s", system, name);
634 }
635
636 static void hist_err(struct trace_array *tr, u8 err_type, u8 err_pos)
637 {
638 tracing_log_err(tr, last_cmd_loc, last_cmd, err_text,
639 err_type, err_pos);
640 }
641
642 static void hist_err_clear(void)
643 {
644 last_cmd[0] = '\0';
645 last_cmd_loc[0] = '\0';
646 }
647
648 struct synth_trace_event {
649 struct trace_entry ent;
650 u64 fields[];
651 };
652
653 static int synth_event_define_fields(struct trace_event_call *call)
654 {
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;
659 char *name, *type;
660 bool is_signed;
661 int ret = 0;
662
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);
670 if (ret)
671 break;
672
673 event->fields[i]->offset = n_u64;
674
675 if (event->fields[i]->is_string) {
676 offset += STR_VAR_LEN_MAX;
677 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
678 } else {
679 offset += sizeof(u64);
680 n_u64++;
681 }
682 }
683
684 event->n_u64 = n_u64;
685
686 return ret;
687 }
688
689 static bool synth_field_signed(char *type)
690 {
691 if (str_has_prefix(type, "u"))
692 return false;
693 if (strcmp(type, "gfp_t") == 0)
694 return false;
695
696 return true;
697 }
698
699 static int synth_field_is_string(char *type)
700 {
701 if (strstr(type, "char[") != NULL)
702 return true;
703
704 return false;
705 }
706
707 static int synth_field_string_size(char *type)
708 {
709 char buf[4], *end, *start;
710 unsigned int len;
711 int size, err;
712
713 start = strstr(type, "char[");
714 if (start == NULL)
715 return -EINVAL;
716 start += sizeof("char[") - 1;
717
718 end = strchr(type, ']');
719 if (!end || end < start)
720 return -EINVAL;
721
722 len = end - start;
723 if (len > 3)
724 return -EINVAL;
725
726 strncpy(buf, start, len);
727 buf[len] = '\0';
728
729 err = kstrtouint(buf, 0, &size);
730 if (err)
731 return err;
732
733 if (size > STR_VAR_LEN_MAX)
734 return -EINVAL;
735
736 return size;
737 }
738
739 static int synth_field_size(char *type)
740 {
741 int size = 0;
742
743 if (strcmp(type, "s64") == 0)
744 size = sizeof(s64);
745 else if (strcmp(type, "u64") == 0)
746 size = sizeof(u64);
747 else if (strcmp(type, "s32") == 0)
748 size = sizeof(s32);
749 else if (strcmp(type, "u32") == 0)
750 size = sizeof(u32);
751 else if (strcmp(type, "s16") == 0)
752 size = sizeof(s16);
753 else if (strcmp(type, "u16") == 0)
754 size = sizeof(u16);
755 else if (strcmp(type, "s8") == 0)
756 size = sizeof(s8);
757 else if (strcmp(type, "u8") == 0)
758 size = sizeof(u8);
759 else if (strcmp(type, "char") == 0)
760 size = sizeof(char);
761 else if (strcmp(type, "unsigned char") == 0)
762 size = sizeof(unsigned char);
763 else if (strcmp(type, "int") == 0)
764 size = sizeof(int);
765 else if (strcmp(type, "unsigned int") == 0)
766 size = sizeof(unsigned int);
767 else if (strcmp(type, "long") == 0)
768 size = sizeof(long);
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);
777
778 return size;
779 }
780
781 static const char *synth_field_fmt(char *type)
782 {
783 const char *fmt = "%llu";
784
785 if (strcmp(type, "s64") == 0)
786 fmt = "%lld";
787 else if (strcmp(type, "u64") == 0)
788 fmt = "%llu";
789 else if (strcmp(type, "s32") == 0)
790 fmt = "%d";
791 else if (strcmp(type, "u32") == 0)
792 fmt = "%u";
793 else if (strcmp(type, "s16") == 0)
794 fmt = "%d";
795 else if (strcmp(type, "u16") == 0)
796 fmt = "%u";
797 else if (strcmp(type, "s8") == 0)
798 fmt = "%d";
799 else if (strcmp(type, "u8") == 0)
800 fmt = "%u";
801 else if (strcmp(type, "char") == 0)
802 fmt = "%d";
803 else if (strcmp(type, "unsigned char") == 0)
804 fmt = "%u";
805 else if (strcmp(type, "int") == 0)
806 fmt = "%d";
807 else if (strcmp(type, "unsigned int") == 0)
808 fmt = "%u";
809 else if (strcmp(type, "long") == 0)
810 fmt = "%ld";
811 else if (strcmp(type, "unsigned long") == 0)
812 fmt = "%lu";
813 else if (strcmp(type, "pid_t") == 0)
814 fmt = "%d";
815 else if (strcmp(type, "gfp_t") == 0)
816 fmt = "%x";
817 else if (synth_field_is_string(type))
818 fmt = "%s";
819
820 return fmt;
821 }
822
823 static enum print_line_t print_synth_event(struct trace_iterator *iter,
824 int flags,
825 struct trace_event *event)
826 {
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;
832 char print_fmt[32];
833 const char *fmt;
834
835 entry = (struct synth_trace_event *)iter->ent;
836 se = container_of(event, struct synth_event, call.event);
837
838 trace_seq_printf(s, "%s: ", se->name);
839
840 for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
841 if (trace_seq_has_overflowed(s))
842 goto end;
843
844 fmt = synth_field_fmt(se->fields[i]->type);
845
846 /* parameter types */
847 if (tr && tr->trace_flags & TRACE_ITER_VERBOSE)
848 trace_seq_printf(s, "%s ", fmt);
849
850 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
851
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);
858 } else {
859 struct trace_print_flags __flags[] = {
860 __def_gfpflag_names, {-1, NULL} };
861
862 trace_seq_printf(s, print_fmt, se->fields[i]->name,
863 entry->fields[n_u64],
864 i == se->n_fields - 1 ? "" : " ");
865
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],
870 __flags);
871 trace_seq_putc(s, ')');
872 }
873 n_u64++;
874 }
875 }
876 end:
877 trace_seq_putc(s, '\n');
878
879 return trace_handle_return(s);
880 }
881
882 static struct trace_event_functions synth_event_funcs = {
883 .trace = print_synth_event
884 };
885
886 static notrace void trace_event_raw_event_synth(void *__data,
887 u64 *var_ref_vals,
888 unsigned int *var_ref_idx)
889 {
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;
896 int fields_size = 0;
897
898 event = trace_file->event_call->data;
899
900 if (trace_trigger_soft_disabled(trace_file))
901 return;
902
903 fields_size = event->n_u64 * sizeof(u64);
904
905 /*
906 * Avoid ring buffer recursion detection, as this event
907 * is being performed within another event.
908 */
909 buffer = trace_file->tr->array_buffer.buffer;
910 ring_buffer_nest_start(buffer);
911
912 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
913 sizeof(*entry) + fields_size);
914 if (!entry)
915 goto out;
916
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];
922
923 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
924 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
925 } else {
926 struct synth_field *field = event->fields[i];
927 u64 val = var_ref_vals[val_idx];
928
929 switch (field->size) {
930 case 1:
931 *(u8 *)&entry->fields[n_u64] = (u8)val;
932 break;
933
934 case 2:
935 *(u16 *)&entry->fields[n_u64] = (u16)val;
936 break;
937
938 case 4:
939 *(u32 *)&entry->fields[n_u64] = (u32)val;
940 break;
941
942 default:
943 entry->fields[n_u64] = val;
944 break;
945 }
946 n_u64++;
947 }
948 }
949
950 trace_event_buffer_commit(&fbuffer);
951 out:
952 ring_buffer_nest_end(buffer);
953 }
954
955 static void free_synth_event_print_fmt(struct trace_event_call *call)
956 {
957 if (call) {
958 kfree(call->print_fmt);
959 call->print_fmt = NULL;
960 }
961 }
962
963 static int __set_synth_event_print_fmt(struct synth_event *event,
964 char *buf, int len)
965 {
966 const char *fmt;
967 int pos = 0;
968 int i;
969
970 /* When len=0, we just calculate the needed length */
971 #define LEN_OR_ZERO (len ? len - pos : 0)
972
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 ? "" : ", ");
979 }
980 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
981
982 for (i = 0; i < event->n_fields; i++) {
983 pos += snprintf(buf + pos, LEN_OR_ZERO,
984 ", REC->%s", event->fields[i]->name);
985 }
986
987 #undef LEN_OR_ZERO
988
989 /* return the length of print_fmt */
990 return pos;
991 }
992
993 static int set_synth_event_print_fmt(struct trace_event_call *call)
994 {
995 struct synth_event *event = call->data;
996 char *print_fmt;
997 int len;
998
999 /* First: called with 0 length to calculate the needed length */
1000 len = __set_synth_event_print_fmt(event, NULL, 0);
1001
1002 print_fmt = kmalloc(len + 1, GFP_KERNEL);
1003 if (!print_fmt)
1004 return -ENOMEM;
1005
1006 /* Second: actually write the @print_fmt */
1007 __set_synth_event_print_fmt(event, print_fmt, len + 1);
1008 call->print_fmt = print_fmt;
1009
1010 return 0;
1011 }
1012
1013 static void free_synth_field(struct synth_field *field)
1014 {
1015 kfree(field->type);
1016 kfree(field->name);
1017 kfree(field);
1018 }
1019
1020 static struct synth_field *parse_synth_field(int argc, const char **argv,
1021 int *consumed)
1022 {
1023 struct synth_field *field;
1024 const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
1025 int len, ret = 0;
1026
1027 if (field_type[0] == ';')
1028 field_type++;
1029
1030 if (!strcmp(field_type, "unsigned")) {
1031 if (argc < 3)
1032 return ERR_PTR(-EINVAL);
1033 prefix = "unsigned ";
1034 field_type = argv[1];
1035 field_name = argv[2];
1036 *consumed = 3;
1037 } else {
1038 field_name = argv[1];
1039 *consumed = 2;
1040 }
1041
1042 field = kzalloc(sizeof(*field), GFP_KERNEL);
1043 if (!field)
1044 return ERR_PTR(-ENOMEM);
1045
1046 len = strlen(field_name);
1047 array = strchr(field_name, '[');
1048 if (array)
1049 len -= strlen(array);
1050 else if (field_name[len - 1] == ';')
1051 len--;
1052
1053 field->name = kmemdup_nul(field_name, len, GFP_KERNEL);
1054 if (!field->name) {
1055 ret = -ENOMEM;
1056 goto free;
1057 }
1058
1059 if (field_type[0] == ';')
1060 field_type++;
1061 len = strlen(field_type) + 1;
1062 if (array)
1063 len += strlen(array);
1064 if (prefix)
1065 len += strlen(prefix);
1066
1067 field->type = kzalloc(len, GFP_KERNEL);
1068 if (!field->type) {
1069 ret = -ENOMEM;
1070 goto free;
1071 }
1072 if (prefix)
1073 strcat(field->type, prefix);
1074 strcat(field->type, field_type);
1075 if (array) {
1076 strcat(field->type, array);
1077 if (field->type[len - 1] == ';')
1078 field->type[len - 1] = '\0';
1079 }
1080
1081 field->size = synth_field_size(field->type);
1082 if (!field->size) {
1083 ret = -EINVAL;
1084 goto free;
1085 }
1086
1087 if (synth_field_is_string(field->type))
1088 field->is_string = true;
1089
1090 field->is_signed = synth_field_signed(field->type);
1091
1092 out:
1093 return field;
1094 free:
1095 free_synth_field(field);
1096 field = ERR_PTR(ret);
1097 goto out;
1098 }
1099
1100 static void free_synth_tracepoint(struct tracepoint *tp)
1101 {
1102 if (!tp)
1103 return;
1104
1105 kfree(tp->name);
1106 kfree(tp);
1107 }
1108
1109 static struct tracepoint *alloc_synth_tracepoint(char *name)
1110 {
1111 struct tracepoint *tp;
1112
1113 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
1114 if (!tp)
1115 return ERR_PTR(-ENOMEM);
1116
1117 tp->name = kstrdup(name, GFP_KERNEL);
1118 if (!tp->name) {
1119 kfree(tp);
1120 return ERR_PTR(-ENOMEM);
1121 }
1122
1123 return tp;
1124 }
1125
1126 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
1127 unsigned int *var_ref_idx);
1128
1129 static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
1130 unsigned int *var_ref_idx)
1131 {
1132 struct tracepoint *tp = event->tp;
1133
1134 if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
1135 struct tracepoint_func *probe_func_ptr;
1136 synth_probe_func_t probe_func;
1137 void *__data;
1138
1139 if (!(cpu_online(raw_smp_processor_id())))
1140 return;
1141
1142 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
1143 if (probe_func_ptr) {
1144 do {
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);
1149 }
1150 }
1151 }
1152
1153 static struct synth_event *find_synth_event(const char *name)
1154 {
1155 struct dyn_event *pos;
1156 struct synth_event *event;
1157
1158 for_each_dyn_event(pos) {
1159 if (!is_synth_event(pos))
1160 continue;
1161 event = to_synth_event(pos);
1162 if (strcmp(event->name, name) == 0)
1163 return event;
1164 }
1165
1166 return NULL;
1167 }
1168
1169 static int register_synth_event(struct synth_event *event)
1170 {
1171 struct trace_event_call *call = &event->call;
1172 int ret = 0;
1173
1174 event->call.class = &event->class;
1175 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
1176 if (!event->class.system) {
1177 ret = -ENOMEM;
1178 goto out;
1179 }
1180
1181 event->tp = alloc_synth_tracepoint(event->name);
1182 if (IS_ERR(event->tp)) {
1183 ret = PTR_ERR(event->tp);
1184 event->tp = NULL;
1185 goto out;
1186 }
1187
1188 INIT_LIST_HEAD(&call->class->fields);
1189 call->event.funcs = &synth_event_funcs;
1190 call->class->define_fields = synth_event_define_fields;
1191
1192 ret = register_trace_event(&call->event);
1193 if (!ret) {
1194 ret = -ENODEV;
1195 goto out;
1196 }
1197 call->flags = TRACE_EVENT_FL_TRACEPOINT;
1198 call->class->reg = trace_event_reg;
1199 call->class->probe = trace_event_raw_event_synth;
1200 call->data = event;
1201 call->tp = event->tp;
1202
1203 ret = trace_add_event_call(call);
1204 if (ret) {
1205 pr_warn("Failed to register synthetic event: %s\n",
1206 trace_event_name(call));
1207 goto err;
1208 }
1209
1210 ret = set_synth_event_print_fmt(call);
1211 if (ret < 0) {
1212 trace_remove_event_call(call);
1213 goto err;
1214 }
1215 out:
1216 return ret;
1217 err:
1218 unregister_trace_event(&call->event);
1219 goto out;
1220 }
1221
1222 static int unregister_synth_event(struct synth_event *event)
1223 {
1224 struct trace_event_call *call = &event->call;
1225 int ret;
1226
1227 ret = trace_remove_event_call(call);
1228
1229 return ret;
1230 }
1231
1232 static void free_synth_event(struct synth_event *event)
1233 {
1234 unsigned int i;
1235
1236 if (!event)
1237 return;
1238
1239 for (i = 0; i < event->n_fields; i++)
1240 free_synth_field(event->fields[i]);
1241
1242 kfree(event->fields);
1243 kfree(event->name);
1244 kfree(event->class.system);
1245 free_synth_tracepoint(event->tp);
1246 free_synth_event_print_fmt(&event->call);
1247 kfree(event);
1248 }
1249
1250 static struct synth_event *alloc_synth_event(const char *name, int n_fields,
1251 struct synth_field **fields)
1252 {
1253 struct synth_event *event;
1254 unsigned int i;
1255
1256 event = kzalloc(sizeof(*event), GFP_KERNEL);
1257 if (!event) {
1258 event = ERR_PTR(-ENOMEM);
1259 goto out;
1260 }
1261
1262 event->name = kstrdup(name, GFP_KERNEL);
1263 if (!event->name) {
1264 kfree(event);
1265 event = ERR_PTR(-ENOMEM);
1266 goto out;
1267 }
1268
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);
1273 goto out;
1274 }
1275
1276 dyn_event_init(&event->devent, &synth_event_ops);
1277
1278 for (i = 0; i < n_fields; i++)
1279 event->fields[i] = fields[i];
1280
1281 event->n_fields = n_fields;
1282 out:
1283 return event;
1284 }
1285
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)
1290 {
1291 struct synth_event *event = data->synth_event;
1292
1293 trace_synth(event, var_ref_vals, data->var_ref_idx);
1294 }
1295
1296 struct hist_var_data {
1297 struct list_head list;
1298 struct hist_trigger_data *hist_data;
1299 };
1300
1301 static int synth_event_check_arg_fn(void *data)
1302 {
1303 struct dynevent_arg_pair *arg_pair = data;
1304 int size;
1305
1306 size = synth_field_size((char *)arg_pair->lhs);
1307
1308 return size ? 0 : -EINVAL;
1309 }
1310
1311 /**
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
1316 *
1317 * Add a new field to a synthetic event cmd object. Field ordering is in
1318 * the same order the fields are added.
1319 *
1320 * See synth_field_size() for available types. If field_name contains
1321 * [n] the field is considered to be an array.
1322 *
1323 * Return: 0 if successful, error otherwise.
1324 */
1325 int synth_event_add_field(struct dynevent_cmd *cmd, const char *type,
1326 const char *name)
1327 {
1328 struct dynevent_arg_pair arg_pair;
1329 int ret;
1330
1331 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1332 return -EINVAL;
1333
1334 if (!type || !name)
1335 return -EINVAL;
1336
1337 dynevent_arg_pair_init(&arg_pair, 0, ';');
1338
1339 arg_pair.lhs = type;
1340 arg_pair.rhs = name;
1341
1342 ret = dynevent_arg_pair_add(cmd, &arg_pair, synth_event_check_arg_fn);
1343 if (ret)
1344 return ret;
1345
1346 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
1347 ret = -EINVAL;
1348
1349 return ret;
1350 }
1351 EXPORT_SYMBOL_GPL(synth_event_add_field);
1352
1353 /**
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
1357 *
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.
1363 *
1364 * See synth_field_size() for available types. If field_name contains
1365 * [n] the field is considered to be an array.
1366 *
1367 * Return: 0 if successful, error otherwise.
1368 */
1369 int synth_event_add_field_str(struct dynevent_cmd *cmd, const char *type_name)
1370 {
1371 struct dynevent_arg arg;
1372 int ret;
1373
1374 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1375 return -EINVAL;
1376
1377 if (!type_name)
1378 return -EINVAL;
1379
1380 dynevent_arg_init(&arg, ';');
1381
1382 arg.str = type_name;
1383
1384 ret = dynevent_arg_add(cmd, &arg, NULL);
1385 if (ret)
1386 return ret;
1387
1388 if (++cmd->n_fields > SYNTH_FIELDS_MAX)
1389 ret = -EINVAL;
1390
1391 return ret;
1392 }
1393 EXPORT_SYMBOL_GPL(synth_event_add_field_str);
1394
1395 /**
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
1400 *
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.
1406 *
1407 * See synth_field_size() for available types. If field_name contains
1408 * [n] the field is considered to be an array.
1409 *
1410 * Return: 0 if successful, error otherwise.
1411 */
1412 int synth_event_add_fields(struct dynevent_cmd *cmd,
1413 struct synth_field_desc *fields,
1414 unsigned int n_fields)
1415 {
1416 unsigned int i;
1417 int ret = 0;
1418
1419 for (i = 0; i < n_fields; i++) {
1420 if (fields[i].type == NULL || fields[i].name == NULL) {
1421 ret = -EINVAL;
1422 break;
1423 }
1424
1425 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1426 if (ret)
1427 break;
1428 }
1429
1430 return ret;
1431 }
1432 EXPORT_SYMBOL_GPL(synth_event_add_fields);
1433
1434 /**
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
1440 *
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
1445 * arg list is NULL.
1446 *
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
1452 * this.
1453 *
1454 * There should be an even number variable args, each pair consisting
1455 * of a type followed by a field name.
1456 *
1457 * See synth_field_size() for available types. If field_name contains
1458 * [n] the field is considered to be an array.
1459 *
1460 * Return: 0 if successful, error otherwise.
1461 */
1462 int __synth_event_gen_cmd_start(struct dynevent_cmd *cmd, const char *name,
1463 struct module *mod, ...)
1464 {
1465 struct dynevent_arg arg;
1466 va_list args;
1467 int ret;
1468
1469 cmd->event_name = name;
1470 cmd->private_data = mod;
1471
1472 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1473 return -EINVAL;
1474
1475 dynevent_arg_init(&arg, 0);
1476 arg.str = name;
1477 ret = dynevent_arg_add(cmd, &arg, NULL);
1478 if (ret)
1479 return ret;
1480
1481 va_start(args, mod);
1482 for (;;) {
1483 const char *type, *name;
1484
1485 type = va_arg(args, const char *);
1486 if (!type)
1487 break;
1488 name = va_arg(args, const char *);
1489 if (!name)
1490 break;
1491
1492 if (++cmd->n_fields > SYNTH_FIELDS_MAX) {
1493 ret = -EINVAL;
1494 break;
1495 }
1496
1497 ret = synth_event_add_field(cmd, type, name);
1498 if (ret)
1499 break;
1500 }
1501 va_end(args);
1502
1503 return ret;
1504 }
1505 EXPORT_SYMBOL_GPL(__synth_event_gen_cmd_start);
1506
1507 /**
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
1513 *
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
1519 * this.
1520 *
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.
1525 *
1526 * See synth_field_size() for available types. If field_name contains
1527 * [n] the field is considered to be an array.
1528 *
1529 * Return: 0 if successful, error otherwise.
1530 */
1531 int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd, const char *name,
1532 struct module *mod,
1533 struct synth_field_desc *fields,
1534 unsigned int n_fields)
1535 {
1536 struct dynevent_arg arg;
1537 unsigned int i;
1538 int ret = 0;
1539
1540 cmd->event_name = name;
1541 cmd->private_data = mod;
1542
1543 if (cmd->type != DYNEVENT_TYPE_SYNTH)
1544 return -EINVAL;
1545
1546 if (n_fields > SYNTH_FIELDS_MAX)
1547 return -EINVAL;
1548
1549 dynevent_arg_init(&arg, 0);
1550 arg.str = name;
1551 ret = dynevent_arg_add(cmd, &arg, NULL);
1552 if (ret)
1553 return ret;
1554
1555 for (i = 0; i < n_fields; i++) {
1556 if (fields[i].type == NULL || fields[i].name == NULL)
1557 return -EINVAL;
1558
1559 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1560 if (ret)
1561 break;
1562 }
1563
1564 return ret;
1565 }
1566 EXPORT_SYMBOL_GPL(synth_event_gen_cmd_array_start);
1567
1568 static int __create_synth_event(int argc, const char *name, const char **argv)
1569 {
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;
1573
1574 /*
1575 * Argument syntax:
1576 * - Add synthetic event: <event_name> field[;field] ...
1577 * - Remove synthetic event: !<event_name> field[;field] ...
1578 * where 'field' = type field_name
1579 */
1580
1581 if (name[0] == '\0' || argc < 1)
1582 return -EINVAL;
1583
1584 mutex_lock(&event_mutex);
1585
1586 event = find_synth_event(name);
1587 if (event) {
1588 ret = -EEXIST;
1589 goto out;
1590 }
1591
1592 for (i = 0; i < argc - 1; i++) {
1593 if (strcmp(argv[i], ";") == 0)
1594 continue;
1595 if (n_fields == SYNTH_FIELDS_MAX) {
1596 ret = -EINVAL;
1597 goto err;
1598 }
1599
1600 field = parse_synth_field(argc - i, &argv[i], &consumed);
1601 if (IS_ERR(field)) {
1602 ret = PTR_ERR(field);
1603 goto err;
1604 }
1605 fields[n_fields++] = field;
1606 i += consumed - 1;
1607 }
1608
1609 if (i < argc && strcmp(argv[i], ";") != 0) {
1610 ret = -EINVAL;
1611 goto err;
1612 }
1613
1614 event = alloc_synth_event(name, n_fields, fields);
1615 if (IS_ERR(event)) {
1616 ret = PTR_ERR(event);
1617 event = NULL;
1618 goto err;
1619 }
1620 ret = register_synth_event(event);
1621 if (!ret)
1622 dyn_event_add(&event->devent);
1623 else
1624 free_synth_event(event);
1625 out:
1626 mutex_unlock(&event_mutex);
1627
1628 return ret;
1629 err:
1630 for (i = 0; i < n_fields; i++)
1631 free_synth_field(fields[i]);
1632
1633 goto out;
1634 }
1635
1636 /**
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
1642 *
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
1648 * fields array.
1649 *
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.
1653 *
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.
1657 *
1658 * Return: 0 if successful, error otherwise.
1659 */
1660 int synth_event_create(const char *name, struct synth_field_desc *fields,
1661 unsigned int n_fields, struct module *mod)
1662 {
1663 struct dynevent_cmd cmd;
1664 char *buf;
1665 int ret;
1666
1667 buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
1668 if (!buf)
1669 return -ENOMEM;
1670
1671 synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
1672
1673 ret = synth_event_gen_cmd_array_start(&cmd, name, mod,
1674 fields, n_fields);
1675 if (ret)
1676 goto out;
1677
1678 ret = synth_event_gen_cmd_end(&cmd);
1679 out:
1680 kfree(buf);
1681
1682 return ret;
1683 }
1684 EXPORT_SYMBOL_GPL(synth_event_create);
1685
1686 static int destroy_synth_event(struct synth_event *se)
1687 {
1688 int ret;
1689
1690 if (se->ref)
1691 ret = -EBUSY;
1692 else {
1693 ret = unregister_synth_event(se);
1694 if (!ret) {
1695 dyn_event_remove(&se->devent);
1696 free_synth_event(se);
1697 }
1698 }
1699
1700 return ret;
1701 }
1702
1703 /**
1704 * synth_event_delete - Delete a synthetic event
1705 * @event_name: The name of the new sythetic event
1706 *
1707 * Delete a synthetic event that was created with synth_event_create().
1708 *
1709 * Return: 0 if successful, error otherwise.
1710 */
1711 int synth_event_delete(const char *event_name)
1712 {
1713 struct synth_event *se = NULL;
1714 struct module *mod = NULL;
1715 int ret = -ENOENT;
1716
1717 mutex_lock(&event_mutex);
1718 se = find_synth_event(event_name);
1719 if (se) {
1720 mod = se->mod;
1721 ret = destroy_synth_event(se);
1722 }
1723 mutex_unlock(&event_mutex);
1724
1725 if (mod) {
1726 mutex_lock(&trace_types_lock);
1727 /*
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
1735 * occur.
1736 */
1737 tracing_reset_all_online_cpus();
1738 mutex_unlock(&trace_types_lock);
1739 }
1740
1741 return ret;
1742 }
1743 EXPORT_SYMBOL_GPL(synth_event_delete);
1744
1745 static int create_or_delete_synth_event(int argc, char **argv)
1746 {
1747 const char *name = argv[0];
1748 int ret;
1749
1750 /* trace_run_command() ensures argc != 0 */
1751 if (name[0] == '!') {
1752 ret = synth_event_delete(name + 1);
1753 return ret;
1754 }
1755
1756 ret = __create_synth_event(argc - 1, name, (const char **)argv + 1);
1757 return ret == -ECANCELED ? -EINVAL : ret;
1758 }
1759
1760 static int synth_event_run_command(struct dynevent_cmd *cmd)
1761 {
1762 struct synth_event *se;
1763 int ret;
1764
1765 ret = trace_run_command(cmd->seq.buffer, create_or_delete_synth_event);
1766 if (ret)
1767 return ret;
1768
1769 se = find_synth_event(cmd->event_name);
1770 if (WARN_ON(!se))
1771 return -ENOENT;
1772
1773 se->mod = cmd->private_data;
1774
1775 return ret;
1776 }
1777
1778 /**
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
1783 *
1784 * Initialize a synthetic event command object. Use this before
1785 * calling any of the other dyenvent_cmd functions.
1786 */
1787 void synth_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
1788 {
1789 dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_SYNTH,
1790 synth_event_run_command);
1791 }
1792 EXPORT_SYMBOL_GPL(synth_event_cmd_init);
1793
1794 /**
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
1799 *
1800 * Trace a synthetic event using the values passed in the variable
1801 * argument list.
1802 *
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.
1806 *
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.
1810 *
1811 * Return: 0 on success, err otherwise.
1812 */
1813 int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
1814 {
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;
1821 va_list args;
1822 int ret = 0;
1823
1824 /*
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.
1830 */
1831 if (!(file->flags & EVENT_FILE_FL_ENABLED) ||
1832 trace_trigger_soft_disabled(file))
1833 return 0;
1834
1835 event = file->event_call->data;
1836
1837 if (n_vals != event->n_fields)
1838 return -EINVAL;
1839
1840 fields_size = event->n_u64 * sizeof(u64);
1841
1842 /*
1843 * Avoid ring buffer recursion detection, as this event
1844 * is being performed within another event.
1845 */
1846 buffer = file->tr->array_buffer.buffer;
1847 ring_buffer_nest_start(buffer);
1848
1849 entry = trace_event_buffer_reserve(&fbuffer, file,
1850 sizeof(*entry) + fields_size);
1851 if (!entry) {
1852 ret = -EINVAL;
1853 goto out;
1854 }
1855
1856 va_start(args, n_vals);
1857 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
1858 u64 val;
1859
1860 val = va_arg(args, u64);
1861
1862 if (event->fields[i]->is_string) {
1863 char *str_val = (char *)(long)val;
1864 char *str_field = (char *)&entry->fields[n_u64];
1865
1866 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
1867 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
1868 } else {
1869 entry->fields[n_u64] = val;
1870 n_u64++;
1871 }
1872 }
1873 va_end(args);
1874
1875 trace_event_buffer_commit(&fbuffer);
1876 out:
1877 ring_buffer_nest_end(buffer);
1878
1879 return ret;
1880 }
1881 EXPORT_SYMBOL_GPL(synth_event_trace);
1882
1883 /**
1884 * synth_event_trace_array - Trace a synthetic event from an array
1885 * @file: The trace_event_file representing the synthetic event
1886 * @vals: Array of values
1887 * @n_vals: The number of values in vals
1888 *
1889 * Trace a synthetic event using the values passed in as 'vals'.
1890 *
1891 * The 'vals' array is just an array of 'n_vals' u64. The number of
1892 * vals must match the number of field in the synthetic event, and
1893 * must be in the same order as the synthetic event fields.
1894 *
1895 * All vals should be cast to u64, and string vals are just pointers
1896 * to strings, cast to u64. Strings will be copied into space
1897 * reserved in the event for the string, using these pointers.
1898 *
1899 * Return: 0 on success, err otherwise.
1900 */
1901 int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
1902 unsigned int n_vals)
1903 {
1904 struct trace_event_buffer fbuffer;
1905 struct synth_trace_event *entry;
1906 struct trace_buffer *buffer;
1907 struct synth_event *event;
1908 unsigned int i, n_u64;
1909 int fields_size = 0;
1910 int ret = 0;
1911
1912 /*
1913 * Normal event generation doesn't get called at all unless
1914 * the ENABLED bit is set (which attaches the probe thus
1915 * allowing this code to be called, etc). Because this is
1916 * called directly by the user, we don't have that but we
1917 * still need to honor not logging when disabled.
1918 */
1919 if (!(file->flags & EVENT_FILE_FL_ENABLED) ||
1920 trace_trigger_soft_disabled(file))
1921 return 0;
1922
1923 event = file->event_call->data;
1924
1925 if (n_vals != event->n_fields)
1926 return -EINVAL;
1927
1928 fields_size = event->n_u64 * sizeof(u64);
1929
1930 /*
1931 * Avoid ring buffer recursion detection, as this event
1932 * is being performed within another event.
1933 */
1934 buffer = file->tr->array_buffer.buffer;
1935 ring_buffer_nest_start(buffer);
1936
1937 entry = trace_event_buffer_reserve(&fbuffer, file,
1938 sizeof(*entry) + fields_size);
1939 if (!entry) {
1940 ret = -EINVAL;
1941 goto out;
1942 }
1943
1944 for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
1945 if (event->fields[i]->is_string) {
1946 char *str_val = (char *)(long)vals[i];
1947 char *str_field = (char *)&entry->fields[n_u64];
1948
1949 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
1950 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
1951 } else {
1952 entry->fields[n_u64] = vals[i];
1953 n_u64++;
1954 }
1955 }
1956
1957 trace_event_buffer_commit(&fbuffer);
1958 out:
1959 ring_buffer_nest_end(buffer);
1960
1961 return ret;
1962 }
1963 EXPORT_SYMBOL_GPL(synth_event_trace_array);
1964
1965 /**
1966 * synth_event_trace_start - Start piecewise synthetic event trace
1967 * @file: The trace_event_file representing the synthetic event
1968 * @trace_state: A pointer to object tracking the piecewise trace state
1969 *
1970 * Start the trace of a synthetic event field-by-field rather than all
1971 * at once.
1972 *
1973 * This function 'opens' an event trace, which means space is reserved
1974 * for the event in the trace buffer, after which the event's
1975 * individual field values can be set through either
1976 * synth_event_add_next_val() or synth_event_add_val().
1977 *
1978 * A pointer to a trace_state object is passed in, which will keep
1979 * track of the current event trace state until the event trace is
1980 * closed (and the event finally traced) using
1981 * synth_event_trace_end().
1982 *
1983 * Note that synth_event_trace_end() must be called after all values
1984 * have been added for each event trace, regardless of whether adding
1985 * all field values succeeded or not.
1986 *
1987 * Note also that for a given event trace, all fields must be added
1988 * using either synth_event_add_next_val() or synth_event_add_val()
1989 * but not both together or interleaved.
1990 *
1991 * Return: 0 on success, err otherwise.
1992 */
1993 int synth_event_trace_start(struct trace_event_file *file,
1994 struct synth_event_trace_state *trace_state)
1995 {
1996 struct synth_trace_event *entry;
1997 int fields_size = 0;
1998 int ret = 0;
1999
2000 if (!trace_state) {
2001 ret = -EINVAL;
2002 goto out;
2003 }
2004
2005 memset(trace_state, '\0', sizeof(*trace_state));
2006
2007 /*
2008 * Normal event tracing doesn't get called at all unless the
2009 * ENABLED bit is set (which attaches the probe thus allowing
2010 * this code to be called, etc). Because this is called
2011 * directly by the user, we don't have that but we still need
2012 * to honor not logging when disabled. For the the iterated
2013 * trace case, we save the enabed state upon start and just
2014 * ignore the following data calls.
2015 */
2016 if (!(file->flags & EVENT_FILE_FL_ENABLED) ||
2017 trace_trigger_soft_disabled(file)) {
2018 trace_state->enabled = false;
2019 goto out;
2020 }
2021
2022 trace_state->enabled = true;
2023
2024 trace_state->event = file->event_call->data;
2025
2026 fields_size = trace_state->event->n_u64 * sizeof(u64);
2027
2028 /*
2029 * Avoid ring buffer recursion detection, as this event
2030 * is being performed within another event.
2031 */
2032 trace_state->buffer = file->tr->array_buffer.buffer;
2033 ring_buffer_nest_start(trace_state->buffer);
2034
2035 entry = trace_event_buffer_reserve(&trace_state->fbuffer, file,
2036 sizeof(*entry) + fields_size);
2037 if (!entry) {
2038 ring_buffer_nest_end(trace_state->buffer);
2039 ret = -EINVAL;
2040 goto out;
2041 }
2042
2043 trace_state->entry = entry;
2044 out:
2045 return ret;
2046 }
2047 EXPORT_SYMBOL_GPL(synth_event_trace_start);
2048
2049 static int __synth_event_add_val(const char *field_name, u64 val,
2050 struct synth_event_trace_state *trace_state)
2051 {
2052 struct synth_field *field = NULL;
2053 struct synth_trace_event *entry;
2054 struct synth_event *event;
2055 int i, ret = 0;
2056
2057 if (!trace_state) {
2058 ret = -EINVAL;
2059 goto out;
2060 }
2061
2062 /* can't mix add_next_synth_val() with add_synth_val() */
2063 if (field_name) {
2064 if (trace_state->add_next) {
2065 ret = -EINVAL;
2066 goto out;
2067 }
2068 trace_state->add_name = true;
2069 } else {
2070 if (trace_state->add_name) {
2071 ret = -EINVAL;
2072 goto out;
2073 }
2074 trace_state->add_next = true;
2075 }
2076
2077 if (!trace_state->enabled)
2078 goto out;
2079
2080 event = trace_state->event;
2081 if (trace_state->add_name) {
2082 for (i = 0; i < event->n_fields; i++) {
2083 field = event->fields[i];
2084 if (strcmp(field->name, field_name) == 0)
2085 break;
2086 }
2087 if (!field) {
2088 ret = -EINVAL;
2089 goto out;
2090 }
2091 } else {
2092 if (trace_state->cur_field >= event->n_fields) {
2093 ret = -EINVAL;
2094 goto out;
2095 }
2096 field = event->fields[trace_state->cur_field++];
2097 }
2098
2099 entry = trace_state->entry;
2100 if (field->is_string) {
2101 char *str_val = (char *)(long)val;
2102 char *str_field;
2103
2104 if (!str_val) {
2105 ret = -EINVAL;
2106 goto out;
2107 }
2108
2109 str_field = (char *)&entry->fields[field->offset];
2110 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
2111 } else
2112 entry->fields[field->offset] = val;
2113 out:
2114 return ret;
2115 }
2116
2117 /**
2118 * synth_event_add_next_val - Add the next field's value to an open synth trace
2119 * @val: The value to set the next field to
2120 * @trace_state: A pointer to object tracking the piecewise trace state
2121 *
2122 * Set the value of the next field in an event that's been opened by
2123 * synth_event_trace_start().
2124 *
2125 * The val param should be the value cast to u64. If the value points
2126 * to a string, the val param should be a char * cast to u64.
2127 *
2128 * This function assumes all the fields in an event are to be set one
2129 * after another - successive calls to this function are made, one for
2130 * each field, in the order of the fields in the event, until all
2131 * fields have been set. If you'd rather set each field individually
2132 * without regard to ordering, synth_event_add_val() can be used
2133 * instead.
2134 *
2135 * Note however that synth_event_add_next_val() and
2136 * synth_event_add_val() can't be intermixed for a given event trace -
2137 * one or the other but not both can be used at the same time.
2138 *
2139 * Note also that synth_event_trace_end() must be called after all
2140 * values have been added for each event trace, regardless of whether
2141 * adding all field values succeeded or not.
2142 *
2143 * Return: 0 on success, err otherwise.
2144 */
2145 int synth_event_add_next_val(u64 val,
2146 struct synth_event_trace_state *trace_state)
2147 {
2148 return __synth_event_add_val(NULL, val, trace_state);
2149 }
2150 EXPORT_SYMBOL_GPL(synth_event_add_next_val);
2151
2152 /**
2153 * synth_event_add_val - Add a named field's value to an open synth trace
2154 * @field_name: The name of the synthetic event field value to set
2155 * @val: The value to set the next field to
2156 * @trace_state: A pointer to object tracking the piecewise trace state
2157 *
2158 * Set the value of the named field in an event that's been opened by
2159 * synth_event_trace_start().
2160 *
2161 * The val param should be the value cast to u64. If the value points
2162 * to a string, the val param should be a char * cast to u64.
2163 *
2164 * This function looks up the field name, and if found, sets the field
2165 * to the specified value. This lookup makes this function more
2166 * expensive than synth_event_add_next_val(), so use that or the
2167 * none-piecewise synth_event_trace() instead if efficiency is more
2168 * important.
2169 *
2170 * Note however that synth_event_add_next_val() and
2171 * synth_event_add_val() can't be intermixed for a given event trace -
2172 * one or the other but not both can be used at the same time.
2173 *
2174 * Note also that synth_event_trace_end() must be called after all
2175 * values have been added for each event trace, regardless of whether
2176 * adding all field values succeeded or not.
2177 *
2178 * Return: 0 on success, err otherwise.
2179 */
2180 int synth_event_add_val(const char *field_name, u64 val,
2181 struct synth_event_trace_state *trace_state)
2182 {
2183 return __synth_event_add_val(field_name, val, trace_state);
2184 }
2185 EXPORT_SYMBOL_GPL(synth_event_add_val);
2186
2187 /**
2188 * synth_event_trace_end - End piecewise synthetic event trace
2189 * @trace_state: A pointer to object tracking the piecewise trace state
2190 *
2191 * End the trace of a synthetic event opened by
2192 * synth_event_trace__start().
2193 *
2194 * This function 'closes' an event trace, which basically means that
2195 * it commits the reserved event and cleans up other loose ends.
2196 *
2197 * A pointer to a trace_state object is passed in, which will keep
2198 * track of the current event trace state opened with
2199 * synth_event_trace_start().
2200 *
2201 * Note that this function must be called after all values have been
2202 * added for each event trace, regardless of whether adding all field
2203 * values succeeded or not.
2204 *
2205 * Return: 0 on success, err otherwise.
2206 */
2207 int synth_event_trace_end(struct synth_event_trace_state *trace_state)
2208 {
2209 if (!trace_state)
2210 return -EINVAL;
2211
2212 trace_event_buffer_commit(&trace_state->fbuffer);
2213
2214 ring_buffer_nest_end(trace_state->buffer);
2215
2216 return 0;
2217 }
2218 EXPORT_SYMBOL_GPL(synth_event_trace_end);
2219
2220 static int create_synth_event(int argc, const char **argv)
2221 {
2222 const char *name = argv[0];
2223 int len;
2224
2225 if (name[0] != 's' || name[1] != ':')
2226 return -ECANCELED;
2227 name += 2;
2228
2229 /* This interface accepts group name prefix */
2230 if (strchr(name, '/')) {
2231 len = str_has_prefix(name, SYNTH_SYSTEM "/");
2232 if (len == 0)
2233 return -EINVAL;
2234 name += len;
2235 }
2236 return __create_synth_event(argc - 1, name, argv + 1);
2237 }
2238
2239 static int synth_event_release(struct dyn_event *ev)
2240 {
2241 struct synth_event *event = to_synth_event(ev);
2242 int ret;
2243
2244 if (event->ref)
2245 return -EBUSY;
2246
2247 ret = unregister_synth_event(event);
2248 if (ret)
2249 return ret;
2250
2251 dyn_event_remove(ev);
2252 free_synth_event(event);
2253 return 0;
2254 }
2255
2256 static int __synth_event_show(struct seq_file *m, struct synth_event *event)
2257 {
2258 struct synth_field *field;
2259 unsigned int i;
2260
2261 seq_printf(m, "%s\t", event->name);
2262
2263 for (i = 0; i < event->n_fields; i++) {
2264 field = event->fields[i];
2265
2266 /* parameter values */
2267 seq_printf(m, "%s %s%s", field->type, field->name,
2268 i == event->n_fields - 1 ? "" : "; ");
2269 }
2270
2271 seq_putc(m, '\n');
2272
2273 return 0;
2274 }
2275
2276 static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
2277 {
2278 struct synth_event *event = to_synth_event(ev);
2279
2280 seq_printf(m, "s:%s/", event->class.system);
2281
2282 return __synth_event_show(m, event);
2283 }
2284
2285 static int synth_events_seq_show(struct seq_file *m, void *v)
2286 {
2287 struct dyn_event *ev = v;
2288
2289 if (!is_synth_event(ev))
2290 return 0;
2291
2292 return __synth_event_show(m, to_synth_event(ev));
2293 }
2294
2295 static const struct seq_operations synth_events_seq_op = {
2296 .start = dyn_event_seq_start,
2297 .next = dyn_event_seq_next,
2298 .stop = dyn_event_seq_stop,
2299 .show = synth_events_seq_show,
2300 };
2301
2302 static int synth_events_open(struct inode *inode, struct file *file)
2303 {
2304 int ret;
2305
2306 ret = security_locked_down(LOCKDOWN_TRACEFS);
2307 if (ret)
2308 return ret;
2309
2310 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
2311 ret = dyn_events_release_all(&synth_event_ops);
2312 if (ret < 0)
2313 return ret;
2314 }
2315
2316 return seq_open(file, &synth_events_seq_op);
2317 }
2318
2319 static ssize_t synth_events_write(struct file *file,
2320 const char __user *buffer,
2321 size_t count, loff_t *ppos)
2322 {
2323 return trace_parse_run_command(file, buffer, count, ppos,
2324 create_or_delete_synth_event);
2325 }
2326
2327 static const struct file_operations synth_events_fops = {
2328 .open = synth_events_open,
2329 .write = synth_events_write,
2330 .read = seq_read,
2331 .llseek = seq_lseek,
2332 .release = seq_release,
2333 };
2334
2335 static u64 hist_field_timestamp(struct hist_field *hist_field,
2336 struct tracing_map_elt *elt,
2337 struct ring_buffer_event *rbe,
2338 void *event)
2339 {
2340 struct hist_trigger_data *hist_data = hist_field->hist_data;
2341 struct trace_array *tr = hist_data->event_file->tr;
2342
2343 u64 ts = ring_buffer_event_time_stamp(rbe);
2344
2345 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
2346 ts = ns2usecs(ts);
2347
2348 return ts;
2349 }
2350
2351 static u64 hist_field_cpu(struct hist_field *hist_field,
2352 struct tracing_map_elt *elt,
2353 struct ring_buffer_event *rbe,
2354 void *event)
2355 {
2356 int cpu = smp_processor_id();
2357
2358 return cpu;
2359 }
2360
2361 /**
2362 * check_field_for_var_ref - Check if a VAR_REF field references a variable
2363 * @hist_field: The VAR_REF field to check
2364 * @var_data: The hist trigger that owns the variable
2365 * @var_idx: The trigger variable identifier
2366 *
2367 * Check the given VAR_REF field to see whether or not it references
2368 * the given variable associated with the given trigger.
2369 *
2370 * Return: The VAR_REF field if it does reference the variable, NULL if not
2371 */
2372 static struct hist_field *
2373 check_field_for_var_ref(struct hist_field *hist_field,
2374 struct hist_trigger_data *var_data,
2375 unsigned int var_idx)
2376 {
2377 WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
2378
2379 if (hist_field && hist_field->var.idx == var_idx &&
2380 hist_field->var.hist_data == var_data)
2381 return hist_field;
2382
2383 return NULL;
2384 }
2385
2386 /**
2387 * find_var_ref - Check if a trigger has a reference to a trigger variable
2388 * @hist_data: The hist trigger that might have a reference to the variable
2389 * @var_data: The hist trigger that owns the variable
2390 * @var_idx: The trigger variable identifier
2391 *
2392 * Check the list of var_refs[] on the first hist trigger to see
2393 * whether any of them are references to the variable on the second
2394 * trigger.
2395 *
2396 * Return: The VAR_REF field referencing the variable if so, NULL if not
2397 */
2398 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
2399 struct hist_trigger_data *var_data,
2400 unsigned int var_idx)
2401 {
2402 struct hist_field *hist_field;
2403 unsigned int i;
2404
2405 for (i = 0; i < hist_data->n_var_refs; i++) {
2406 hist_field = hist_data->var_refs[i];
2407 if (check_field_for_var_ref(hist_field, var_data, var_idx))
2408 return hist_field;
2409 }
2410
2411 return NULL;
2412 }
2413
2414 /**
2415 * find_any_var_ref - Check if there is a reference to a given trigger variable
2416 * @hist_data: The hist trigger
2417 * @var_idx: The trigger variable identifier
2418 *
2419 * Check to see whether the given variable is currently referenced by
2420 * any other trigger.
2421 *
2422 * The trigger the variable is defined on is explicitly excluded - the
2423 * assumption being that a self-reference doesn't prevent a trigger
2424 * from being removed.
2425 *
2426 * Return: The VAR_REF field referencing the variable if so, NULL if not
2427 */
2428 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
2429 unsigned int var_idx)
2430 {
2431 struct trace_array *tr = hist_data->event_file->tr;
2432 struct hist_field *found = NULL;
2433 struct hist_var_data *var_data;
2434
2435 list_for_each_entry(var_data, &tr->hist_vars, list) {
2436 if (var_data->hist_data == hist_data)
2437 continue;
2438 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
2439 if (found)
2440 break;
2441 }
2442
2443 return found;
2444 }
2445
2446 /**
2447 * check_var_refs - Check if there is a reference to any of trigger's variables
2448 * @hist_data: The hist trigger
2449 *
2450 * A trigger can define one or more variables. If any one of them is
2451 * currently referenced by any other trigger, this function will
2452 * determine that.
2453
2454 * Typically used to determine whether or not a trigger can be removed
2455 * - if there are any references to a trigger's variables, it cannot.
2456 *
2457 * Return: True if there is a reference to any of trigger's variables
2458 */
2459 static bool check_var_refs(struct hist_trigger_data *hist_data)
2460 {
2461 struct hist_field *field;
2462 bool found = false;
2463 int i;
2464
2465 for_each_hist_field(i, hist_data) {
2466 field = hist_data->fields[i];
2467 if (field && field->flags & HIST_FIELD_FL_VAR) {
2468 if (find_any_var_ref(hist_data, field->var.idx)) {
2469 found = true;
2470 break;
2471 }
2472 }
2473 }
2474
2475 return found;
2476 }
2477
2478 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
2479 {
2480 struct trace_array *tr = hist_data->event_file->tr;
2481 struct hist_var_data *var_data, *found = NULL;
2482
2483 list_for_each_entry(var_data, &tr->hist_vars, list) {
2484 if (var_data->hist_data == hist_data) {
2485 found = var_data;
2486 break;
2487 }
2488 }
2489
2490 return found;
2491 }
2492
2493 static bool field_has_hist_vars(struct hist_field *hist_field,
2494 unsigned int level)
2495 {
2496 int i;
2497
2498 if (level > 3)
2499 return false;
2500
2501 if (!hist_field)
2502 return false;
2503
2504 if (hist_field->flags & HIST_FIELD_FL_VAR ||
2505 hist_field->flags & HIST_FIELD_FL_VAR_REF)
2506 return true;
2507
2508 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
2509 struct hist_field *operand;
2510
2511 operand = hist_field->operands[i];
2512 if (field_has_hist_vars(operand, level + 1))
2513 return true;
2514 }
2515
2516 return false;
2517 }
2518
2519 static bool has_hist_vars(struct hist_trigger_data *hist_data)
2520 {
2521 struct hist_field *hist_field;
2522 int i;
2523
2524 for_each_hist_field(i, hist_data) {
2525 hist_field = hist_data->fields[i];
2526 if (field_has_hist_vars(hist_field, 0))
2527 return true;
2528 }
2529
2530 return false;
2531 }
2532
2533 static int save_hist_vars(struct hist_trigger_data *hist_data)
2534 {
2535 struct trace_array *tr = hist_data->event_file->tr;
2536 struct hist_var_data *var_data;
2537
2538 var_data = find_hist_vars(hist_data);
2539 if (var_data)
2540 return 0;
2541
2542 if (tracing_check_open_get_tr(tr))
2543 return -ENODEV;
2544
2545 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
2546 if (!var_data) {
2547 trace_array_put(tr);
2548 return -ENOMEM;
2549 }
2550
2551 var_data->hist_data = hist_data;
2552 list_add(&var_data->list, &tr->hist_vars);
2553
2554 return 0;
2555 }
2556
2557 static void remove_hist_vars(struct hist_trigger_data *hist_data)
2558 {
2559 struct trace_array *tr = hist_data->event_file->tr;
2560 struct hist_var_data *var_data;
2561
2562 var_data = find_hist_vars(hist_data);
2563 if (!var_data)
2564 return;
2565
2566 if (WARN_ON(check_var_refs(hist_data)))
2567 return;
2568
2569 list_del(&var_data->list);
2570
2571 kfree(var_data);
2572
2573 trace_array_put(tr);
2574 }
2575
2576 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
2577 const char *var_name)
2578 {
2579 struct hist_field *hist_field, *found = NULL;
2580 int i;
2581
2582 for_each_hist_field(i, hist_data) {
2583 hist_field = hist_data->fields[i];
2584 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
2585 strcmp(hist_field->var.name, var_name) == 0) {
2586 found = hist_field;
2587 break;
2588 }
2589 }
2590
2591 return found;
2592 }
2593
2594 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
2595 struct trace_event_file *file,
2596 const char *var_name)
2597 {
2598 struct hist_trigger_data *test_data;
2599 struct event_trigger_data *test;
2600 struct hist_field *hist_field;
2601
2602 lockdep_assert_held(&event_mutex);
2603
2604 hist_field = find_var_field(hist_data, var_name);
2605 if (hist_field)
2606 return hist_field;
2607
2608 list_for_each_entry(test, &file->triggers, list) {
2609 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2610 test_data = test->private_data;
2611 hist_field = find_var_field(test_data, var_name);
2612 if (hist_field)
2613 return hist_field;
2614 }
2615 }
2616
2617 return NULL;
2618 }
2619
2620 static struct trace_event_file *find_var_file(struct trace_array *tr,
2621 char *system,
2622 char *event_name,
2623 char *var_name)
2624 {
2625 struct hist_trigger_data *var_hist_data;
2626 struct hist_var_data *var_data;
2627 struct trace_event_file *file, *found = NULL;
2628
2629 if (system)
2630 return find_event_file(tr, system, event_name);
2631
2632 list_for_each_entry(var_data, &tr->hist_vars, list) {
2633 var_hist_data = var_data->hist_data;
2634 file = var_hist_data->event_file;
2635 if (file == found)
2636 continue;
2637
2638 if (find_var_field(var_hist_data, var_name)) {
2639 if (found) {
2640 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name));
2641 return NULL;
2642 }
2643
2644 found = file;
2645 }
2646 }
2647
2648 return found;
2649 }
2650
2651 static struct hist_field *find_file_var(struct trace_event_file *file,
2652 const char *var_name)
2653 {
2654 struct hist_trigger_data *test_data;
2655 struct event_trigger_data *test;
2656 struct hist_field *hist_field;
2657
2658 lockdep_assert_held(&event_mutex);
2659
2660 list_for_each_entry(test, &file->triggers, list) {
2661 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
2662 test_data = test->private_data;
2663 hist_field = find_var_field(test_data, var_name);
2664 if (hist_field)
2665 return hist_field;
2666 }
2667 }
2668
2669 return NULL;
2670 }
2671
2672 static struct hist_field *
2673 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
2674 {
2675 struct trace_array *tr = hist_data->event_file->tr;
2676 struct hist_field *hist_field, *found = NULL;
2677 struct trace_event_file *file;
2678 unsigned int i;
2679
2680 for (i = 0; i < hist_data->n_actions; i++) {
2681 struct action_data *data = hist_data->actions[i];
2682
2683 if (data->handler == HANDLER_ONMATCH) {
2684 char *system = data->match_data.event_system;
2685 char *event_name = data->match_data.event;
2686
2687 file = find_var_file(tr, system, event_name, var_name);
2688 if (!file)
2689 continue;
2690 hist_field = find_file_var(file, var_name);
2691 if (hist_field) {
2692 if (found) {
2693 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE,
2694 errpos(var_name));
2695 return ERR_PTR(-EINVAL);
2696 }
2697
2698 found = hist_field;
2699 }
2700 }
2701 }
2702 return found;
2703 }
2704
2705 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
2706 char *system,
2707 char *event_name,
2708 char *var_name)
2709 {
2710 struct trace_array *tr = hist_data->event_file->tr;
2711 struct hist_field *hist_field = NULL;
2712 struct trace_event_file *file;
2713
2714 if (!system || !event_name) {
2715 hist_field = find_match_var(hist_data, var_name);
2716 if (IS_ERR(hist_field))
2717 return NULL;
2718 if (hist_field)
2719 return hist_field;
2720 }
2721
2722 file = find_var_file(tr, system, event_name, var_name);
2723 if (!file)
2724 return NULL;
2725
2726 hist_field = find_file_var(file, var_name);
2727
2728 return hist_field;
2729 }
2730
2731 static u64 hist_field_var_ref(struct hist_field *hist_field,
2732 struct tracing_map_elt *elt,
2733 struct ring_buffer_event *rbe,
2734 void *event)
2735 {
2736 struct hist_elt_data *elt_data;
2737 u64 var_val = 0;
2738
2739 if (WARN_ON_ONCE(!elt))
2740 return var_val;
2741
2742 elt_data = elt->private_data;
2743 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
2744
2745 return var_val;
2746 }
2747
2748 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
2749 u64 *var_ref_vals, bool self)
2750 {
2751 struct hist_trigger_data *var_data;
2752 struct tracing_map_elt *var_elt;
2753 struct hist_field *hist_field;
2754 unsigned int i, var_idx;
2755 bool resolved = true;
2756 u64 var_val = 0;
2757
2758 for (i = 0; i < hist_data->n_var_refs; i++) {
2759 hist_field = hist_data->var_refs[i];
2760 var_idx = hist_field->var.idx;
2761 var_data = hist_field->var.hist_data;
2762
2763 if (var_data == NULL) {
2764 resolved = false;
2765 break;
2766 }
2767
2768 if ((self && var_data != hist_data) ||
2769 (!self && var_data == hist_data))
2770 continue;
2771
2772 var_elt = tracing_map_lookup(var_data->map, key);
2773 if (!var_elt) {
2774 resolved = false;
2775 break;
2776 }
2777
2778 if (!tracing_map_var_set(var_elt, var_idx)) {
2779 resolved = false;
2780 break;
2781 }
2782
2783 if (self || !hist_field->read_once)
2784 var_val = tracing_map_read_var(var_elt, var_idx);
2785 else
2786 var_val = tracing_map_read_var_once(var_elt, var_idx);
2787
2788 var_ref_vals[i] = var_val;
2789 }
2790
2791 return resolved;
2792 }
2793
2794 static const char *hist_field_name(struct hist_field *field,
2795 unsigned int level)
2796 {
2797 const char *field_name = "";
2798
2799 if (level > 1)
2800 return field_name;
2801
2802 if (field->field)
2803 field_name = field->field->name;
2804 else if (field->flags & HIST_FIELD_FL_LOG2 ||
2805 field->flags & HIST_FIELD_FL_ALIAS)
2806 field_name = hist_field_name(field->operands[0], ++level);
2807 else if (field->flags & HIST_FIELD_FL_CPU)
2808 field_name = "cpu";
2809 else if (field->flags & HIST_FIELD_FL_EXPR ||
2810 field->flags & HIST_FIELD_FL_VAR_REF) {
2811 if (field->system) {
2812 static char full_name[MAX_FILTER_STR_VAL];
2813
2814 strcat(full_name, field->system);
2815 strcat(full_name, ".");
2816 strcat(full_name, field->event_name);
2817 strcat(full_name, ".");
2818 strcat(full_name, field->name);
2819 field_name = full_name;
2820 } else
2821 field_name = field->name;
2822 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
2823 field_name = "common_timestamp";
2824
2825 if (field_name == NULL)
2826 field_name = "";
2827
2828 return field_name;
2829 }
2830
2831 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
2832 {
2833 hist_field_fn_t fn = NULL;
2834
2835 switch (field_size) {
2836 case 8:
2837 if (field_is_signed)
2838 fn = hist_field_s64;
2839 else
2840 fn = hist_field_u64;
2841 break;
2842 case 4:
2843 if (field_is_signed)
2844 fn = hist_field_s32;
2845 else
2846 fn = hist_field_u32;
2847 break;
2848 case 2:
2849 if (field_is_signed)
2850 fn = hist_field_s16;
2851 else
2852 fn = hist_field_u16;
2853 break;
2854 case 1:
2855 if (field_is_signed)
2856 fn = hist_field_s8;
2857 else
2858 fn = hist_field_u8;
2859 break;
2860 }
2861
2862 return fn;
2863 }
2864
2865 static int parse_map_size(char *str)
2866 {
2867 unsigned long size, map_bits;
2868 int ret;
2869
2870 ret = kstrtoul(str, 0, &size);
2871 if (ret)
2872 goto out;
2873
2874 map_bits = ilog2(roundup_pow_of_two(size));
2875 if (map_bits < TRACING_MAP_BITS_MIN ||
2876 map_bits > TRACING_MAP_BITS_MAX)
2877 ret = -EINVAL;
2878 else
2879 ret = map_bits;
2880 out:
2881 return ret;
2882 }
2883
2884 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
2885 {
2886 unsigned int i;
2887
2888 if (!attrs)
2889 return;
2890
2891 for (i = 0; i < attrs->n_assignments; i++)
2892 kfree(attrs->assignment_str[i]);
2893
2894 for (i = 0; i < attrs->n_actions; i++)
2895 kfree(attrs->action_str[i]);
2896
2897 kfree(attrs->name);
2898 kfree(attrs->sort_key_str);
2899 kfree(attrs->keys_str);
2900 kfree(attrs->vals_str);
2901 kfree(attrs->clock);
2902 kfree(attrs);
2903 }
2904
2905 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
2906 {
2907 int ret = -EINVAL;
2908
2909 if (attrs->n_actions >= HIST_ACTIONS_MAX)
2910 return ret;
2911
2912 if ((str_has_prefix(str, "onmatch(")) ||
2913 (str_has_prefix(str, "onmax(")) ||
2914 (str_has_prefix(str, "onchange("))) {
2915 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
2916 if (!attrs->action_str[attrs->n_actions]) {
2917 ret = -ENOMEM;
2918 return ret;
2919 }
2920 attrs->n_actions++;
2921 ret = 0;
2922 }
2923 return ret;
2924 }
2925
2926 static int parse_assignment(struct trace_array *tr,
2927 char *str, struct hist_trigger_attrs *attrs)
2928 {
2929 int len, ret = 0;
2930
2931 if ((len = str_has_prefix(str, "key=")) ||
2932 (len = str_has_prefix(str, "keys="))) {
2933 attrs->keys_str = kstrdup(str + len, GFP_KERNEL);
2934 if (!attrs->keys_str) {
2935 ret = -ENOMEM;
2936 goto out;
2937 }
2938 } else if ((len = str_has_prefix(str, "val=")) ||
2939 (len = str_has_prefix(str, "vals=")) ||
2940 (len = str_has_prefix(str, "values="))) {
2941 attrs->vals_str = kstrdup(str + len, GFP_KERNEL);
2942 if (!attrs->vals_str) {
2943 ret = -ENOMEM;
2944 goto out;
2945 }
2946 } else if ((len = str_has_prefix(str, "sort="))) {
2947 attrs->sort_key_str = kstrdup(str + len, GFP_KERNEL);
2948 if (!attrs->sort_key_str) {
2949 ret = -ENOMEM;
2950 goto out;
2951 }
2952 } else if (str_has_prefix(str, "name=")) {
2953 attrs->name = kstrdup(str, GFP_KERNEL);
2954 if (!attrs->name) {
2955 ret = -ENOMEM;
2956 goto out;
2957 }
2958 } else if ((len = str_has_prefix(str, "clock="))) {
2959 str += len;
2960
2961 str = strstrip(str);
2962 attrs->clock = kstrdup(str, GFP_KERNEL);
2963 if (!attrs->clock) {
2964 ret = -ENOMEM;
2965 goto out;
2966 }
2967 } else if ((len = str_has_prefix(str, "size="))) {
2968 int map_bits = parse_map_size(str + len);
2969
2970 if (map_bits < 0) {
2971 ret = map_bits;
2972 goto out;
2973 }
2974 attrs->map_bits = map_bits;
2975 } else {
2976 char *assignment;
2977
2978 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
2979 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str));
2980 ret = -EINVAL;
2981 goto out;
2982 }
2983
2984 assignment = kstrdup(str, GFP_KERNEL);
2985 if (!assignment) {
2986 ret = -ENOMEM;
2987 goto out;
2988 }
2989
2990 attrs->assignment_str[attrs->n_assignments++] = assignment;
2991 }
2992 out:
2993 return ret;
2994 }
2995
2996 static struct hist_trigger_attrs *
2997 parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str)
2998 {
2999 struct hist_trigger_attrs *attrs;
3000 int ret = 0;
3001
3002 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
3003 if (!attrs)
3004 return ERR_PTR(-ENOMEM);
3005
3006 while (trigger_str) {
3007 char *str = strsep(&trigger_str, ":");
3008 char *rhs;
3009
3010 rhs = strchr(str, '=');
3011 if (rhs) {
3012 if (!strlen(++rhs)) {
3013 ret = -EINVAL;
3014 hist_err(tr, HIST_ERR_EMPTY_ASSIGNMENT, errpos(str));
3015 goto free;
3016 }
3017 ret = parse_assignment(tr, str, attrs);
3018 if (ret)
3019 goto free;
3020 } else if (strcmp(str, "pause") == 0)
3021 attrs->pause = true;
3022 else if ((strcmp(str, "cont") == 0) ||
3023 (strcmp(str, "continue") == 0))
3024 attrs->cont = true;
3025 else if (strcmp(str, "clear") == 0)
3026 attrs->clear = true;
3027 else {
3028 ret = parse_action(str, attrs);
3029 if (ret)
3030 goto free;
3031 }
3032 }
3033
3034 if (!attrs->keys_str) {
3035 ret = -EINVAL;
3036 goto free;
3037 }
3038
3039 if (!attrs->clock) {
3040 attrs->clock = kstrdup("global", GFP_KERNEL);
3041 if (!attrs->clock) {
3042 ret = -ENOMEM;
3043 goto free;
3044 }
3045 }
3046
3047 return attrs;
3048 free:
3049 destroy_hist_trigger_attrs(attrs);
3050
3051 return ERR_PTR(ret);
3052 }
3053
3054 static inline void save_comm(char *comm, struct task_struct *task)
3055 {
3056 if (!task->pid) {
3057 strcpy(comm, "<idle>");
3058 return;
3059 }
3060
3061 if (WARN_ON_ONCE(task->pid < 0)) {
3062 strcpy(comm, "<XXX>");
3063 return;
3064 }
3065
3066 strncpy(comm, task->comm, TASK_COMM_LEN);
3067 }
3068
3069 static void hist_elt_data_free(struct hist_elt_data *elt_data)
3070 {
3071 unsigned int i;
3072
3073 for (i = 0; i < SYNTH_FIELDS_MAX; i++)
3074 kfree(elt_data->field_var_str[i]);
3075
3076 kfree(elt_data->comm);
3077 kfree(elt_data);
3078 }
3079
3080 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
3081 {
3082 struct hist_elt_data *elt_data = elt->private_data;
3083
3084 hist_elt_data_free(elt_data);
3085 }
3086
3087 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
3088 {
3089 struct hist_trigger_data *hist_data = elt->map->private_data;
3090 unsigned int size = TASK_COMM_LEN;
3091 struct hist_elt_data *elt_data;
3092 struct hist_field *key_field;
3093 unsigned int i, n_str;
3094
3095 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
3096 if (!elt_data)
3097 return -ENOMEM;
3098
3099 for_each_hist_key_field(i, hist_data) {
3100 key_field = hist_data->fields[i];
3101
3102 if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
3103 elt_data->comm = kzalloc(size, GFP_KERNEL);
3104 if (!elt_data->comm) {
3105 kfree(elt_data);
3106 return -ENOMEM;
3107 }
3108 break;
3109 }
3110 }
3111
3112 n_str = hist_data->n_field_var_str + hist_data->n_save_var_str;
3113
3114 size = STR_VAR_LEN_MAX;
3115
3116 for (i = 0; i < n_str; i++) {
3117 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
3118 if (!elt_data->field_var_str[i]) {
3119 hist_elt_data_free(elt_data);
3120 return -ENOMEM;
3121 }
3122 }
3123
3124 elt->private_data = elt_data;
3125
3126 return 0;
3127 }
3128
3129 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
3130 {
3131 struct hist_elt_data *elt_data = elt->private_data;
3132
3133 if (elt_data->comm)
3134 save_comm(elt_data->comm, current);
3135 }
3136
3137 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
3138 .elt_alloc = hist_trigger_elt_data_alloc,
3139 .elt_free = hist_trigger_elt_data_free,
3140 .elt_init = hist_trigger_elt_data_init,
3141 };
3142
3143 static const char *get_hist_field_flags(struct hist_field *hist_field)
3144 {
3145 const char *flags_str = NULL;
3146
3147 if (hist_field->flags & HIST_FIELD_FL_HEX)
3148 flags_str = "hex";
3149 else if (hist_field->flags & HIST_FIELD_FL_SYM)
3150 flags_str = "sym";
3151 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
3152 flags_str = "sym-offset";
3153 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
3154 flags_str = "execname";
3155 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
3156 flags_str = "syscall";
3157 else if (hist_field->flags & HIST_FIELD_FL_LOG2)
3158 flags_str = "log2";
3159 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
3160 flags_str = "usecs";
3161
3162 return flags_str;
3163 }
3164
3165 static void expr_field_str(struct hist_field *field, char *expr)
3166 {
3167 if (field->flags & HIST_FIELD_FL_VAR_REF)
3168 strcat(expr, "$");
3169
3170 strcat(expr, hist_field_name(field, 0));
3171
3172 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
3173 const char *flags_str = get_hist_field_flags(field);
3174
3175 if (flags_str) {
3176 strcat(expr, ".");
3177 strcat(expr, flags_str);
3178 }
3179 }
3180 }
3181
3182 static char *expr_str(struct hist_field *field, unsigned int level)
3183 {
3184 char *expr;
3185
3186 if (level > 1)
3187 return NULL;
3188
3189 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3190 if (!expr)
3191 return NULL;
3192
3193 if (!field->operands[0]) {
3194 expr_field_str(field, expr);
3195 return expr;
3196 }
3197
3198 if (field->operator == FIELD_OP_UNARY_MINUS) {
3199 char *subexpr;
3200
3201 strcat(expr, "-(");
3202 subexpr = expr_str(field->operands[0], ++level);
3203 if (!subexpr) {
3204 kfree(expr);
3205 return NULL;
3206 }
3207 strcat(expr, subexpr);
3208 strcat(expr, ")");
3209
3210 kfree(subexpr);
3211
3212 return expr;
3213 }
3214
3215 expr_field_str(field->operands[0], expr);
3216
3217 switch (field->operator) {
3218 case FIELD_OP_MINUS:
3219 strcat(expr, "-");
3220 break;
3221 case FIELD_OP_PLUS:
3222 strcat(expr, "+");
3223 break;
3224 default:
3225 kfree(expr);
3226 return NULL;
3227 }
3228
3229 expr_field_str(field->operands[1], expr);
3230
3231 return expr;
3232 }
3233
3234 static int contains_operator(char *str)
3235 {
3236 enum field_op_id field_op = FIELD_OP_NONE;
3237 char *op;
3238
3239 op = strpbrk(str, "+-");
3240 if (!op)
3241 return FIELD_OP_NONE;
3242
3243 switch (*op) {
3244 case '-':
3245 if (*str == '-')
3246 field_op = FIELD_OP_UNARY_MINUS;
3247 else
3248 field_op = FIELD_OP_MINUS;
3249 break;
3250 case '+':
3251 field_op = FIELD_OP_PLUS;
3252 break;
3253 default:
3254 break;
3255 }
3256
3257 return field_op;
3258 }
3259
3260 static void __destroy_hist_field(struct hist_field *hist_field)
3261 {
3262 kfree(hist_field->var.name);
3263 kfree(hist_field->name);
3264 kfree(hist_field->type);
3265
3266 kfree(hist_field);
3267 }
3268
3269 static void destroy_hist_field(struct hist_field *hist_field,
3270 unsigned int level)
3271 {
3272 unsigned int i;
3273
3274 if (level > 3)
3275 return;
3276
3277 if (!hist_field)
3278 return;
3279
3280 if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
3281 return; /* var refs will be destroyed separately */
3282
3283 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
3284 destroy_hist_field(hist_field->operands[i], level + 1);
3285
3286 __destroy_hist_field(hist_field);
3287 }
3288
3289 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
3290 struct ftrace_event_field *field,
3291 unsigned long flags,
3292 char *var_name)
3293 {
3294 struct hist_field *hist_field;
3295
3296 if (field && is_function_field(field))
3297 return NULL;
3298
3299 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3300 if (!hist_field)
3301 return NULL;
3302
3303 hist_field->hist_data = hist_data;
3304
3305 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
3306 goto out; /* caller will populate */
3307
3308 if (flags & HIST_FIELD_FL_VAR_REF) {
3309 hist_field->fn = hist_field_var_ref;
3310 goto out;
3311 }
3312
3313 if (flags & HIST_FIELD_FL_HITCOUNT) {
3314 hist_field->fn = hist_field_counter;
3315 hist_field->size = sizeof(u64);
3316 hist_field->type = kstrdup("u64", GFP_KERNEL);
3317 if (!hist_field->type)
3318 goto free;
3319 goto out;
3320 }
3321
3322 if (flags & HIST_FIELD_FL_STACKTRACE) {
3323 hist_field->fn = hist_field_none;
3324 goto out;
3325 }
3326
3327 if (flags & HIST_FIELD_FL_LOG2) {
3328 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2;
3329 hist_field->fn = hist_field_log2;
3330 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
3331 hist_field->size = hist_field->operands[0]->size;
3332 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL);
3333 if (!hist_field->type)
3334 goto free;
3335 goto out;
3336 }
3337
3338 if (flags & HIST_FIELD_FL_TIMESTAMP) {
3339 hist_field->fn = hist_field_timestamp;
3340 hist_field->size = sizeof(u64);
3341 hist_field->type = kstrdup("u64", GFP_KERNEL);
3342 if (!hist_field->type)
3343 goto free;
3344 goto out;
3345 }
3346
3347 if (flags & HIST_FIELD_FL_CPU) {
3348 hist_field->fn = hist_field_cpu;
3349 hist_field->size = sizeof(int);
3350 hist_field->type = kstrdup("unsigned int", GFP_KERNEL);
3351 if (!hist_field->type)
3352 goto free;
3353 goto out;
3354 }
3355
3356 if (WARN_ON_ONCE(!field))
3357 goto out;
3358
3359 if (is_string_field(field)) {
3360 flags |= HIST_FIELD_FL_STRING;
3361
3362 hist_field->size = MAX_FILTER_STR_VAL;
3363 hist_field->type = kstrdup(field->type, GFP_KERNEL);
3364 if (!hist_field->type)
3365 goto free;
3366
3367 if (field->filter_type == FILTER_STATIC_STRING)
3368 hist_field->fn = hist_field_string;
3369 else if (field->filter_type == FILTER_DYN_STRING)
3370 hist_field->fn = hist_field_dynstring;
3371 else
3372 hist_field->fn = hist_field_pstring;
3373 } else {
3374 hist_field->size = field->size;
3375 hist_field->is_signed = field->is_signed;
3376 hist_field->type = kstrdup(field->type, GFP_KERNEL);
3377 if (!hist_field->type)
3378 goto free;
3379
3380 hist_field->fn = select_value_fn(field->size,
3381 field->is_signed);
3382 if (!hist_field->fn) {
3383 destroy_hist_field(hist_field, 0);
3384 return NULL;
3385 }
3386 }
3387 out:
3388 hist_field->field = field;
3389 hist_field->flags = flags;
3390
3391 if (var_name) {
3392 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
3393 if (!hist_field->var.name)
3394 goto free;
3395 }
3396
3397 return hist_field;
3398 free:
3399 destroy_hist_field(hist_field, 0);
3400 return NULL;
3401 }
3402
3403 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
3404 {
3405 unsigned int i;
3406
3407 for (i = 0; i < HIST_FIELDS_MAX; i++) {
3408 if (hist_data->fields[i]) {
3409 destroy_hist_field(hist_data->fields[i], 0);
3410 hist_data->fields[i] = NULL;
3411 }
3412 }
3413
3414 for (i = 0; i < hist_data->n_var_refs; i++) {
3415 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
3416 __destroy_hist_field(hist_data->var_refs[i]);
3417 hist_data->var_refs[i] = NULL;
3418 }
3419 }
3420
3421 static int init_var_ref(struct hist_field *ref_field,
3422 struct hist_field *var_field,
3423 char *system, char *event_name)
3424 {
3425 int err = 0;
3426
3427 ref_field->var.idx = var_field->var.idx;
3428 ref_field->var.hist_data = var_field->hist_data;
3429 ref_field->size = var_field->size;
3430 ref_field->is_signed = var_field->is_signed;
3431 ref_field->flags |= var_field->flags &
3432 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
3433
3434 if (system) {
3435 ref_field->system = kstrdup(system, GFP_KERNEL);
3436 if (!ref_field->system)
3437 return -ENOMEM;
3438 }
3439
3440 if (event_name) {
3441 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
3442 if (!ref_field->event_name) {
3443 err = -ENOMEM;
3444 goto free;
3445 }
3446 }
3447
3448 if (var_field->var.name) {
3449 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
3450 if (!ref_field->name) {
3451 err = -ENOMEM;
3452 goto free;
3453 }
3454 } else if (var_field->name) {
3455 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
3456 if (!ref_field->name) {
3457 err = -ENOMEM;
3458 goto free;
3459 }
3460 }
3461
3462 ref_field->type = kstrdup(var_field->type, GFP_KERNEL);
3463 if (!ref_field->type) {
3464 err = -ENOMEM;
3465 goto free;
3466 }
3467 out:
3468 return err;
3469 free:
3470 kfree(ref_field->system);
3471 kfree(ref_field->event_name);
3472 kfree(ref_field->name);
3473
3474 goto out;
3475 }
3476
3477 static int find_var_ref_idx(struct hist_trigger_data *hist_data,
3478 struct hist_field *var_field)
3479 {
3480 struct hist_field *ref_field;
3481 int i;
3482
3483 for (i = 0; i < hist_data->n_var_refs; i++) {
3484 ref_field = hist_data->var_refs[i];
3485 if (ref_field->var.idx == var_field->var.idx &&
3486 ref_field->var.hist_data == var_field->hist_data)
3487 return i;
3488 }
3489
3490 return -ENOENT;
3491 }
3492
3493 /**
3494 * create_var_ref - Create a variable reference and attach it to trigger
3495 * @hist_data: The trigger that will be referencing the variable
3496 * @var_field: The VAR field to create a reference to
3497 * @system: The optional system string
3498 * @event_name: The optional event_name string
3499 *
3500 * Given a variable hist_field, create a VAR_REF hist_field that
3501 * represents a reference to it.
3502 *
3503 * This function also adds the reference to the trigger that
3504 * now references the variable.
3505 *
3506 * Return: The VAR_REF field if successful, NULL if not
3507 */
3508 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
3509 struct hist_field *var_field,
3510 char *system, char *event_name)
3511 {
3512 unsigned long flags = HIST_FIELD_FL_VAR_REF;
3513 struct hist_field *ref_field;
3514
3515 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
3516 if (ref_field) {
3517 if (init_var_ref(ref_field, var_field, system, event_name)) {
3518 destroy_hist_field(ref_field, 0);
3519 return NULL;
3520 }
3521
3522 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
3523 ref_field->var_ref_idx = hist_data->n_var_refs++;
3524 }
3525
3526 return ref_field;
3527 }
3528
3529 static bool is_var_ref(char *var_name)
3530 {
3531 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
3532 return false;
3533
3534 return true;
3535 }
3536
3537 static char *field_name_from_var(struct hist_trigger_data *hist_data,
3538 char *var_name)
3539 {
3540 char *name, *field;
3541 unsigned int i;
3542
3543 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
3544 name = hist_data->attrs->var_defs.name[i];
3545
3546 if (strcmp(var_name, name) == 0) {
3547 field = hist_data->attrs->var_defs.expr[i];
3548 if (contains_operator(field) || is_var_ref(field))
3549 continue;
3550 return field;
3551 }
3552 }
3553
3554 return NULL;
3555 }
3556
3557 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
3558 char *system, char *event_name,
3559 char *var_name)
3560 {
3561 struct trace_event_call *call;
3562
3563 if (system && event_name) {
3564 call = hist_data->event_file->event_call;
3565
3566 if (strcmp(system, call->class->system) != 0)
3567 return NULL;
3568
3569 if (strcmp(event_name, trace_event_name(call)) != 0)
3570 return NULL;
3571 }
3572
3573 if (!!system != !!event_name)
3574 return NULL;
3575
3576 if (!is_var_ref(var_name))
3577 return NULL;
3578
3579 var_name++;
3580
3581 return field_name_from_var(hist_data, var_name);
3582 }
3583
3584 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
3585 char *system, char *event_name,
3586 char *var_name)
3587 {
3588 struct hist_field *var_field = NULL, *ref_field = NULL;
3589 struct trace_array *tr = hist_data->event_file->tr;
3590
3591 if (!is_var_ref(var_name))
3592 return NULL;
3593
3594 var_name++;
3595
3596 var_field = find_event_var(hist_data, system, event_name, var_name);
3597 if (var_field)
3598 ref_field = create_var_ref(hist_data, var_field,
3599 system, event_name);
3600
3601 if (!ref_field)
3602 hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name));
3603
3604 return ref_field;
3605 }
3606
3607 static struct ftrace_event_field *
3608 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
3609 char *field_str, unsigned long *flags)
3610 {
3611 struct ftrace_event_field *field = NULL;
3612 char *field_name, *modifier, *str;
3613 struct trace_array *tr = file->tr;
3614
3615 modifier = str = kstrdup(field_str, GFP_KERNEL);
3616 if (!modifier)
3617 return ERR_PTR(-ENOMEM);
3618
3619 field_name = strsep(&modifier, ".");
3620 if (modifier) {
3621 if (strcmp(modifier, "hex") == 0)
3622 *flags |= HIST_FIELD_FL_HEX;
3623 else if (strcmp(modifier, "sym") == 0)
3624 *flags |= HIST_FIELD_FL_SYM;
3625 else if (strcmp(modifier, "sym-offset") == 0)
3626 *flags |= HIST_FIELD_FL_SYM_OFFSET;
3627 else if ((strcmp(modifier, "execname") == 0) &&
3628 (strcmp(field_name, "common_pid") == 0))
3629 *flags |= HIST_FIELD_FL_EXECNAME;
3630 else if (strcmp(modifier, "syscall") == 0)
3631 *flags |= HIST_FIELD_FL_SYSCALL;
3632 else if (strcmp(modifier, "log2") == 0)
3633 *flags |= HIST_FIELD_FL_LOG2;
3634 else if (strcmp(modifier, "usecs") == 0)
3635 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
3636 else {
3637 hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier));
3638 field = ERR_PTR(-EINVAL);
3639 goto out;
3640 }
3641 }
3642
3643 if (strcmp(field_name, "common_timestamp") == 0) {
3644 *flags |= HIST_FIELD_FL_TIMESTAMP;
3645 hist_data->enable_timestamps = true;
3646 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
3647 hist_data->attrs->ts_in_usecs = true;
3648 } else if (strcmp(field_name, "cpu") == 0)
3649 *flags |= HIST_FIELD_FL_CPU;
3650 else {
3651 field = trace_find_event_field(file->event_call, field_name);
3652 if (!field || !field->size) {
3653 hist_err(tr, HIST_ERR_FIELD_NOT_FOUND, errpos(field_name));
3654 field = ERR_PTR(-EINVAL);
3655 goto out;
3656 }
3657 }
3658 out:
3659 kfree(str);
3660
3661 return field;
3662 }
3663
3664 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
3665 struct hist_field *var_ref,
3666 char *var_name)
3667 {
3668 struct hist_field *alias = NULL;
3669 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
3670
3671 alias = create_hist_field(hist_data, NULL, flags, var_name);
3672 if (!alias)
3673 return NULL;
3674
3675 alias->fn = var_ref->fn;
3676 alias->operands[0] = var_ref;
3677
3678 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
3679 destroy_hist_field(alias, 0);
3680 return NULL;
3681 }
3682
3683 alias->var_ref_idx = var_ref->var_ref_idx;
3684
3685 return alias;
3686 }
3687
3688 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
3689 struct trace_event_file *file, char *str,
3690 unsigned long *flags, char *var_name)
3691 {
3692 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
3693 struct ftrace_event_field *field = NULL;
3694 struct hist_field *hist_field = NULL;
3695 int ret = 0;
3696
3697 s = strchr(str, '.');
3698 if (s) {
3699 s = strchr(++s, '.');
3700 if (s) {
3701 ref_system = strsep(&str, ".");
3702 if (!str) {
3703 ret = -EINVAL;
3704 goto out;
3705 }
3706 ref_event = strsep(&str, ".");
3707 if (!str) {
3708 ret = -EINVAL;
3709 goto out;
3710 }
3711 ref_var = str;
3712 }
3713 }
3714
3715 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
3716 if (!s) {
3717 hist_field = parse_var_ref(hist_data, ref_system,
3718 ref_event, ref_var);
3719 if (hist_field) {
3720 if (var_name) {
3721 hist_field = create_alias(hist_data, hist_field, var_name);
3722 if (!hist_field) {
3723 ret = -ENOMEM;
3724 goto out;
3725 }
3726 }
3727 return hist_field;
3728 }
3729 } else
3730 str = s;
3731
3732 field = parse_field(hist_data, file, str, flags);
3733 if (IS_ERR(field)) {
3734 ret = PTR_ERR(field);
3735 goto out;
3736 }
3737
3738 hist_field = create_hist_field(hist_data, field, *flags, var_name);
3739 if (!hist_field) {
3740 ret = -ENOMEM;
3741 goto out;
3742 }
3743
3744 return hist_field;
3745 out:
3746 return ERR_PTR(ret);
3747 }
3748
3749 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
3750 struct trace_event_file *file,
3751 char *str, unsigned long flags,
3752 char *var_name, unsigned int level);
3753
3754 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
3755 struct trace_event_file *file,
3756 char *str, unsigned long flags,
3757 char *var_name, unsigned int level)
3758 {
3759 struct hist_field *operand1, *expr = NULL;
3760 unsigned long operand_flags;
3761 int ret = 0;
3762 char *s;
3763
3764 /* we support only -(xxx) i.e. explicit parens required */
3765
3766 if (level > 3) {
3767 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
3768 ret = -EINVAL;
3769 goto free;
3770 }
3771
3772 str++; /* skip leading '-' */
3773
3774 s = strchr(str, '(');
3775 if (s)
3776 str++;
3777 else {
3778 ret = -EINVAL;
3779 goto free;
3780 }
3781
3782 s = strrchr(str, ')');
3783 if (s)
3784 *s = '\0';
3785 else {
3786 ret = -EINVAL; /* no closing ')' */
3787 goto free;
3788 }
3789
3790 flags |= HIST_FIELD_FL_EXPR;
3791 expr = create_hist_field(hist_data, NULL, flags, var_name);
3792 if (!expr) {
3793 ret = -ENOMEM;
3794 goto free;
3795 }
3796
3797 operand_flags = 0;
3798 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
3799 if (IS_ERR(operand1)) {
3800 ret = PTR_ERR(operand1);
3801 goto free;
3802 }
3803
3804 expr->flags |= operand1->flags &
3805 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
3806 expr->fn = hist_field_unary_minus;
3807 expr->operands[0] = operand1;
3808 expr->operator = FIELD_OP_UNARY_MINUS;
3809 expr->name = expr_str(expr, 0);
3810 expr->type = kstrdup(operand1->type, GFP_KERNEL);
3811 if (!expr->type) {
3812 ret = -ENOMEM;
3813 goto free;
3814 }
3815
3816 return expr;
3817 free:
3818 destroy_hist_field(expr, 0);
3819 return ERR_PTR(ret);
3820 }
3821
3822 static int check_expr_operands(struct trace_array *tr,
3823 struct hist_field *operand1,
3824 struct hist_field *operand2)
3825 {
3826 unsigned long operand1_flags = operand1->flags;
3827 unsigned long operand2_flags = operand2->flags;
3828
3829 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
3830 (operand1_flags & HIST_FIELD_FL_ALIAS)) {
3831 struct hist_field *var;
3832
3833 var = find_var_field(operand1->var.hist_data, operand1->name);
3834 if (!var)
3835 return -EINVAL;
3836 operand1_flags = var->flags;
3837 }
3838
3839 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
3840 (operand2_flags & HIST_FIELD_FL_ALIAS)) {
3841 struct hist_field *var;
3842
3843 var = find_var_field(operand2->var.hist_data, operand2->name);
3844 if (!var)
3845 return -EINVAL;
3846 operand2_flags = var->flags;
3847 }
3848
3849 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
3850 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
3851 hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0);
3852 return -EINVAL;
3853 }
3854
3855 return 0;
3856 }
3857
3858 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
3859 struct trace_event_file *file,
3860 char *str, unsigned long flags,
3861 char *var_name, unsigned int level)
3862 {
3863 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
3864 unsigned long operand_flags;
3865 int field_op, ret = -EINVAL;
3866 char *sep, *operand1_str;
3867
3868 if (level > 3) {
3869 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
3870 return ERR_PTR(-EINVAL);
3871 }
3872
3873 field_op = contains_operator(str);
3874
3875 if (field_op == FIELD_OP_NONE)
3876 return parse_atom(hist_data, file, str, &flags, var_name);
3877
3878 if (field_op == FIELD_OP_UNARY_MINUS)
3879 return parse_unary(hist_data, file, str, flags, var_name, ++level);
3880
3881 switch (field_op) {
3882 case FIELD_OP_MINUS:
3883 sep = "-";
3884 break;
3885 case FIELD_OP_PLUS:
3886 sep = "+";
3887 break;
3888 default:
3889 goto free;
3890 }
3891
3892 operand1_str = strsep(&str, sep);
3893 if (!operand1_str || !str)
3894 goto free;
3895
3896 operand_flags = 0;
3897 operand1 = parse_atom(hist_data, file, operand1_str,
3898 &operand_flags, NULL);
3899 if (IS_ERR(operand1)) {
3900 ret = PTR_ERR(operand1);
3901 operand1 = NULL;
3902 goto free;
3903 }
3904
3905 /* rest of string could be another expression e.g. b+c in a+b+c */
3906 operand_flags = 0;
3907 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
3908 if (IS_ERR(operand2)) {
3909 ret = PTR_ERR(operand2);
3910 operand2 = NULL;
3911 goto free;
3912 }
3913
3914 ret = check_expr_operands(file->tr, operand1, operand2);
3915 if (ret)
3916 goto free;
3917
3918 flags |= HIST_FIELD_FL_EXPR;
3919
3920 flags |= operand1->flags &
3921 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
3922
3923 expr = create_hist_field(hist_data, NULL, flags, var_name);
3924 if (!expr) {
3925 ret = -ENOMEM;
3926 goto free;
3927 }
3928
3929 operand1->read_once = true;
3930 operand2->read_once = true;
3931
3932 expr->operands[0] = operand1;
3933 expr->operands[1] = operand2;
3934 expr->operator = field_op;
3935 expr->name = expr_str(expr, 0);
3936 expr->type = kstrdup(operand1->type, GFP_KERNEL);
3937 if (!expr->type) {
3938 ret = -ENOMEM;
3939 goto free;
3940 }
3941
3942 switch (field_op) {
3943 case FIELD_OP_MINUS:
3944 expr->fn = hist_field_minus;
3945 break;
3946 case FIELD_OP_PLUS:
3947 expr->fn = hist_field_plus;
3948 break;
3949 default:
3950 ret = -EINVAL;
3951 goto free;
3952 }
3953
3954 return expr;
3955 free:
3956 destroy_hist_field(operand1, 0);
3957 destroy_hist_field(operand2, 0);
3958 destroy_hist_field(expr, 0);
3959
3960 return ERR_PTR(ret);
3961 }
3962
3963 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
3964 struct trace_event_file *file)
3965 {
3966 struct event_trigger_data *test;
3967
3968 lockdep_assert_held(&event_mutex);
3969
3970 list_for_each_entry(test, &file->triggers, list) {
3971 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
3972 if (test->private_data == hist_data)
3973 return test->filter_str;
3974 }
3975 }
3976
3977 return NULL;
3978 }
3979
3980 static struct event_command trigger_hist_cmd;
3981 static int event_hist_trigger_func(struct event_command *cmd_ops,
3982 struct trace_event_file *file,
3983 char *glob, char *cmd, char *param);
3984
3985 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
3986 struct hist_trigger_data *hist_data,
3987 unsigned int n_keys)
3988 {
3989 struct hist_field *target_hist_field, *hist_field;
3990 unsigned int n, i, j;
3991
3992 if (hist_data->n_fields - hist_data->n_vals != n_keys)
3993 return false;
3994
3995 i = hist_data->n_vals;
3996 j = target_hist_data->n_vals;
3997
3998 for (n = 0; n < n_keys; n++) {
3999 hist_field = hist_data->fields[i + n];
4000 target_hist_field = target_hist_data->fields[j + n];
4001
4002 if (strcmp(hist_field->type, target_hist_field->type) != 0)
4003 return false;
4004 if (hist_field->size != target_hist_field->size)
4005 return false;
4006 if (hist_field->is_signed != target_hist_field->is_signed)
4007 return false;
4008 }
4009
4010 return true;
4011 }
4012
4013 static struct hist_trigger_data *
4014 find_compatible_hist(struct hist_trigger_data *target_hist_data,
4015 struct trace_event_file *file)
4016 {
4017 struct hist_trigger_data *hist_data;
4018 struct event_trigger_data *test;
4019 unsigned int n_keys;
4020
4021 lockdep_assert_held(&event_mutex);
4022
4023 n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
4024
4025 list_for_each_entry(test, &file->triggers, list) {
4026 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
4027 hist_data = test->private_data;
4028
4029 if (compatible_keys(target_hist_data, hist_data, n_keys))
4030 return hist_data;
4031 }
4032 }
4033
4034 return NULL;
4035 }
4036
4037 static struct trace_event_file *event_file(struct trace_array *tr,
4038 char *system, char *event_name)
4039 {
4040 struct trace_event_file *file;
4041
4042 file = __find_event_file(tr, system, event_name);
4043 if (!file)
4044 return ERR_PTR(-EINVAL);
4045
4046 return file;
4047 }
4048
4049 static struct hist_field *
4050 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
4051 char *system, char *event_name, char *field_name)
4052 {
4053 struct hist_field *event_var;
4054 char *synthetic_name;
4055
4056 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
4057 if (!synthetic_name)
4058 return ERR_PTR(-ENOMEM);
4059
4060 strcpy(synthetic_name, "synthetic_");
4061 strcat(synthetic_name, field_name);
4062
4063 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
4064
4065 kfree(synthetic_name);
4066
4067 return event_var;
4068 }
4069
4070 /**
4071 * create_field_var_hist - Automatically create a histogram and var for a field
4072 * @target_hist_data: The target hist trigger
4073 * @subsys_name: Optional subsystem name
4074 * @event_name: Optional event name
4075 * @field_name: The name of the field (and the resulting variable)
4076 *
4077 * Hist trigger actions fetch data from variables, not directly from
4078 * events. However, for convenience, users are allowed to directly
4079 * specify an event field in an action, which will be automatically
4080 * converted into a variable on their behalf.
4081
4082 * If a user specifies a field on an event that isn't the event the
4083 * histogram currently being defined (the target event histogram), the
4084 * only way that can be accomplished is if a new hist trigger is
4085 * created and the field variable defined on that.
4086 *
4087 * This function creates a new histogram compatible with the target
4088 * event (meaning a histogram with the same key as the target
4089 * histogram), and creates a variable for the specified field, but
4090 * with 'synthetic_' prepended to the variable name in order to avoid
4091 * collision with normal field variables.
4092 *
4093 * Return: The variable created for the field.
4094 */
4095 static struct hist_field *
4096 create_field_var_hist(struct hist_trigger_data *target_hist_data,
4097 char *subsys_name, char *event_name, char *field_name)
4098 {
4099 struct trace_array *tr = target_hist_data->event_file->tr;
4100 struct hist_field *event_var = ERR_PTR(-EINVAL);
4101 struct hist_trigger_data *hist_data;
4102 unsigned int i, n, first = true;
4103 struct field_var_hist *var_hist;
4104 struct trace_event_file *file;
4105 struct hist_field *key_field;
4106 char *saved_filter;
4107 char *cmd;
4108 int ret;
4109
4110 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
4111 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
4112 return ERR_PTR(-EINVAL);
4113 }
4114
4115 file = event_file(tr, subsys_name, event_name);
4116
4117 if (IS_ERR(file)) {
4118 hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name));
4119 ret = PTR_ERR(file);
4120 return ERR_PTR(ret);
4121 }
4122
4123 /*
4124 * Look for a histogram compatible with target. We'll use the
4125 * found histogram specification to create a new matching
4126 * histogram with our variable on it. target_hist_data is not
4127 * yet a registered histogram so we can't use that.
4128 */
4129 hist_data = find_compatible_hist(target_hist_data, file);
4130 if (!hist_data) {
4131 hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name));
4132 return ERR_PTR(-EINVAL);
4133 }
4134
4135 /* See if a synthetic field variable has already been created */
4136 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
4137 event_name, field_name);
4138 if (!IS_ERR_OR_NULL(event_var))
4139 return event_var;
4140
4141 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
4142 if (!var_hist)
4143 return ERR_PTR(-ENOMEM);
4144
4145 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
4146 if (!cmd) {
4147 kfree(var_hist);
4148 return ERR_PTR(-ENOMEM);
4149 }
4150
4151 /* Use the same keys as the compatible histogram */
4152 strcat(cmd, "keys=");
4153
4154 for_each_hist_key_field(i, hist_data) {
4155 key_field = hist_data->fields[i];
4156 if (!first)
4157 strcat(cmd, ",");
4158 strcat(cmd, key_field->field->name);
4159 first = false;
4160 }
4161
4162 /* Create the synthetic field variable specification */
4163 strcat(cmd, ":synthetic_");
4164 strcat(cmd, field_name);
4165 strcat(cmd, "=");
4166 strcat(cmd, field_name);
4167
4168 /* Use the same filter as the compatible histogram */
4169 saved_filter = find_trigger_filter(hist_data, file);
4170 if (saved_filter) {
4171 strcat(cmd, " if ");
4172 strcat(cmd, saved_filter);
4173 }
4174
4175 var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
4176 if (!var_hist->cmd) {
4177 kfree(cmd);
4178 kfree(var_hist);
4179 return ERR_PTR(-ENOMEM);
4180 }
4181
4182 /* Save the compatible histogram information */
4183 var_hist->hist_data = hist_data;
4184
4185 /* Create the new histogram with our variable */
4186 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
4187 "", "hist", cmd);
4188 if (ret) {
4189 kfree(cmd);
4190 kfree(var_hist->cmd);
4191 kfree(var_hist);
4192 hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name));
4193 return ERR_PTR(ret);
4194 }
4195
4196 kfree(cmd);
4197
4198 /* If we can't find the variable, something went wrong */
4199 event_var = find_synthetic_field_var(target_hist_data, subsys_name,
4200 event_name, field_name);
4201 if (IS_ERR_OR_NULL(event_var)) {
4202 kfree(var_hist->cmd);
4203 kfree(var_hist);
4204 hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
4205 return ERR_PTR(-EINVAL);
4206 }
4207
4208 n = target_hist_data->n_field_var_hists;
4209 target_hist_data->field_var_hists[n] = var_hist;
4210 target_hist_data->n_field_var_hists++;
4211
4212 return event_var;
4213 }
4214
4215 static struct hist_field *
4216 find_target_event_var(struct hist_trigger_data *hist_data,
4217 char *subsys_name, char *event_name, char *var_name)
4218 {
4219 struct trace_event_file *file = hist_data->event_file;
4220 struct hist_field *hist_field = NULL;
4221
4222 if (subsys_name) {
4223 struct trace_event_call *call;
4224
4225 if (!event_name)
4226 return NULL;
4227
4228 call = file->event_call;
4229
4230 if (strcmp(subsys_name, call->class->system) != 0)
4231 return NULL;
4232
4233 if (strcmp(event_name, trace_event_name(call)) != 0)
4234 return NULL;
4235 }
4236
4237 hist_field = find_var_field(hist_data, var_name);
4238
4239 return hist_field;
4240 }
4241
4242 static inline void __update_field_vars(struct tracing_map_elt *elt,
4243 struct ring_buffer_event *rbe,
4244 void *rec,
4245 struct field_var **field_vars,
4246 unsigned int n_field_vars,
4247 unsigned int field_var_str_start)
4248 {
4249 struct hist_elt_data *elt_data = elt->private_data;
4250 unsigned int i, j, var_idx;
4251 u64 var_val;
4252
4253 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
4254 struct field_var *field_var = field_vars[i];
4255 struct hist_field *var = field_var->var;
4256 struct hist_field *val = field_var->val;
4257
4258 var_val = val->fn(val, elt, rbe, rec);
4259 var_idx = var->var.idx;
4260
4261 if (val->flags & HIST_FIELD_FL_STRING) {
4262 char *str = elt_data->field_var_str[j++];
4263 char *val_str = (char *)(uintptr_t)var_val;
4264
4265 strscpy(str, val_str, STR_VAR_LEN_MAX);
4266 var_val = (u64)(uintptr_t)str;
4267 }
4268 tracing_map_set_var(elt, var_idx, var_val);
4269 }
4270 }
4271
4272 static void update_field_vars(struct hist_trigger_data *hist_data,
4273 struct tracing_map_elt *elt,
4274 struct ring_buffer_event *rbe,
4275 void *rec)
4276 {
4277 __update_field_vars(elt, rbe, rec, hist_data->field_vars,
4278 hist_data->n_field_vars, 0);
4279 }
4280
4281 static void save_track_data_vars(struct hist_trigger_data *hist_data,
4282 struct tracing_map_elt *elt, void *rec,
4283 struct ring_buffer_event *rbe, void *key,
4284 struct action_data *data, u64 *var_ref_vals)
4285 {
4286 __update_field_vars(elt, rbe, rec, hist_data->save_vars,
4287 hist_data->n_save_vars, hist_data->n_field_var_str);
4288 }
4289
4290 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
4291 struct trace_event_file *file,
4292 char *name, int size, const char *type)
4293 {
4294 struct hist_field *var;
4295 int idx;
4296
4297 if (find_var(hist_data, file, name) && !hist_data->remove) {
4298 var = ERR_PTR(-EINVAL);
4299 goto out;
4300 }
4301
4302 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
4303 if (!var) {
4304 var = ERR_PTR(-ENOMEM);
4305 goto out;
4306 }
4307
4308 idx = tracing_map_add_var(hist_data->map);
4309 if (idx < 0) {
4310 kfree(var);
4311 var = ERR_PTR(-EINVAL);
4312 goto out;
4313 }
4314
4315 var->flags = HIST_FIELD_FL_VAR;
4316 var->var.idx = idx;
4317 var->var.hist_data = var->hist_data = hist_data;
4318 var->size = size;
4319 var->var.name = kstrdup(name, GFP_KERNEL);
4320 var->type = kstrdup(type, GFP_KERNEL);
4321 if (!var->var.name || !var->type) {
4322 kfree(var->var.name);
4323 kfree(var->type);
4324 kfree(var);
4325 var = ERR_PTR(-ENOMEM);
4326 }
4327 out:
4328 return var;
4329 }
4330
4331 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
4332 struct trace_event_file *file,
4333 char *field_name)
4334 {
4335 struct hist_field *val = NULL, *var = NULL;
4336 unsigned long flags = HIST_FIELD_FL_VAR;
4337 struct trace_array *tr = file->tr;
4338 struct field_var *field_var;
4339 int ret = 0;
4340
4341 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
4342 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
4343 ret = -EINVAL;
4344 goto err;
4345 }
4346
4347 val = parse_atom(hist_data, file, field_name, &flags, NULL);
4348 if (IS_ERR(val)) {
4349 hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name));
4350 ret = PTR_ERR(val);
4351 goto err;
4352 }
4353
4354 var = create_var(hist_data, file, field_name, val->size, val->type);
4355 if (IS_ERR(var)) {
4356 hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name));
4357 kfree(val);
4358 ret = PTR_ERR(var);
4359 goto err;
4360 }
4361
4362 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
4363 if (!field_var) {
4364 kfree(val);
4365 kfree(var);
4366 ret = -ENOMEM;
4367 goto err;
4368 }
4369
4370 field_var->var = var;
4371 field_var->val = val;
4372 out:
4373 return field_var;
4374 err:
4375 field_var = ERR_PTR(ret);
4376 goto out;
4377 }
4378
4379 /**
4380 * create_target_field_var - Automatically create a variable for a field
4381 * @target_hist_data: The target hist trigger
4382 * @subsys_name: Optional subsystem name
4383 * @event_name: Optional event name
4384 * @var_name: The name of the field (and the resulting variable)
4385 *
4386 * Hist trigger actions fetch data from variables, not directly from
4387 * events. However, for convenience, users are allowed to directly
4388 * specify an event field in an action, which will be automatically
4389 * converted into a variable on their behalf.
4390
4391 * This function creates a field variable with the name var_name on
4392 * the hist trigger currently being defined on the target event. If
4393 * subsys_name and event_name are specified, this function simply
4394 * verifies that they do in fact match the target event subsystem and
4395 * event name.
4396 *
4397 * Return: The variable created for the field.
4398 */
4399 static struct field_var *
4400 create_target_field_var(struct hist_trigger_data *target_hist_data,
4401 char *subsys_name, char *event_name, char *var_name)
4402 {
4403 struct trace_event_file *file = target_hist_data->event_file;
4404
4405 if (subsys_name) {
4406 struct trace_event_call *call;
4407
4408 if (!event_name)
4409 return NULL;
4410
4411 call = file->event_call;
4412
4413 if (strcmp(subsys_name, call->class->system) != 0)
4414 return NULL;
4415
4416 if (strcmp(event_name, trace_event_name(call)) != 0)
4417 return NULL;
4418 }
4419
4420 return create_field_var(target_hist_data, file, var_name);
4421 }
4422
4423 static bool check_track_val_max(u64 track_val, u64 var_val)
4424 {
4425 if (var_val <= track_val)
4426 return false;
4427
4428 return true;
4429 }
4430
4431 static bool check_track_val_changed(u64 track_val, u64 var_val)
4432 {
4433 if (var_val == track_val)
4434 return false;
4435
4436 return true;
4437 }
4438
4439 static u64 get_track_val(struct hist_trigger_data *hist_data,
4440 struct tracing_map_elt *elt,
4441 struct action_data *data)
4442 {
4443 unsigned int track_var_idx = data->track_data.track_var->var.idx;
4444 u64 track_val;
4445
4446 track_val = tracing_map_read_var(elt, track_var_idx);
4447
4448 return track_val;
4449 }
4450
4451 static void save_track_val(struct hist_trigger_data *hist_data,
4452 struct tracing_map_elt *elt,
4453 struct action_data *data, u64 var_val)
4454 {
4455 unsigned int track_var_idx = data->track_data.track_var->var.idx;
4456
4457 tracing_map_set_var(elt, track_var_idx, var_val);
4458 }
4459
4460 static void save_track_data(struct hist_trigger_data *hist_data,
4461 struct tracing_map_elt *elt, void *rec,
4462 struct ring_buffer_event *rbe, void *key,
4463 struct action_data *data, u64 *var_ref_vals)
4464 {
4465 if (data->track_data.save_data)
4466 data->track_data.save_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
4467 }
4468
4469 static bool check_track_val(struct tracing_map_elt *elt,
4470 struct action_data *data,
4471 u64 var_val)
4472 {
4473 struct hist_trigger_data *hist_data;
4474 u64 track_val;
4475
4476 hist_data = data->track_data.track_var->hist_data;
4477 track_val = get_track_val(hist_data, elt, data);
4478
4479 return data->track_data.check_val(track_val, var_val);
4480 }
4481
4482 #ifdef CONFIG_TRACER_SNAPSHOT
4483 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
4484 {
4485 /* called with tr->max_lock held */
4486 struct track_data *track_data = tr->cond_snapshot->cond_data;
4487 struct hist_elt_data *elt_data, *track_elt_data;
4488 struct snapshot_context *context = cond_data;
4489 struct action_data *action;
4490 u64 track_val;
4491
4492 if (!track_data)
4493 return false;
4494
4495 action = track_data->action_data;
4496
4497 track_val = get_track_val(track_data->hist_data, context->elt,
4498 track_data->action_data);
4499
4500 if (!action->track_data.check_val(track_data->track_val, track_val))
4501 return false;
4502
4503 track_data->track_val = track_val;
4504 memcpy(track_data->key, context->key, track_data->key_len);
4505
4506 elt_data = context->elt->private_data;
4507 track_elt_data = track_data->elt.private_data;
4508 if (elt_data->comm)
4509 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
4510
4511 track_data->updated = true;
4512
4513 return true;
4514 }
4515
4516 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
4517 struct tracing_map_elt *elt, void *rec,
4518 struct ring_buffer_event *rbe, void *key,
4519 struct action_data *data,
4520 u64 *var_ref_vals)
4521 {
4522 struct trace_event_file *file = hist_data->event_file;
4523 struct snapshot_context context;
4524
4525 context.elt = elt;
4526 context.key = key;
4527
4528 tracing_snapshot_cond(file->tr, &context);
4529 }
4530
4531 static void hist_trigger_print_key(struct seq_file *m,
4532 struct hist_trigger_data *hist_data,
4533 void *key,
4534 struct tracing_map_elt *elt);
4535
4536 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
4537 {
4538 unsigned int i;
4539
4540 if (!hist_data->n_actions)
4541 return NULL;
4542
4543 for (i = 0; i < hist_data->n_actions; i++) {
4544 struct action_data *data = hist_data->actions[i];
4545
4546 if (data->action == ACTION_SNAPSHOT)
4547 return data;
4548 }
4549
4550 return NULL;
4551 }
4552
4553 static void track_data_snapshot_print(struct seq_file *m,
4554 struct hist_trigger_data *hist_data)
4555 {
4556 struct trace_event_file *file = hist_data->event_file;
4557 struct track_data *track_data;
4558 struct action_data *action;
4559
4560 track_data = tracing_cond_snapshot_data(file->tr);
4561 if (!track_data)
4562 return;
4563
4564 if (!track_data->updated)
4565 return;
4566
4567 action = snapshot_action(hist_data);
4568 if (!action)
4569 return;
4570
4571 seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n");
4572 seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
4573 action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
4574 action->track_data.var_str, track_data->track_val);
4575
4576 seq_puts(m, "\ttriggered by event with key: ");
4577 hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
4578 seq_putc(m, '\n');
4579 }
4580 #else
4581 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
4582 {
4583 return false;
4584 }
4585 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
4586 struct tracing_map_elt *elt, void *rec,
4587 struct ring_buffer_event *rbe, void *key,
4588 struct action_data *data,
4589 u64 *var_ref_vals) {}
4590 static void track_data_snapshot_print(struct seq_file *m,
4591 struct hist_trigger_data *hist_data) {}
4592 #endif /* CONFIG_TRACER_SNAPSHOT */
4593
4594 static void track_data_print(struct seq_file *m,
4595 struct hist_trigger_data *hist_data,
4596 struct tracing_map_elt *elt,
4597 struct action_data *data)
4598 {
4599 u64 track_val = get_track_val(hist_data, elt, data);
4600 unsigned int i, save_var_idx;
4601
4602 if (data->handler == HANDLER_ONMAX)
4603 seq_printf(m, "\n\tmax: %10llu", track_val);
4604 else if (data->handler == HANDLER_ONCHANGE)
4605 seq_printf(m, "\n\tchanged: %10llu", track_val);
4606
4607 if (data->action == ACTION_SNAPSHOT)
4608 return;
4609
4610 for (i = 0; i < hist_data->n_save_vars; i++) {
4611 struct hist_field *save_val = hist_data->save_vars[i]->val;
4612 struct hist_field *save_var = hist_data->save_vars[i]->var;
4613 u64 val;
4614
4615 save_var_idx = save_var->var.idx;
4616
4617 val = tracing_map_read_var(elt, save_var_idx);
4618
4619 if (save_val->flags & HIST_FIELD_FL_STRING) {
4620 seq_printf(m, " %s: %-32s", save_var->var.name,
4621 (char *)(uintptr_t)(val));
4622 } else
4623 seq_printf(m, " %s: %10llu", save_var->var.name, val);
4624 }
4625 }
4626
4627 static void ontrack_action(struct hist_trigger_data *hist_data,
4628 struct tracing_map_elt *elt, void *rec,
4629 struct ring_buffer_event *rbe, void *key,
4630 struct action_data *data, u64 *var_ref_vals)
4631 {
4632 u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
4633
4634 if (check_track_val(elt, data, var_val)) {
4635 save_track_val(hist_data, elt, data, var_val);
4636 save_track_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
4637 }
4638 }
4639
4640 static void action_data_destroy(struct action_data *data)
4641 {
4642 unsigned int i;
4643
4644 lockdep_assert_held(&event_mutex);
4645
4646 kfree(data->action_name);
4647
4648 for (i = 0; i < data->n_params; i++)
4649 kfree(data->params[i]);
4650
4651 if (data->synth_event)
4652 data->synth_event->ref--;
4653
4654 kfree(data->synth_event_name);
4655
4656 kfree(data);
4657 }
4658
4659 static void track_data_destroy(struct hist_trigger_data *hist_data,
4660 struct action_data *data)
4661 {
4662 struct trace_event_file *file = hist_data->event_file;
4663
4664 destroy_hist_field(data->track_data.track_var, 0);
4665
4666 if (data->action == ACTION_SNAPSHOT) {
4667 struct track_data *track_data;
4668
4669 track_data = tracing_cond_snapshot_data(file->tr);
4670 if (track_data && track_data->hist_data == hist_data) {
4671 tracing_snapshot_cond_disable(file->tr);
4672 track_data_free(track_data);
4673 }
4674 }
4675
4676 kfree(data->track_data.var_str);
4677
4678 action_data_destroy(data);
4679 }
4680
4681 static int action_create(struct hist_trigger_data *hist_data,
4682 struct action_data *data);
4683
4684 static int track_data_create(struct hist_trigger_data *hist_data,
4685 struct action_data *data)
4686 {
4687 struct hist_field *var_field, *ref_field, *track_var = NULL;
4688 struct trace_event_file *file = hist_data->event_file;
4689 struct trace_array *tr = file->tr;
4690 char *track_data_var_str;
4691 int ret = 0;
4692
4693 track_data_var_str = data->track_data.var_str;
4694 if (track_data_var_str[0] != '$') {
4695 hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str));
4696 return -EINVAL;
4697 }
4698 track_data_var_str++;
4699
4700 var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
4701 if (!var_field) {
4702 hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str));
4703 return -EINVAL;
4704 }
4705
4706 ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
4707 if (!ref_field)
4708 return -ENOMEM;
4709
4710 data->track_data.var_ref = ref_field;
4711
4712 if (data->handler == HANDLER_ONMAX)
4713 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
4714 if (IS_ERR(track_var)) {
4715 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
4716 ret = PTR_ERR(track_var);
4717 goto out;
4718 }
4719
4720 if (data->handler == HANDLER_ONCHANGE)
4721 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
4722 if (IS_ERR(track_var)) {
4723 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
4724 ret = PTR_ERR(track_var);
4725 goto out;
4726 }
4727 data->track_data.track_var = track_var;
4728
4729 ret = action_create(hist_data, data);
4730 out:
4731 return ret;
4732 }
4733
4734 static int parse_action_params(struct trace_array *tr, char *params,
4735 struct action_data *data)
4736 {
4737 char *param, *saved_param;
4738 bool first_param = true;
4739 int ret = 0;
4740
4741 while (params) {
4742 if (data->n_params >= SYNTH_FIELDS_MAX) {
4743 hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0);
4744 goto out;
4745 }
4746
4747 param = strsep(&params, ",");
4748 if (!param) {
4749 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0);
4750 ret = -EINVAL;
4751 goto out;
4752 }
4753
4754 param = strstrip(param);
4755 if (strlen(param) < 2) {
4756 hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param));
4757 ret = -EINVAL;
4758 goto out;
4759 }
4760
4761 saved_param = kstrdup(param, GFP_KERNEL);
4762 if (!saved_param) {
4763 ret = -ENOMEM;
4764 goto out;
4765 }
4766
4767 if (first_param && data->use_trace_keyword) {
4768 data->synth_event_name = saved_param;
4769 first_param = false;
4770 continue;
4771 }
4772 first_param = false;
4773
4774 data->params[data->n_params++] = saved_param;
4775 }
4776 out:
4777 return ret;
4778 }
4779
4780 static int action_parse(struct trace_array *tr, char *str, struct action_data *data,
4781 enum handler_id handler)
4782 {
4783 char *action_name;
4784 int ret = 0;
4785
4786 strsep(&str, ".");
4787 if (!str) {
4788 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
4789 ret = -EINVAL;
4790 goto out;
4791 }
4792
4793 action_name = strsep(&str, "(");
4794 if (!action_name || !str) {
4795 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
4796 ret = -EINVAL;
4797 goto out;
4798 }
4799
4800 if (str_has_prefix(action_name, "save")) {
4801 char *params = strsep(&str, ")");
4802
4803 if (!params) {
4804 hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0);
4805 ret = -EINVAL;
4806 goto out;
4807 }
4808
4809 ret = parse_action_params(tr, params, data);
4810 if (ret)
4811 goto out;
4812
4813 if (handler == HANDLER_ONMAX)
4814 data->track_data.check_val = check_track_val_max;
4815 else if (handler == HANDLER_ONCHANGE)
4816 data->track_data.check_val = check_track_val_changed;
4817 else {
4818 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
4819 ret = -EINVAL;
4820 goto out;
4821 }
4822
4823 data->track_data.save_data = save_track_data_vars;
4824 data->fn = ontrack_action;
4825 data->action = ACTION_SAVE;
4826 } else if (str_has_prefix(action_name, "snapshot")) {
4827 char *params = strsep(&str, ")");
4828
4829 if (!str) {
4830 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params));
4831 ret = -EINVAL;
4832 goto out;
4833 }
4834
4835 if (handler == HANDLER_ONMAX)
4836 data->track_data.check_val = check_track_val_max;
4837 else if (handler == HANDLER_ONCHANGE)
4838 data->track_data.check_val = check_track_val_changed;
4839 else {
4840 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
4841 ret = -EINVAL;
4842 goto out;
4843 }
4844
4845 data->track_data.save_data = save_track_data_snapshot;
4846 data->fn = ontrack_action;
4847 data->action = ACTION_SNAPSHOT;
4848 } else {
4849 char *params = strsep(&str, ")");
4850
4851 if (str_has_prefix(action_name, "trace"))
4852 data->use_trace_keyword = true;
4853
4854 if (params) {
4855 ret = parse_action_params(tr, params, data);
4856 if (ret)
4857 goto out;
4858 }
4859
4860 if (handler == HANDLER_ONMAX)
4861 data->track_data.check_val = check_track_val_max;
4862 else if (handler == HANDLER_ONCHANGE)
4863 data->track_data.check_val = check_track_val_changed;
4864
4865 if (handler != HANDLER_ONMATCH) {
4866 data->track_data.save_data = action_trace;
4867 data->fn = ontrack_action;
4868 } else
4869 data->fn = action_trace;
4870
4871 data->action = ACTION_TRACE;
4872 }
4873
4874 data->action_name = kstrdup(action_name, GFP_KERNEL);
4875 if (!data->action_name) {
4876 ret = -ENOMEM;
4877 goto out;
4878 }
4879
4880 data->handler = handler;
4881 out:
4882 return ret;
4883 }
4884
4885 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
4886 char *str, enum handler_id handler)
4887 {
4888 struct action_data *data;
4889 int ret = -EINVAL;
4890 char *var_str;
4891
4892 data = kzalloc(sizeof(*data), GFP_KERNEL);
4893 if (!data)
4894 return ERR_PTR(-ENOMEM);
4895
4896 var_str = strsep(&str, ")");
4897 if (!var_str || !str) {
4898 ret = -EINVAL;
4899 goto free;
4900 }
4901
4902 data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
4903 if (!data->track_data.var_str) {
4904 ret = -ENOMEM;
4905 goto free;
4906 }
4907
4908 ret = action_parse(hist_data->event_file->tr, str, data, handler);
4909 if (ret)
4910 goto free;
4911 out:
4912 return data;
4913 free:
4914 track_data_destroy(hist_data, data);
4915 data = ERR_PTR(ret);
4916 goto out;
4917 }
4918
4919 static void onmatch_destroy(struct action_data *data)
4920 {
4921 kfree(data->match_data.event);
4922 kfree(data->match_data.event_system);
4923
4924 action_data_destroy(data);
4925 }
4926
4927 static void destroy_field_var(struct field_var *field_var)
4928 {
4929 if (!field_var)
4930 return;
4931
4932 destroy_hist_field(field_var->var, 0);
4933 destroy_hist_field(field_var->val, 0);
4934
4935 kfree(field_var);
4936 }
4937
4938 static void destroy_field_vars(struct hist_trigger_data *hist_data)
4939 {
4940 unsigned int i;
4941
4942 for (i = 0; i < hist_data->n_field_vars; i++)
4943 destroy_field_var(hist_data->field_vars[i]);
4944 }
4945
4946 static void save_field_var(struct hist_trigger_data *hist_data,
4947 struct field_var *field_var)
4948 {
4949 hist_data->field_vars[hist_data->n_field_vars++] = field_var;
4950
4951 if (field_var->val->flags & HIST_FIELD_FL_STRING)
4952 hist_data->n_field_var_str++;
4953 }
4954
4955
4956 static int check_synth_field(struct synth_event *event,
4957 struct hist_field *hist_field,
4958 unsigned int field_pos)
4959 {
4960 struct synth_field *field;
4961
4962 if (field_pos >= event->n_fields)
4963 return -EINVAL;
4964
4965 field = event->fields[field_pos];
4966
4967 if (strcmp(field->type, hist_field->type) != 0) {
4968 if (field->size != hist_field->size ||
4969 field->is_signed != hist_field->is_signed)
4970 return -EINVAL;
4971 }
4972
4973 return 0;
4974 }
4975
4976 static struct hist_field *
4977 trace_action_find_var(struct hist_trigger_data *hist_data,
4978 struct action_data *data,
4979 char *system, char *event, char *var)
4980 {
4981 struct trace_array *tr = hist_data->event_file->tr;
4982 struct hist_field *hist_field;
4983
4984 var++; /* skip '$' */
4985
4986 hist_field = find_target_event_var(hist_data, system, event, var);
4987 if (!hist_field) {
4988 if (!system && data->handler == HANDLER_ONMATCH) {
4989 system = data->match_data.event_system;
4990 event = data->match_data.event;
4991 }
4992
4993 hist_field = find_event_var(hist_data, system, event, var);
4994 }
4995
4996 if (!hist_field)
4997 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var));
4998
4999 return hist_field;
5000 }
5001
5002 static struct hist_field *
5003 trace_action_create_field_var(struct hist_trigger_data *hist_data,
5004 struct action_data *data, char *system,
5005 char *event, char *var)
5006 {
5007 struct hist_field *hist_field = NULL;
5008 struct field_var *field_var;
5009
5010 /*
5011 * First try to create a field var on the target event (the
5012 * currently being defined). This will create a variable for
5013 * unqualified fields on the target event, or if qualified,
5014 * target fields that have qualified names matching the target.
5015 */
5016 field_var = create_target_field_var(hist_data, system, event, var);
5017
5018 if (field_var && !IS_ERR(field_var)) {
5019 save_field_var(hist_data, field_var);
5020 hist_field = field_var->var;
5021 } else {
5022 field_var = NULL;
5023 /*
5024 * If no explicit system.event is specfied, default to
5025 * looking for fields on the onmatch(system.event.xxx)
5026 * event.
5027 */
5028 if (!system && data->handler == HANDLER_ONMATCH) {
5029 system = data->match_data.event_system;
5030 event = data->match_data.event;
5031 }
5032
5033 /*
5034 * At this point, we're looking at a field on another
5035 * event. Because we can't modify a hist trigger on
5036 * another event to add a variable for a field, we need
5037 * to create a new trigger on that event and create the
5038 * variable at the same time.
5039 */
5040 hist_field = create_field_var_hist(hist_data, system, event, var);
5041 if (IS_ERR(hist_field))
5042 goto free;
5043 }
5044 out:
5045 return hist_field;
5046 free:
5047 destroy_field_var(field_var);
5048 hist_field = NULL;
5049 goto out;
5050 }
5051
5052 static int trace_action_create(struct hist_trigger_data *hist_data,
5053 struct action_data *data)
5054 {
5055 struct trace_array *tr = hist_data->event_file->tr;
5056 char *event_name, *param, *system = NULL;
5057 struct hist_field *hist_field, *var_ref;
5058 unsigned int i;
5059 unsigned int field_pos = 0;
5060 struct synth_event *event;
5061 char *synth_event_name;
5062 int var_ref_idx, ret = 0;
5063
5064 lockdep_assert_held(&event_mutex);
5065
5066 if (data->use_trace_keyword)
5067 synth_event_name = data->synth_event_name;
5068 else
5069 synth_event_name = data->action_name;
5070
5071 event = find_synth_event(synth_event_name);
5072 if (!event) {
5073 hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name));
5074 return -EINVAL;
5075 }
5076
5077 event->ref++;
5078
5079 for (i = 0; i < data->n_params; i++) {
5080 char *p;
5081
5082 p = param = kstrdup(data->params[i], GFP_KERNEL);
5083 if (!param) {
5084 ret = -ENOMEM;
5085 goto err;
5086 }
5087
5088 system = strsep(&param, ".");
5089 if (!param) {
5090 param = (char *)system;
5091 system = event_name = NULL;
5092 } else {
5093 event_name = strsep(&param, ".");
5094 if (!param) {
5095 kfree(p);
5096 ret = -EINVAL;
5097 goto err;
5098 }
5099 }
5100
5101 if (param[0] == '$')
5102 hist_field = trace_action_find_var(hist_data, data,
5103 system, event_name,
5104 param);
5105 else
5106 hist_field = trace_action_create_field_var(hist_data,
5107 data,
5108 system,
5109 event_name,
5110 param);
5111
5112 if (!hist_field) {
5113 kfree(p);
5114 ret = -EINVAL;
5115 goto err;
5116 }
5117
5118 if (check_synth_field(event, hist_field, field_pos) == 0) {
5119 var_ref = create_var_ref(hist_data, hist_field,
5120 system, event_name);
5121 if (!var_ref) {
5122 kfree(p);
5123 ret = -ENOMEM;
5124 goto err;
5125 }
5126
5127 var_ref_idx = find_var_ref_idx(hist_data, var_ref);
5128 if (WARN_ON(var_ref_idx < 0)) {
5129 ret = var_ref_idx;
5130 goto err;
5131 }
5132
5133 data->var_ref_idx[i] = var_ref_idx;
5134
5135 field_pos++;
5136 kfree(p);
5137 continue;
5138 }
5139
5140 hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param));
5141 kfree(p);
5142 ret = -EINVAL;
5143 goto err;
5144 }
5145
5146 if (field_pos != event->n_fields) {
5147 hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name));
5148 ret = -EINVAL;
5149 goto err;
5150 }
5151
5152 data->synth_event = event;
5153 out:
5154 return ret;
5155 err:
5156 event->ref--;
5157
5158 goto out;
5159 }
5160
5161 static int action_create(struct hist_trigger_data *hist_data,
5162 struct action_data *data)
5163 {
5164 struct trace_event_file *file = hist_data->event_file;
5165 struct trace_array *tr = file->tr;
5166 struct track_data *track_data;
5167 struct field_var *field_var;
5168 unsigned int i;
5169 char *param;
5170 int ret = 0;
5171
5172 if (data->action == ACTION_TRACE)
5173 return trace_action_create(hist_data, data);
5174
5175 if (data->action == ACTION_SNAPSHOT) {
5176 track_data = track_data_alloc(hist_data->key_size, data, hist_data);
5177 if (IS_ERR(track_data)) {
5178 ret = PTR_ERR(track_data);
5179 goto out;
5180 }
5181
5182 ret = tracing_snapshot_cond_enable(file->tr, track_data,
5183 cond_snapshot_update);
5184 if (ret)
5185 track_data_free(track_data);
5186
5187 goto out;
5188 }
5189
5190 if (data->action == ACTION_SAVE) {
5191 if (hist_data->n_save_vars) {
5192 ret = -EEXIST;
5193 hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0);
5194 goto out;
5195 }
5196
5197 for (i = 0; i < data->n_params; i++) {
5198 param = kstrdup(data->params[i], GFP_KERNEL);
5199 if (!param) {
5200 ret = -ENOMEM;
5201 goto out;
5202 }
5203
5204 field_var = create_target_field_var(hist_data, NULL, NULL, param);
5205 if (IS_ERR(field_var)) {
5206 hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL,
5207 errpos(param));
5208 ret = PTR_ERR(field_var);
5209 kfree(param);
5210 goto out;
5211 }
5212
5213 hist_data->save_vars[hist_data->n_save_vars++] = field_var;
5214 if (field_var->val->flags & HIST_FIELD_FL_STRING)
5215 hist_data->n_save_var_str++;
5216 kfree(param);
5217 }
5218 }
5219 out:
5220 return ret;
5221 }
5222
5223 static int onmatch_create(struct hist_trigger_data *hist_data,
5224 struct action_data *data)
5225 {
5226 return action_create(hist_data, data);
5227 }
5228
5229 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
5230 {
5231 char *match_event, *match_event_system;
5232 struct action_data *data;
5233 int ret = -EINVAL;
5234
5235 data = kzalloc(sizeof(*data), GFP_KERNEL);
5236 if (!data)
5237 return ERR_PTR(-ENOMEM);
5238
5239 match_event = strsep(&str, ")");
5240 if (!match_event || !str) {
5241 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event));
5242 goto free;
5243 }
5244
5245 match_event_system = strsep(&match_event, ".");
5246 if (!match_event) {
5247 hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system));
5248 goto free;
5249 }
5250
5251 if (IS_ERR(event_file(tr, match_event_system, match_event))) {
5252 hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event));
5253 goto free;
5254 }
5255
5256 data->match_data.event = kstrdup(match_event, GFP_KERNEL);
5257 if (!data->match_data.event) {
5258 ret = -ENOMEM;
5259 goto free;
5260 }
5261
5262 data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
5263 if (!data->match_data.event_system) {
5264 ret = -ENOMEM;
5265 goto free;
5266 }
5267
5268 ret = action_parse(tr, str, data, HANDLER_ONMATCH);
5269 if (ret)
5270 goto free;
5271 out:
5272 return data;
5273 free:
5274 onmatch_destroy(data);
5275 data = ERR_PTR(ret);
5276 goto out;
5277 }
5278
5279 static int create_hitcount_val(struct hist_trigger_data *hist_data)
5280 {
5281 hist_data->fields[HITCOUNT_IDX] =
5282 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
5283 if (!hist_data->fields[HITCOUNT_IDX])
5284 return -ENOMEM;
5285
5286 hist_data->n_vals++;
5287 hist_data->n_fields++;
5288
5289 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
5290 return -EINVAL;
5291
5292 return 0;
5293 }
5294
5295 static int __create_val_field(struct hist_trigger_data *hist_data,
5296 unsigned int val_idx,
5297 struct trace_event_file *file,
5298 char *var_name, char *field_str,
5299 unsigned long flags)
5300 {
5301 struct hist_field *hist_field;
5302 int ret = 0;
5303
5304 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
5305 if (IS_ERR(hist_field)) {
5306 ret = PTR_ERR(hist_field);
5307 goto out;
5308 }
5309
5310 hist_data->fields[val_idx] = hist_field;
5311
5312 ++hist_data->n_vals;
5313 ++hist_data->n_fields;
5314
5315 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
5316 ret = -EINVAL;
5317 out:
5318 return ret;
5319 }
5320
5321 static int create_val_field(struct hist_trigger_data *hist_data,
5322 unsigned int val_idx,
5323 struct trace_event_file *file,
5324 char *field_str)
5325 {
5326 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
5327 return -EINVAL;
5328
5329 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
5330 }
5331
5332 static int create_var_field(struct hist_trigger_data *hist_data,
5333 unsigned int val_idx,
5334 struct trace_event_file *file,
5335 char *var_name, char *expr_str)
5336 {
5337 struct trace_array *tr = hist_data->event_file->tr;
5338 unsigned long flags = 0;
5339
5340 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
5341 return -EINVAL;
5342
5343 if (find_var(hist_data, file, var_name) && !hist_data->remove) {
5344 hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name));
5345 return -EINVAL;
5346 }
5347
5348 flags |= HIST_FIELD_FL_VAR;
5349 hist_data->n_vars++;
5350 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
5351 return -EINVAL;
5352
5353 return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
5354 }
5355
5356 static int create_val_fields(struct hist_trigger_data *hist_data,
5357 struct trace_event_file *file)
5358 {
5359 char *fields_str, *field_str;
5360 unsigned int i, j = 1;
5361 int ret;
5362
5363 ret = create_hitcount_val(hist_data);
5364 if (ret)
5365 goto out;
5366
5367 fields_str = hist_data->attrs->vals_str;
5368 if (!fields_str)
5369 goto out;
5370
5371 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
5372 j < TRACING_MAP_VALS_MAX; i++) {
5373 field_str = strsep(&fields_str, ",");
5374 if (!field_str)
5375 break;
5376
5377 if (strcmp(field_str, "hitcount") == 0)
5378 continue;
5379
5380 ret = create_val_field(hist_data, j++, file, field_str);
5381 if (ret)
5382 goto out;
5383 }
5384
5385 if (fields_str && (strcmp(fields_str, "hitcount") != 0))
5386 ret = -EINVAL;
5387 out:
5388 return ret;
5389 }
5390
5391 static int create_key_field(struct hist_trigger_data *hist_data,
5392 unsigned int key_idx,
5393 unsigned int key_offset,
5394 struct trace_event_file *file,
5395 char *field_str)
5396 {
5397 struct trace_array *tr = hist_data->event_file->tr;
5398 struct hist_field *hist_field = NULL;
5399 unsigned long flags = 0;
5400 unsigned int key_size;
5401 int ret = 0;
5402
5403 if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
5404 return -EINVAL;
5405
5406 flags |= HIST_FIELD_FL_KEY;
5407
5408 if (strcmp(field_str, "stacktrace") == 0) {
5409 flags |= HIST_FIELD_FL_STACKTRACE;
5410 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
5411 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
5412 } else {
5413 hist_field = parse_expr(hist_data, file, field_str, flags,
5414 NULL, 0);
5415 if (IS_ERR(hist_field)) {
5416 ret = PTR_ERR(hist_field);
5417 goto out;
5418 }
5419
5420 if (field_has_hist_vars(hist_field, 0)) {
5421 hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str));
5422 destroy_hist_field(hist_field, 0);
5423 ret = -EINVAL;
5424 goto out;
5425 }
5426
5427 key_size = hist_field->size;
5428 }
5429
5430 hist_data->fields[key_idx] = hist_field;
5431
5432 key_size = ALIGN(key_size, sizeof(u64));
5433 hist_data->fields[key_idx]->size = key_size;
5434 hist_data->fields[key_idx]->offset = key_offset;
5435
5436 hist_data->key_size += key_size;
5437
5438 if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
5439 ret = -EINVAL;
5440 goto out;
5441 }
5442
5443 hist_data->n_keys++;
5444 hist_data->n_fields++;
5445
5446 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
5447 return -EINVAL;
5448
5449 ret = key_size;
5450 out:
5451 return ret;
5452 }
5453
5454 static int create_key_fields(struct hist_trigger_data *hist_data,
5455 struct trace_event_file *file)
5456 {
5457 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
5458 char *fields_str, *field_str;
5459 int ret = -EINVAL;
5460
5461 fields_str = hist_data->attrs->keys_str;
5462 if (!fields_str)
5463 goto out;
5464
5465 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
5466 field_str = strsep(&fields_str, ",");
5467 if (!field_str)
5468 break;
5469 ret = create_key_field(hist_data, i, key_offset,
5470 file, field_str);
5471 if (ret < 0)
5472 goto out;
5473 key_offset += ret;
5474 }
5475 if (fields_str) {
5476 ret = -EINVAL;
5477 goto out;
5478 }
5479 ret = 0;
5480 out:
5481 return ret;
5482 }
5483
5484 static int create_var_fields(struct hist_trigger_data *hist_data,
5485 struct trace_event_file *file)
5486 {
5487 unsigned int i, j = hist_data->n_vals;
5488 int ret = 0;
5489
5490 unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
5491
5492 for (i = 0; i < n_vars; i++) {
5493 char *var_name = hist_data->attrs->var_defs.name[i];
5494 char *expr = hist_data->attrs->var_defs.expr[i];
5495
5496 ret = create_var_field(hist_data, j++, file, var_name, expr);
5497 if (ret)
5498 goto out;
5499 }
5500 out:
5501 return ret;
5502 }
5503
5504 static void free_var_defs(struct hist_trigger_data *hist_data)
5505 {
5506 unsigned int i;
5507
5508 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
5509 kfree(hist_data->attrs->var_defs.name[i]);
5510 kfree(hist_data->attrs->var_defs.expr[i]);
5511 }
5512
5513 hist_data->attrs->var_defs.n_vars = 0;
5514 }
5515
5516 static int parse_var_defs(struct hist_trigger_data *hist_data)
5517 {
5518 struct trace_array *tr = hist_data->event_file->tr;
5519 char *s, *str, *var_name, *field_str;
5520 unsigned int i, j, n_vars = 0;
5521 int ret = 0;
5522
5523 for (i = 0; i < hist_data->attrs->n_assignments; i++) {
5524 str = hist_data->attrs->assignment_str[i];
5525 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
5526 field_str = strsep(&str, ",");
5527 if (!field_str)
5528 break;
5529
5530 var_name = strsep(&field_str, "=");
5531 if (!var_name || !field_str) {
5532 hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT,
5533 errpos(var_name));
5534 ret = -EINVAL;
5535 goto free;
5536 }
5537
5538 if (n_vars == TRACING_MAP_VARS_MAX) {
5539 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name));
5540 ret = -EINVAL;
5541 goto free;
5542 }
5543
5544 s = kstrdup(var_name, GFP_KERNEL);
5545 if (!s) {
5546 ret = -ENOMEM;
5547 goto free;
5548 }
5549 hist_data->attrs->var_defs.name[n_vars] = s;
5550
5551 s = kstrdup(field_str, GFP_KERNEL);
5552 if (!s) {
5553 kfree(hist_data->attrs->var_defs.name[n_vars]);
5554 ret = -ENOMEM;
5555 goto free;
5556 }
5557 hist_data->attrs->var_defs.expr[n_vars++] = s;
5558
5559 hist_data->attrs->var_defs.n_vars = n_vars;
5560 }
5561 }
5562
5563 return ret;
5564 free:
5565 free_var_defs(hist_data);
5566
5567 return ret;
5568 }
5569
5570 static int create_hist_fields(struct hist_trigger_data *hist_data,
5571 struct trace_event_file *file)
5572 {
5573 int ret;
5574
5575 ret = parse_var_defs(hist_data);
5576 if (ret)
5577 goto out;
5578
5579 ret = create_val_fields(hist_data, file);
5580 if (ret)
5581 goto out;
5582
5583 ret = create_var_fields(hist_data, file);
5584 if (ret)
5585 goto out;
5586
5587 ret = create_key_fields(hist_data, file);
5588 if (ret)
5589 goto out;
5590 out:
5591 free_var_defs(hist_data);
5592
5593 return ret;
5594 }
5595
5596 static int is_descending(struct trace_array *tr, const char *str)
5597 {
5598 if (!str)
5599 return 0;
5600
5601 if (strcmp(str, "descending") == 0)
5602 return 1;
5603
5604 if (strcmp(str, "ascending") == 0)
5605 return 0;
5606
5607 hist_err(tr, HIST_ERR_INVALID_SORT_MODIFIER, errpos((char *)str));
5608
5609 return -EINVAL;
5610 }
5611
5612 static int create_sort_keys(struct hist_trigger_data *hist_data)
5613 {
5614 struct trace_array *tr = hist_data->event_file->tr;
5615 char *fields_str = hist_data->attrs->sort_key_str;
5616 struct tracing_map_sort_key *sort_key;
5617 int descending, ret = 0;
5618 unsigned int i, j, k;
5619
5620 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
5621
5622 if (!fields_str)
5623 goto out;
5624
5625 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
5626 struct hist_field *hist_field;
5627 char *field_str, *field_name;
5628 const char *test_name;
5629
5630 sort_key = &hist_data->sort_keys[i];
5631
5632 field_str = strsep(&fields_str, ",");
5633 if (!field_str)
5634 break;
5635
5636 if (!*field_str) {
5637 ret = -EINVAL;
5638 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
5639 break;
5640 }
5641
5642 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
5643 hist_err(tr, HIST_ERR_TOO_MANY_SORT_FIELDS, errpos("sort="));
5644 ret = -EINVAL;
5645 break;
5646 }
5647
5648 field_name = strsep(&field_str, ".");
5649 if (!field_name || !*field_name) {
5650 ret = -EINVAL;
5651 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
5652 break;
5653 }
5654
5655 if (strcmp(field_name, "hitcount") == 0) {
5656 descending = is_descending(tr, field_str);
5657 if (descending < 0) {
5658 ret = descending;
5659 break;
5660 }
5661 sort_key->descending = descending;
5662 continue;
5663 }
5664
5665 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
5666 unsigned int idx;
5667
5668 hist_field = hist_data->fields[j];
5669 if (hist_field->flags & HIST_FIELD_FL_VAR)
5670 continue;
5671
5672 idx = k++;
5673
5674 test_name = hist_field_name(hist_field, 0);
5675
5676 if (strcmp(field_name, test_name) == 0) {
5677 sort_key->field_idx = idx;
5678 descending = is_descending(tr, field_str);
5679 if (descending < 0) {
5680 ret = descending;
5681 goto out;
5682 }
5683 sort_key->descending = descending;
5684 break;
5685 }
5686 }
5687 if (j == hist_data->n_fields) {
5688 ret = -EINVAL;
5689 hist_err(tr, HIST_ERR_INVALID_SORT_FIELD, errpos(field_name));
5690 break;
5691 }
5692 }
5693
5694 hist_data->n_sort_keys = i;
5695 out:
5696 return ret;
5697 }
5698
5699 static void destroy_actions(struct hist_trigger_data *hist_data)
5700 {
5701 unsigned int i;
5702
5703 for (i = 0; i < hist_data->n_actions; i++) {
5704 struct action_data *data = hist_data->actions[i];
5705
5706 if (data->handler == HANDLER_ONMATCH)
5707 onmatch_destroy(data);
5708 else if (data->handler == HANDLER_ONMAX ||
5709 data->handler == HANDLER_ONCHANGE)
5710 track_data_destroy(hist_data, data);
5711 else
5712 kfree(data);
5713 }
5714 }
5715
5716 static int parse_actions(struct hist_trigger_data *hist_data)
5717 {
5718 struct trace_array *tr = hist_data->event_file->tr;
5719 struct action_data *data;
5720 unsigned int i;
5721 int ret = 0;
5722 char *str;
5723 int len;
5724
5725 for (i = 0; i < hist_data->attrs->n_actions; i++) {
5726 str = hist_data->attrs->action_str[i];
5727
5728 if ((len = str_has_prefix(str, "onmatch("))) {
5729 char *action_str = str + len;
5730
5731 data = onmatch_parse(tr, action_str);
5732 if (IS_ERR(data)) {
5733 ret = PTR_ERR(data);
5734 break;
5735 }
5736 } else if ((len = str_has_prefix(str, "onmax("))) {
5737 char *action_str = str + len;
5738
5739 data = track_data_parse(hist_data, action_str,
5740 HANDLER_ONMAX);
5741 if (IS_ERR(data)) {
5742 ret = PTR_ERR(data);
5743 break;
5744 }
5745 } else if ((len = str_has_prefix(str, "onchange("))) {
5746 char *action_str = str + len;
5747
5748 data = track_data_parse(hist_data, action_str,
5749 HANDLER_ONCHANGE);
5750 if (IS_ERR(data)) {
5751 ret = PTR_ERR(data);
5752 break;
5753 }
5754 } else {
5755 ret = -EINVAL;
5756 break;
5757 }
5758
5759 hist_data->actions[hist_data->n_actions++] = data;
5760 }
5761
5762 return ret;
5763 }
5764
5765 static int create_actions(struct hist_trigger_data *hist_data)
5766 {
5767 struct action_data *data;
5768 unsigned int i;
5769 int ret = 0;
5770
5771 for (i = 0; i < hist_data->attrs->n_actions; i++) {
5772 data = hist_data->actions[i];
5773
5774 if (data->handler == HANDLER_ONMATCH) {
5775 ret = onmatch_create(hist_data, data);
5776 if (ret)
5777 break;
5778 } else if (data->handler == HANDLER_ONMAX ||
5779 data->handler == HANDLER_ONCHANGE) {
5780 ret = track_data_create(hist_data, data);
5781 if (ret)
5782 break;
5783 } else {
5784 ret = -EINVAL;
5785 break;
5786 }
5787 }
5788
5789 return ret;
5790 }
5791
5792 static void print_actions(struct seq_file *m,
5793 struct hist_trigger_data *hist_data,
5794 struct tracing_map_elt *elt)
5795 {
5796 unsigned int i;
5797
5798 for (i = 0; i < hist_data->n_actions; i++) {
5799 struct action_data *data = hist_data->actions[i];
5800
5801 if (data->action == ACTION_SNAPSHOT)
5802 continue;
5803
5804 if (data->handler == HANDLER_ONMAX ||
5805 data->handler == HANDLER_ONCHANGE)
5806 track_data_print(m, hist_data, elt, data);
5807 }
5808 }
5809
5810 static void print_action_spec(struct seq_file *m,
5811 struct hist_trigger_data *hist_data,
5812 struct action_data *data)
5813 {
5814 unsigned int i;
5815
5816 if (data->action == ACTION_SAVE) {
5817 for (i = 0; i < hist_data->n_save_vars; i++) {
5818 seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
5819 if (i < hist_data->n_save_vars - 1)
5820 seq_puts(m, ",");
5821 }
5822 } else if (data->action == ACTION_TRACE) {
5823 if (data->use_trace_keyword)
5824 seq_printf(m, "%s", data->synth_event_name);
5825 for (i = 0; i < data->n_params; i++) {
5826 if (i || data->use_trace_keyword)
5827 seq_puts(m, ",");
5828 seq_printf(m, "%s", data->params[i]);
5829 }
5830 }
5831 }
5832
5833 static void print_track_data_spec(struct seq_file *m,
5834 struct hist_trigger_data *hist_data,
5835 struct action_data *data)
5836 {
5837 if (data->handler == HANDLER_ONMAX)
5838 seq_puts(m, ":onmax(");
5839 else if (data->handler == HANDLER_ONCHANGE)
5840 seq_puts(m, ":onchange(");
5841 seq_printf(m, "%s", data->track_data.var_str);
5842 seq_printf(m, ").%s(", data->action_name);
5843
5844 print_action_spec(m, hist_data, data);
5845
5846 seq_puts(m, ")");
5847 }
5848
5849 static void print_onmatch_spec(struct seq_file *m,
5850 struct hist_trigger_data *hist_data,
5851 struct action_data *data)
5852 {
5853 seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
5854 data->match_data.event);
5855
5856 seq_printf(m, "%s(", data->action_name);
5857
5858 print_action_spec(m, hist_data, data);
5859
5860 seq_puts(m, ")");
5861 }
5862
5863 static bool actions_match(struct hist_trigger_data *hist_data,
5864 struct hist_trigger_data *hist_data_test)
5865 {
5866 unsigned int i, j;
5867
5868 if (hist_data->n_actions != hist_data_test->n_actions)
5869 return false;
5870
5871 for (i = 0; i < hist_data->n_actions; i++) {
5872 struct action_data *data = hist_data->actions[i];
5873 struct action_data *data_test = hist_data_test->actions[i];
5874 char *action_name, *action_name_test;
5875
5876 if (data->handler != data_test->handler)
5877 return false;
5878 if (data->action != data_test->action)
5879 return false;
5880
5881 if (data->n_params != data_test->n_params)
5882 return false;
5883
5884 for (j = 0; j < data->n_params; j++) {
5885 if (strcmp(data->params[j], data_test->params[j]) != 0)
5886 return false;
5887 }
5888
5889 if (data->use_trace_keyword)
5890 action_name = data->synth_event_name;
5891 else
5892 action_name = data->action_name;
5893
5894 if (data_test->use_trace_keyword)
5895 action_name_test = data_test->synth_event_name;
5896 else
5897 action_name_test = data_test->action_name;
5898
5899 if (strcmp(action_name, action_name_test) != 0)
5900 return false;
5901
5902 if (data->handler == HANDLER_ONMATCH) {
5903 if (strcmp(data->match_data.event_system,
5904 data_test->match_data.event_system) != 0)
5905 return false;
5906 if (strcmp(data->match_data.event,
5907 data_test->match_data.event) != 0)
5908 return false;
5909 } else if (data->handler == HANDLER_ONMAX ||
5910 data->handler == HANDLER_ONCHANGE) {
5911 if (strcmp(data->track_data.var_str,
5912 data_test->track_data.var_str) != 0)
5913 return false;
5914 }
5915 }
5916
5917 return true;
5918 }
5919
5920
5921 static void print_actions_spec(struct seq_file *m,
5922 struct hist_trigger_data *hist_data)
5923 {
5924 unsigned int i;
5925
5926 for (i = 0; i < hist_data->n_actions; i++) {
5927 struct action_data *data = hist_data->actions[i];
5928
5929 if (data->handler == HANDLER_ONMATCH)
5930 print_onmatch_spec(m, hist_data, data);
5931 else if (data->handler == HANDLER_ONMAX ||
5932 data->handler == HANDLER_ONCHANGE)
5933 print_track_data_spec(m, hist_data, data);
5934 }
5935 }
5936
5937 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
5938 {
5939 unsigned int i;
5940
5941 for (i = 0; i < hist_data->n_field_var_hists; i++) {
5942 kfree(hist_data->field_var_hists[i]->cmd);
5943 kfree(hist_data->field_var_hists[i]);
5944 }
5945 }
5946
5947 static void destroy_hist_data(struct hist_trigger_data *hist_data)
5948 {
5949 if (!hist_data)
5950 return;
5951
5952 destroy_hist_trigger_attrs(hist_data->attrs);
5953 destroy_hist_fields(hist_data);
5954 tracing_map_destroy(hist_data->map);
5955
5956 destroy_actions(hist_data);
5957 destroy_field_vars(hist_data);
5958 destroy_field_var_hists(hist_data);
5959
5960 kfree(hist_data);
5961 }
5962
5963 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
5964 {
5965 struct tracing_map *map = hist_data->map;
5966 struct ftrace_event_field *field;
5967 struct hist_field *hist_field;
5968 int i, idx = 0;
5969
5970 for_each_hist_field(i, hist_data) {
5971 hist_field = hist_data->fields[i];
5972 if (hist_field->flags & HIST_FIELD_FL_KEY) {
5973 tracing_map_cmp_fn_t cmp_fn;
5974
5975 field = hist_field->field;
5976
5977 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
5978 cmp_fn = tracing_map_cmp_none;
5979 else if (!field)
5980 cmp_fn = tracing_map_cmp_num(hist_field->size,
5981 hist_field->is_signed);
5982 else if (is_string_field(field))
5983 cmp_fn = tracing_map_cmp_string;
5984 else
5985 cmp_fn = tracing_map_cmp_num(field->size,
5986 field->is_signed);
5987 idx = tracing_map_add_key_field(map,
5988 hist_field->offset,
5989 cmp_fn);
5990 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
5991 idx = tracing_map_add_sum_field(map);
5992
5993 if (idx < 0)
5994 return idx;
5995
5996 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5997 idx = tracing_map_add_var(map);
5998 if (idx < 0)
5999 return idx;
6000 hist_field->var.idx = idx;
6001 hist_field->var.hist_data = hist_data;
6002 }
6003 }
6004
6005 return 0;
6006 }
6007
6008 static struct hist_trigger_data *
6009 create_hist_data(unsigned int map_bits,
6010 struct hist_trigger_attrs *attrs,
6011 struct trace_event_file *file,
6012 bool remove)
6013 {
6014 const struct tracing_map_ops *map_ops = NULL;
6015 struct hist_trigger_data *hist_data;
6016 int ret = 0;
6017
6018 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
6019 if (!hist_data)
6020 return ERR_PTR(-ENOMEM);
6021
6022 hist_data->attrs = attrs;
6023 hist_data->remove = remove;
6024 hist_data->event_file = file;
6025
6026 ret = parse_actions(hist_data);
6027 if (ret)
6028 goto free;
6029
6030 ret = create_hist_fields(hist_data, file);
6031 if (ret)
6032 goto free;
6033
6034 ret = create_sort_keys(hist_data);
6035 if (ret)
6036 goto free;
6037
6038 map_ops = &hist_trigger_elt_data_ops;
6039
6040 hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
6041 map_ops, hist_data);
6042 if (IS_ERR(hist_data->map)) {
6043 ret = PTR_ERR(hist_data->map);
6044 hist_data->map = NULL;
6045 goto free;
6046 }
6047
6048 ret = create_tracing_map_fields(hist_data);
6049 if (ret)
6050 goto free;
6051 out:
6052 return hist_data;
6053 free:
6054 hist_data->attrs = NULL;
6055
6056 destroy_hist_data(hist_data);
6057
6058 hist_data = ERR_PTR(ret);
6059
6060 goto out;
6061 }
6062
6063 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
6064 struct tracing_map_elt *elt, void *rec,
6065 struct ring_buffer_event *rbe,
6066 u64 *var_ref_vals)
6067 {
6068 struct hist_elt_data *elt_data;
6069 struct hist_field *hist_field;
6070 unsigned int i, var_idx;
6071 u64 hist_val;
6072
6073 elt_data = elt->private_data;
6074 elt_data->var_ref_vals = var_ref_vals;
6075
6076 for_each_hist_val_field(i, hist_data) {
6077 hist_field = hist_data->fields[i];
6078 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
6079 if (hist_field->flags & HIST_FIELD_FL_VAR) {
6080 var_idx = hist_field->var.idx;
6081 tracing_map_set_var(elt, var_idx, hist_val);
6082 continue;
6083 }
6084 tracing_map_update_sum(elt, i, hist_val);
6085 }
6086
6087 for_each_hist_key_field(i, hist_data) {
6088 hist_field = hist_data->fields[i];
6089 if (hist_field->flags & HIST_FIELD_FL_VAR) {
6090 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
6091 var_idx = hist_field->var.idx;
6092 tracing_map_set_var(elt, var_idx, hist_val);
6093 }
6094 }
6095
6096 update_field_vars(hist_data, elt, rbe, rec);
6097 }
6098
6099 static inline void add_to_key(char *compound_key, void *key,
6100 struct hist_field *key_field, void *rec)
6101 {
6102 size_t size = key_field->size;
6103
6104 if (key_field->flags & HIST_FIELD_FL_STRING) {
6105 struct ftrace_event_field *field;
6106
6107 field = key_field->field;
6108 if (field->filter_type == FILTER_DYN_STRING)
6109 size = *(u32 *)(rec + field->offset) >> 16;
6110 else if (field->filter_type == FILTER_PTR_STRING)
6111 size = strlen(key);
6112 else if (field->filter_type == FILTER_STATIC_STRING)
6113 size = field->size;
6114
6115 /* ensure NULL-termination */
6116 if (size > key_field->size - 1)
6117 size = key_field->size - 1;
6118
6119 strncpy(compound_key + key_field->offset, (char *)key, size);
6120 } else
6121 memcpy(compound_key + key_field->offset, key, size);
6122 }
6123
6124 static void
6125 hist_trigger_actions(struct hist_trigger_data *hist_data,
6126 struct tracing_map_elt *elt, void *rec,
6127 struct ring_buffer_event *rbe, void *key,
6128 u64 *var_ref_vals)
6129 {
6130 struct action_data *data;
6131 unsigned int i;
6132
6133 for (i = 0; i < hist_data->n_actions; i++) {
6134 data = hist_data->actions[i];
6135 data->fn(hist_data, elt, rec, rbe, key, data, var_ref_vals);
6136 }
6137 }
6138
6139 static void event_hist_trigger(struct event_trigger_data *data, void *rec,
6140 struct ring_buffer_event *rbe)
6141 {
6142 struct hist_trigger_data *hist_data = data->private_data;
6143 bool use_compound_key = (hist_data->n_keys > 1);
6144 unsigned long entries[HIST_STACKTRACE_DEPTH];
6145 u64 var_ref_vals[TRACING_MAP_VARS_MAX];
6146 char compound_key[HIST_KEY_SIZE_MAX];
6147 struct tracing_map_elt *elt = NULL;
6148 struct hist_field *key_field;
6149 u64 field_contents;
6150 void *key = NULL;
6151 unsigned int i;
6152
6153 memset(compound_key, 0, hist_data->key_size);
6154
6155 for_each_hist_key_field(i, hist_data) {
6156 key_field = hist_data->fields[i];
6157
6158 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
6159 memset(entries, 0, HIST_STACKTRACE_SIZE);
6160 stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
6161 HIST_STACKTRACE_SKIP);
6162 key = entries;
6163 } else {
6164 field_contents = key_field->fn(key_field, elt, rbe, rec);
6165 if (key_field->flags & HIST_FIELD_FL_STRING) {
6166 key = (void *)(unsigned long)field_contents;
6167 use_compound_key = true;
6168 } else
6169 key = (void *)&field_contents;
6170 }
6171
6172 if (use_compound_key)
6173 add_to_key(compound_key, key, key_field, rec);
6174 }
6175
6176 if (use_compound_key)
6177 key = compound_key;
6178
6179 if (hist_data->n_var_refs &&
6180 !resolve_var_refs(hist_data, key, var_ref_vals, false))
6181 return;
6182
6183 elt = tracing_map_insert(hist_data->map, key);
6184 if (!elt)
6185 return;
6186
6187 hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals);
6188
6189 if (resolve_var_refs(hist_data, key, var_ref_vals, true))
6190 hist_trigger_actions(hist_data, elt, rec, rbe, key, var_ref_vals);
6191 }
6192
6193 static void hist_trigger_stacktrace_print(struct seq_file *m,
6194 unsigned long *stacktrace_entries,
6195 unsigned int max_entries)
6196 {
6197 char str[KSYM_SYMBOL_LEN];
6198 unsigned int spaces = 8;
6199 unsigned int i;
6200
6201 for (i = 0; i < max_entries; i++) {
6202 if (!stacktrace_entries[i])
6203 return;
6204
6205 seq_printf(m, "%*c", 1 + spaces, ' ');
6206 sprint_symbol(str, stacktrace_entries[i]);
6207 seq_printf(m, "%s\n", str);
6208 }
6209 }
6210
6211 static void hist_trigger_print_key(struct seq_file *m,
6212 struct hist_trigger_data *hist_data,
6213 void *key,
6214 struct tracing_map_elt *elt)
6215 {
6216 struct hist_field *key_field;
6217 char str[KSYM_SYMBOL_LEN];
6218 bool multiline = false;
6219 const char *field_name;
6220 unsigned int i;
6221 u64 uval;
6222
6223 seq_puts(m, "{ ");
6224
6225 for_each_hist_key_field(i, hist_data) {
6226 key_field = hist_data->fields[i];
6227
6228 if (i > hist_data->n_vals)
6229 seq_puts(m, ", ");
6230
6231 field_name = hist_field_name(key_field, 0);
6232
6233 if (key_field->flags & HIST_FIELD_FL_HEX) {
6234 uval = *(u64 *)(key + key_field->offset);
6235 seq_printf(m, "%s: %llx", field_name, uval);
6236 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
6237 uval = *(u64 *)(key + key_field->offset);
6238 sprint_symbol_no_offset(str, uval);
6239 seq_printf(m, "%s: [%llx] %-45s", field_name,
6240 uval, str);
6241 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
6242 uval = *(u64 *)(key + key_field->offset);
6243 sprint_symbol(str, uval);
6244 seq_printf(m, "%s: [%llx] %-55s", field_name,
6245 uval, str);
6246 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
6247 struct hist_elt_data *elt_data = elt->private_data;
6248 char *comm;
6249
6250 if (WARN_ON_ONCE(!elt_data))
6251 return;
6252
6253 comm = elt_data->comm;
6254
6255 uval = *(u64 *)(key + key_field->offset);
6256 seq_printf(m, "%s: %-16s[%10llu]", field_name,
6257 comm, uval);
6258 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
6259 const char *syscall_name;
6260
6261 uval = *(u64 *)(key + key_field->offset);
6262 syscall_name = get_syscall_name(uval);
6263 if (!syscall_name)
6264 syscall_name = "unknown_syscall";
6265
6266 seq_printf(m, "%s: %-30s[%3llu]", field_name,
6267 syscall_name, uval);
6268 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
6269 seq_puts(m, "stacktrace:\n");
6270 hist_trigger_stacktrace_print(m,
6271 key + key_field->offset,
6272 HIST_STACKTRACE_DEPTH);
6273 multiline = true;
6274 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
6275 seq_printf(m, "%s: ~ 2^%-2llu", field_name,
6276 *(u64 *)(key + key_field->offset));
6277 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
6278 seq_printf(m, "%s: %-50s", field_name,
6279 (char *)(key + key_field->offset));
6280 } else {
6281 uval = *(u64 *)(key + key_field->offset);
6282 seq_printf(m, "%s: %10llu", field_name, uval);
6283 }
6284 }
6285
6286 if (!multiline)
6287 seq_puts(m, " ");
6288
6289 seq_puts(m, "}");
6290 }
6291
6292 static void hist_trigger_entry_print(struct seq_file *m,
6293 struct hist_trigger_data *hist_data,
6294 void *key,
6295 struct tracing_map_elt *elt)
6296 {
6297 const char *field_name;
6298 unsigned int i;
6299
6300 hist_trigger_print_key(m, hist_data, key, elt);
6301
6302 seq_printf(m, " hitcount: %10llu",
6303 tracing_map_read_sum(elt, HITCOUNT_IDX));
6304
6305 for (i = 1; i < hist_data->n_vals; i++) {
6306 field_name = hist_field_name(hist_data->fields[i], 0);
6307
6308 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
6309 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
6310 continue;
6311
6312 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
6313 seq_printf(m, " %s: %10llx", field_name,
6314 tracing_map_read_sum(elt, i));
6315 } else {
6316 seq_printf(m, " %s: %10llu", field_name,
6317 tracing_map_read_sum(elt, i));
6318 }
6319 }
6320
6321 print_actions(m, hist_data, elt);
6322
6323 seq_puts(m, "\n");
6324 }
6325
6326 static int print_entries(struct seq_file *m,
6327 struct hist_trigger_data *hist_data)
6328 {
6329 struct tracing_map_sort_entry **sort_entries = NULL;
6330 struct tracing_map *map = hist_data->map;
6331 int i, n_entries;
6332
6333 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
6334 hist_data->n_sort_keys,
6335 &sort_entries);
6336 if (n_entries < 0)
6337 return n_entries;
6338
6339 for (i = 0; i < n_entries; i++)
6340 hist_trigger_entry_print(m, hist_data,
6341 sort_entries[i]->key,
6342 sort_entries[i]->elt);
6343
6344 tracing_map_destroy_sort_entries(sort_entries, n_entries);
6345
6346 return n_entries;
6347 }
6348
6349 static void hist_trigger_show(struct seq_file *m,
6350 struct event_trigger_data *data, int n)
6351 {
6352 struct hist_trigger_data *hist_data;
6353 int n_entries;
6354
6355 if (n > 0)
6356 seq_puts(m, "\n\n");
6357
6358 seq_puts(m, "# event histogram\n#\n# trigger info: ");
6359 data->ops->print(m, data->ops, data);
6360 seq_puts(m, "#\n\n");
6361
6362 hist_data = data->private_data;
6363 n_entries = print_entries(m, hist_data);
6364 if (n_entries < 0)
6365 n_entries = 0;
6366
6367 track_data_snapshot_print(m, hist_data);
6368
6369 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
6370 (u64)atomic64_read(&hist_data->map->hits),
6371 n_entries, (u64)atomic64_read(&hist_data->map->drops));
6372 }
6373
6374 static int hist_show(struct seq_file *m, void *v)
6375 {
6376 struct event_trigger_data *data;
6377 struct trace_event_file *event_file;
6378 int n = 0, ret = 0;
6379
6380 mutex_lock(&event_mutex);
6381
6382 event_file = event_file_data(m->private);
6383 if (unlikely(!event_file)) {
6384 ret = -ENODEV;
6385 goto out_unlock;
6386 }
6387
6388 list_for_each_entry(data, &event_file->triggers, list) {
6389 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
6390 hist_trigger_show(m, data, n++);
6391 }
6392
6393 out_unlock:
6394 mutex_unlock(&event_mutex);
6395
6396 return ret;
6397 }
6398
6399 static int event_hist_open(struct inode *inode, struct file *file)
6400 {
6401 int ret;
6402
6403 ret = security_locked_down(LOCKDOWN_TRACEFS);
6404 if (ret)
6405 return ret;
6406
6407 return single_open(file, hist_show, file);
6408 }
6409
6410 const struct file_operations event_hist_fops = {
6411 .open = event_hist_open,
6412 .read = seq_read,
6413 .llseek = seq_lseek,
6414 .release = single_release,
6415 };
6416
6417 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
6418 {
6419 const char *field_name = hist_field_name(hist_field, 0);
6420
6421 if (hist_field->var.name)
6422 seq_printf(m, "%s=", hist_field->var.name);
6423
6424 if (hist_field->flags & HIST_FIELD_FL_CPU)
6425 seq_puts(m, "cpu");
6426 else if (field_name) {
6427 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
6428 hist_field->flags & HIST_FIELD_FL_ALIAS)
6429 seq_putc(m, '$');
6430 seq_printf(m, "%s", field_name);
6431 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
6432 seq_puts(m, "common_timestamp");
6433
6434 if (hist_field->flags) {
6435 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
6436 !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
6437 const char *flags = get_hist_field_flags(hist_field);
6438
6439 if (flags)
6440 seq_printf(m, ".%s", flags);
6441 }
6442 }
6443 }
6444
6445 static int event_hist_trigger_print(struct seq_file *m,
6446 struct event_trigger_ops *ops,
6447 struct event_trigger_data *data)
6448 {
6449 struct hist_trigger_data *hist_data = data->private_data;
6450 struct hist_field *field;
6451 bool have_var = false;
6452 unsigned int i;
6453
6454 seq_puts(m, "hist:");
6455
6456 if (data->name)
6457 seq_printf(m, "%s:", data->name);
6458
6459 seq_puts(m, "keys=");
6460
6461 for_each_hist_key_field(i, hist_data) {
6462 field = hist_data->fields[i];
6463
6464 if (i > hist_data->n_vals)
6465 seq_puts(m, ",");
6466
6467 if (field->flags & HIST_FIELD_FL_STACKTRACE)
6468 seq_puts(m, "stacktrace");
6469 else
6470 hist_field_print(m, field);
6471 }
6472
6473 seq_puts(m, ":vals=");
6474
6475 for_each_hist_val_field(i, hist_data) {
6476 field = hist_data->fields[i];
6477 if (field->flags & HIST_FIELD_FL_VAR) {
6478 have_var = true;
6479 continue;
6480 }
6481
6482 if (i == HITCOUNT_IDX)
6483 seq_puts(m, "hitcount");
6484 else {
6485 seq_puts(m, ",");
6486 hist_field_print(m, field);
6487 }
6488 }
6489
6490 if (have_var) {
6491 unsigned int n = 0;
6492
6493 seq_puts(m, ":");
6494
6495 for_each_hist_val_field(i, hist_data) {
6496 field = hist_data->fields[i];
6497
6498 if (field->flags & HIST_FIELD_FL_VAR) {
6499 if (n++)
6500 seq_puts(m, ",");
6501 hist_field_print(m, field);
6502 }
6503 }
6504 }
6505
6506 seq_puts(m, ":sort=");
6507
6508 for (i = 0; i < hist_data->n_sort_keys; i++) {
6509 struct tracing_map_sort_key *sort_key;
6510 unsigned int idx, first_key_idx;
6511
6512 /* skip VAR vals */
6513 first_key_idx = hist_data->n_vals - hist_data->n_vars;
6514
6515 sort_key = &hist_data->sort_keys[i];
6516 idx = sort_key->field_idx;
6517
6518 if (WARN_ON(idx >= HIST_FIELDS_MAX))
6519 return -EINVAL;
6520
6521 if (i > 0)
6522 seq_puts(m, ",");
6523
6524 if (idx == HITCOUNT_IDX)
6525 seq_puts(m, "hitcount");
6526 else {
6527 if (idx >= first_key_idx)
6528 idx += hist_data->n_vars;
6529 hist_field_print(m, hist_data->fields[idx]);
6530 }
6531
6532 if (sort_key->descending)
6533 seq_puts(m, ".descending");
6534 }
6535 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
6536 if (hist_data->enable_timestamps)
6537 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
6538
6539 print_actions_spec(m, hist_data);
6540
6541 if (data->filter_str)
6542 seq_printf(m, " if %s", data->filter_str);
6543
6544 if (data->paused)
6545 seq_puts(m, " [paused]");
6546 else
6547 seq_puts(m, " [active]");
6548
6549 seq_putc(m, '\n');
6550
6551 return 0;
6552 }
6553
6554 static int event_hist_trigger_init(struct event_trigger_ops *ops,
6555 struct event_trigger_data *data)
6556 {
6557 struct hist_trigger_data *hist_data = data->private_data;
6558
6559 if (!data->ref && hist_data->attrs->name)
6560 save_named_trigger(hist_data->attrs->name, data);
6561
6562 data->ref++;
6563
6564 return 0;
6565 }
6566
6567 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
6568 {
6569 struct trace_event_file *file;
6570 unsigned int i;
6571 char *cmd;
6572 int ret;
6573
6574 for (i = 0; i < hist_data->n_field_var_hists; i++) {
6575 file = hist_data->field_var_hists[i]->hist_data->event_file;
6576 cmd = hist_data->field_var_hists[i]->cmd;
6577 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
6578 "!hist", "hist", cmd);
6579 }
6580 }
6581
6582 static void event_hist_trigger_free(struct event_trigger_ops *ops,
6583 struct event_trigger_data *data)
6584 {
6585 struct hist_trigger_data *hist_data = data->private_data;
6586
6587 if (WARN_ON_ONCE(data->ref <= 0))
6588 return;
6589
6590 data->ref--;
6591 if (!data->ref) {
6592 if (data->name)
6593 del_named_trigger(data);
6594
6595 trigger_data_free(data);
6596
6597 remove_hist_vars(hist_data);
6598
6599 unregister_field_var_hists(hist_data);
6600
6601 destroy_hist_data(hist_data);
6602 }
6603 }
6604
6605 static struct event_trigger_ops event_hist_trigger_ops = {
6606 .func = event_hist_trigger,
6607 .print = event_hist_trigger_print,
6608 .init = event_hist_trigger_init,
6609 .free = event_hist_trigger_free,
6610 };
6611
6612 static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
6613 struct event_trigger_data *data)
6614 {
6615 data->ref++;
6616
6617 save_named_trigger(data->named_data->name, data);
6618
6619 event_hist_trigger_init(ops, data->named_data);
6620
6621 return 0;
6622 }
6623
6624 static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
6625 struct event_trigger_data *data)
6626 {
6627 if (WARN_ON_ONCE(data->ref <= 0))
6628 return;
6629
6630 event_hist_trigger_free(ops, data->named_data);
6631
6632 data->ref--;
6633 if (!data->ref) {
6634 del_named_trigger(data);
6635 trigger_data_free(data);
6636 }
6637 }
6638
6639 static struct event_trigger_ops event_hist_trigger_named_ops = {
6640 .func = event_hist_trigger,
6641 .print = event_hist_trigger_print,
6642 .init = event_hist_trigger_named_init,
6643 .free = event_hist_trigger_named_free,
6644 };
6645
6646 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
6647 char *param)
6648 {
6649 return &event_hist_trigger_ops;
6650 }
6651
6652 static void hist_clear(struct event_trigger_data *data)
6653 {
6654 struct hist_trigger_data *hist_data = data->private_data;
6655
6656 if (data->name)
6657 pause_named_trigger(data);
6658
6659 tracepoint_synchronize_unregister();
6660
6661 tracing_map_clear(hist_data->map);
6662
6663 if (data->name)
6664 unpause_named_trigger(data);
6665 }
6666
6667 static bool compatible_field(struct ftrace_event_field *field,
6668 struct ftrace_event_field *test_field)
6669 {
6670 if (field == test_field)
6671 return true;
6672 if (field == NULL || test_field == NULL)
6673 return false;
6674 if (strcmp(field->name, test_field->name) != 0)
6675 return false;
6676 if (strcmp(field->type, test_field->type) != 0)
6677 return false;
6678 if (field->size != test_field->size)
6679 return false;
6680 if (field->is_signed != test_field->is_signed)
6681 return false;
6682
6683 return true;
6684 }
6685
6686 static bool hist_trigger_match(struct event_trigger_data *data,
6687 struct event_trigger_data *data_test,
6688 struct event_trigger_data *named_data,
6689 bool ignore_filter)
6690 {
6691 struct tracing_map_sort_key *sort_key, *sort_key_test;
6692 struct hist_trigger_data *hist_data, *hist_data_test;
6693 struct hist_field *key_field, *key_field_test;
6694 unsigned int i;
6695
6696 if (named_data && (named_data != data_test) &&
6697 (named_data != data_test->named_data))
6698 return false;
6699
6700 if (!named_data && is_named_trigger(data_test))
6701 return false;
6702
6703 hist_data = data->private_data;
6704 hist_data_test = data_test->private_data;
6705
6706 if (hist_data->n_vals != hist_data_test->n_vals ||
6707 hist_data->n_fields != hist_data_test->n_fields ||
6708 hist_data->n_sort_keys != hist_data_test->n_sort_keys)
6709 return false;
6710
6711 if (!ignore_filter) {
6712 if ((data->filter_str && !data_test->filter_str) ||
6713 (!data->filter_str && data_test->filter_str))
6714 return false;
6715 }
6716
6717 for_each_hist_field(i, hist_data) {
6718 key_field = hist_data->fields[i];
6719 key_field_test = hist_data_test->fields[i];
6720
6721 if (key_field->flags != key_field_test->flags)
6722 return false;
6723 if (!compatible_field(key_field->field, key_field_test->field))
6724 return false;
6725 if (key_field->offset != key_field_test->offset)
6726 return false;
6727 if (key_field->size != key_field_test->size)
6728 return false;
6729 if (key_field->is_signed != key_field_test->is_signed)
6730 return false;
6731 if (!!key_field->var.name != !!key_field_test->var.name)
6732 return false;
6733 if (key_field->var.name &&
6734 strcmp(key_field->var.name, key_field_test->var.name) != 0)
6735 return false;
6736 }
6737
6738 for (i = 0; i < hist_data->n_sort_keys; i++) {
6739 sort_key = &hist_data->sort_keys[i];
6740 sort_key_test = &hist_data_test->sort_keys[i];
6741
6742 if (sort_key->field_idx != sort_key_test->field_idx ||
6743 sort_key->descending != sort_key_test->descending)
6744 return false;
6745 }
6746
6747 if (!ignore_filter && data->filter_str &&
6748 (strcmp(data->filter_str, data_test->filter_str) != 0))
6749 return false;
6750
6751 if (!actions_match(hist_data, hist_data_test))
6752 return false;
6753
6754 return true;
6755 }
6756
6757 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
6758 struct event_trigger_data *data,
6759 struct trace_event_file *file)
6760 {
6761 struct hist_trigger_data *hist_data = data->private_data;
6762 struct event_trigger_data *test, *named_data = NULL;
6763 struct trace_array *tr = file->tr;
6764 int ret = 0;
6765
6766 if (hist_data->attrs->name) {
6767 named_data = find_named_trigger(hist_data->attrs->name);
6768 if (named_data) {
6769 if (!hist_trigger_match(data, named_data, named_data,
6770 true)) {
6771 hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name));
6772 ret = -EINVAL;
6773 goto out;
6774 }
6775 }
6776 }
6777
6778 if (hist_data->attrs->name && !named_data)
6779 goto new;
6780
6781 lockdep_assert_held(&event_mutex);
6782
6783 list_for_each_entry(test, &file->triggers, list) {
6784 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6785 if (!hist_trigger_match(data, test, named_data, false))
6786 continue;
6787 if (hist_data->attrs->pause)
6788 test->paused = true;
6789 else if (hist_data->attrs->cont)
6790 test->paused = false;
6791 else if (hist_data->attrs->clear)
6792 hist_clear(test);
6793 else {
6794 hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
6795 ret = -EEXIST;
6796 }
6797 goto out;
6798 }
6799 }
6800 new:
6801 if (hist_data->attrs->cont || hist_data->attrs->clear) {
6802 hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0);
6803 ret = -ENOENT;
6804 goto out;
6805 }
6806
6807 if (hist_data->attrs->pause)
6808 data->paused = true;
6809
6810 if (named_data) {
6811 data->private_data = named_data->private_data;
6812 set_named_trigger_data(data, named_data);
6813 data->ops = &event_hist_trigger_named_ops;
6814 }
6815
6816 if (data->ops->init) {
6817 ret = data->ops->init(data->ops, data);
6818 if (ret < 0)
6819 goto out;
6820 }
6821
6822 if (hist_data->enable_timestamps) {
6823 char *clock = hist_data->attrs->clock;
6824
6825 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
6826 if (ret) {
6827 hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock));
6828 goto out;
6829 }
6830
6831 tracing_set_time_stamp_abs(file->tr, true);
6832 }
6833
6834 if (named_data)
6835 destroy_hist_data(hist_data);
6836
6837 ret++;
6838 out:
6839 return ret;
6840 }
6841
6842 static int hist_trigger_enable(struct event_trigger_data *data,
6843 struct trace_event_file *file)
6844 {
6845 int ret = 0;
6846
6847 list_add_tail_rcu(&data->list, &file->triggers);
6848
6849 update_cond_flag(file);
6850
6851 if (trace_event_trigger_enable_disable(file, 1) < 0) {
6852 list_del_rcu(&data->list);
6853 update_cond_flag(file);
6854 ret--;
6855 }
6856
6857 return ret;
6858 }
6859
6860 static bool have_hist_trigger_match(struct event_trigger_data *data,
6861 struct trace_event_file *file)
6862 {
6863 struct hist_trigger_data *hist_data = data->private_data;
6864 struct event_trigger_data *test, *named_data = NULL;
6865 bool match = false;
6866
6867 lockdep_assert_held(&event_mutex);
6868
6869 if (hist_data->attrs->name)
6870 named_data = find_named_trigger(hist_data->attrs->name);
6871
6872 list_for_each_entry(test, &file->triggers, list) {
6873 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6874 if (hist_trigger_match(data, test, named_data, false)) {
6875 match = true;
6876 break;
6877 }
6878 }
6879 }
6880
6881 return match;
6882 }
6883
6884 static bool hist_trigger_check_refs(struct event_trigger_data *data,
6885 struct trace_event_file *file)
6886 {
6887 struct hist_trigger_data *hist_data = data->private_data;
6888 struct event_trigger_data *test, *named_data = NULL;
6889
6890 lockdep_assert_held(&event_mutex);
6891
6892 if (hist_data->attrs->name)
6893 named_data = find_named_trigger(hist_data->attrs->name);
6894
6895 list_for_each_entry(test, &file->triggers, list) {
6896 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6897 if (!hist_trigger_match(data, test, named_data, false))
6898 continue;
6899 hist_data = test->private_data;
6900 if (check_var_refs(hist_data))
6901 return true;
6902 break;
6903 }
6904 }
6905
6906 return false;
6907 }
6908
6909 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
6910 struct event_trigger_data *data,
6911 struct trace_event_file *file)
6912 {
6913 struct hist_trigger_data *hist_data = data->private_data;
6914 struct event_trigger_data *test, *named_data = NULL;
6915 bool unregistered = false;
6916
6917 lockdep_assert_held(&event_mutex);
6918
6919 if (hist_data->attrs->name)
6920 named_data = find_named_trigger(hist_data->attrs->name);
6921
6922 list_for_each_entry(test, &file->triggers, list) {
6923 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6924 if (!hist_trigger_match(data, test, named_data, false))
6925 continue;
6926 unregistered = true;
6927 list_del_rcu(&test->list);
6928 trace_event_trigger_enable_disable(file, 0);
6929 update_cond_flag(file);
6930 break;
6931 }
6932 }
6933
6934 if (unregistered && test->ops->free)
6935 test->ops->free(test->ops, test);
6936
6937 if (hist_data->enable_timestamps) {
6938 if (!hist_data->remove || unregistered)
6939 tracing_set_time_stamp_abs(file->tr, false);
6940 }
6941 }
6942
6943 static bool hist_file_check_refs(struct trace_event_file *file)
6944 {
6945 struct hist_trigger_data *hist_data;
6946 struct event_trigger_data *test;
6947
6948 lockdep_assert_held(&event_mutex);
6949
6950 list_for_each_entry(test, &file->triggers, list) {
6951 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6952 hist_data = test->private_data;
6953 if (check_var_refs(hist_data))
6954 return true;
6955 }
6956 }
6957
6958 return false;
6959 }
6960
6961 static void hist_unreg_all(struct trace_event_file *file)
6962 {
6963 struct event_trigger_data *test, *n;
6964 struct hist_trigger_data *hist_data;
6965 struct synth_event *se;
6966 const char *se_name;
6967
6968 lockdep_assert_held(&event_mutex);
6969
6970 if (hist_file_check_refs(file))
6971 return;
6972
6973 list_for_each_entry_safe(test, n, &file->triggers, list) {
6974 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6975 hist_data = test->private_data;
6976 list_del_rcu(&test->list);
6977 trace_event_trigger_enable_disable(file, 0);
6978
6979 se_name = trace_event_name(file->event_call);
6980 se = find_synth_event(se_name);
6981 if (se)
6982 se->ref--;
6983
6984 update_cond_flag(file);
6985 if (hist_data->enable_timestamps)
6986 tracing_set_time_stamp_abs(file->tr, false);
6987 if (test->ops->free)
6988 test->ops->free(test->ops, test);
6989 }
6990 }
6991 }
6992
6993 static int event_hist_trigger_func(struct event_command *cmd_ops,
6994 struct trace_event_file *file,
6995 char *glob, char *cmd, char *param)
6996 {
6997 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
6998 struct event_trigger_data *trigger_data;
6999 struct hist_trigger_attrs *attrs;
7000 struct event_trigger_ops *trigger_ops;
7001 struct hist_trigger_data *hist_data;
7002 struct synth_event *se;
7003 const char *se_name;
7004 bool remove = false;
7005 char *trigger, *p;
7006 int ret = 0;
7007
7008 lockdep_assert_held(&event_mutex);
7009
7010 if (glob && strlen(glob)) {
7011 hist_err_clear();
7012 last_cmd_set(file, param);
7013 }
7014
7015 if (!param)
7016 return -EINVAL;
7017
7018 if (glob[0] == '!')
7019 remove = true;
7020
7021 /*
7022 * separate the trigger from the filter (k:v [if filter])
7023 * allowing for whitespace in the trigger
7024 */
7025 p = trigger = param;
7026 do {
7027 p = strstr(p, "if");
7028 if (!p)
7029 break;
7030 if (p == param)
7031 return -EINVAL;
7032 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
7033 p++;
7034 continue;
7035 }
7036 if (p >= param + strlen(param) - (sizeof("if") - 1) - 1)
7037 return -EINVAL;
7038 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
7039 p++;
7040 continue;
7041 }
7042 break;
7043 } while (p);
7044
7045 if (!p)
7046 param = NULL;
7047 else {
7048 *(p - 1) = '\0';
7049 param = strstrip(p);
7050 trigger = strstrip(trigger);
7051 }
7052
7053 attrs = parse_hist_trigger_attrs(file->tr, trigger);
7054 if (IS_ERR(attrs))
7055 return PTR_ERR(attrs);
7056
7057 if (attrs->map_bits)
7058 hist_trigger_bits = attrs->map_bits;
7059
7060 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
7061 if (IS_ERR(hist_data)) {
7062 destroy_hist_trigger_attrs(attrs);
7063 return PTR_ERR(hist_data);
7064 }
7065
7066 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
7067
7068 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
7069 if (!trigger_data) {
7070 ret = -ENOMEM;
7071 goto out_free;
7072 }
7073
7074 trigger_data->count = -1;
7075 trigger_data->ops = trigger_ops;
7076 trigger_data->cmd_ops = cmd_ops;
7077
7078 INIT_LIST_HEAD(&trigger_data->list);
7079 RCU_INIT_POINTER(trigger_data->filter, NULL);
7080
7081 trigger_data->private_data = hist_data;
7082
7083 /* if param is non-empty, it's supposed to be a filter */
7084 if (param && cmd_ops->set_filter) {
7085 ret = cmd_ops->set_filter(param, trigger_data, file);
7086 if (ret < 0)
7087 goto out_free;
7088 }
7089
7090 if (remove) {
7091 if (!have_hist_trigger_match(trigger_data, file))
7092 goto out_free;
7093
7094 if (hist_trigger_check_refs(trigger_data, file)) {
7095 ret = -EBUSY;
7096 goto out_free;
7097 }
7098
7099 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
7100 se_name = trace_event_name(file->event_call);
7101 se = find_synth_event(se_name);
7102 if (se)
7103 se->ref--;
7104 ret = 0;
7105 goto out_free;
7106 }
7107
7108 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
7109 /*
7110 * The above returns on success the # of triggers registered,
7111 * but if it didn't register any it returns zero. Consider no
7112 * triggers registered a failure too.
7113 */
7114 if (!ret) {
7115 if (!(attrs->pause || attrs->cont || attrs->clear))
7116 ret = -ENOENT;
7117 goto out_free;
7118 } else if (ret < 0)
7119 goto out_free;
7120
7121 if (get_named_trigger_data(trigger_data))
7122 goto enable;
7123
7124 if (has_hist_vars(hist_data))
7125 save_hist_vars(hist_data);
7126
7127 ret = create_actions(hist_data);
7128 if (ret)
7129 goto out_unreg;
7130
7131 ret = tracing_map_init(hist_data->map);
7132 if (ret)
7133 goto out_unreg;
7134 enable:
7135 ret = hist_trigger_enable(trigger_data, file);
7136 if (ret)
7137 goto out_unreg;
7138
7139 se_name = trace_event_name(file->event_call);
7140 se = find_synth_event(se_name);
7141 if (se)
7142 se->ref++;
7143 /* Just return zero, not the number of registered triggers */
7144 ret = 0;
7145 out:
7146 if (ret == 0)
7147 hist_err_clear();
7148
7149 return ret;
7150 out_unreg:
7151 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
7152 out_free:
7153 if (cmd_ops->set_filter)
7154 cmd_ops->set_filter(NULL, trigger_data, NULL);
7155
7156 remove_hist_vars(hist_data);
7157
7158 kfree(trigger_data);
7159
7160 destroy_hist_data(hist_data);
7161 goto out;
7162 }
7163
7164 static struct event_command trigger_hist_cmd = {
7165 .name = "hist",
7166 .trigger_type = ETT_EVENT_HIST,
7167 .flags = EVENT_CMD_FL_NEEDS_REC,
7168 .func = event_hist_trigger_func,
7169 .reg = hist_register_trigger,
7170 .unreg = hist_unregister_trigger,
7171 .unreg_all = hist_unreg_all,
7172 .get_trigger_ops = event_hist_get_trigger_ops,
7173 .set_filter = set_trigger_filter,
7174 };
7175
7176 __init int register_trigger_hist_cmd(void)
7177 {
7178 int ret;
7179
7180 ret = register_event_command(&trigger_hist_cmd);
7181 WARN_ON(ret < 0);
7182
7183 return ret;
7184 }
7185
7186 static void
7187 hist_enable_trigger(struct event_trigger_data *data, void *rec,
7188 struct ring_buffer_event *event)
7189 {
7190 struct enable_trigger_data *enable_data = data->private_data;
7191 struct event_trigger_data *test;
7192
7193 list_for_each_entry_rcu(test, &enable_data->file->triggers, list,
7194 lockdep_is_held(&event_mutex)) {
7195 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
7196 if (enable_data->enable)
7197 test->paused = false;
7198 else
7199 test->paused = true;
7200 }
7201 }
7202 }
7203
7204 static void
7205 hist_enable_count_trigger(struct event_trigger_data *data, void *rec,
7206 struct ring_buffer_event *event)
7207 {
7208 if (!data->count)
7209 return;
7210
7211 if (data->count != -1)
7212 (data->count)--;
7213
7214 hist_enable_trigger(data, rec, event);
7215 }
7216
7217 static struct event_trigger_ops hist_enable_trigger_ops = {
7218 .func = hist_enable_trigger,
7219 .print = event_enable_trigger_print,
7220 .init = event_trigger_init,
7221 .free = event_enable_trigger_free,
7222 };
7223
7224 static struct event_trigger_ops hist_enable_count_trigger_ops = {
7225 .func = hist_enable_count_trigger,
7226 .print = event_enable_trigger_print,
7227 .init = event_trigger_init,
7228 .free = event_enable_trigger_free,
7229 };
7230
7231 static struct event_trigger_ops hist_disable_trigger_ops = {
7232 .func = hist_enable_trigger,
7233 .print = event_enable_trigger_print,
7234 .init = event_trigger_init,
7235 .free = event_enable_trigger_free,
7236 };
7237
7238 static struct event_trigger_ops hist_disable_count_trigger_ops = {
7239 .func = hist_enable_count_trigger,
7240 .print = event_enable_trigger_print,
7241 .init = event_trigger_init,
7242 .free = event_enable_trigger_free,
7243 };
7244
7245 static struct event_trigger_ops *
7246 hist_enable_get_trigger_ops(char *cmd, char *param)
7247 {
7248 struct event_trigger_ops *ops;
7249 bool enable;
7250
7251 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
7252
7253 if (enable)
7254 ops = param ? &hist_enable_count_trigger_ops :
7255 &hist_enable_trigger_ops;
7256 else
7257 ops = param ? &hist_disable_count_trigger_ops :
7258 &hist_disable_trigger_ops;
7259
7260 return ops;
7261 }
7262
7263 static void hist_enable_unreg_all(struct trace_event_file *file)
7264 {
7265 struct event_trigger_data *test, *n;
7266
7267 list_for_each_entry_safe(test, n, &file->triggers, list) {
7268 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
7269 list_del_rcu(&test->list);
7270 update_cond_flag(file);
7271 trace_event_trigger_enable_disable(file, 0);
7272 if (test->ops->free)
7273 test->ops->free(test->ops, test);
7274 }
7275 }
7276 }
7277
7278 static struct event_command trigger_hist_enable_cmd = {
7279 .name = ENABLE_HIST_STR,
7280 .trigger_type = ETT_HIST_ENABLE,
7281 .func = event_enable_trigger_func,
7282 .reg = event_enable_register_trigger,
7283 .unreg = event_enable_unregister_trigger,
7284 .unreg_all = hist_enable_unreg_all,
7285 .get_trigger_ops = hist_enable_get_trigger_ops,
7286 .set_filter = set_trigger_filter,
7287 };
7288
7289 static struct event_command trigger_hist_disable_cmd = {
7290 .name = DISABLE_HIST_STR,
7291 .trigger_type = ETT_HIST_ENABLE,
7292 .func = event_enable_trigger_func,
7293 .reg = event_enable_register_trigger,
7294 .unreg = event_enable_unregister_trigger,
7295 .unreg_all = hist_enable_unreg_all,
7296 .get_trigger_ops = hist_enable_get_trigger_ops,
7297 .set_filter = set_trigger_filter,
7298 };
7299
7300 static __init void unregister_trigger_hist_enable_disable_cmds(void)
7301 {
7302 unregister_event_command(&trigger_hist_enable_cmd);
7303 unregister_event_command(&trigger_hist_disable_cmd);
7304 }
7305
7306 __init int register_trigger_hist_enable_disable_cmds(void)
7307 {
7308 int ret;
7309
7310 ret = register_event_command(&trigger_hist_enable_cmd);
7311 if (WARN_ON(ret < 0))
7312 return ret;
7313 ret = register_event_command(&trigger_hist_disable_cmd);
7314 if (WARN_ON(ret < 0))
7315 unregister_trigger_hist_enable_disable_cmds();
7316
7317 return ret;
7318 }
7319
7320 static __init int trace_events_hist_init(void)
7321 {
7322 struct dentry *entry = NULL;
7323 struct dentry *d_tracer;
7324 int err = 0;
7325
7326 err = dyn_event_register(&synth_event_ops);
7327 if (err) {
7328 pr_warn("Could not register synth_event_ops\n");
7329 return err;
7330 }
7331
7332 d_tracer = tracing_init_dentry();
7333 if (IS_ERR(d_tracer)) {
7334 err = PTR_ERR(d_tracer);
7335 goto err;
7336 }
7337
7338 entry = tracefs_create_file("synthetic_events", 0644, d_tracer,
7339 NULL, &synth_events_fops);
7340 if (!entry) {
7341 err = -ENODEV;
7342 goto err;
7343 }
7344
7345 return err;
7346 err:
7347 pr_warn("Could not create tracefs 'synthetic_events' entry\n");
7348
7349 return err;
7350 }
7351
7352 fs_initcall(trace_events_hist_init);