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 return 0; 1833 1834 event = file->event_call->data; 1835 1836 if (n_vals != event->n_fields) 1837 return -EINVAL; 1838 1839 if (trace_trigger_soft_disabled(file)) 1840 return -EINVAL; 1841 1842 fields_size = event->n_u64 * sizeof(u64); 1843 1844 /* 1845 * Avoid ring buffer recursion detection, as this event 1846 * is being performed within another event. 1847 */ 1848 buffer = file->tr->array_buffer.buffer; 1849 ring_buffer_nest_start(buffer); 1850 1851 entry = trace_event_buffer_reserve(&fbuffer, file, 1852 sizeof(*entry) + fields_size); 1853 if (!entry) { 1854 ret = -EINVAL; 1855 goto out; 1856 } 1857 1858 va_start(args, n_vals); 1859 for (i = 0, n_u64 = 0; i < event->n_fields; i++) { 1860 u64 val; 1861 1862 val = va_arg(args, u64); 1863 1864 if (event->fields[i]->is_string) { 1865 char *str_val = (char *)(long)val; 1866 char *str_field = (char *)&entry->fields[n_u64]; 1867 1868 strscpy(str_field, str_val, STR_VAR_LEN_MAX); 1869 n_u64 += STR_VAR_LEN_MAX / sizeof(u64); 1870 } else { 1871 entry->fields[n_u64] = val; 1872 n_u64++; 1873 } 1874 } 1875 va_end(args); 1876 1877 trace_event_buffer_commit(&fbuffer); 1878 out: 1879 ring_buffer_nest_end(buffer); 1880 1881 return ret; 1882 } 1883 EXPORT_SYMBOL_GPL(synth_event_trace); 1884 1885 /** 1886 * synth_event_trace_array - Trace a synthetic event from an array 1887 * @file: The trace_event_file representing the synthetic event 1888 * @vals: Array of values 1889 * @n_vals: The number of values in vals 1890 * 1891 * Trace a synthetic event using the values passed in as 'vals'. 1892 * 1893 * The 'vals' array is just an array of 'n_vals' u64. The number of 1894 * vals must match the number of field in the synthetic event, and 1895 * must be in the same order as the synthetic event fields. 1896 * 1897 * All vals should be cast to u64, and string vals are just pointers 1898 * to strings, cast to u64. Strings will be copied into space 1899 * reserved in the event for the string, using these pointers. 1900 * 1901 * Return: 0 on success, err otherwise. 1902 */ 1903 int synth_event_trace_array(struct trace_event_file *file, u64 *vals, 1904 unsigned int n_vals) 1905 { 1906 struct trace_event_buffer fbuffer; 1907 struct synth_trace_event *entry; 1908 struct trace_buffer *buffer; 1909 struct synth_event *event; 1910 unsigned int i, n_u64; 1911 int fields_size = 0; 1912 int ret = 0; 1913 1914 /* 1915 * Normal event generation doesn't get called at all unless 1916 * the ENABLED bit is set (which attaches the probe thus 1917 * allowing this code to be called, etc). Because this is 1918 * called directly by the user, we don't have that but we 1919 * still need to honor not logging when disabled. 1920 */ 1921 if (!(file->flags & EVENT_FILE_FL_ENABLED)) 1922 return 0; 1923 1924 event = file->event_call->data; 1925 1926 if (n_vals != event->n_fields) 1927 return -EINVAL; 1928 1929 if (trace_trigger_soft_disabled(file)) 1930 return -EINVAL; 1931 1932 fields_size = event->n_u64 * sizeof(u64); 1933 1934 /* 1935 * Avoid ring buffer recursion detection, as this event 1936 * is being performed within another event. 1937 */ 1938 buffer = file->tr->array_buffer.buffer; 1939 ring_buffer_nest_start(buffer); 1940 1941 entry = trace_event_buffer_reserve(&fbuffer, file, 1942 sizeof(*entry) + fields_size); 1943 if (!entry) { 1944 ret = -EINVAL; 1945 goto out; 1946 } 1947 1948 for (i = 0, n_u64 = 0; i < event->n_fields; i++) { 1949 if (event->fields[i]->is_string) { 1950 char *str_val = (char *)(long)vals[i]; 1951 char *str_field = (char *)&entry->fields[n_u64]; 1952 1953 strscpy(str_field, str_val, STR_VAR_LEN_MAX); 1954 n_u64 += STR_VAR_LEN_MAX / sizeof(u64); 1955 } else { 1956 entry->fields[n_u64] = vals[i]; 1957 n_u64++; 1958 } 1959 } 1960 1961 trace_event_buffer_commit(&fbuffer); 1962 out: 1963 ring_buffer_nest_end(buffer); 1964 1965 return ret; 1966 } 1967 EXPORT_SYMBOL_GPL(synth_event_trace_array); 1968 1969 /** 1970 * synth_event_trace_start - Start piecewise synthetic event trace 1971 * @file: The trace_event_file representing the synthetic event 1972 * @trace_state: A pointer to object tracking the piecewise trace state 1973 * 1974 * Start the trace of a synthetic event field-by-field rather than all 1975 * at once. 1976 * 1977 * This function 'opens' an event trace, which means space is reserved 1978 * for the event in the trace buffer, after which the event's 1979 * individual field values can be set through either 1980 * synth_event_add_next_val() or synth_event_add_val(). 1981 * 1982 * A pointer to a trace_state object is passed in, which will keep 1983 * track of the current event trace state until the event trace is 1984 * closed (and the event finally traced) using 1985 * synth_event_trace_end(). 1986 * 1987 * Note that synth_event_trace_end() must be called after all values 1988 * have been added for each event trace, regardless of whether adding 1989 * all field values succeeded or not. 1990 * 1991 * Note also that for a given event trace, all fields must be added 1992 * using either synth_event_add_next_val() or synth_event_add_val() 1993 * but not both together or interleaved. 1994 * 1995 * Return: 0 on success, err otherwise. 1996 */ 1997 int synth_event_trace_start(struct trace_event_file *file, 1998 struct synth_event_trace_state *trace_state) 1999 { 2000 struct synth_trace_event *entry; 2001 int fields_size = 0; 2002 int ret = 0; 2003 2004 if (!trace_state) { 2005 ret = -EINVAL; 2006 goto out; 2007 } 2008 2009 memset(trace_state, '\0', sizeof(*trace_state)); 2010 2011 /* 2012 * Normal event tracing doesn't get called at all unless the 2013 * ENABLED bit is set (which attaches the probe thus allowing 2014 * this code to be called, etc). Because this is called 2015 * directly by the user, we don't have that but we still need 2016 * to honor not logging when disabled. For the the iterated 2017 * trace case, we save the enabed state upon start and just 2018 * ignore the following data calls. 2019 */ 2020 if (!(file->flags & EVENT_FILE_FL_ENABLED)) { 2021 trace_state->enabled = false; 2022 goto out; 2023 } 2024 2025 trace_state->enabled = true; 2026 2027 trace_state->event = file->event_call->data; 2028 2029 if (trace_trigger_soft_disabled(file)) { 2030 ret = -EINVAL; 2031 goto out; 2032 } 2033 2034 fields_size = trace_state->event->n_u64 * sizeof(u64); 2035 2036 /* 2037 * Avoid ring buffer recursion detection, as this event 2038 * is being performed within another event. 2039 */ 2040 trace_state->buffer = file->tr->array_buffer.buffer; 2041 ring_buffer_nest_start(trace_state->buffer); 2042 2043 entry = trace_event_buffer_reserve(&trace_state->fbuffer, file, 2044 sizeof(*entry) + fields_size); 2045 if (!entry) { 2046 ret = -EINVAL; 2047 goto out; 2048 } 2049 2050 trace_state->entry = entry; 2051 out: 2052 return ret; 2053 } 2054 EXPORT_SYMBOL_GPL(synth_event_trace_start); 2055 2056 static int __synth_event_add_val(const char *field_name, u64 val, 2057 struct synth_event_trace_state *trace_state) 2058 { 2059 struct synth_field *field = NULL; 2060 struct synth_trace_event *entry; 2061 struct synth_event *event; 2062 int i, ret = 0; 2063 2064 if (!trace_state) { 2065 ret = -EINVAL; 2066 goto out; 2067 } 2068 2069 /* can't mix add_next_synth_val() with add_synth_val() */ 2070 if (field_name) { 2071 if (trace_state->add_next) { 2072 ret = -EINVAL; 2073 goto out; 2074 } 2075 trace_state->add_name = true; 2076 } else { 2077 if (trace_state->add_name) { 2078 ret = -EINVAL; 2079 goto out; 2080 } 2081 trace_state->add_next = true; 2082 } 2083 2084 if (!trace_state->enabled) 2085 goto out; 2086 2087 event = trace_state->event; 2088 if (trace_state->add_name) { 2089 for (i = 0; i < event->n_fields; i++) { 2090 field = event->fields[i]; 2091 if (strcmp(field->name, field_name) == 0) 2092 break; 2093 } 2094 if (!field) { 2095 ret = -EINVAL; 2096 goto out; 2097 } 2098 } else { 2099 if (trace_state->cur_field >= event->n_fields) { 2100 ret = -EINVAL; 2101 goto out; 2102 } 2103 field = event->fields[trace_state->cur_field++]; 2104 } 2105 2106 entry = trace_state->entry; 2107 if (field->is_string) { 2108 char *str_val = (char *)(long)val; 2109 char *str_field; 2110 2111 if (!str_val) { 2112 ret = -EINVAL; 2113 goto out; 2114 } 2115 2116 str_field = (char *)&entry->fields[field->offset]; 2117 strscpy(str_field, str_val, STR_VAR_LEN_MAX); 2118 } else 2119 entry->fields[field->offset] = val; 2120 out: 2121 return ret; 2122 } 2123 2124 /** 2125 * synth_event_add_next_val - Add the next field's value to an open synth trace 2126 * @val: The value to set the next field to 2127 * @trace_state: A pointer to object tracking the piecewise trace state 2128 * 2129 * Set the value of the next field in an event that's been opened by 2130 * synth_event_trace_start(). 2131 * 2132 * The val param should be the value cast to u64. If the value points 2133 * to a string, the val param should be a char * cast to u64. 2134 * 2135 * This function assumes all the fields in an event are to be set one 2136 * after another - successive calls to this function are made, one for 2137 * each field, in the order of the fields in the event, until all 2138 * fields have been set. If you'd rather set each field individually 2139 * without regard to ordering, synth_event_add_val() can be used 2140 * instead. 2141 * 2142 * Note however that synth_event_add_next_val() and 2143 * synth_event_add_val() can't be intermixed for a given event trace - 2144 * one or the other but not both can be used at the same time. 2145 * 2146 * Note also that synth_event_trace_end() must be called after all 2147 * values have been added for each event trace, regardless of whether 2148 * adding all field values succeeded or not. 2149 * 2150 * Return: 0 on success, err otherwise. 2151 */ 2152 int synth_event_add_next_val(u64 val, 2153 struct synth_event_trace_state *trace_state) 2154 { 2155 return __synth_event_add_val(NULL, val, trace_state); 2156 } 2157 EXPORT_SYMBOL_GPL(synth_event_add_next_val); 2158 2159 /** 2160 * synth_event_add_val - Add a named field's value to an open synth trace 2161 * @field_name: The name of the synthetic event field value to set 2162 * @val: The value to set the next field to 2163 * @trace_state: A pointer to object tracking the piecewise trace state 2164 * 2165 * Set the value of the named field in an event that's been opened by 2166 * synth_event_trace_start(). 2167 * 2168 * The val param should be the value cast to u64. If the value points 2169 * to a string, the val param should be a char * cast to u64. 2170 * 2171 * This function looks up the field name, and if found, sets the field 2172 * to the specified value. This lookup makes this function more 2173 * expensive than synth_event_add_next_val(), so use that or the 2174 * none-piecewise synth_event_trace() instead if efficiency is more 2175 * important. 2176 * 2177 * Note however that synth_event_add_next_val() and 2178 * synth_event_add_val() can't be intermixed for a given event trace - 2179 * one or the other but not both can be used at the same time. 2180 * 2181 * Note also that synth_event_trace_end() must be called after all 2182 * values have been added for each event trace, regardless of whether 2183 * adding all field values succeeded or not. 2184 * 2185 * Return: 0 on success, err otherwise. 2186 */ 2187 int synth_event_add_val(const char *field_name, u64 val, 2188 struct synth_event_trace_state *trace_state) 2189 { 2190 return __synth_event_add_val(field_name, val, trace_state); 2191 } 2192 EXPORT_SYMBOL_GPL(synth_event_add_val); 2193 2194 /** 2195 * synth_event_trace_end - End piecewise synthetic event trace 2196 * @trace_state: A pointer to object tracking the piecewise trace state 2197 * 2198 * End the trace of a synthetic event opened by 2199 * synth_event_trace__start(). 2200 * 2201 * This function 'closes' an event trace, which basically means that 2202 * it commits the reserved event and cleans up other loose ends. 2203 * 2204 * A pointer to a trace_state object is passed in, which will keep 2205 * track of the current event trace state opened with 2206 * synth_event_trace_start(). 2207 * 2208 * Note that this function must be called after all values have been 2209 * added for each event trace, regardless of whether adding all field 2210 * values succeeded or not. 2211 * 2212 * Return: 0 on success, err otherwise. 2213 */ 2214 int synth_event_trace_end(struct synth_event_trace_state *trace_state) 2215 { 2216 if (!trace_state) 2217 return -EINVAL; 2218 2219 trace_event_buffer_commit(&trace_state->fbuffer); 2220 2221 ring_buffer_nest_end(trace_state->buffer); 2222 2223 return 0; 2224 } 2225 EXPORT_SYMBOL_GPL(synth_event_trace_end); 2226 2227 static int create_synth_event(int argc, const char **argv) 2228 { 2229 const char *name = argv[0]; 2230 int len; 2231 2232 if (name[0] != 's' || name[1] != ':') 2233 return -ECANCELED; 2234 name += 2; 2235 2236 /* This interface accepts group name prefix */ 2237 if (strchr(name, '/')) { 2238 len = str_has_prefix(name, SYNTH_SYSTEM "/"); 2239 if (len == 0) 2240 return -EINVAL; 2241 name += len; 2242 } 2243 return __create_synth_event(argc - 1, name, argv + 1); 2244 } 2245 2246 static int synth_event_release(struct dyn_event *ev) 2247 { 2248 struct synth_event *event = to_synth_event(ev); 2249 int ret; 2250 2251 if (event->ref) 2252 return -EBUSY; 2253 2254 ret = unregister_synth_event(event); 2255 if (ret) 2256 return ret; 2257 2258 dyn_event_remove(ev); 2259 free_synth_event(event); 2260 return 0; 2261 } 2262 2263 static int __synth_event_show(struct seq_file *m, struct synth_event *event) 2264 { 2265 struct synth_field *field; 2266 unsigned int i; 2267 2268 seq_printf(m, "%s\t", event->name); 2269 2270 for (i = 0; i < event->n_fields; i++) { 2271 field = event->fields[i]; 2272 2273 /* parameter values */ 2274 seq_printf(m, "%s %s%s", field->type, field->name, 2275 i == event->n_fields - 1 ? "" : "; "); 2276 } 2277 2278 seq_putc(m, '\n'); 2279 2280 return 0; 2281 } 2282 2283 static int synth_event_show(struct seq_file *m, struct dyn_event *ev) 2284 { 2285 struct synth_event *event = to_synth_event(ev); 2286 2287 seq_printf(m, "s:%s/", event->class.system); 2288 2289 return __synth_event_show(m, event); 2290 } 2291 2292 static int synth_events_seq_show(struct seq_file *m, void *v) 2293 { 2294 struct dyn_event *ev = v; 2295 2296 if (!is_synth_event(ev)) 2297 return 0; 2298 2299 return __synth_event_show(m, to_synth_event(ev)); 2300 } 2301 2302 static const struct seq_operations synth_events_seq_op = { 2303 .start = dyn_event_seq_start, 2304 .next = dyn_event_seq_next, 2305 .stop = dyn_event_seq_stop, 2306 .show = synth_events_seq_show, 2307 }; 2308 2309 static int synth_events_open(struct inode *inode, struct file *file) 2310 { 2311 int ret; 2312 2313 ret = security_locked_down(LOCKDOWN_TRACEFS); 2314 if (ret) 2315 return ret; 2316 2317 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { 2318 ret = dyn_events_release_all(&synth_event_ops); 2319 if (ret < 0) 2320 return ret; 2321 } 2322 2323 return seq_open(file, &synth_events_seq_op); 2324 } 2325 2326 static ssize_t synth_events_write(struct file *file, 2327 const char __user *buffer, 2328 size_t count, loff_t *ppos) 2329 { 2330 return trace_parse_run_command(file, buffer, count, ppos, 2331 create_or_delete_synth_event); 2332 } 2333 2334 static const struct file_operations synth_events_fops = { 2335 .open = synth_events_open, 2336 .write = synth_events_write, 2337 .read = seq_read, 2338 .llseek = seq_lseek, 2339 .release = seq_release, 2340 }; 2341 2342 static u64 hist_field_timestamp(struct hist_field *hist_field, 2343 struct tracing_map_elt *elt, 2344 struct ring_buffer_event *rbe, 2345 void *event) 2346 { 2347 struct hist_trigger_data *hist_data = hist_field->hist_data; 2348 struct trace_array *tr = hist_data->event_file->tr; 2349 2350 u64 ts = ring_buffer_event_time_stamp(rbe); 2351 2352 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr)) 2353 ts = ns2usecs(ts); 2354 2355 return ts; 2356 } 2357 2358 static u64 hist_field_cpu(struct hist_field *hist_field, 2359 struct tracing_map_elt *elt, 2360 struct ring_buffer_event *rbe, 2361 void *event) 2362 { 2363 int cpu = smp_processor_id(); 2364 2365 return cpu; 2366 } 2367 2368 /** 2369 * check_field_for_var_ref - Check if a VAR_REF field references a variable 2370 * @hist_field: The VAR_REF field to check 2371 * @var_data: The hist trigger that owns the variable 2372 * @var_idx: The trigger variable identifier 2373 * 2374 * Check the given VAR_REF field to see whether or not it references 2375 * the given variable associated with the given trigger. 2376 * 2377 * Return: The VAR_REF field if it does reference the variable, NULL if not 2378 */ 2379 static struct hist_field * 2380 check_field_for_var_ref(struct hist_field *hist_field, 2381 struct hist_trigger_data *var_data, 2382 unsigned int var_idx) 2383 { 2384 WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF)); 2385 2386 if (hist_field && hist_field->var.idx == var_idx && 2387 hist_field->var.hist_data == var_data) 2388 return hist_field; 2389 2390 return NULL; 2391 } 2392 2393 /** 2394 * find_var_ref - Check if a trigger has a reference to a trigger variable 2395 * @hist_data: The hist trigger that might have a reference to the variable 2396 * @var_data: The hist trigger that owns the variable 2397 * @var_idx: The trigger variable identifier 2398 * 2399 * Check the list of var_refs[] on the first hist trigger to see 2400 * whether any of them are references to the variable on the second 2401 * trigger. 2402 * 2403 * Return: The VAR_REF field referencing the variable if so, NULL if not 2404 */ 2405 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data, 2406 struct hist_trigger_data *var_data, 2407 unsigned int var_idx) 2408 { 2409 struct hist_field *hist_field; 2410 unsigned int i; 2411 2412 for (i = 0; i < hist_data->n_var_refs; i++) { 2413 hist_field = hist_data->var_refs[i]; 2414 if (check_field_for_var_ref(hist_field, var_data, var_idx)) 2415 return hist_field; 2416 } 2417 2418 return NULL; 2419 } 2420 2421 /** 2422 * find_any_var_ref - Check if there is a reference to a given trigger variable 2423 * @hist_data: The hist trigger 2424 * @var_idx: The trigger variable identifier 2425 * 2426 * Check to see whether the given variable is currently referenced by 2427 * any other trigger. 2428 * 2429 * The trigger the variable is defined on is explicitly excluded - the 2430 * assumption being that a self-reference doesn't prevent a trigger 2431 * from being removed. 2432 * 2433 * Return: The VAR_REF field referencing the variable if so, NULL if not 2434 */ 2435 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data, 2436 unsigned int var_idx) 2437 { 2438 struct trace_array *tr = hist_data->event_file->tr; 2439 struct hist_field *found = NULL; 2440 struct hist_var_data *var_data; 2441 2442 list_for_each_entry(var_data, &tr->hist_vars, list) { 2443 if (var_data->hist_data == hist_data) 2444 continue; 2445 found = find_var_ref(var_data->hist_data, hist_data, var_idx); 2446 if (found) 2447 break; 2448 } 2449 2450 return found; 2451 } 2452 2453 /** 2454 * check_var_refs - Check if there is a reference to any of trigger's variables 2455 * @hist_data: The hist trigger 2456 * 2457 * A trigger can define one or more variables. If any one of them is 2458 * currently referenced by any other trigger, this function will 2459 * determine that. 2460 2461 * Typically used to determine whether or not a trigger can be removed 2462 * - if there are any references to a trigger's variables, it cannot. 2463 * 2464 * Return: True if there is a reference to any of trigger's variables 2465 */ 2466 static bool check_var_refs(struct hist_trigger_data *hist_data) 2467 { 2468 struct hist_field *field; 2469 bool found = false; 2470 int i; 2471 2472 for_each_hist_field(i, hist_data) { 2473 field = hist_data->fields[i]; 2474 if (field && field->flags & HIST_FIELD_FL_VAR) { 2475 if (find_any_var_ref(hist_data, field->var.idx)) { 2476 found = true; 2477 break; 2478 } 2479 } 2480 } 2481 2482 return found; 2483 } 2484 2485 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data) 2486 { 2487 struct trace_array *tr = hist_data->event_file->tr; 2488 struct hist_var_data *var_data, *found = NULL; 2489 2490 list_for_each_entry(var_data, &tr->hist_vars, list) { 2491 if (var_data->hist_data == hist_data) { 2492 found = var_data; 2493 break; 2494 } 2495 } 2496 2497 return found; 2498 } 2499 2500 static bool field_has_hist_vars(struct hist_field *hist_field, 2501 unsigned int level) 2502 { 2503 int i; 2504 2505 if (level > 3) 2506 return false; 2507 2508 if (!hist_field) 2509 return false; 2510 2511 if (hist_field->flags & HIST_FIELD_FL_VAR || 2512 hist_field->flags & HIST_FIELD_FL_VAR_REF) 2513 return true; 2514 2515 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) { 2516 struct hist_field *operand; 2517 2518 operand = hist_field->operands[i]; 2519 if (field_has_hist_vars(operand, level + 1)) 2520 return true; 2521 } 2522 2523 return false; 2524 } 2525 2526 static bool has_hist_vars(struct hist_trigger_data *hist_data) 2527 { 2528 struct hist_field *hist_field; 2529 int i; 2530 2531 for_each_hist_field(i, hist_data) { 2532 hist_field = hist_data->fields[i]; 2533 if (field_has_hist_vars(hist_field, 0)) 2534 return true; 2535 } 2536 2537 return false; 2538 } 2539 2540 static int save_hist_vars(struct hist_trigger_data *hist_data) 2541 { 2542 struct trace_array *tr = hist_data->event_file->tr; 2543 struct hist_var_data *var_data; 2544 2545 var_data = find_hist_vars(hist_data); 2546 if (var_data) 2547 return 0; 2548 2549 if (tracing_check_open_get_tr(tr)) 2550 return -ENODEV; 2551 2552 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL); 2553 if (!var_data) { 2554 trace_array_put(tr); 2555 return -ENOMEM; 2556 } 2557 2558 var_data->hist_data = hist_data; 2559 list_add(&var_data->list, &tr->hist_vars); 2560 2561 return 0; 2562 } 2563 2564 static void remove_hist_vars(struct hist_trigger_data *hist_data) 2565 { 2566 struct trace_array *tr = hist_data->event_file->tr; 2567 struct hist_var_data *var_data; 2568 2569 var_data = find_hist_vars(hist_data); 2570 if (!var_data) 2571 return; 2572 2573 if (WARN_ON(check_var_refs(hist_data))) 2574 return; 2575 2576 list_del(&var_data->list); 2577 2578 kfree(var_data); 2579 2580 trace_array_put(tr); 2581 } 2582 2583 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data, 2584 const char *var_name) 2585 { 2586 struct hist_field *hist_field, *found = NULL; 2587 int i; 2588 2589 for_each_hist_field(i, hist_data) { 2590 hist_field = hist_data->fields[i]; 2591 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR && 2592 strcmp(hist_field->var.name, var_name) == 0) { 2593 found = hist_field; 2594 break; 2595 } 2596 } 2597 2598 return found; 2599 } 2600 2601 static struct hist_field *find_var(struct hist_trigger_data *hist_data, 2602 struct trace_event_file *file, 2603 const char *var_name) 2604 { 2605 struct hist_trigger_data *test_data; 2606 struct event_trigger_data *test; 2607 struct hist_field *hist_field; 2608 2609 lockdep_assert_held(&event_mutex); 2610 2611 hist_field = find_var_field(hist_data, var_name); 2612 if (hist_field) 2613 return hist_field; 2614 2615 list_for_each_entry(test, &file->triggers, list) { 2616 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 2617 test_data = test->private_data; 2618 hist_field = find_var_field(test_data, var_name); 2619 if (hist_field) 2620 return hist_field; 2621 } 2622 } 2623 2624 return NULL; 2625 } 2626 2627 static struct trace_event_file *find_var_file(struct trace_array *tr, 2628 char *system, 2629 char *event_name, 2630 char *var_name) 2631 { 2632 struct hist_trigger_data *var_hist_data; 2633 struct hist_var_data *var_data; 2634 struct trace_event_file *file, *found = NULL; 2635 2636 if (system) 2637 return find_event_file(tr, system, event_name); 2638 2639 list_for_each_entry(var_data, &tr->hist_vars, list) { 2640 var_hist_data = var_data->hist_data; 2641 file = var_hist_data->event_file; 2642 if (file == found) 2643 continue; 2644 2645 if (find_var_field(var_hist_data, var_name)) { 2646 if (found) { 2647 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name)); 2648 return NULL; 2649 } 2650 2651 found = file; 2652 } 2653 } 2654 2655 return found; 2656 } 2657 2658 static struct hist_field *find_file_var(struct trace_event_file *file, 2659 const char *var_name) 2660 { 2661 struct hist_trigger_data *test_data; 2662 struct event_trigger_data *test; 2663 struct hist_field *hist_field; 2664 2665 lockdep_assert_held(&event_mutex); 2666 2667 list_for_each_entry(test, &file->triggers, list) { 2668 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 2669 test_data = test->private_data; 2670 hist_field = find_var_field(test_data, var_name); 2671 if (hist_field) 2672 return hist_field; 2673 } 2674 } 2675 2676 return NULL; 2677 } 2678 2679 static struct hist_field * 2680 find_match_var(struct hist_trigger_data *hist_data, char *var_name) 2681 { 2682 struct trace_array *tr = hist_data->event_file->tr; 2683 struct hist_field *hist_field, *found = NULL; 2684 struct trace_event_file *file; 2685 unsigned int i; 2686 2687 for (i = 0; i < hist_data->n_actions; i++) { 2688 struct action_data *data = hist_data->actions[i]; 2689 2690 if (data->handler == HANDLER_ONMATCH) { 2691 char *system = data->match_data.event_system; 2692 char *event_name = data->match_data.event; 2693 2694 file = find_var_file(tr, system, event_name, var_name); 2695 if (!file) 2696 continue; 2697 hist_field = find_file_var(file, var_name); 2698 if (hist_field) { 2699 if (found) { 2700 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, 2701 errpos(var_name)); 2702 return ERR_PTR(-EINVAL); 2703 } 2704 2705 found = hist_field; 2706 } 2707 } 2708 } 2709 return found; 2710 } 2711 2712 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data, 2713 char *system, 2714 char *event_name, 2715 char *var_name) 2716 { 2717 struct trace_array *tr = hist_data->event_file->tr; 2718 struct hist_field *hist_field = NULL; 2719 struct trace_event_file *file; 2720 2721 if (!system || !event_name) { 2722 hist_field = find_match_var(hist_data, var_name); 2723 if (IS_ERR(hist_field)) 2724 return NULL; 2725 if (hist_field) 2726 return hist_field; 2727 } 2728 2729 file = find_var_file(tr, system, event_name, var_name); 2730 if (!file) 2731 return NULL; 2732 2733 hist_field = find_file_var(file, var_name); 2734 2735 return hist_field; 2736 } 2737 2738 static u64 hist_field_var_ref(struct hist_field *hist_field, 2739 struct tracing_map_elt *elt, 2740 struct ring_buffer_event *rbe, 2741 void *event) 2742 { 2743 struct hist_elt_data *elt_data; 2744 u64 var_val = 0; 2745 2746 if (WARN_ON_ONCE(!elt)) 2747 return var_val; 2748 2749 elt_data = elt->private_data; 2750 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx]; 2751 2752 return var_val; 2753 } 2754 2755 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key, 2756 u64 *var_ref_vals, bool self) 2757 { 2758 struct hist_trigger_data *var_data; 2759 struct tracing_map_elt *var_elt; 2760 struct hist_field *hist_field; 2761 unsigned int i, var_idx; 2762 bool resolved = true; 2763 u64 var_val = 0; 2764 2765 for (i = 0; i < hist_data->n_var_refs; i++) { 2766 hist_field = hist_data->var_refs[i]; 2767 var_idx = hist_field->var.idx; 2768 var_data = hist_field->var.hist_data; 2769 2770 if (var_data == NULL) { 2771 resolved = false; 2772 break; 2773 } 2774 2775 if ((self && var_data != hist_data) || 2776 (!self && var_data == hist_data)) 2777 continue; 2778 2779 var_elt = tracing_map_lookup(var_data->map, key); 2780 if (!var_elt) { 2781 resolved = false; 2782 break; 2783 } 2784 2785 if (!tracing_map_var_set(var_elt, var_idx)) { 2786 resolved = false; 2787 break; 2788 } 2789 2790 if (self || !hist_field->read_once) 2791 var_val = tracing_map_read_var(var_elt, var_idx); 2792 else 2793 var_val = tracing_map_read_var_once(var_elt, var_idx); 2794 2795 var_ref_vals[i] = var_val; 2796 } 2797 2798 return resolved; 2799 } 2800 2801 static const char *hist_field_name(struct hist_field *field, 2802 unsigned int level) 2803 { 2804 const char *field_name = ""; 2805 2806 if (level > 1) 2807 return field_name; 2808 2809 if (field->field) 2810 field_name = field->field->name; 2811 else if (field->flags & HIST_FIELD_FL_LOG2 || 2812 field->flags & HIST_FIELD_FL_ALIAS) 2813 field_name = hist_field_name(field->operands[0], ++level); 2814 else if (field->flags & HIST_FIELD_FL_CPU) 2815 field_name = "cpu"; 2816 else if (field->flags & HIST_FIELD_FL_EXPR || 2817 field->flags & HIST_FIELD_FL_VAR_REF) { 2818 if (field->system) { 2819 static char full_name[MAX_FILTER_STR_VAL]; 2820 2821 strcat(full_name, field->system); 2822 strcat(full_name, "."); 2823 strcat(full_name, field->event_name); 2824 strcat(full_name, "."); 2825 strcat(full_name, field->name); 2826 field_name = full_name; 2827 } else 2828 field_name = field->name; 2829 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP) 2830 field_name = "common_timestamp"; 2831 2832 if (field_name == NULL) 2833 field_name = ""; 2834 2835 return field_name; 2836 } 2837 2838 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed) 2839 { 2840 hist_field_fn_t fn = NULL; 2841 2842 switch (field_size) { 2843 case 8: 2844 if (field_is_signed) 2845 fn = hist_field_s64; 2846 else 2847 fn = hist_field_u64; 2848 break; 2849 case 4: 2850 if (field_is_signed) 2851 fn = hist_field_s32; 2852 else 2853 fn = hist_field_u32; 2854 break; 2855 case 2: 2856 if (field_is_signed) 2857 fn = hist_field_s16; 2858 else 2859 fn = hist_field_u16; 2860 break; 2861 case 1: 2862 if (field_is_signed) 2863 fn = hist_field_s8; 2864 else 2865 fn = hist_field_u8; 2866 break; 2867 } 2868 2869 return fn; 2870 } 2871 2872 static int parse_map_size(char *str) 2873 { 2874 unsigned long size, map_bits; 2875 int ret; 2876 2877 ret = kstrtoul(str, 0, &size); 2878 if (ret) 2879 goto out; 2880 2881 map_bits = ilog2(roundup_pow_of_two(size)); 2882 if (map_bits < TRACING_MAP_BITS_MIN || 2883 map_bits > TRACING_MAP_BITS_MAX) 2884 ret = -EINVAL; 2885 else 2886 ret = map_bits; 2887 out: 2888 return ret; 2889 } 2890 2891 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs) 2892 { 2893 unsigned int i; 2894 2895 if (!attrs) 2896 return; 2897 2898 for (i = 0; i < attrs->n_assignments; i++) 2899 kfree(attrs->assignment_str[i]); 2900 2901 for (i = 0; i < attrs->n_actions; i++) 2902 kfree(attrs->action_str[i]); 2903 2904 kfree(attrs->name); 2905 kfree(attrs->sort_key_str); 2906 kfree(attrs->keys_str); 2907 kfree(attrs->vals_str); 2908 kfree(attrs->clock); 2909 kfree(attrs); 2910 } 2911 2912 static int parse_action(char *str, struct hist_trigger_attrs *attrs) 2913 { 2914 int ret = -EINVAL; 2915 2916 if (attrs->n_actions >= HIST_ACTIONS_MAX) 2917 return ret; 2918 2919 if ((str_has_prefix(str, "onmatch(")) || 2920 (str_has_prefix(str, "onmax(")) || 2921 (str_has_prefix(str, "onchange("))) { 2922 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL); 2923 if (!attrs->action_str[attrs->n_actions]) { 2924 ret = -ENOMEM; 2925 return ret; 2926 } 2927 attrs->n_actions++; 2928 ret = 0; 2929 } 2930 return ret; 2931 } 2932 2933 static int parse_assignment(struct trace_array *tr, 2934 char *str, struct hist_trigger_attrs *attrs) 2935 { 2936 int len, ret = 0; 2937 2938 if ((len = str_has_prefix(str, "key=")) || 2939 (len = str_has_prefix(str, "keys="))) { 2940 attrs->keys_str = kstrdup(str + len, GFP_KERNEL); 2941 if (!attrs->keys_str) { 2942 ret = -ENOMEM; 2943 goto out; 2944 } 2945 } else if ((len = str_has_prefix(str, "val=")) || 2946 (len = str_has_prefix(str, "vals=")) || 2947 (len = str_has_prefix(str, "values="))) { 2948 attrs->vals_str = kstrdup(str + len, GFP_KERNEL); 2949 if (!attrs->vals_str) { 2950 ret = -ENOMEM; 2951 goto out; 2952 } 2953 } else if ((len = str_has_prefix(str, "sort="))) { 2954 attrs->sort_key_str = kstrdup(str + len, GFP_KERNEL); 2955 if (!attrs->sort_key_str) { 2956 ret = -ENOMEM; 2957 goto out; 2958 } 2959 } else if (str_has_prefix(str, "name=")) { 2960 attrs->name = kstrdup(str, GFP_KERNEL); 2961 if (!attrs->name) { 2962 ret = -ENOMEM; 2963 goto out; 2964 } 2965 } else if ((len = str_has_prefix(str, "clock="))) { 2966 str += len; 2967 2968 str = strstrip(str); 2969 attrs->clock = kstrdup(str, GFP_KERNEL); 2970 if (!attrs->clock) { 2971 ret = -ENOMEM; 2972 goto out; 2973 } 2974 } else if ((len = str_has_prefix(str, "size="))) { 2975 int map_bits = parse_map_size(str + len); 2976 2977 if (map_bits < 0) { 2978 ret = map_bits; 2979 goto out; 2980 } 2981 attrs->map_bits = map_bits; 2982 } else { 2983 char *assignment; 2984 2985 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) { 2986 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str)); 2987 ret = -EINVAL; 2988 goto out; 2989 } 2990 2991 assignment = kstrdup(str, GFP_KERNEL); 2992 if (!assignment) { 2993 ret = -ENOMEM; 2994 goto out; 2995 } 2996 2997 attrs->assignment_str[attrs->n_assignments++] = assignment; 2998 } 2999 out: 3000 return ret; 3001 } 3002 3003 static struct hist_trigger_attrs * 3004 parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str) 3005 { 3006 struct hist_trigger_attrs *attrs; 3007 int ret = 0; 3008 3009 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 3010 if (!attrs) 3011 return ERR_PTR(-ENOMEM); 3012 3013 while (trigger_str) { 3014 char *str = strsep(&trigger_str, ":"); 3015 char *rhs; 3016 3017 rhs = strchr(str, '='); 3018 if (rhs) { 3019 if (!strlen(++rhs)) { 3020 ret = -EINVAL; 3021 hist_err(tr, HIST_ERR_EMPTY_ASSIGNMENT, errpos(str)); 3022 goto free; 3023 } 3024 ret = parse_assignment(tr, str, attrs); 3025 if (ret) 3026 goto free; 3027 } else if (strcmp(str, "pause") == 0) 3028 attrs->pause = true; 3029 else if ((strcmp(str, "cont") == 0) || 3030 (strcmp(str, "continue") == 0)) 3031 attrs->cont = true; 3032 else if (strcmp(str, "clear") == 0) 3033 attrs->clear = true; 3034 else { 3035 ret = parse_action(str, attrs); 3036 if (ret) 3037 goto free; 3038 } 3039 } 3040 3041 if (!attrs->keys_str) { 3042 ret = -EINVAL; 3043 goto free; 3044 } 3045 3046 if (!attrs->clock) { 3047 attrs->clock = kstrdup("global", GFP_KERNEL); 3048 if (!attrs->clock) { 3049 ret = -ENOMEM; 3050 goto free; 3051 } 3052 } 3053 3054 return attrs; 3055 free: 3056 destroy_hist_trigger_attrs(attrs); 3057 3058 return ERR_PTR(ret); 3059 } 3060 3061 static inline void save_comm(char *comm, struct task_struct *task) 3062 { 3063 if (!task->pid) { 3064 strcpy(comm, "<idle>"); 3065 return; 3066 } 3067 3068 if (WARN_ON_ONCE(task->pid < 0)) { 3069 strcpy(comm, "<XXX>"); 3070 return; 3071 } 3072 3073 strncpy(comm, task->comm, TASK_COMM_LEN); 3074 } 3075 3076 static void hist_elt_data_free(struct hist_elt_data *elt_data) 3077 { 3078 unsigned int i; 3079 3080 for (i = 0; i < SYNTH_FIELDS_MAX; i++) 3081 kfree(elt_data->field_var_str[i]); 3082 3083 kfree(elt_data->comm); 3084 kfree(elt_data); 3085 } 3086 3087 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt) 3088 { 3089 struct hist_elt_data *elt_data = elt->private_data; 3090 3091 hist_elt_data_free(elt_data); 3092 } 3093 3094 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt) 3095 { 3096 struct hist_trigger_data *hist_data = elt->map->private_data; 3097 unsigned int size = TASK_COMM_LEN; 3098 struct hist_elt_data *elt_data; 3099 struct hist_field *key_field; 3100 unsigned int i, n_str; 3101 3102 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL); 3103 if (!elt_data) 3104 return -ENOMEM; 3105 3106 for_each_hist_key_field(i, hist_data) { 3107 key_field = hist_data->fields[i]; 3108 3109 if (key_field->flags & HIST_FIELD_FL_EXECNAME) { 3110 elt_data->comm = kzalloc(size, GFP_KERNEL); 3111 if (!elt_data->comm) { 3112 kfree(elt_data); 3113 return -ENOMEM; 3114 } 3115 break; 3116 } 3117 } 3118 3119 n_str = hist_data->n_field_var_str + hist_data->n_save_var_str; 3120 3121 size = STR_VAR_LEN_MAX; 3122 3123 for (i = 0; i < n_str; i++) { 3124 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL); 3125 if (!elt_data->field_var_str[i]) { 3126 hist_elt_data_free(elt_data); 3127 return -ENOMEM; 3128 } 3129 } 3130 3131 elt->private_data = elt_data; 3132 3133 return 0; 3134 } 3135 3136 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt) 3137 { 3138 struct hist_elt_data *elt_data = elt->private_data; 3139 3140 if (elt_data->comm) 3141 save_comm(elt_data->comm, current); 3142 } 3143 3144 static const struct tracing_map_ops hist_trigger_elt_data_ops = { 3145 .elt_alloc = hist_trigger_elt_data_alloc, 3146 .elt_free = hist_trigger_elt_data_free, 3147 .elt_init = hist_trigger_elt_data_init, 3148 }; 3149 3150 static const char *get_hist_field_flags(struct hist_field *hist_field) 3151 { 3152 const char *flags_str = NULL; 3153 3154 if (hist_field->flags & HIST_FIELD_FL_HEX) 3155 flags_str = "hex"; 3156 else if (hist_field->flags & HIST_FIELD_FL_SYM) 3157 flags_str = "sym"; 3158 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET) 3159 flags_str = "sym-offset"; 3160 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME) 3161 flags_str = "execname"; 3162 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL) 3163 flags_str = "syscall"; 3164 else if (hist_field->flags & HIST_FIELD_FL_LOG2) 3165 flags_str = "log2"; 3166 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS) 3167 flags_str = "usecs"; 3168 3169 return flags_str; 3170 } 3171 3172 static void expr_field_str(struct hist_field *field, char *expr) 3173 { 3174 if (field->flags & HIST_FIELD_FL_VAR_REF) 3175 strcat(expr, "$"); 3176 3177 strcat(expr, hist_field_name(field, 0)); 3178 3179 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) { 3180 const char *flags_str = get_hist_field_flags(field); 3181 3182 if (flags_str) { 3183 strcat(expr, "."); 3184 strcat(expr, flags_str); 3185 } 3186 } 3187 } 3188 3189 static char *expr_str(struct hist_field *field, unsigned int level) 3190 { 3191 char *expr; 3192 3193 if (level > 1) 3194 return NULL; 3195 3196 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL); 3197 if (!expr) 3198 return NULL; 3199 3200 if (!field->operands[0]) { 3201 expr_field_str(field, expr); 3202 return expr; 3203 } 3204 3205 if (field->operator == FIELD_OP_UNARY_MINUS) { 3206 char *subexpr; 3207 3208 strcat(expr, "-("); 3209 subexpr = expr_str(field->operands[0], ++level); 3210 if (!subexpr) { 3211 kfree(expr); 3212 return NULL; 3213 } 3214 strcat(expr, subexpr); 3215 strcat(expr, ")"); 3216 3217 kfree(subexpr); 3218 3219 return expr; 3220 } 3221 3222 expr_field_str(field->operands[0], expr); 3223 3224 switch (field->operator) { 3225 case FIELD_OP_MINUS: 3226 strcat(expr, "-"); 3227 break; 3228 case FIELD_OP_PLUS: 3229 strcat(expr, "+"); 3230 break; 3231 default: 3232 kfree(expr); 3233 return NULL; 3234 } 3235 3236 expr_field_str(field->operands[1], expr); 3237 3238 return expr; 3239 } 3240 3241 static int contains_operator(char *str) 3242 { 3243 enum field_op_id field_op = FIELD_OP_NONE; 3244 char *op; 3245 3246 op = strpbrk(str, "+-"); 3247 if (!op) 3248 return FIELD_OP_NONE; 3249 3250 switch (*op) { 3251 case '-': 3252 if (*str == '-') 3253 field_op = FIELD_OP_UNARY_MINUS; 3254 else 3255 field_op = FIELD_OP_MINUS; 3256 break; 3257 case '+': 3258 field_op = FIELD_OP_PLUS; 3259 break; 3260 default: 3261 break; 3262 } 3263 3264 return field_op; 3265 } 3266 3267 static void __destroy_hist_field(struct hist_field *hist_field) 3268 { 3269 kfree(hist_field->var.name); 3270 kfree(hist_field->name); 3271 kfree(hist_field->type); 3272 3273 kfree(hist_field); 3274 } 3275 3276 static void destroy_hist_field(struct hist_field *hist_field, 3277 unsigned int level) 3278 { 3279 unsigned int i; 3280 3281 if (level > 3) 3282 return; 3283 3284 if (!hist_field) 3285 return; 3286 3287 if (hist_field->flags & HIST_FIELD_FL_VAR_REF) 3288 return; /* var refs will be destroyed separately */ 3289 3290 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) 3291 destroy_hist_field(hist_field->operands[i], level + 1); 3292 3293 __destroy_hist_field(hist_field); 3294 } 3295 3296 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data, 3297 struct ftrace_event_field *field, 3298 unsigned long flags, 3299 char *var_name) 3300 { 3301 struct hist_field *hist_field; 3302 3303 if (field && is_function_field(field)) 3304 return NULL; 3305 3306 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL); 3307 if (!hist_field) 3308 return NULL; 3309 3310 hist_field->hist_data = hist_data; 3311 3312 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS) 3313 goto out; /* caller will populate */ 3314 3315 if (flags & HIST_FIELD_FL_VAR_REF) { 3316 hist_field->fn = hist_field_var_ref; 3317 goto out; 3318 } 3319 3320 if (flags & HIST_FIELD_FL_HITCOUNT) { 3321 hist_field->fn = hist_field_counter; 3322 hist_field->size = sizeof(u64); 3323 hist_field->type = kstrdup("u64", GFP_KERNEL); 3324 if (!hist_field->type) 3325 goto free; 3326 goto out; 3327 } 3328 3329 if (flags & HIST_FIELD_FL_STACKTRACE) { 3330 hist_field->fn = hist_field_none; 3331 goto out; 3332 } 3333 3334 if (flags & HIST_FIELD_FL_LOG2) { 3335 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2; 3336 hist_field->fn = hist_field_log2; 3337 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL); 3338 hist_field->size = hist_field->operands[0]->size; 3339 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL); 3340 if (!hist_field->type) 3341 goto free; 3342 goto out; 3343 } 3344 3345 if (flags & HIST_FIELD_FL_TIMESTAMP) { 3346 hist_field->fn = hist_field_timestamp; 3347 hist_field->size = sizeof(u64); 3348 hist_field->type = kstrdup("u64", GFP_KERNEL); 3349 if (!hist_field->type) 3350 goto free; 3351 goto out; 3352 } 3353 3354 if (flags & HIST_FIELD_FL_CPU) { 3355 hist_field->fn = hist_field_cpu; 3356 hist_field->size = sizeof(int); 3357 hist_field->type = kstrdup("unsigned int", GFP_KERNEL); 3358 if (!hist_field->type) 3359 goto free; 3360 goto out; 3361 } 3362 3363 if (WARN_ON_ONCE(!field)) 3364 goto out; 3365 3366 if (is_string_field(field)) { 3367 flags |= HIST_FIELD_FL_STRING; 3368 3369 hist_field->size = MAX_FILTER_STR_VAL; 3370 hist_field->type = kstrdup(field->type, GFP_KERNEL); 3371 if (!hist_field->type) 3372 goto free; 3373 3374 if (field->filter_type == FILTER_STATIC_STRING) 3375 hist_field->fn = hist_field_string; 3376 else if (field->filter_type == FILTER_DYN_STRING) 3377 hist_field->fn = hist_field_dynstring; 3378 else 3379 hist_field->fn = hist_field_pstring; 3380 } else { 3381 hist_field->size = field->size; 3382 hist_field->is_signed = field->is_signed; 3383 hist_field->type = kstrdup(field->type, GFP_KERNEL); 3384 if (!hist_field->type) 3385 goto free; 3386 3387 hist_field->fn = select_value_fn(field->size, 3388 field->is_signed); 3389 if (!hist_field->fn) { 3390 destroy_hist_field(hist_field, 0); 3391 return NULL; 3392 } 3393 } 3394 out: 3395 hist_field->field = field; 3396 hist_field->flags = flags; 3397 3398 if (var_name) { 3399 hist_field->var.name = kstrdup(var_name, GFP_KERNEL); 3400 if (!hist_field->var.name) 3401 goto free; 3402 } 3403 3404 return hist_field; 3405 free: 3406 destroy_hist_field(hist_field, 0); 3407 return NULL; 3408 } 3409 3410 static void destroy_hist_fields(struct hist_trigger_data *hist_data) 3411 { 3412 unsigned int i; 3413 3414 for (i = 0; i < HIST_FIELDS_MAX; i++) { 3415 if (hist_data->fields[i]) { 3416 destroy_hist_field(hist_data->fields[i], 0); 3417 hist_data->fields[i] = NULL; 3418 } 3419 } 3420 3421 for (i = 0; i < hist_data->n_var_refs; i++) { 3422 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF)); 3423 __destroy_hist_field(hist_data->var_refs[i]); 3424 hist_data->var_refs[i] = NULL; 3425 } 3426 } 3427 3428 static int init_var_ref(struct hist_field *ref_field, 3429 struct hist_field *var_field, 3430 char *system, char *event_name) 3431 { 3432 int err = 0; 3433 3434 ref_field->var.idx = var_field->var.idx; 3435 ref_field->var.hist_data = var_field->hist_data; 3436 ref_field->size = var_field->size; 3437 ref_field->is_signed = var_field->is_signed; 3438 ref_field->flags |= var_field->flags & 3439 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS); 3440 3441 if (system) { 3442 ref_field->system = kstrdup(system, GFP_KERNEL); 3443 if (!ref_field->system) 3444 return -ENOMEM; 3445 } 3446 3447 if (event_name) { 3448 ref_field->event_name = kstrdup(event_name, GFP_KERNEL); 3449 if (!ref_field->event_name) { 3450 err = -ENOMEM; 3451 goto free; 3452 } 3453 } 3454 3455 if (var_field->var.name) { 3456 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL); 3457 if (!ref_field->name) { 3458 err = -ENOMEM; 3459 goto free; 3460 } 3461 } else if (var_field->name) { 3462 ref_field->name = kstrdup(var_field->name, GFP_KERNEL); 3463 if (!ref_field->name) { 3464 err = -ENOMEM; 3465 goto free; 3466 } 3467 } 3468 3469 ref_field->type = kstrdup(var_field->type, GFP_KERNEL); 3470 if (!ref_field->type) { 3471 err = -ENOMEM; 3472 goto free; 3473 } 3474 out: 3475 return err; 3476 free: 3477 kfree(ref_field->system); 3478 kfree(ref_field->event_name); 3479 kfree(ref_field->name); 3480 3481 goto out; 3482 } 3483 3484 static int find_var_ref_idx(struct hist_trigger_data *hist_data, 3485 struct hist_field *var_field) 3486 { 3487 struct hist_field *ref_field; 3488 int i; 3489 3490 for (i = 0; i < hist_data->n_var_refs; i++) { 3491 ref_field = hist_data->var_refs[i]; 3492 if (ref_field->var.idx == var_field->var.idx && 3493 ref_field->var.hist_data == var_field->hist_data) 3494 return i; 3495 } 3496 3497 return -ENOENT; 3498 } 3499 3500 /** 3501 * create_var_ref - Create a variable reference and attach it to trigger 3502 * @hist_data: The trigger that will be referencing the variable 3503 * @var_field: The VAR field to create a reference to 3504 * @system: The optional system string 3505 * @event_name: The optional event_name string 3506 * 3507 * Given a variable hist_field, create a VAR_REF hist_field that 3508 * represents a reference to it. 3509 * 3510 * This function also adds the reference to the trigger that 3511 * now references the variable. 3512 * 3513 * Return: The VAR_REF field if successful, NULL if not 3514 */ 3515 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data, 3516 struct hist_field *var_field, 3517 char *system, char *event_name) 3518 { 3519 unsigned long flags = HIST_FIELD_FL_VAR_REF; 3520 struct hist_field *ref_field; 3521 3522 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL); 3523 if (ref_field) { 3524 if (init_var_ref(ref_field, var_field, system, event_name)) { 3525 destroy_hist_field(ref_field, 0); 3526 return NULL; 3527 } 3528 3529 hist_data->var_refs[hist_data->n_var_refs] = ref_field; 3530 ref_field->var_ref_idx = hist_data->n_var_refs++; 3531 } 3532 3533 return ref_field; 3534 } 3535 3536 static bool is_var_ref(char *var_name) 3537 { 3538 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$') 3539 return false; 3540 3541 return true; 3542 } 3543 3544 static char *field_name_from_var(struct hist_trigger_data *hist_data, 3545 char *var_name) 3546 { 3547 char *name, *field; 3548 unsigned int i; 3549 3550 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) { 3551 name = hist_data->attrs->var_defs.name[i]; 3552 3553 if (strcmp(var_name, name) == 0) { 3554 field = hist_data->attrs->var_defs.expr[i]; 3555 if (contains_operator(field) || is_var_ref(field)) 3556 continue; 3557 return field; 3558 } 3559 } 3560 3561 return NULL; 3562 } 3563 3564 static char *local_field_var_ref(struct hist_trigger_data *hist_data, 3565 char *system, char *event_name, 3566 char *var_name) 3567 { 3568 struct trace_event_call *call; 3569 3570 if (system && event_name) { 3571 call = hist_data->event_file->event_call; 3572 3573 if (strcmp(system, call->class->system) != 0) 3574 return NULL; 3575 3576 if (strcmp(event_name, trace_event_name(call)) != 0) 3577 return NULL; 3578 } 3579 3580 if (!!system != !!event_name) 3581 return NULL; 3582 3583 if (!is_var_ref(var_name)) 3584 return NULL; 3585 3586 var_name++; 3587 3588 return field_name_from_var(hist_data, var_name); 3589 } 3590 3591 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data, 3592 char *system, char *event_name, 3593 char *var_name) 3594 { 3595 struct hist_field *var_field = NULL, *ref_field = NULL; 3596 struct trace_array *tr = hist_data->event_file->tr; 3597 3598 if (!is_var_ref(var_name)) 3599 return NULL; 3600 3601 var_name++; 3602 3603 var_field = find_event_var(hist_data, system, event_name, var_name); 3604 if (var_field) 3605 ref_field = create_var_ref(hist_data, var_field, 3606 system, event_name); 3607 3608 if (!ref_field) 3609 hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name)); 3610 3611 return ref_field; 3612 } 3613 3614 static struct ftrace_event_field * 3615 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file, 3616 char *field_str, unsigned long *flags) 3617 { 3618 struct ftrace_event_field *field = NULL; 3619 char *field_name, *modifier, *str; 3620 struct trace_array *tr = file->tr; 3621 3622 modifier = str = kstrdup(field_str, GFP_KERNEL); 3623 if (!modifier) 3624 return ERR_PTR(-ENOMEM); 3625 3626 field_name = strsep(&modifier, "."); 3627 if (modifier) { 3628 if (strcmp(modifier, "hex") == 0) 3629 *flags |= HIST_FIELD_FL_HEX; 3630 else if (strcmp(modifier, "sym") == 0) 3631 *flags |= HIST_FIELD_FL_SYM; 3632 else if (strcmp(modifier, "sym-offset") == 0) 3633 *flags |= HIST_FIELD_FL_SYM_OFFSET; 3634 else if ((strcmp(modifier, "execname") == 0) && 3635 (strcmp(field_name, "common_pid") == 0)) 3636 *flags |= HIST_FIELD_FL_EXECNAME; 3637 else if (strcmp(modifier, "syscall") == 0) 3638 *flags |= HIST_FIELD_FL_SYSCALL; 3639 else if (strcmp(modifier, "log2") == 0) 3640 *flags |= HIST_FIELD_FL_LOG2; 3641 else if (strcmp(modifier, "usecs") == 0) 3642 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS; 3643 else { 3644 hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier)); 3645 field = ERR_PTR(-EINVAL); 3646 goto out; 3647 } 3648 } 3649 3650 if (strcmp(field_name, "common_timestamp") == 0) { 3651 *flags |= HIST_FIELD_FL_TIMESTAMP; 3652 hist_data->enable_timestamps = true; 3653 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS) 3654 hist_data->attrs->ts_in_usecs = true; 3655 } else if (strcmp(field_name, "cpu") == 0) 3656 *flags |= HIST_FIELD_FL_CPU; 3657 else { 3658 field = trace_find_event_field(file->event_call, field_name); 3659 if (!field || !field->size) { 3660 hist_err(tr, HIST_ERR_FIELD_NOT_FOUND, errpos(field_name)); 3661 field = ERR_PTR(-EINVAL); 3662 goto out; 3663 } 3664 } 3665 out: 3666 kfree(str); 3667 3668 return field; 3669 } 3670 3671 static struct hist_field *create_alias(struct hist_trigger_data *hist_data, 3672 struct hist_field *var_ref, 3673 char *var_name) 3674 { 3675 struct hist_field *alias = NULL; 3676 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR; 3677 3678 alias = create_hist_field(hist_data, NULL, flags, var_name); 3679 if (!alias) 3680 return NULL; 3681 3682 alias->fn = var_ref->fn; 3683 alias->operands[0] = var_ref; 3684 3685 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) { 3686 destroy_hist_field(alias, 0); 3687 return NULL; 3688 } 3689 3690 alias->var_ref_idx = var_ref->var_ref_idx; 3691 3692 return alias; 3693 } 3694 3695 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data, 3696 struct trace_event_file *file, char *str, 3697 unsigned long *flags, char *var_name) 3698 { 3699 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str; 3700 struct ftrace_event_field *field = NULL; 3701 struct hist_field *hist_field = NULL; 3702 int ret = 0; 3703 3704 s = strchr(str, '.'); 3705 if (s) { 3706 s = strchr(++s, '.'); 3707 if (s) { 3708 ref_system = strsep(&str, "."); 3709 if (!str) { 3710 ret = -EINVAL; 3711 goto out; 3712 } 3713 ref_event = strsep(&str, "."); 3714 if (!str) { 3715 ret = -EINVAL; 3716 goto out; 3717 } 3718 ref_var = str; 3719 } 3720 } 3721 3722 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var); 3723 if (!s) { 3724 hist_field = parse_var_ref(hist_data, ref_system, 3725 ref_event, ref_var); 3726 if (hist_field) { 3727 if (var_name) { 3728 hist_field = create_alias(hist_data, hist_field, var_name); 3729 if (!hist_field) { 3730 ret = -ENOMEM; 3731 goto out; 3732 } 3733 } 3734 return hist_field; 3735 } 3736 } else 3737 str = s; 3738 3739 field = parse_field(hist_data, file, str, flags); 3740 if (IS_ERR(field)) { 3741 ret = PTR_ERR(field); 3742 goto out; 3743 } 3744 3745 hist_field = create_hist_field(hist_data, field, *flags, var_name); 3746 if (!hist_field) { 3747 ret = -ENOMEM; 3748 goto out; 3749 } 3750 3751 return hist_field; 3752 out: 3753 return ERR_PTR(ret); 3754 } 3755 3756 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data, 3757 struct trace_event_file *file, 3758 char *str, unsigned long flags, 3759 char *var_name, unsigned int level); 3760 3761 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data, 3762 struct trace_event_file *file, 3763 char *str, unsigned long flags, 3764 char *var_name, unsigned int level) 3765 { 3766 struct hist_field *operand1, *expr = NULL; 3767 unsigned long operand_flags; 3768 int ret = 0; 3769 char *s; 3770 3771 /* we support only -(xxx) i.e. explicit parens required */ 3772 3773 if (level > 3) { 3774 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str)); 3775 ret = -EINVAL; 3776 goto free; 3777 } 3778 3779 str++; /* skip leading '-' */ 3780 3781 s = strchr(str, '('); 3782 if (s) 3783 str++; 3784 else { 3785 ret = -EINVAL; 3786 goto free; 3787 } 3788 3789 s = strrchr(str, ')'); 3790 if (s) 3791 *s = '\0'; 3792 else { 3793 ret = -EINVAL; /* no closing ')' */ 3794 goto free; 3795 } 3796 3797 flags |= HIST_FIELD_FL_EXPR; 3798 expr = create_hist_field(hist_data, NULL, flags, var_name); 3799 if (!expr) { 3800 ret = -ENOMEM; 3801 goto free; 3802 } 3803 3804 operand_flags = 0; 3805 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level); 3806 if (IS_ERR(operand1)) { 3807 ret = PTR_ERR(operand1); 3808 goto free; 3809 } 3810 3811 expr->flags |= operand1->flags & 3812 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS); 3813 expr->fn = hist_field_unary_minus; 3814 expr->operands[0] = operand1; 3815 expr->operator = FIELD_OP_UNARY_MINUS; 3816 expr->name = expr_str(expr, 0); 3817 expr->type = kstrdup(operand1->type, GFP_KERNEL); 3818 if (!expr->type) { 3819 ret = -ENOMEM; 3820 goto free; 3821 } 3822 3823 return expr; 3824 free: 3825 destroy_hist_field(expr, 0); 3826 return ERR_PTR(ret); 3827 } 3828 3829 static int check_expr_operands(struct trace_array *tr, 3830 struct hist_field *operand1, 3831 struct hist_field *operand2) 3832 { 3833 unsigned long operand1_flags = operand1->flags; 3834 unsigned long operand2_flags = operand2->flags; 3835 3836 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) || 3837 (operand1_flags & HIST_FIELD_FL_ALIAS)) { 3838 struct hist_field *var; 3839 3840 var = find_var_field(operand1->var.hist_data, operand1->name); 3841 if (!var) 3842 return -EINVAL; 3843 operand1_flags = var->flags; 3844 } 3845 3846 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) || 3847 (operand2_flags & HIST_FIELD_FL_ALIAS)) { 3848 struct hist_field *var; 3849 3850 var = find_var_field(operand2->var.hist_data, operand2->name); 3851 if (!var) 3852 return -EINVAL; 3853 operand2_flags = var->flags; 3854 } 3855 3856 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) != 3857 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) { 3858 hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0); 3859 return -EINVAL; 3860 } 3861 3862 return 0; 3863 } 3864 3865 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data, 3866 struct trace_event_file *file, 3867 char *str, unsigned long flags, 3868 char *var_name, unsigned int level) 3869 { 3870 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL; 3871 unsigned long operand_flags; 3872 int field_op, ret = -EINVAL; 3873 char *sep, *operand1_str; 3874 3875 if (level > 3) { 3876 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str)); 3877 return ERR_PTR(-EINVAL); 3878 } 3879 3880 field_op = contains_operator(str); 3881 3882 if (field_op == FIELD_OP_NONE) 3883 return parse_atom(hist_data, file, str, &flags, var_name); 3884 3885 if (field_op == FIELD_OP_UNARY_MINUS) 3886 return parse_unary(hist_data, file, str, flags, var_name, ++level); 3887 3888 switch (field_op) { 3889 case FIELD_OP_MINUS: 3890 sep = "-"; 3891 break; 3892 case FIELD_OP_PLUS: 3893 sep = "+"; 3894 break; 3895 default: 3896 goto free; 3897 } 3898 3899 operand1_str = strsep(&str, sep); 3900 if (!operand1_str || !str) 3901 goto free; 3902 3903 operand_flags = 0; 3904 operand1 = parse_atom(hist_data, file, operand1_str, 3905 &operand_flags, NULL); 3906 if (IS_ERR(operand1)) { 3907 ret = PTR_ERR(operand1); 3908 operand1 = NULL; 3909 goto free; 3910 } 3911 3912 /* rest of string could be another expression e.g. b+c in a+b+c */ 3913 operand_flags = 0; 3914 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level); 3915 if (IS_ERR(operand2)) { 3916 ret = PTR_ERR(operand2); 3917 operand2 = NULL; 3918 goto free; 3919 } 3920 3921 ret = check_expr_operands(file->tr, operand1, operand2); 3922 if (ret) 3923 goto free; 3924 3925 flags |= HIST_FIELD_FL_EXPR; 3926 3927 flags |= operand1->flags & 3928 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS); 3929 3930 expr = create_hist_field(hist_data, NULL, flags, var_name); 3931 if (!expr) { 3932 ret = -ENOMEM; 3933 goto free; 3934 } 3935 3936 operand1->read_once = true; 3937 operand2->read_once = true; 3938 3939 expr->operands[0] = operand1; 3940 expr->operands[1] = operand2; 3941 expr->operator = field_op; 3942 expr->name = expr_str(expr, 0); 3943 expr->type = kstrdup(operand1->type, GFP_KERNEL); 3944 if (!expr->type) { 3945 ret = -ENOMEM; 3946 goto free; 3947 } 3948 3949 switch (field_op) { 3950 case FIELD_OP_MINUS: 3951 expr->fn = hist_field_minus; 3952 break; 3953 case FIELD_OP_PLUS: 3954 expr->fn = hist_field_plus; 3955 break; 3956 default: 3957 ret = -EINVAL; 3958 goto free; 3959 } 3960 3961 return expr; 3962 free: 3963 destroy_hist_field(operand1, 0); 3964 destroy_hist_field(operand2, 0); 3965 destroy_hist_field(expr, 0); 3966 3967 return ERR_PTR(ret); 3968 } 3969 3970 static char *find_trigger_filter(struct hist_trigger_data *hist_data, 3971 struct trace_event_file *file) 3972 { 3973 struct event_trigger_data *test; 3974 3975 lockdep_assert_held(&event_mutex); 3976 3977 list_for_each_entry(test, &file->triggers, list) { 3978 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 3979 if (test->private_data == hist_data) 3980 return test->filter_str; 3981 } 3982 } 3983 3984 return NULL; 3985 } 3986 3987 static struct event_command trigger_hist_cmd; 3988 static int event_hist_trigger_func(struct event_command *cmd_ops, 3989 struct trace_event_file *file, 3990 char *glob, char *cmd, char *param); 3991 3992 static bool compatible_keys(struct hist_trigger_data *target_hist_data, 3993 struct hist_trigger_data *hist_data, 3994 unsigned int n_keys) 3995 { 3996 struct hist_field *target_hist_field, *hist_field; 3997 unsigned int n, i, j; 3998 3999 if (hist_data->n_fields - hist_data->n_vals != n_keys) 4000 return false; 4001 4002 i = hist_data->n_vals; 4003 j = target_hist_data->n_vals; 4004 4005 for (n = 0; n < n_keys; n++) { 4006 hist_field = hist_data->fields[i + n]; 4007 target_hist_field = target_hist_data->fields[j + n]; 4008 4009 if (strcmp(hist_field->type, target_hist_field->type) != 0) 4010 return false; 4011 if (hist_field->size != target_hist_field->size) 4012 return false; 4013 if (hist_field->is_signed != target_hist_field->is_signed) 4014 return false; 4015 } 4016 4017 return true; 4018 } 4019 4020 static struct hist_trigger_data * 4021 find_compatible_hist(struct hist_trigger_data *target_hist_data, 4022 struct trace_event_file *file) 4023 { 4024 struct hist_trigger_data *hist_data; 4025 struct event_trigger_data *test; 4026 unsigned int n_keys; 4027 4028 lockdep_assert_held(&event_mutex); 4029 4030 n_keys = target_hist_data->n_fields - target_hist_data->n_vals; 4031 4032 list_for_each_entry(test, &file->triggers, list) { 4033 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 4034 hist_data = test->private_data; 4035 4036 if (compatible_keys(target_hist_data, hist_data, n_keys)) 4037 return hist_data; 4038 } 4039 } 4040 4041 return NULL; 4042 } 4043 4044 static struct trace_event_file *event_file(struct trace_array *tr, 4045 char *system, char *event_name) 4046 { 4047 struct trace_event_file *file; 4048 4049 file = __find_event_file(tr, system, event_name); 4050 if (!file) 4051 return ERR_PTR(-EINVAL); 4052 4053 return file; 4054 } 4055 4056 static struct hist_field * 4057 find_synthetic_field_var(struct hist_trigger_data *target_hist_data, 4058 char *system, char *event_name, char *field_name) 4059 { 4060 struct hist_field *event_var; 4061 char *synthetic_name; 4062 4063 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL); 4064 if (!synthetic_name) 4065 return ERR_PTR(-ENOMEM); 4066 4067 strcpy(synthetic_name, "synthetic_"); 4068 strcat(synthetic_name, field_name); 4069 4070 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name); 4071 4072 kfree(synthetic_name); 4073 4074 return event_var; 4075 } 4076 4077 /** 4078 * create_field_var_hist - Automatically create a histogram and var for a field 4079 * @target_hist_data: The target hist trigger 4080 * @subsys_name: Optional subsystem name 4081 * @event_name: Optional event name 4082 * @field_name: The name of the field (and the resulting variable) 4083 * 4084 * Hist trigger actions fetch data from variables, not directly from 4085 * events. However, for convenience, users are allowed to directly 4086 * specify an event field in an action, which will be automatically 4087 * converted into a variable on their behalf. 4088 4089 * If a user specifies a field on an event that isn't the event the 4090 * histogram currently being defined (the target event histogram), the 4091 * only way that can be accomplished is if a new hist trigger is 4092 * created and the field variable defined on that. 4093 * 4094 * This function creates a new histogram compatible with the target 4095 * event (meaning a histogram with the same key as the target 4096 * histogram), and creates a variable for the specified field, but 4097 * with 'synthetic_' prepended to the variable name in order to avoid 4098 * collision with normal field variables. 4099 * 4100 * Return: The variable created for the field. 4101 */ 4102 static struct hist_field * 4103 create_field_var_hist(struct hist_trigger_data *target_hist_data, 4104 char *subsys_name, char *event_name, char *field_name) 4105 { 4106 struct trace_array *tr = target_hist_data->event_file->tr; 4107 struct hist_field *event_var = ERR_PTR(-EINVAL); 4108 struct hist_trigger_data *hist_data; 4109 unsigned int i, n, first = true; 4110 struct field_var_hist *var_hist; 4111 struct trace_event_file *file; 4112 struct hist_field *key_field; 4113 char *saved_filter; 4114 char *cmd; 4115 int ret; 4116 4117 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) { 4118 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name)); 4119 return ERR_PTR(-EINVAL); 4120 } 4121 4122 file = event_file(tr, subsys_name, event_name); 4123 4124 if (IS_ERR(file)) { 4125 hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name)); 4126 ret = PTR_ERR(file); 4127 return ERR_PTR(ret); 4128 } 4129 4130 /* 4131 * Look for a histogram compatible with target. We'll use the 4132 * found histogram specification to create a new matching 4133 * histogram with our variable on it. target_hist_data is not 4134 * yet a registered histogram so we can't use that. 4135 */ 4136 hist_data = find_compatible_hist(target_hist_data, file); 4137 if (!hist_data) { 4138 hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name)); 4139 return ERR_PTR(-EINVAL); 4140 } 4141 4142 /* See if a synthetic field variable has already been created */ 4143 event_var = find_synthetic_field_var(target_hist_data, subsys_name, 4144 event_name, field_name); 4145 if (!IS_ERR_OR_NULL(event_var)) 4146 return event_var; 4147 4148 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL); 4149 if (!var_hist) 4150 return ERR_PTR(-ENOMEM); 4151 4152 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL); 4153 if (!cmd) { 4154 kfree(var_hist); 4155 return ERR_PTR(-ENOMEM); 4156 } 4157 4158 /* Use the same keys as the compatible histogram */ 4159 strcat(cmd, "keys="); 4160 4161 for_each_hist_key_field(i, hist_data) { 4162 key_field = hist_data->fields[i]; 4163 if (!first) 4164 strcat(cmd, ","); 4165 strcat(cmd, key_field->field->name); 4166 first = false; 4167 } 4168 4169 /* Create the synthetic field variable specification */ 4170 strcat(cmd, ":synthetic_"); 4171 strcat(cmd, field_name); 4172 strcat(cmd, "="); 4173 strcat(cmd, field_name); 4174 4175 /* Use the same filter as the compatible histogram */ 4176 saved_filter = find_trigger_filter(hist_data, file); 4177 if (saved_filter) { 4178 strcat(cmd, " if "); 4179 strcat(cmd, saved_filter); 4180 } 4181 4182 var_hist->cmd = kstrdup(cmd, GFP_KERNEL); 4183 if (!var_hist->cmd) { 4184 kfree(cmd); 4185 kfree(var_hist); 4186 return ERR_PTR(-ENOMEM); 4187 } 4188 4189 /* Save the compatible histogram information */ 4190 var_hist->hist_data = hist_data; 4191 4192 /* Create the new histogram with our variable */ 4193 ret = event_hist_trigger_func(&trigger_hist_cmd, file, 4194 "", "hist", cmd); 4195 if (ret) { 4196 kfree(cmd); 4197 kfree(var_hist->cmd); 4198 kfree(var_hist); 4199 hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name)); 4200 return ERR_PTR(ret); 4201 } 4202 4203 kfree(cmd); 4204 4205 /* If we can't find the variable, something went wrong */ 4206 event_var = find_synthetic_field_var(target_hist_data, subsys_name, 4207 event_name, field_name); 4208 if (IS_ERR_OR_NULL(event_var)) { 4209 kfree(var_hist->cmd); 4210 kfree(var_hist); 4211 hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name)); 4212 return ERR_PTR(-EINVAL); 4213 } 4214 4215 n = target_hist_data->n_field_var_hists; 4216 target_hist_data->field_var_hists[n] = var_hist; 4217 target_hist_data->n_field_var_hists++; 4218 4219 return event_var; 4220 } 4221 4222 static struct hist_field * 4223 find_target_event_var(struct hist_trigger_data *hist_data, 4224 char *subsys_name, char *event_name, char *var_name) 4225 { 4226 struct trace_event_file *file = hist_data->event_file; 4227 struct hist_field *hist_field = NULL; 4228 4229 if (subsys_name) { 4230 struct trace_event_call *call; 4231 4232 if (!event_name) 4233 return NULL; 4234 4235 call = file->event_call; 4236 4237 if (strcmp(subsys_name, call->class->system) != 0) 4238 return NULL; 4239 4240 if (strcmp(event_name, trace_event_name(call)) != 0) 4241 return NULL; 4242 } 4243 4244 hist_field = find_var_field(hist_data, var_name); 4245 4246 return hist_field; 4247 } 4248 4249 static inline void __update_field_vars(struct tracing_map_elt *elt, 4250 struct ring_buffer_event *rbe, 4251 void *rec, 4252 struct field_var **field_vars, 4253 unsigned int n_field_vars, 4254 unsigned int field_var_str_start) 4255 { 4256 struct hist_elt_data *elt_data = elt->private_data; 4257 unsigned int i, j, var_idx; 4258 u64 var_val; 4259 4260 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) { 4261 struct field_var *field_var = field_vars[i]; 4262 struct hist_field *var = field_var->var; 4263 struct hist_field *val = field_var->val; 4264 4265 var_val = val->fn(val, elt, rbe, rec); 4266 var_idx = var->var.idx; 4267 4268 if (val->flags & HIST_FIELD_FL_STRING) { 4269 char *str = elt_data->field_var_str[j++]; 4270 char *val_str = (char *)(uintptr_t)var_val; 4271 4272 strscpy(str, val_str, STR_VAR_LEN_MAX); 4273 var_val = (u64)(uintptr_t)str; 4274 } 4275 tracing_map_set_var(elt, var_idx, var_val); 4276 } 4277 } 4278 4279 static void update_field_vars(struct hist_trigger_data *hist_data, 4280 struct tracing_map_elt *elt, 4281 struct ring_buffer_event *rbe, 4282 void *rec) 4283 { 4284 __update_field_vars(elt, rbe, rec, hist_data->field_vars, 4285 hist_data->n_field_vars, 0); 4286 } 4287 4288 static void save_track_data_vars(struct hist_trigger_data *hist_data, 4289 struct tracing_map_elt *elt, void *rec, 4290 struct ring_buffer_event *rbe, void *key, 4291 struct action_data *data, u64 *var_ref_vals) 4292 { 4293 __update_field_vars(elt, rbe, rec, hist_data->save_vars, 4294 hist_data->n_save_vars, hist_data->n_field_var_str); 4295 } 4296 4297 static struct hist_field *create_var(struct hist_trigger_data *hist_data, 4298 struct trace_event_file *file, 4299 char *name, int size, const char *type) 4300 { 4301 struct hist_field *var; 4302 int idx; 4303 4304 if (find_var(hist_data, file, name) && !hist_data->remove) { 4305 var = ERR_PTR(-EINVAL); 4306 goto out; 4307 } 4308 4309 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL); 4310 if (!var) { 4311 var = ERR_PTR(-ENOMEM); 4312 goto out; 4313 } 4314 4315 idx = tracing_map_add_var(hist_data->map); 4316 if (idx < 0) { 4317 kfree(var); 4318 var = ERR_PTR(-EINVAL); 4319 goto out; 4320 } 4321 4322 var->flags = HIST_FIELD_FL_VAR; 4323 var->var.idx = idx; 4324 var->var.hist_data = var->hist_data = hist_data; 4325 var->size = size; 4326 var->var.name = kstrdup(name, GFP_KERNEL); 4327 var->type = kstrdup(type, GFP_KERNEL); 4328 if (!var->var.name || !var->type) { 4329 kfree(var->var.name); 4330 kfree(var->type); 4331 kfree(var); 4332 var = ERR_PTR(-ENOMEM); 4333 } 4334 out: 4335 return var; 4336 } 4337 4338 static struct field_var *create_field_var(struct hist_trigger_data *hist_data, 4339 struct trace_event_file *file, 4340 char *field_name) 4341 { 4342 struct hist_field *val = NULL, *var = NULL; 4343 unsigned long flags = HIST_FIELD_FL_VAR; 4344 struct trace_array *tr = file->tr; 4345 struct field_var *field_var; 4346 int ret = 0; 4347 4348 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) { 4349 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name)); 4350 ret = -EINVAL; 4351 goto err; 4352 } 4353 4354 val = parse_atom(hist_data, file, field_name, &flags, NULL); 4355 if (IS_ERR(val)) { 4356 hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name)); 4357 ret = PTR_ERR(val); 4358 goto err; 4359 } 4360 4361 var = create_var(hist_data, file, field_name, val->size, val->type); 4362 if (IS_ERR(var)) { 4363 hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name)); 4364 kfree(val); 4365 ret = PTR_ERR(var); 4366 goto err; 4367 } 4368 4369 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL); 4370 if (!field_var) { 4371 kfree(val); 4372 kfree(var); 4373 ret = -ENOMEM; 4374 goto err; 4375 } 4376 4377 field_var->var = var; 4378 field_var->val = val; 4379 out: 4380 return field_var; 4381 err: 4382 field_var = ERR_PTR(ret); 4383 goto out; 4384 } 4385 4386 /** 4387 * create_target_field_var - Automatically create a variable for a field 4388 * @target_hist_data: The target hist trigger 4389 * @subsys_name: Optional subsystem name 4390 * @event_name: Optional event name 4391 * @var_name: The name of the field (and the resulting variable) 4392 * 4393 * Hist trigger actions fetch data from variables, not directly from 4394 * events. However, for convenience, users are allowed to directly 4395 * specify an event field in an action, which will be automatically 4396 * converted into a variable on their behalf. 4397 4398 * This function creates a field variable with the name var_name on 4399 * the hist trigger currently being defined on the target event. If 4400 * subsys_name and event_name are specified, this function simply 4401 * verifies that they do in fact match the target event subsystem and 4402 * event name. 4403 * 4404 * Return: The variable created for the field. 4405 */ 4406 static struct field_var * 4407 create_target_field_var(struct hist_trigger_data *target_hist_data, 4408 char *subsys_name, char *event_name, char *var_name) 4409 { 4410 struct trace_event_file *file = target_hist_data->event_file; 4411 4412 if (subsys_name) { 4413 struct trace_event_call *call; 4414 4415 if (!event_name) 4416 return NULL; 4417 4418 call = file->event_call; 4419 4420 if (strcmp(subsys_name, call->class->system) != 0) 4421 return NULL; 4422 4423 if (strcmp(event_name, trace_event_name(call)) != 0) 4424 return NULL; 4425 } 4426 4427 return create_field_var(target_hist_data, file, var_name); 4428 } 4429 4430 static bool check_track_val_max(u64 track_val, u64 var_val) 4431 { 4432 if (var_val <= track_val) 4433 return false; 4434 4435 return true; 4436 } 4437 4438 static bool check_track_val_changed(u64 track_val, u64 var_val) 4439 { 4440 if (var_val == track_val) 4441 return false; 4442 4443 return true; 4444 } 4445 4446 static u64 get_track_val(struct hist_trigger_data *hist_data, 4447 struct tracing_map_elt *elt, 4448 struct action_data *data) 4449 { 4450 unsigned int track_var_idx = data->track_data.track_var->var.idx; 4451 u64 track_val; 4452 4453 track_val = tracing_map_read_var(elt, track_var_idx); 4454 4455 return track_val; 4456 } 4457 4458 static void save_track_val(struct hist_trigger_data *hist_data, 4459 struct tracing_map_elt *elt, 4460 struct action_data *data, u64 var_val) 4461 { 4462 unsigned int track_var_idx = data->track_data.track_var->var.idx; 4463 4464 tracing_map_set_var(elt, track_var_idx, var_val); 4465 } 4466 4467 static void save_track_data(struct hist_trigger_data *hist_data, 4468 struct tracing_map_elt *elt, void *rec, 4469 struct ring_buffer_event *rbe, void *key, 4470 struct action_data *data, u64 *var_ref_vals) 4471 { 4472 if (data->track_data.save_data) 4473 data->track_data.save_data(hist_data, elt, rec, rbe, key, data, var_ref_vals); 4474 } 4475 4476 static bool check_track_val(struct tracing_map_elt *elt, 4477 struct action_data *data, 4478 u64 var_val) 4479 { 4480 struct hist_trigger_data *hist_data; 4481 u64 track_val; 4482 4483 hist_data = data->track_data.track_var->hist_data; 4484 track_val = get_track_val(hist_data, elt, data); 4485 4486 return data->track_data.check_val(track_val, var_val); 4487 } 4488 4489 #ifdef CONFIG_TRACER_SNAPSHOT 4490 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data) 4491 { 4492 /* called with tr->max_lock held */ 4493 struct track_data *track_data = tr->cond_snapshot->cond_data; 4494 struct hist_elt_data *elt_data, *track_elt_data; 4495 struct snapshot_context *context = cond_data; 4496 struct action_data *action; 4497 u64 track_val; 4498 4499 if (!track_data) 4500 return false; 4501 4502 action = track_data->action_data; 4503 4504 track_val = get_track_val(track_data->hist_data, context->elt, 4505 track_data->action_data); 4506 4507 if (!action->track_data.check_val(track_data->track_val, track_val)) 4508 return false; 4509 4510 track_data->track_val = track_val; 4511 memcpy(track_data->key, context->key, track_data->key_len); 4512 4513 elt_data = context->elt->private_data; 4514 track_elt_data = track_data->elt.private_data; 4515 if (elt_data->comm) 4516 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN); 4517 4518 track_data->updated = true; 4519 4520 return true; 4521 } 4522 4523 static void save_track_data_snapshot(struct hist_trigger_data *hist_data, 4524 struct tracing_map_elt *elt, void *rec, 4525 struct ring_buffer_event *rbe, void *key, 4526 struct action_data *data, 4527 u64 *var_ref_vals) 4528 { 4529 struct trace_event_file *file = hist_data->event_file; 4530 struct snapshot_context context; 4531 4532 context.elt = elt; 4533 context.key = key; 4534 4535 tracing_snapshot_cond(file->tr, &context); 4536 } 4537 4538 static void hist_trigger_print_key(struct seq_file *m, 4539 struct hist_trigger_data *hist_data, 4540 void *key, 4541 struct tracing_map_elt *elt); 4542 4543 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data) 4544 { 4545 unsigned int i; 4546 4547 if (!hist_data->n_actions) 4548 return NULL; 4549 4550 for (i = 0; i < hist_data->n_actions; i++) { 4551 struct action_data *data = hist_data->actions[i]; 4552 4553 if (data->action == ACTION_SNAPSHOT) 4554 return data; 4555 } 4556 4557 return NULL; 4558 } 4559 4560 static void track_data_snapshot_print(struct seq_file *m, 4561 struct hist_trigger_data *hist_data) 4562 { 4563 struct trace_event_file *file = hist_data->event_file; 4564 struct track_data *track_data; 4565 struct action_data *action; 4566 4567 track_data = tracing_cond_snapshot_data(file->tr); 4568 if (!track_data) 4569 return; 4570 4571 if (!track_data->updated) 4572 return; 4573 4574 action = snapshot_action(hist_data); 4575 if (!action) 4576 return; 4577 4578 seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n"); 4579 seq_printf(m, "\ttriggering value { %s(%s) }: %10llu", 4580 action->handler == HANDLER_ONMAX ? "onmax" : "onchange", 4581 action->track_data.var_str, track_data->track_val); 4582 4583 seq_puts(m, "\ttriggered by event with key: "); 4584 hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt); 4585 seq_putc(m, '\n'); 4586 } 4587 #else 4588 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data) 4589 { 4590 return false; 4591 } 4592 static void save_track_data_snapshot(struct hist_trigger_data *hist_data, 4593 struct tracing_map_elt *elt, void *rec, 4594 struct ring_buffer_event *rbe, void *key, 4595 struct action_data *data, 4596 u64 *var_ref_vals) {} 4597 static void track_data_snapshot_print(struct seq_file *m, 4598 struct hist_trigger_data *hist_data) {} 4599 #endif /* CONFIG_TRACER_SNAPSHOT */ 4600 4601 static void track_data_print(struct seq_file *m, 4602 struct hist_trigger_data *hist_data, 4603 struct tracing_map_elt *elt, 4604 struct action_data *data) 4605 { 4606 u64 track_val = get_track_val(hist_data, elt, data); 4607 unsigned int i, save_var_idx; 4608 4609 if (data->handler == HANDLER_ONMAX) 4610 seq_printf(m, "\n\tmax: %10llu", track_val); 4611 else if (data->handler == HANDLER_ONCHANGE) 4612 seq_printf(m, "\n\tchanged: %10llu", track_val); 4613 4614 if (data->action == ACTION_SNAPSHOT) 4615 return; 4616 4617 for (i = 0; i < hist_data->n_save_vars; i++) { 4618 struct hist_field *save_val = hist_data->save_vars[i]->val; 4619 struct hist_field *save_var = hist_data->save_vars[i]->var; 4620 u64 val; 4621 4622 save_var_idx = save_var->var.idx; 4623 4624 val = tracing_map_read_var(elt, save_var_idx); 4625 4626 if (save_val->flags & HIST_FIELD_FL_STRING) { 4627 seq_printf(m, " %s: %-32s", save_var->var.name, 4628 (char *)(uintptr_t)(val)); 4629 } else 4630 seq_printf(m, " %s: %10llu", save_var->var.name, val); 4631 } 4632 } 4633 4634 static void ontrack_action(struct hist_trigger_data *hist_data, 4635 struct tracing_map_elt *elt, void *rec, 4636 struct ring_buffer_event *rbe, void *key, 4637 struct action_data *data, u64 *var_ref_vals) 4638 { 4639 u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx]; 4640 4641 if (check_track_val(elt, data, var_val)) { 4642 save_track_val(hist_data, elt, data, var_val); 4643 save_track_data(hist_data, elt, rec, rbe, key, data, var_ref_vals); 4644 } 4645 } 4646 4647 static void action_data_destroy(struct action_data *data) 4648 { 4649 unsigned int i; 4650 4651 lockdep_assert_held(&event_mutex); 4652 4653 kfree(data->action_name); 4654 4655 for (i = 0; i < data->n_params; i++) 4656 kfree(data->params[i]); 4657 4658 if (data->synth_event) 4659 data->synth_event->ref--; 4660 4661 kfree(data->synth_event_name); 4662 4663 kfree(data); 4664 } 4665 4666 static void track_data_destroy(struct hist_trigger_data *hist_data, 4667 struct action_data *data) 4668 { 4669 struct trace_event_file *file = hist_data->event_file; 4670 4671 destroy_hist_field(data->track_data.track_var, 0); 4672 4673 if (data->action == ACTION_SNAPSHOT) { 4674 struct track_data *track_data; 4675 4676 track_data = tracing_cond_snapshot_data(file->tr); 4677 if (track_data && track_data->hist_data == hist_data) { 4678 tracing_snapshot_cond_disable(file->tr); 4679 track_data_free(track_data); 4680 } 4681 } 4682 4683 kfree(data->track_data.var_str); 4684 4685 action_data_destroy(data); 4686 } 4687 4688 static int action_create(struct hist_trigger_data *hist_data, 4689 struct action_data *data); 4690 4691 static int track_data_create(struct hist_trigger_data *hist_data, 4692 struct action_data *data) 4693 { 4694 struct hist_field *var_field, *ref_field, *track_var = NULL; 4695 struct trace_event_file *file = hist_data->event_file; 4696 struct trace_array *tr = file->tr; 4697 char *track_data_var_str; 4698 int ret = 0; 4699 4700 track_data_var_str = data->track_data.var_str; 4701 if (track_data_var_str[0] != '$') { 4702 hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str)); 4703 return -EINVAL; 4704 } 4705 track_data_var_str++; 4706 4707 var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str); 4708 if (!var_field) { 4709 hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str)); 4710 return -EINVAL; 4711 } 4712 4713 ref_field = create_var_ref(hist_data, var_field, NULL, NULL); 4714 if (!ref_field) 4715 return -ENOMEM; 4716 4717 data->track_data.var_ref = ref_field; 4718 4719 if (data->handler == HANDLER_ONMAX) 4720 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64"); 4721 if (IS_ERR(track_var)) { 4722 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0); 4723 ret = PTR_ERR(track_var); 4724 goto out; 4725 } 4726 4727 if (data->handler == HANDLER_ONCHANGE) 4728 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64"); 4729 if (IS_ERR(track_var)) { 4730 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0); 4731 ret = PTR_ERR(track_var); 4732 goto out; 4733 } 4734 data->track_data.track_var = track_var; 4735 4736 ret = action_create(hist_data, data); 4737 out: 4738 return ret; 4739 } 4740 4741 static int parse_action_params(struct trace_array *tr, char *params, 4742 struct action_data *data) 4743 { 4744 char *param, *saved_param; 4745 bool first_param = true; 4746 int ret = 0; 4747 4748 while (params) { 4749 if (data->n_params >= SYNTH_FIELDS_MAX) { 4750 hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0); 4751 goto out; 4752 } 4753 4754 param = strsep(¶ms, ","); 4755 if (!param) { 4756 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0); 4757 ret = -EINVAL; 4758 goto out; 4759 } 4760 4761 param = strstrip(param); 4762 if (strlen(param) < 2) { 4763 hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param)); 4764 ret = -EINVAL; 4765 goto out; 4766 } 4767 4768 saved_param = kstrdup(param, GFP_KERNEL); 4769 if (!saved_param) { 4770 ret = -ENOMEM; 4771 goto out; 4772 } 4773 4774 if (first_param && data->use_trace_keyword) { 4775 data->synth_event_name = saved_param; 4776 first_param = false; 4777 continue; 4778 } 4779 first_param = false; 4780 4781 data->params[data->n_params++] = saved_param; 4782 } 4783 out: 4784 return ret; 4785 } 4786 4787 static int action_parse(struct trace_array *tr, char *str, struct action_data *data, 4788 enum handler_id handler) 4789 { 4790 char *action_name; 4791 int ret = 0; 4792 4793 strsep(&str, "."); 4794 if (!str) { 4795 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0); 4796 ret = -EINVAL; 4797 goto out; 4798 } 4799 4800 action_name = strsep(&str, "("); 4801 if (!action_name || !str) { 4802 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0); 4803 ret = -EINVAL; 4804 goto out; 4805 } 4806 4807 if (str_has_prefix(action_name, "save")) { 4808 char *params = strsep(&str, ")"); 4809 4810 if (!params) { 4811 hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0); 4812 ret = -EINVAL; 4813 goto out; 4814 } 4815 4816 ret = parse_action_params(tr, params, data); 4817 if (ret) 4818 goto out; 4819 4820 if (handler == HANDLER_ONMAX) 4821 data->track_data.check_val = check_track_val_max; 4822 else if (handler == HANDLER_ONCHANGE) 4823 data->track_data.check_val = check_track_val_changed; 4824 else { 4825 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name)); 4826 ret = -EINVAL; 4827 goto out; 4828 } 4829 4830 data->track_data.save_data = save_track_data_vars; 4831 data->fn = ontrack_action; 4832 data->action = ACTION_SAVE; 4833 } else if (str_has_prefix(action_name, "snapshot")) { 4834 char *params = strsep(&str, ")"); 4835 4836 if (!str) { 4837 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params)); 4838 ret = -EINVAL; 4839 goto out; 4840 } 4841 4842 if (handler == HANDLER_ONMAX) 4843 data->track_data.check_val = check_track_val_max; 4844 else if (handler == HANDLER_ONCHANGE) 4845 data->track_data.check_val = check_track_val_changed; 4846 else { 4847 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name)); 4848 ret = -EINVAL; 4849 goto out; 4850 } 4851 4852 data->track_data.save_data = save_track_data_snapshot; 4853 data->fn = ontrack_action; 4854 data->action = ACTION_SNAPSHOT; 4855 } else { 4856 char *params = strsep(&str, ")"); 4857 4858 if (str_has_prefix(action_name, "trace")) 4859 data->use_trace_keyword = true; 4860 4861 if (params) { 4862 ret = parse_action_params(tr, params, data); 4863 if (ret) 4864 goto out; 4865 } 4866 4867 if (handler == HANDLER_ONMAX) 4868 data->track_data.check_val = check_track_val_max; 4869 else if (handler == HANDLER_ONCHANGE) 4870 data->track_data.check_val = check_track_val_changed; 4871 4872 if (handler != HANDLER_ONMATCH) { 4873 data->track_data.save_data = action_trace; 4874 data->fn = ontrack_action; 4875 } else 4876 data->fn = action_trace; 4877 4878 data->action = ACTION_TRACE; 4879 } 4880 4881 data->action_name = kstrdup(action_name, GFP_KERNEL); 4882 if (!data->action_name) { 4883 ret = -ENOMEM; 4884 goto out; 4885 } 4886 4887 data->handler = handler; 4888 out: 4889 return ret; 4890 } 4891 4892 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data, 4893 char *str, enum handler_id handler) 4894 { 4895 struct action_data *data; 4896 int ret = -EINVAL; 4897 char *var_str; 4898 4899 data = kzalloc(sizeof(*data), GFP_KERNEL); 4900 if (!data) 4901 return ERR_PTR(-ENOMEM); 4902 4903 var_str = strsep(&str, ")"); 4904 if (!var_str || !str) { 4905 ret = -EINVAL; 4906 goto free; 4907 } 4908 4909 data->track_data.var_str = kstrdup(var_str, GFP_KERNEL); 4910 if (!data->track_data.var_str) { 4911 ret = -ENOMEM; 4912 goto free; 4913 } 4914 4915 ret = action_parse(hist_data->event_file->tr, str, data, handler); 4916 if (ret) 4917 goto free; 4918 out: 4919 return data; 4920 free: 4921 track_data_destroy(hist_data, data); 4922 data = ERR_PTR(ret); 4923 goto out; 4924 } 4925 4926 static void onmatch_destroy(struct action_data *data) 4927 { 4928 kfree(data->match_data.event); 4929 kfree(data->match_data.event_system); 4930 4931 action_data_destroy(data); 4932 } 4933 4934 static void destroy_field_var(struct field_var *field_var) 4935 { 4936 if (!field_var) 4937 return; 4938 4939 destroy_hist_field(field_var->var, 0); 4940 destroy_hist_field(field_var->val, 0); 4941 4942 kfree(field_var); 4943 } 4944 4945 static void destroy_field_vars(struct hist_trigger_data *hist_data) 4946 { 4947 unsigned int i; 4948 4949 for (i = 0; i < hist_data->n_field_vars; i++) 4950 destroy_field_var(hist_data->field_vars[i]); 4951 } 4952 4953 static void save_field_var(struct hist_trigger_data *hist_data, 4954 struct field_var *field_var) 4955 { 4956 hist_data->field_vars[hist_data->n_field_vars++] = field_var; 4957 4958 if (field_var->val->flags & HIST_FIELD_FL_STRING) 4959 hist_data->n_field_var_str++; 4960 } 4961 4962 4963 static int check_synth_field(struct synth_event *event, 4964 struct hist_field *hist_field, 4965 unsigned int field_pos) 4966 { 4967 struct synth_field *field; 4968 4969 if (field_pos >= event->n_fields) 4970 return -EINVAL; 4971 4972 field = event->fields[field_pos]; 4973 4974 if (strcmp(field->type, hist_field->type) != 0) { 4975 if (field->size != hist_field->size || 4976 field->is_signed != hist_field->is_signed) 4977 return -EINVAL; 4978 } 4979 4980 return 0; 4981 } 4982 4983 static struct hist_field * 4984 trace_action_find_var(struct hist_trigger_data *hist_data, 4985 struct action_data *data, 4986 char *system, char *event, char *var) 4987 { 4988 struct trace_array *tr = hist_data->event_file->tr; 4989 struct hist_field *hist_field; 4990 4991 var++; /* skip '$' */ 4992 4993 hist_field = find_target_event_var(hist_data, system, event, var); 4994 if (!hist_field) { 4995 if (!system && data->handler == HANDLER_ONMATCH) { 4996 system = data->match_data.event_system; 4997 event = data->match_data.event; 4998 } 4999 5000 hist_field = find_event_var(hist_data, system, event, var); 5001 } 5002 5003 if (!hist_field) 5004 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var)); 5005 5006 return hist_field; 5007 } 5008 5009 static struct hist_field * 5010 trace_action_create_field_var(struct hist_trigger_data *hist_data, 5011 struct action_data *data, char *system, 5012 char *event, char *var) 5013 { 5014 struct hist_field *hist_field = NULL; 5015 struct field_var *field_var; 5016 5017 /* 5018 * First try to create a field var on the target event (the 5019 * currently being defined). This will create a variable for 5020 * unqualified fields on the target event, or if qualified, 5021 * target fields that have qualified names matching the target. 5022 */ 5023 field_var = create_target_field_var(hist_data, system, event, var); 5024 5025 if (field_var && !IS_ERR(field_var)) { 5026 save_field_var(hist_data, field_var); 5027 hist_field = field_var->var; 5028 } else { 5029 field_var = NULL; 5030 /* 5031 * If no explicit system.event is specfied, default to 5032 * looking for fields on the onmatch(system.event.xxx) 5033 * event. 5034 */ 5035 if (!system && data->handler == HANDLER_ONMATCH) { 5036 system = data->match_data.event_system; 5037 event = data->match_data.event; 5038 } 5039 5040 /* 5041 * At this point, we're looking at a field on another 5042 * event. Because we can't modify a hist trigger on 5043 * another event to add a variable for a field, we need 5044 * to create a new trigger on that event and create the 5045 * variable at the same time. 5046 */ 5047 hist_field = create_field_var_hist(hist_data, system, event, var); 5048 if (IS_ERR(hist_field)) 5049 goto free; 5050 } 5051 out: 5052 return hist_field; 5053 free: 5054 destroy_field_var(field_var); 5055 hist_field = NULL; 5056 goto out; 5057 } 5058 5059 static int trace_action_create(struct hist_trigger_data *hist_data, 5060 struct action_data *data) 5061 { 5062 struct trace_array *tr = hist_data->event_file->tr; 5063 char *event_name, *param, *system = NULL; 5064 struct hist_field *hist_field, *var_ref; 5065 unsigned int i; 5066 unsigned int field_pos = 0; 5067 struct synth_event *event; 5068 char *synth_event_name; 5069 int var_ref_idx, ret = 0; 5070 5071 lockdep_assert_held(&event_mutex); 5072 5073 if (data->use_trace_keyword) 5074 synth_event_name = data->synth_event_name; 5075 else 5076 synth_event_name = data->action_name; 5077 5078 event = find_synth_event(synth_event_name); 5079 if (!event) { 5080 hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name)); 5081 return -EINVAL; 5082 } 5083 5084 event->ref++; 5085 5086 for (i = 0; i < data->n_params; i++) { 5087 char *p; 5088 5089 p = param = kstrdup(data->params[i], GFP_KERNEL); 5090 if (!param) { 5091 ret = -ENOMEM; 5092 goto err; 5093 } 5094 5095 system = strsep(¶m, "."); 5096 if (!param) { 5097 param = (char *)system; 5098 system = event_name = NULL; 5099 } else { 5100 event_name = strsep(¶m, "."); 5101 if (!param) { 5102 kfree(p); 5103 ret = -EINVAL; 5104 goto err; 5105 } 5106 } 5107 5108 if (param[0] == '$') 5109 hist_field = trace_action_find_var(hist_data, data, 5110 system, event_name, 5111 param); 5112 else 5113 hist_field = trace_action_create_field_var(hist_data, 5114 data, 5115 system, 5116 event_name, 5117 param); 5118 5119 if (!hist_field) { 5120 kfree(p); 5121 ret = -EINVAL; 5122 goto err; 5123 } 5124 5125 if (check_synth_field(event, hist_field, field_pos) == 0) { 5126 var_ref = create_var_ref(hist_data, hist_field, 5127 system, event_name); 5128 if (!var_ref) { 5129 kfree(p); 5130 ret = -ENOMEM; 5131 goto err; 5132 } 5133 5134 var_ref_idx = find_var_ref_idx(hist_data, var_ref); 5135 if (WARN_ON(var_ref_idx < 0)) { 5136 ret = var_ref_idx; 5137 goto err; 5138 } 5139 5140 data->var_ref_idx[i] = var_ref_idx; 5141 5142 field_pos++; 5143 kfree(p); 5144 continue; 5145 } 5146 5147 hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param)); 5148 kfree(p); 5149 ret = -EINVAL; 5150 goto err; 5151 } 5152 5153 if (field_pos != event->n_fields) { 5154 hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name)); 5155 ret = -EINVAL; 5156 goto err; 5157 } 5158 5159 data->synth_event = event; 5160 out: 5161 return ret; 5162 err: 5163 event->ref--; 5164 5165 goto out; 5166 } 5167 5168 static int action_create(struct hist_trigger_data *hist_data, 5169 struct action_data *data) 5170 { 5171 struct trace_event_file *file = hist_data->event_file; 5172 struct trace_array *tr = file->tr; 5173 struct track_data *track_data; 5174 struct field_var *field_var; 5175 unsigned int i; 5176 char *param; 5177 int ret = 0; 5178 5179 if (data->action == ACTION_TRACE) 5180 return trace_action_create(hist_data, data); 5181 5182 if (data->action == ACTION_SNAPSHOT) { 5183 track_data = track_data_alloc(hist_data->key_size, data, hist_data); 5184 if (IS_ERR(track_data)) { 5185 ret = PTR_ERR(track_data); 5186 goto out; 5187 } 5188 5189 ret = tracing_snapshot_cond_enable(file->tr, track_data, 5190 cond_snapshot_update); 5191 if (ret) 5192 track_data_free(track_data); 5193 5194 goto out; 5195 } 5196 5197 if (data->action == ACTION_SAVE) { 5198 if (hist_data->n_save_vars) { 5199 ret = -EEXIST; 5200 hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0); 5201 goto out; 5202 } 5203 5204 for (i = 0; i < data->n_params; i++) { 5205 param = kstrdup(data->params[i], GFP_KERNEL); 5206 if (!param) { 5207 ret = -ENOMEM; 5208 goto out; 5209 } 5210 5211 field_var = create_target_field_var(hist_data, NULL, NULL, param); 5212 if (IS_ERR(field_var)) { 5213 hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL, 5214 errpos(param)); 5215 ret = PTR_ERR(field_var); 5216 kfree(param); 5217 goto out; 5218 } 5219 5220 hist_data->save_vars[hist_data->n_save_vars++] = field_var; 5221 if (field_var->val->flags & HIST_FIELD_FL_STRING) 5222 hist_data->n_save_var_str++; 5223 kfree(param); 5224 } 5225 } 5226 out: 5227 return ret; 5228 } 5229 5230 static int onmatch_create(struct hist_trigger_data *hist_data, 5231 struct action_data *data) 5232 { 5233 return action_create(hist_data, data); 5234 } 5235 5236 static struct action_data *onmatch_parse(struct trace_array *tr, char *str) 5237 { 5238 char *match_event, *match_event_system; 5239 struct action_data *data; 5240 int ret = -EINVAL; 5241 5242 data = kzalloc(sizeof(*data), GFP_KERNEL); 5243 if (!data) 5244 return ERR_PTR(-ENOMEM); 5245 5246 match_event = strsep(&str, ")"); 5247 if (!match_event || !str) { 5248 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event)); 5249 goto free; 5250 } 5251 5252 match_event_system = strsep(&match_event, "."); 5253 if (!match_event) { 5254 hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system)); 5255 goto free; 5256 } 5257 5258 if (IS_ERR(event_file(tr, match_event_system, match_event))) { 5259 hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event)); 5260 goto free; 5261 } 5262 5263 data->match_data.event = kstrdup(match_event, GFP_KERNEL); 5264 if (!data->match_data.event) { 5265 ret = -ENOMEM; 5266 goto free; 5267 } 5268 5269 data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL); 5270 if (!data->match_data.event_system) { 5271 ret = -ENOMEM; 5272 goto free; 5273 } 5274 5275 ret = action_parse(tr, str, data, HANDLER_ONMATCH); 5276 if (ret) 5277 goto free; 5278 out: 5279 return data; 5280 free: 5281 onmatch_destroy(data); 5282 data = ERR_PTR(ret); 5283 goto out; 5284 } 5285 5286 static int create_hitcount_val(struct hist_trigger_data *hist_data) 5287 { 5288 hist_data->fields[HITCOUNT_IDX] = 5289 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL); 5290 if (!hist_data->fields[HITCOUNT_IDX]) 5291 return -ENOMEM; 5292 5293 hist_data->n_vals++; 5294 hist_data->n_fields++; 5295 5296 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX)) 5297 return -EINVAL; 5298 5299 return 0; 5300 } 5301 5302 static int __create_val_field(struct hist_trigger_data *hist_data, 5303 unsigned int val_idx, 5304 struct trace_event_file *file, 5305 char *var_name, char *field_str, 5306 unsigned long flags) 5307 { 5308 struct hist_field *hist_field; 5309 int ret = 0; 5310 5311 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0); 5312 if (IS_ERR(hist_field)) { 5313 ret = PTR_ERR(hist_field); 5314 goto out; 5315 } 5316 5317 hist_data->fields[val_idx] = hist_field; 5318 5319 ++hist_data->n_vals; 5320 ++hist_data->n_fields; 5321 5322 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX)) 5323 ret = -EINVAL; 5324 out: 5325 return ret; 5326 } 5327 5328 static int create_val_field(struct hist_trigger_data *hist_data, 5329 unsigned int val_idx, 5330 struct trace_event_file *file, 5331 char *field_str) 5332 { 5333 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX)) 5334 return -EINVAL; 5335 5336 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0); 5337 } 5338 5339 static int create_var_field(struct hist_trigger_data *hist_data, 5340 unsigned int val_idx, 5341 struct trace_event_file *file, 5342 char *var_name, char *expr_str) 5343 { 5344 struct trace_array *tr = hist_data->event_file->tr; 5345 unsigned long flags = 0; 5346 5347 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX)) 5348 return -EINVAL; 5349 5350 if (find_var(hist_data, file, var_name) && !hist_data->remove) { 5351 hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name)); 5352 return -EINVAL; 5353 } 5354 5355 flags |= HIST_FIELD_FL_VAR; 5356 hist_data->n_vars++; 5357 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX)) 5358 return -EINVAL; 5359 5360 return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags); 5361 } 5362 5363 static int create_val_fields(struct hist_trigger_data *hist_data, 5364 struct trace_event_file *file) 5365 { 5366 char *fields_str, *field_str; 5367 unsigned int i, j = 1; 5368 int ret; 5369 5370 ret = create_hitcount_val(hist_data); 5371 if (ret) 5372 goto out; 5373 5374 fields_str = hist_data->attrs->vals_str; 5375 if (!fields_str) 5376 goto out; 5377 5378 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX && 5379 j < TRACING_MAP_VALS_MAX; i++) { 5380 field_str = strsep(&fields_str, ","); 5381 if (!field_str) 5382 break; 5383 5384 if (strcmp(field_str, "hitcount") == 0) 5385 continue; 5386 5387 ret = create_val_field(hist_data, j++, file, field_str); 5388 if (ret) 5389 goto out; 5390 } 5391 5392 if (fields_str && (strcmp(fields_str, "hitcount") != 0)) 5393 ret = -EINVAL; 5394 out: 5395 return ret; 5396 } 5397 5398 static int create_key_field(struct hist_trigger_data *hist_data, 5399 unsigned int key_idx, 5400 unsigned int key_offset, 5401 struct trace_event_file *file, 5402 char *field_str) 5403 { 5404 struct trace_array *tr = hist_data->event_file->tr; 5405 struct hist_field *hist_field = NULL; 5406 unsigned long flags = 0; 5407 unsigned int key_size; 5408 int ret = 0; 5409 5410 if (WARN_ON(key_idx >= HIST_FIELDS_MAX)) 5411 return -EINVAL; 5412 5413 flags |= HIST_FIELD_FL_KEY; 5414 5415 if (strcmp(field_str, "stacktrace") == 0) { 5416 flags |= HIST_FIELD_FL_STACKTRACE; 5417 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH; 5418 hist_field = create_hist_field(hist_data, NULL, flags, NULL); 5419 } else { 5420 hist_field = parse_expr(hist_data, file, field_str, flags, 5421 NULL, 0); 5422 if (IS_ERR(hist_field)) { 5423 ret = PTR_ERR(hist_field); 5424 goto out; 5425 } 5426 5427 if (field_has_hist_vars(hist_field, 0)) { 5428 hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str)); 5429 destroy_hist_field(hist_field, 0); 5430 ret = -EINVAL; 5431 goto out; 5432 } 5433 5434 key_size = hist_field->size; 5435 } 5436 5437 hist_data->fields[key_idx] = hist_field; 5438 5439 key_size = ALIGN(key_size, sizeof(u64)); 5440 hist_data->fields[key_idx]->size = key_size; 5441 hist_data->fields[key_idx]->offset = key_offset; 5442 5443 hist_data->key_size += key_size; 5444 5445 if (hist_data->key_size > HIST_KEY_SIZE_MAX) { 5446 ret = -EINVAL; 5447 goto out; 5448 } 5449 5450 hist_data->n_keys++; 5451 hist_data->n_fields++; 5452 5453 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX)) 5454 return -EINVAL; 5455 5456 ret = key_size; 5457 out: 5458 return ret; 5459 } 5460 5461 static int create_key_fields(struct hist_trigger_data *hist_data, 5462 struct trace_event_file *file) 5463 { 5464 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals; 5465 char *fields_str, *field_str; 5466 int ret = -EINVAL; 5467 5468 fields_str = hist_data->attrs->keys_str; 5469 if (!fields_str) 5470 goto out; 5471 5472 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) { 5473 field_str = strsep(&fields_str, ","); 5474 if (!field_str) 5475 break; 5476 ret = create_key_field(hist_data, i, key_offset, 5477 file, field_str); 5478 if (ret < 0) 5479 goto out; 5480 key_offset += ret; 5481 } 5482 if (fields_str) { 5483 ret = -EINVAL; 5484 goto out; 5485 } 5486 ret = 0; 5487 out: 5488 return ret; 5489 } 5490 5491 static int create_var_fields(struct hist_trigger_data *hist_data, 5492 struct trace_event_file *file) 5493 { 5494 unsigned int i, j = hist_data->n_vals; 5495 int ret = 0; 5496 5497 unsigned int n_vars = hist_data->attrs->var_defs.n_vars; 5498 5499 for (i = 0; i < n_vars; i++) { 5500 char *var_name = hist_data->attrs->var_defs.name[i]; 5501 char *expr = hist_data->attrs->var_defs.expr[i]; 5502 5503 ret = create_var_field(hist_data, j++, file, var_name, expr); 5504 if (ret) 5505 goto out; 5506 } 5507 out: 5508 return ret; 5509 } 5510 5511 static void free_var_defs(struct hist_trigger_data *hist_data) 5512 { 5513 unsigned int i; 5514 5515 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) { 5516 kfree(hist_data->attrs->var_defs.name[i]); 5517 kfree(hist_data->attrs->var_defs.expr[i]); 5518 } 5519 5520 hist_data->attrs->var_defs.n_vars = 0; 5521 } 5522 5523 static int parse_var_defs(struct hist_trigger_data *hist_data) 5524 { 5525 struct trace_array *tr = hist_data->event_file->tr; 5526 char *s, *str, *var_name, *field_str; 5527 unsigned int i, j, n_vars = 0; 5528 int ret = 0; 5529 5530 for (i = 0; i < hist_data->attrs->n_assignments; i++) { 5531 str = hist_data->attrs->assignment_str[i]; 5532 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) { 5533 field_str = strsep(&str, ","); 5534 if (!field_str) 5535 break; 5536 5537 var_name = strsep(&field_str, "="); 5538 if (!var_name || !field_str) { 5539 hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT, 5540 errpos(var_name)); 5541 ret = -EINVAL; 5542 goto free; 5543 } 5544 5545 if (n_vars == TRACING_MAP_VARS_MAX) { 5546 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name)); 5547 ret = -EINVAL; 5548 goto free; 5549 } 5550 5551 s = kstrdup(var_name, GFP_KERNEL); 5552 if (!s) { 5553 ret = -ENOMEM; 5554 goto free; 5555 } 5556 hist_data->attrs->var_defs.name[n_vars] = s; 5557 5558 s = kstrdup(field_str, GFP_KERNEL); 5559 if (!s) { 5560 kfree(hist_data->attrs->var_defs.name[n_vars]); 5561 ret = -ENOMEM; 5562 goto free; 5563 } 5564 hist_data->attrs->var_defs.expr[n_vars++] = s; 5565 5566 hist_data->attrs->var_defs.n_vars = n_vars; 5567 } 5568 } 5569 5570 return ret; 5571 free: 5572 free_var_defs(hist_data); 5573 5574 return ret; 5575 } 5576 5577 static int create_hist_fields(struct hist_trigger_data *hist_data, 5578 struct trace_event_file *file) 5579 { 5580 int ret; 5581 5582 ret = parse_var_defs(hist_data); 5583 if (ret) 5584 goto out; 5585 5586 ret = create_val_fields(hist_data, file); 5587 if (ret) 5588 goto out; 5589 5590 ret = create_var_fields(hist_data, file); 5591 if (ret) 5592 goto out; 5593 5594 ret = create_key_fields(hist_data, file); 5595 if (ret) 5596 goto out; 5597 out: 5598 free_var_defs(hist_data); 5599 5600 return ret; 5601 } 5602 5603 static int is_descending(struct trace_array *tr, const char *str) 5604 { 5605 if (!str) 5606 return 0; 5607 5608 if (strcmp(str, "descending") == 0) 5609 return 1; 5610 5611 if (strcmp(str, "ascending") == 0) 5612 return 0; 5613 5614 hist_err(tr, HIST_ERR_INVALID_SORT_MODIFIER, errpos((char *)str)); 5615 5616 return -EINVAL; 5617 } 5618 5619 static int create_sort_keys(struct hist_trigger_data *hist_data) 5620 { 5621 struct trace_array *tr = hist_data->event_file->tr; 5622 char *fields_str = hist_data->attrs->sort_key_str; 5623 struct tracing_map_sort_key *sort_key; 5624 int descending, ret = 0; 5625 unsigned int i, j, k; 5626 5627 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */ 5628 5629 if (!fields_str) 5630 goto out; 5631 5632 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) { 5633 struct hist_field *hist_field; 5634 char *field_str, *field_name; 5635 const char *test_name; 5636 5637 sort_key = &hist_data->sort_keys[i]; 5638 5639 field_str = strsep(&fields_str, ","); 5640 if (!field_str) 5641 break; 5642 5643 if (!*field_str) { 5644 ret = -EINVAL; 5645 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort=")); 5646 break; 5647 } 5648 5649 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) { 5650 hist_err(tr, HIST_ERR_TOO_MANY_SORT_FIELDS, errpos("sort=")); 5651 ret = -EINVAL; 5652 break; 5653 } 5654 5655 field_name = strsep(&field_str, "."); 5656 if (!field_name || !*field_name) { 5657 ret = -EINVAL; 5658 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort=")); 5659 break; 5660 } 5661 5662 if (strcmp(field_name, "hitcount") == 0) { 5663 descending = is_descending(tr, field_str); 5664 if (descending < 0) { 5665 ret = descending; 5666 break; 5667 } 5668 sort_key->descending = descending; 5669 continue; 5670 } 5671 5672 for (j = 1, k = 1; j < hist_data->n_fields; j++) { 5673 unsigned int idx; 5674 5675 hist_field = hist_data->fields[j]; 5676 if (hist_field->flags & HIST_FIELD_FL_VAR) 5677 continue; 5678 5679 idx = k++; 5680 5681 test_name = hist_field_name(hist_field, 0); 5682 5683 if (strcmp(field_name, test_name) == 0) { 5684 sort_key->field_idx = idx; 5685 descending = is_descending(tr, field_str); 5686 if (descending < 0) { 5687 ret = descending; 5688 goto out; 5689 } 5690 sort_key->descending = descending; 5691 break; 5692 } 5693 } 5694 if (j == hist_data->n_fields) { 5695 ret = -EINVAL; 5696 hist_err(tr, HIST_ERR_INVALID_SORT_FIELD, errpos(field_name)); 5697 break; 5698 } 5699 } 5700 5701 hist_data->n_sort_keys = i; 5702 out: 5703 return ret; 5704 } 5705 5706 static void destroy_actions(struct hist_trigger_data *hist_data) 5707 { 5708 unsigned int i; 5709 5710 for (i = 0; i < hist_data->n_actions; i++) { 5711 struct action_data *data = hist_data->actions[i]; 5712 5713 if (data->handler == HANDLER_ONMATCH) 5714 onmatch_destroy(data); 5715 else if (data->handler == HANDLER_ONMAX || 5716 data->handler == HANDLER_ONCHANGE) 5717 track_data_destroy(hist_data, data); 5718 else 5719 kfree(data); 5720 } 5721 } 5722 5723 static int parse_actions(struct hist_trigger_data *hist_data) 5724 { 5725 struct trace_array *tr = hist_data->event_file->tr; 5726 struct action_data *data; 5727 unsigned int i; 5728 int ret = 0; 5729 char *str; 5730 int len; 5731 5732 for (i = 0; i < hist_data->attrs->n_actions; i++) { 5733 str = hist_data->attrs->action_str[i]; 5734 5735 if ((len = str_has_prefix(str, "onmatch("))) { 5736 char *action_str = str + len; 5737 5738 data = onmatch_parse(tr, action_str); 5739 if (IS_ERR(data)) { 5740 ret = PTR_ERR(data); 5741 break; 5742 } 5743 } else if ((len = str_has_prefix(str, "onmax("))) { 5744 char *action_str = str + len; 5745 5746 data = track_data_parse(hist_data, action_str, 5747 HANDLER_ONMAX); 5748 if (IS_ERR(data)) { 5749 ret = PTR_ERR(data); 5750 break; 5751 } 5752 } else if ((len = str_has_prefix(str, "onchange("))) { 5753 char *action_str = str + len; 5754 5755 data = track_data_parse(hist_data, action_str, 5756 HANDLER_ONCHANGE); 5757 if (IS_ERR(data)) { 5758 ret = PTR_ERR(data); 5759 break; 5760 } 5761 } else { 5762 ret = -EINVAL; 5763 break; 5764 } 5765 5766 hist_data->actions[hist_data->n_actions++] = data; 5767 } 5768 5769 return ret; 5770 } 5771 5772 static int create_actions(struct hist_trigger_data *hist_data) 5773 { 5774 struct action_data *data; 5775 unsigned int i; 5776 int ret = 0; 5777 5778 for (i = 0; i < hist_data->attrs->n_actions; i++) { 5779 data = hist_data->actions[i]; 5780 5781 if (data->handler == HANDLER_ONMATCH) { 5782 ret = onmatch_create(hist_data, data); 5783 if (ret) 5784 break; 5785 } else if (data->handler == HANDLER_ONMAX || 5786 data->handler == HANDLER_ONCHANGE) { 5787 ret = track_data_create(hist_data, data); 5788 if (ret) 5789 break; 5790 } else { 5791 ret = -EINVAL; 5792 break; 5793 } 5794 } 5795 5796 return ret; 5797 } 5798 5799 static void print_actions(struct seq_file *m, 5800 struct hist_trigger_data *hist_data, 5801 struct tracing_map_elt *elt) 5802 { 5803 unsigned int i; 5804 5805 for (i = 0; i < hist_data->n_actions; i++) { 5806 struct action_data *data = hist_data->actions[i]; 5807 5808 if (data->action == ACTION_SNAPSHOT) 5809 continue; 5810 5811 if (data->handler == HANDLER_ONMAX || 5812 data->handler == HANDLER_ONCHANGE) 5813 track_data_print(m, hist_data, elt, data); 5814 } 5815 } 5816 5817 static void print_action_spec(struct seq_file *m, 5818 struct hist_trigger_data *hist_data, 5819 struct action_data *data) 5820 { 5821 unsigned int i; 5822 5823 if (data->action == ACTION_SAVE) { 5824 for (i = 0; i < hist_data->n_save_vars; i++) { 5825 seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name); 5826 if (i < hist_data->n_save_vars - 1) 5827 seq_puts(m, ","); 5828 } 5829 } else if (data->action == ACTION_TRACE) { 5830 if (data->use_trace_keyword) 5831 seq_printf(m, "%s", data->synth_event_name); 5832 for (i = 0; i < data->n_params; i++) { 5833 if (i || data->use_trace_keyword) 5834 seq_puts(m, ","); 5835 seq_printf(m, "%s", data->params[i]); 5836 } 5837 } 5838 } 5839 5840 static void print_track_data_spec(struct seq_file *m, 5841 struct hist_trigger_data *hist_data, 5842 struct action_data *data) 5843 { 5844 if (data->handler == HANDLER_ONMAX) 5845 seq_puts(m, ":onmax("); 5846 else if (data->handler == HANDLER_ONCHANGE) 5847 seq_puts(m, ":onchange("); 5848 seq_printf(m, "%s", data->track_data.var_str); 5849 seq_printf(m, ").%s(", data->action_name); 5850 5851 print_action_spec(m, hist_data, data); 5852 5853 seq_puts(m, ")"); 5854 } 5855 5856 static void print_onmatch_spec(struct seq_file *m, 5857 struct hist_trigger_data *hist_data, 5858 struct action_data *data) 5859 { 5860 seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system, 5861 data->match_data.event); 5862 5863 seq_printf(m, "%s(", data->action_name); 5864 5865 print_action_spec(m, hist_data, data); 5866 5867 seq_puts(m, ")"); 5868 } 5869 5870 static bool actions_match(struct hist_trigger_data *hist_data, 5871 struct hist_trigger_data *hist_data_test) 5872 { 5873 unsigned int i, j; 5874 5875 if (hist_data->n_actions != hist_data_test->n_actions) 5876 return false; 5877 5878 for (i = 0; i < hist_data->n_actions; i++) { 5879 struct action_data *data = hist_data->actions[i]; 5880 struct action_data *data_test = hist_data_test->actions[i]; 5881 char *action_name, *action_name_test; 5882 5883 if (data->handler != data_test->handler) 5884 return false; 5885 if (data->action != data_test->action) 5886 return false; 5887 5888 if (data->n_params != data_test->n_params) 5889 return false; 5890 5891 for (j = 0; j < data->n_params; j++) { 5892 if (strcmp(data->params[j], data_test->params[j]) != 0) 5893 return false; 5894 } 5895 5896 if (data->use_trace_keyword) 5897 action_name = data->synth_event_name; 5898 else 5899 action_name = data->action_name; 5900 5901 if (data_test->use_trace_keyword) 5902 action_name_test = data_test->synth_event_name; 5903 else 5904 action_name_test = data_test->action_name; 5905 5906 if (strcmp(action_name, action_name_test) != 0) 5907 return false; 5908 5909 if (data->handler == HANDLER_ONMATCH) { 5910 if (strcmp(data->match_data.event_system, 5911 data_test->match_data.event_system) != 0) 5912 return false; 5913 if (strcmp(data->match_data.event, 5914 data_test->match_data.event) != 0) 5915 return false; 5916 } else if (data->handler == HANDLER_ONMAX || 5917 data->handler == HANDLER_ONCHANGE) { 5918 if (strcmp(data->track_data.var_str, 5919 data_test->track_data.var_str) != 0) 5920 return false; 5921 } 5922 } 5923 5924 return true; 5925 } 5926 5927 5928 static void print_actions_spec(struct seq_file *m, 5929 struct hist_trigger_data *hist_data) 5930 { 5931 unsigned int i; 5932 5933 for (i = 0; i < hist_data->n_actions; i++) { 5934 struct action_data *data = hist_data->actions[i]; 5935 5936 if (data->handler == HANDLER_ONMATCH) 5937 print_onmatch_spec(m, hist_data, data); 5938 else if (data->handler == HANDLER_ONMAX || 5939 data->handler == HANDLER_ONCHANGE) 5940 print_track_data_spec(m, hist_data, data); 5941 } 5942 } 5943 5944 static void destroy_field_var_hists(struct hist_trigger_data *hist_data) 5945 { 5946 unsigned int i; 5947 5948 for (i = 0; i < hist_data->n_field_var_hists; i++) { 5949 kfree(hist_data->field_var_hists[i]->cmd); 5950 kfree(hist_data->field_var_hists[i]); 5951 } 5952 } 5953 5954 static void destroy_hist_data(struct hist_trigger_data *hist_data) 5955 { 5956 if (!hist_data) 5957 return; 5958 5959 destroy_hist_trigger_attrs(hist_data->attrs); 5960 destroy_hist_fields(hist_data); 5961 tracing_map_destroy(hist_data->map); 5962 5963 destroy_actions(hist_data); 5964 destroy_field_vars(hist_data); 5965 destroy_field_var_hists(hist_data); 5966 5967 kfree(hist_data); 5968 } 5969 5970 static int create_tracing_map_fields(struct hist_trigger_data *hist_data) 5971 { 5972 struct tracing_map *map = hist_data->map; 5973 struct ftrace_event_field *field; 5974 struct hist_field *hist_field; 5975 int i, idx = 0; 5976 5977 for_each_hist_field(i, hist_data) { 5978 hist_field = hist_data->fields[i]; 5979 if (hist_field->flags & HIST_FIELD_FL_KEY) { 5980 tracing_map_cmp_fn_t cmp_fn; 5981 5982 field = hist_field->field; 5983 5984 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE) 5985 cmp_fn = tracing_map_cmp_none; 5986 else if (!field) 5987 cmp_fn = tracing_map_cmp_num(hist_field->size, 5988 hist_field->is_signed); 5989 else if (is_string_field(field)) 5990 cmp_fn = tracing_map_cmp_string; 5991 else 5992 cmp_fn = tracing_map_cmp_num(field->size, 5993 field->is_signed); 5994 idx = tracing_map_add_key_field(map, 5995 hist_field->offset, 5996 cmp_fn); 5997 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR)) 5998 idx = tracing_map_add_sum_field(map); 5999 6000 if (idx < 0) 6001 return idx; 6002 6003 if (hist_field->flags & HIST_FIELD_FL_VAR) { 6004 idx = tracing_map_add_var(map); 6005 if (idx < 0) 6006 return idx; 6007 hist_field->var.idx = idx; 6008 hist_field->var.hist_data = hist_data; 6009 } 6010 } 6011 6012 return 0; 6013 } 6014 6015 static struct hist_trigger_data * 6016 create_hist_data(unsigned int map_bits, 6017 struct hist_trigger_attrs *attrs, 6018 struct trace_event_file *file, 6019 bool remove) 6020 { 6021 const struct tracing_map_ops *map_ops = NULL; 6022 struct hist_trigger_data *hist_data; 6023 int ret = 0; 6024 6025 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL); 6026 if (!hist_data) 6027 return ERR_PTR(-ENOMEM); 6028 6029 hist_data->attrs = attrs; 6030 hist_data->remove = remove; 6031 hist_data->event_file = file; 6032 6033 ret = parse_actions(hist_data); 6034 if (ret) 6035 goto free; 6036 6037 ret = create_hist_fields(hist_data, file); 6038 if (ret) 6039 goto free; 6040 6041 ret = create_sort_keys(hist_data); 6042 if (ret) 6043 goto free; 6044 6045 map_ops = &hist_trigger_elt_data_ops; 6046 6047 hist_data->map = tracing_map_create(map_bits, hist_data->key_size, 6048 map_ops, hist_data); 6049 if (IS_ERR(hist_data->map)) { 6050 ret = PTR_ERR(hist_data->map); 6051 hist_data->map = NULL; 6052 goto free; 6053 } 6054 6055 ret = create_tracing_map_fields(hist_data); 6056 if (ret) 6057 goto free; 6058 out: 6059 return hist_data; 6060 free: 6061 hist_data->attrs = NULL; 6062 6063 destroy_hist_data(hist_data); 6064 6065 hist_data = ERR_PTR(ret); 6066 6067 goto out; 6068 } 6069 6070 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data, 6071 struct tracing_map_elt *elt, void *rec, 6072 struct ring_buffer_event *rbe, 6073 u64 *var_ref_vals) 6074 { 6075 struct hist_elt_data *elt_data; 6076 struct hist_field *hist_field; 6077 unsigned int i, var_idx; 6078 u64 hist_val; 6079 6080 elt_data = elt->private_data; 6081 elt_data->var_ref_vals = var_ref_vals; 6082 6083 for_each_hist_val_field(i, hist_data) { 6084 hist_field = hist_data->fields[i]; 6085 hist_val = hist_field->fn(hist_field, elt, rbe, rec); 6086 if (hist_field->flags & HIST_FIELD_FL_VAR) { 6087 var_idx = hist_field->var.idx; 6088 tracing_map_set_var(elt, var_idx, hist_val); 6089 continue; 6090 } 6091 tracing_map_update_sum(elt, i, hist_val); 6092 } 6093 6094 for_each_hist_key_field(i, hist_data) { 6095 hist_field = hist_data->fields[i]; 6096 if (hist_field->flags & HIST_FIELD_FL_VAR) { 6097 hist_val = hist_field->fn(hist_field, elt, rbe, rec); 6098 var_idx = hist_field->var.idx; 6099 tracing_map_set_var(elt, var_idx, hist_val); 6100 } 6101 } 6102 6103 update_field_vars(hist_data, elt, rbe, rec); 6104 } 6105 6106 static inline void add_to_key(char *compound_key, void *key, 6107 struct hist_field *key_field, void *rec) 6108 { 6109 size_t size = key_field->size; 6110 6111 if (key_field->flags & HIST_FIELD_FL_STRING) { 6112 struct ftrace_event_field *field; 6113 6114 field = key_field->field; 6115 if (field->filter_type == FILTER_DYN_STRING) 6116 size = *(u32 *)(rec + field->offset) >> 16; 6117 else if (field->filter_type == FILTER_PTR_STRING) 6118 size = strlen(key); 6119 else if (field->filter_type == FILTER_STATIC_STRING) 6120 size = field->size; 6121 6122 /* ensure NULL-termination */ 6123 if (size > key_field->size - 1) 6124 size = key_field->size - 1; 6125 6126 strncpy(compound_key + key_field->offset, (char *)key, size); 6127 } else 6128 memcpy(compound_key + key_field->offset, key, size); 6129 } 6130 6131 static void 6132 hist_trigger_actions(struct hist_trigger_data *hist_data, 6133 struct tracing_map_elt *elt, void *rec, 6134 struct ring_buffer_event *rbe, void *key, 6135 u64 *var_ref_vals) 6136 { 6137 struct action_data *data; 6138 unsigned int i; 6139 6140 for (i = 0; i < hist_data->n_actions; i++) { 6141 data = hist_data->actions[i]; 6142 data->fn(hist_data, elt, rec, rbe, key, data, var_ref_vals); 6143 } 6144 } 6145 6146 static void event_hist_trigger(struct event_trigger_data *data, void *rec, 6147 struct ring_buffer_event *rbe) 6148 { 6149 struct hist_trigger_data *hist_data = data->private_data; 6150 bool use_compound_key = (hist_data->n_keys > 1); 6151 unsigned long entries[HIST_STACKTRACE_DEPTH]; 6152 u64 var_ref_vals[TRACING_MAP_VARS_MAX]; 6153 char compound_key[HIST_KEY_SIZE_MAX]; 6154 struct tracing_map_elt *elt = NULL; 6155 struct hist_field *key_field; 6156 u64 field_contents; 6157 void *key = NULL; 6158 unsigned int i; 6159 6160 memset(compound_key, 0, hist_data->key_size); 6161 6162 for_each_hist_key_field(i, hist_data) { 6163 key_field = hist_data->fields[i]; 6164 6165 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) { 6166 memset(entries, 0, HIST_STACKTRACE_SIZE); 6167 stack_trace_save(entries, HIST_STACKTRACE_DEPTH, 6168 HIST_STACKTRACE_SKIP); 6169 key = entries; 6170 } else { 6171 field_contents = key_field->fn(key_field, elt, rbe, rec); 6172 if (key_field->flags & HIST_FIELD_FL_STRING) { 6173 key = (void *)(unsigned long)field_contents; 6174 use_compound_key = true; 6175 } else 6176 key = (void *)&field_contents; 6177 } 6178 6179 if (use_compound_key) 6180 add_to_key(compound_key, key, key_field, rec); 6181 } 6182 6183 if (use_compound_key) 6184 key = compound_key; 6185 6186 if (hist_data->n_var_refs && 6187 !resolve_var_refs(hist_data, key, var_ref_vals, false)) 6188 return; 6189 6190 elt = tracing_map_insert(hist_data->map, key); 6191 if (!elt) 6192 return; 6193 6194 hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals); 6195 6196 if (resolve_var_refs(hist_data, key, var_ref_vals, true)) 6197 hist_trigger_actions(hist_data, elt, rec, rbe, key, var_ref_vals); 6198 } 6199 6200 static void hist_trigger_stacktrace_print(struct seq_file *m, 6201 unsigned long *stacktrace_entries, 6202 unsigned int max_entries) 6203 { 6204 char str[KSYM_SYMBOL_LEN]; 6205 unsigned int spaces = 8; 6206 unsigned int i; 6207 6208 for (i = 0; i < max_entries; i++) { 6209 if (!stacktrace_entries[i]) 6210 return; 6211 6212 seq_printf(m, "%*c", 1 + spaces, ' '); 6213 sprint_symbol(str, stacktrace_entries[i]); 6214 seq_printf(m, "%s\n", str); 6215 } 6216 } 6217 6218 static void hist_trigger_print_key(struct seq_file *m, 6219 struct hist_trigger_data *hist_data, 6220 void *key, 6221 struct tracing_map_elt *elt) 6222 { 6223 struct hist_field *key_field; 6224 char str[KSYM_SYMBOL_LEN]; 6225 bool multiline = false; 6226 const char *field_name; 6227 unsigned int i; 6228 u64 uval; 6229 6230 seq_puts(m, "{ "); 6231 6232 for_each_hist_key_field(i, hist_data) { 6233 key_field = hist_data->fields[i]; 6234 6235 if (i > hist_data->n_vals) 6236 seq_puts(m, ", "); 6237 6238 field_name = hist_field_name(key_field, 0); 6239 6240 if (key_field->flags & HIST_FIELD_FL_HEX) { 6241 uval = *(u64 *)(key + key_field->offset); 6242 seq_printf(m, "%s: %llx", field_name, uval); 6243 } else if (key_field->flags & HIST_FIELD_FL_SYM) { 6244 uval = *(u64 *)(key + key_field->offset); 6245 sprint_symbol_no_offset(str, uval); 6246 seq_printf(m, "%s: [%llx] %-45s", field_name, 6247 uval, str); 6248 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) { 6249 uval = *(u64 *)(key + key_field->offset); 6250 sprint_symbol(str, uval); 6251 seq_printf(m, "%s: [%llx] %-55s", field_name, 6252 uval, str); 6253 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) { 6254 struct hist_elt_data *elt_data = elt->private_data; 6255 char *comm; 6256 6257 if (WARN_ON_ONCE(!elt_data)) 6258 return; 6259 6260 comm = elt_data->comm; 6261 6262 uval = *(u64 *)(key + key_field->offset); 6263 seq_printf(m, "%s: %-16s[%10llu]", field_name, 6264 comm, uval); 6265 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) { 6266 const char *syscall_name; 6267 6268 uval = *(u64 *)(key + key_field->offset); 6269 syscall_name = get_syscall_name(uval); 6270 if (!syscall_name) 6271 syscall_name = "unknown_syscall"; 6272 6273 seq_printf(m, "%s: %-30s[%3llu]", field_name, 6274 syscall_name, uval); 6275 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) { 6276 seq_puts(m, "stacktrace:\n"); 6277 hist_trigger_stacktrace_print(m, 6278 key + key_field->offset, 6279 HIST_STACKTRACE_DEPTH); 6280 multiline = true; 6281 } else if (key_field->flags & HIST_FIELD_FL_LOG2) { 6282 seq_printf(m, "%s: ~ 2^%-2llu", field_name, 6283 *(u64 *)(key + key_field->offset)); 6284 } else if (key_field->flags & HIST_FIELD_FL_STRING) { 6285 seq_printf(m, "%s: %-50s", field_name, 6286 (char *)(key + key_field->offset)); 6287 } else { 6288 uval = *(u64 *)(key + key_field->offset); 6289 seq_printf(m, "%s: %10llu", field_name, uval); 6290 } 6291 } 6292 6293 if (!multiline) 6294 seq_puts(m, " "); 6295 6296 seq_puts(m, "}"); 6297 } 6298 6299 static void hist_trigger_entry_print(struct seq_file *m, 6300 struct hist_trigger_data *hist_data, 6301 void *key, 6302 struct tracing_map_elt *elt) 6303 { 6304 const char *field_name; 6305 unsigned int i; 6306 6307 hist_trigger_print_key(m, hist_data, key, elt); 6308 6309 seq_printf(m, " hitcount: %10llu", 6310 tracing_map_read_sum(elt, HITCOUNT_IDX)); 6311 6312 for (i = 1; i < hist_data->n_vals; i++) { 6313 field_name = hist_field_name(hist_data->fields[i], 0); 6314 6315 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR || 6316 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR) 6317 continue; 6318 6319 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) { 6320 seq_printf(m, " %s: %10llx", field_name, 6321 tracing_map_read_sum(elt, i)); 6322 } else { 6323 seq_printf(m, " %s: %10llu", field_name, 6324 tracing_map_read_sum(elt, i)); 6325 } 6326 } 6327 6328 print_actions(m, hist_data, elt); 6329 6330 seq_puts(m, "\n"); 6331 } 6332 6333 static int print_entries(struct seq_file *m, 6334 struct hist_trigger_data *hist_data) 6335 { 6336 struct tracing_map_sort_entry **sort_entries = NULL; 6337 struct tracing_map *map = hist_data->map; 6338 int i, n_entries; 6339 6340 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys, 6341 hist_data->n_sort_keys, 6342 &sort_entries); 6343 if (n_entries < 0) 6344 return n_entries; 6345 6346 for (i = 0; i < n_entries; i++) 6347 hist_trigger_entry_print(m, hist_data, 6348 sort_entries[i]->key, 6349 sort_entries[i]->elt); 6350 6351 tracing_map_destroy_sort_entries(sort_entries, n_entries); 6352 6353 return n_entries; 6354 } 6355 6356 static void hist_trigger_show(struct seq_file *m, 6357 struct event_trigger_data *data, int n) 6358 { 6359 struct hist_trigger_data *hist_data; 6360 int n_entries; 6361 6362 if (n > 0) 6363 seq_puts(m, "\n\n"); 6364 6365 seq_puts(m, "# event histogram\n#\n# trigger info: "); 6366 data->ops->print(m, data->ops, data); 6367 seq_puts(m, "#\n\n"); 6368 6369 hist_data = data->private_data; 6370 n_entries = print_entries(m, hist_data); 6371 if (n_entries < 0) 6372 n_entries = 0; 6373 6374 track_data_snapshot_print(m, hist_data); 6375 6376 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n", 6377 (u64)atomic64_read(&hist_data->map->hits), 6378 n_entries, (u64)atomic64_read(&hist_data->map->drops)); 6379 } 6380 6381 static int hist_show(struct seq_file *m, void *v) 6382 { 6383 struct event_trigger_data *data; 6384 struct trace_event_file *event_file; 6385 int n = 0, ret = 0; 6386 6387 mutex_lock(&event_mutex); 6388 6389 event_file = event_file_data(m->private); 6390 if (unlikely(!event_file)) { 6391 ret = -ENODEV; 6392 goto out_unlock; 6393 } 6394 6395 list_for_each_entry(data, &event_file->triggers, list) { 6396 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST) 6397 hist_trigger_show(m, data, n++); 6398 } 6399 6400 out_unlock: 6401 mutex_unlock(&event_mutex); 6402 6403 return ret; 6404 } 6405 6406 static int event_hist_open(struct inode *inode, struct file *file) 6407 { 6408 int ret; 6409 6410 ret = security_locked_down(LOCKDOWN_TRACEFS); 6411 if (ret) 6412 return ret; 6413 6414 return single_open(file, hist_show, file); 6415 } 6416 6417 const struct file_operations event_hist_fops = { 6418 .open = event_hist_open, 6419 .read = seq_read, 6420 .llseek = seq_lseek, 6421 .release = single_release, 6422 }; 6423 6424 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field) 6425 { 6426 const char *field_name = hist_field_name(hist_field, 0); 6427 6428 if (hist_field->var.name) 6429 seq_printf(m, "%s=", hist_field->var.name); 6430 6431 if (hist_field->flags & HIST_FIELD_FL_CPU) 6432 seq_puts(m, "cpu"); 6433 else if (field_name) { 6434 if (hist_field->flags & HIST_FIELD_FL_VAR_REF || 6435 hist_field->flags & HIST_FIELD_FL_ALIAS) 6436 seq_putc(m, '$'); 6437 seq_printf(m, "%s", field_name); 6438 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP) 6439 seq_puts(m, "common_timestamp"); 6440 6441 if (hist_field->flags) { 6442 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) && 6443 !(hist_field->flags & HIST_FIELD_FL_EXPR)) { 6444 const char *flags = get_hist_field_flags(hist_field); 6445 6446 if (flags) 6447 seq_printf(m, ".%s", flags); 6448 } 6449 } 6450 } 6451 6452 static int event_hist_trigger_print(struct seq_file *m, 6453 struct event_trigger_ops *ops, 6454 struct event_trigger_data *data) 6455 { 6456 struct hist_trigger_data *hist_data = data->private_data; 6457 struct hist_field *field; 6458 bool have_var = false; 6459 unsigned int i; 6460 6461 seq_puts(m, "hist:"); 6462 6463 if (data->name) 6464 seq_printf(m, "%s:", data->name); 6465 6466 seq_puts(m, "keys="); 6467 6468 for_each_hist_key_field(i, hist_data) { 6469 field = hist_data->fields[i]; 6470 6471 if (i > hist_data->n_vals) 6472 seq_puts(m, ","); 6473 6474 if (field->flags & HIST_FIELD_FL_STACKTRACE) 6475 seq_puts(m, "stacktrace"); 6476 else 6477 hist_field_print(m, field); 6478 } 6479 6480 seq_puts(m, ":vals="); 6481 6482 for_each_hist_val_field(i, hist_data) { 6483 field = hist_data->fields[i]; 6484 if (field->flags & HIST_FIELD_FL_VAR) { 6485 have_var = true; 6486 continue; 6487 } 6488 6489 if (i == HITCOUNT_IDX) 6490 seq_puts(m, "hitcount"); 6491 else { 6492 seq_puts(m, ","); 6493 hist_field_print(m, field); 6494 } 6495 } 6496 6497 if (have_var) { 6498 unsigned int n = 0; 6499 6500 seq_puts(m, ":"); 6501 6502 for_each_hist_val_field(i, hist_data) { 6503 field = hist_data->fields[i]; 6504 6505 if (field->flags & HIST_FIELD_FL_VAR) { 6506 if (n++) 6507 seq_puts(m, ","); 6508 hist_field_print(m, field); 6509 } 6510 } 6511 } 6512 6513 seq_puts(m, ":sort="); 6514 6515 for (i = 0; i < hist_data->n_sort_keys; i++) { 6516 struct tracing_map_sort_key *sort_key; 6517 unsigned int idx, first_key_idx; 6518 6519 /* skip VAR vals */ 6520 first_key_idx = hist_data->n_vals - hist_data->n_vars; 6521 6522 sort_key = &hist_data->sort_keys[i]; 6523 idx = sort_key->field_idx; 6524 6525 if (WARN_ON(idx >= HIST_FIELDS_MAX)) 6526 return -EINVAL; 6527 6528 if (i > 0) 6529 seq_puts(m, ","); 6530 6531 if (idx == HITCOUNT_IDX) 6532 seq_puts(m, "hitcount"); 6533 else { 6534 if (idx >= first_key_idx) 6535 idx += hist_data->n_vars; 6536 hist_field_print(m, hist_data->fields[idx]); 6537 } 6538 6539 if (sort_key->descending) 6540 seq_puts(m, ".descending"); 6541 } 6542 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits)); 6543 if (hist_data->enable_timestamps) 6544 seq_printf(m, ":clock=%s", hist_data->attrs->clock); 6545 6546 print_actions_spec(m, hist_data); 6547 6548 if (data->filter_str) 6549 seq_printf(m, " if %s", data->filter_str); 6550 6551 if (data->paused) 6552 seq_puts(m, " [paused]"); 6553 else 6554 seq_puts(m, " [active]"); 6555 6556 seq_putc(m, '\n'); 6557 6558 return 0; 6559 } 6560 6561 static int event_hist_trigger_init(struct event_trigger_ops *ops, 6562 struct event_trigger_data *data) 6563 { 6564 struct hist_trigger_data *hist_data = data->private_data; 6565 6566 if (!data->ref && hist_data->attrs->name) 6567 save_named_trigger(hist_data->attrs->name, data); 6568 6569 data->ref++; 6570 6571 return 0; 6572 } 6573 6574 static void unregister_field_var_hists(struct hist_trigger_data *hist_data) 6575 { 6576 struct trace_event_file *file; 6577 unsigned int i; 6578 char *cmd; 6579 int ret; 6580 6581 for (i = 0; i < hist_data->n_field_var_hists; i++) { 6582 file = hist_data->field_var_hists[i]->hist_data->event_file; 6583 cmd = hist_data->field_var_hists[i]->cmd; 6584 ret = event_hist_trigger_func(&trigger_hist_cmd, file, 6585 "!hist", "hist", cmd); 6586 } 6587 } 6588 6589 static void event_hist_trigger_free(struct event_trigger_ops *ops, 6590 struct event_trigger_data *data) 6591 { 6592 struct hist_trigger_data *hist_data = data->private_data; 6593 6594 if (WARN_ON_ONCE(data->ref <= 0)) 6595 return; 6596 6597 data->ref--; 6598 if (!data->ref) { 6599 if (data->name) 6600 del_named_trigger(data); 6601 6602 trigger_data_free(data); 6603 6604 remove_hist_vars(hist_data); 6605 6606 unregister_field_var_hists(hist_data); 6607 6608 destroy_hist_data(hist_data); 6609 } 6610 } 6611 6612 static struct event_trigger_ops event_hist_trigger_ops = { 6613 .func = event_hist_trigger, 6614 .print = event_hist_trigger_print, 6615 .init = event_hist_trigger_init, 6616 .free = event_hist_trigger_free, 6617 }; 6618 6619 static int event_hist_trigger_named_init(struct event_trigger_ops *ops, 6620 struct event_trigger_data *data) 6621 { 6622 data->ref++; 6623 6624 save_named_trigger(data->named_data->name, data); 6625 6626 event_hist_trigger_init(ops, data->named_data); 6627 6628 return 0; 6629 } 6630 6631 static void event_hist_trigger_named_free(struct event_trigger_ops *ops, 6632 struct event_trigger_data *data) 6633 { 6634 if (WARN_ON_ONCE(data->ref <= 0)) 6635 return; 6636 6637 event_hist_trigger_free(ops, data->named_data); 6638 6639 data->ref--; 6640 if (!data->ref) { 6641 del_named_trigger(data); 6642 trigger_data_free(data); 6643 } 6644 } 6645 6646 static struct event_trigger_ops event_hist_trigger_named_ops = { 6647 .func = event_hist_trigger, 6648 .print = event_hist_trigger_print, 6649 .init = event_hist_trigger_named_init, 6650 .free = event_hist_trigger_named_free, 6651 }; 6652 6653 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd, 6654 char *param) 6655 { 6656 return &event_hist_trigger_ops; 6657 } 6658 6659 static void hist_clear(struct event_trigger_data *data) 6660 { 6661 struct hist_trigger_data *hist_data = data->private_data; 6662 6663 if (data->name) 6664 pause_named_trigger(data); 6665 6666 tracepoint_synchronize_unregister(); 6667 6668 tracing_map_clear(hist_data->map); 6669 6670 if (data->name) 6671 unpause_named_trigger(data); 6672 } 6673 6674 static bool compatible_field(struct ftrace_event_field *field, 6675 struct ftrace_event_field *test_field) 6676 { 6677 if (field == test_field) 6678 return true; 6679 if (field == NULL || test_field == NULL) 6680 return false; 6681 if (strcmp(field->name, test_field->name) != 0) 6682 return false; 6683 if (strcmp(field->type, test_field->type) != 0) 6684 return false; 6685 if (field->size != test_field->size) 6686 return false; 6687 if (field->is_signed != test_field->is_signed) 6688 return false; 6689 6690 return true; 6691 } 6692 6693 static bool hist_trigger_match(struct event_trigger_data *data, 6694 struct event_trigger_data *data_test, 6695 struct event_trigger_data *named_data, 6696 bool ignore_filter) 6697 { 6698 struct tracing_map_sort_key *sort_key, *sort_key_test; 6699 struct hist_trigger_data *hist_data, *hist_data_test; 6700 struct hist_field *key_field, *key_field_test; 6701 unsigned int i; 6702 6703 if (named_data && (named_data != data_test) && 6704 (named_data != data_test->named_data)) 6705 return false; 6706 6707 if (!named_data && is_named_trigger(data_test)) 6708 return false; 6709 6710 hist_data = data->private_data; 6711 hist_data_test = data_test->private_data; 6712 6713 if (hist_data->n_vals != hist_data_test->n_vals || 6714 hist_data->n_fields != hist_data_test->n_fields || 6715 hist_data->n_sort_keys != hist_data_test->n_sort_keys) 6716 return false; 6717 6718 if (!ignore_filter) { 6719 if ((data->filter_str && !data_test->filter_str) || 6720 (!data->filter_str && data_test->filter_str)) 6721 return false; 6722 } 6723 6724 for_each_hist_field(i, hist_data) { 6725 key_field = hist_data->fields[i]; 6726 key_field_test = hist_data_test->fields[i]; 6727 6728 if (key_field->flags != key_field_test->flags) 6729 return false; 6730 if (!compatible_field(key_field->field, key_field_test->field)) 6731 return false; 6732 if (key_field->offset != key_field_test->offset) 6733 return false; 6734 if (key_field->size != key_field_test->size) 6735 return false; 6736 if (key_field->is_signed != key_field_test->is_signed) 6737 return false; 6738 if (!!key_field->var.name != !!key_field_test->var.name) 6739 return false; 6740 if (key_field->var.name && 6741 strcmp(key_field->var.name, key_field_test->var.name) != 0) 6742 return false; 6743 } 6744 6745 for (i = 0; i < hist_data->n_sort_keys; i++) { 6746 sort_key = &hist_data->sort_keys[i]; 6747 sort_key_test = &hist_data_test->sort_keys[i]; 6748 6749 if (sort_key->field_idx != sort_key_test->field_idx || 6750 sort_key->descending != sort_key_test->descending) 6751 return false; 6752 } 6753 6754 if (!ignore_filter && data->filter_str && 6755 (strcmp(data->filter_str, data_test->filter_str) != 0)) 6756 return false; 6757 6758 if (!actions_match(hist_data, hist_data_test)) 6759 return false; 6760 6761 return true; 6762 } 6763 6764 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops, 6765 struct event_trigger_data *data, 6766 struct trace_event_file *file) 6767 { 6768 struct hist_trigger_data *hist_data = data->private_data; 6769 struct event_trigger_data *test, *named_data = NULL; 6770 struct trace_array *tr = file->tr; 6771 int ret = 0; 6772 6773 if (hist_data->attrs->name) { 6774 named_data = find_named_trigger(hist_data->attrs->name); 6775 if (named_data) { 6776 if (!hist_trigger_match(data, named_data, named_data, 6777 true)) { 6778 hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name)); 6779 ret = -EINVAL; 6780 goto out; 6781 } 6782 } 6783 } 6784 6785 if (hist_data->attrs->name && !named_data) 6786 goto new; 6787 6788 lockdep_assert_held(&event_mutex); 6789 6790 list_for_each_entry(test, &file->triggers, list) { 6791 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 6792 if (!hist_trigger_match(data, test, named_data, false)) 6793 continue; 6794 if (hist_data->attrs->pause) 6795 test->paused = true; 6796 else if (hist_data->attrs->cont) 6797 test->paused = false; 6798 else if (hist_data->attrs->clear) 6799 hist_clear(test); 6800 else { 6801 hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0); 6802 ret = -EEXIST; 6803 } 6804 goto out; 6805 } 6806 } 6807 new: 6808 if (hist_data->attrs->cont || hist_data->attrs->clear) { 6809 hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0); 6810 ret = -ENOENT; 6811 goto out; 6812 } 6813 6814 if (hist_data->attrs->pause) 6815 data->paused = true; 6816 6817 if (named_data) { 6818 data->private_data = named_data->private_data; 6819 set_named_trigger_data(data, named_data); 6820 data->ops = &event_hist_trigger_named_ops; 6821 } 6822 6823 if (data->ops->init) { 6824 ret = data->ops->init(data->ops, data); 6825 if (ret < 0) 6826 goto out; 6827 } 6828 6829 if (hist_data->enable_timestamps) { 6830 char *clock = hist_data->attrs->clock; 6831 6832 ret = tracing_set_clock(file->tr, hist_data->attrs->clock); 6833 if (ret) { 6834 hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock)); 6835 goto out; 6836 } 6837 6838 tracing_set_time_stamp_abs(file->tr, true); 6839 } 6840 6841 if (named_data) 6842 destroy_hist_data(hist_data); 6843 6844 ret++; 6845 out: 6846 return ret; 6847 } 6848 6849 static int hist_trigger_enable(struct event_trigger_data *data, 6850 struct trace_event_file *file) 6851 { 6852 int ret = 0; 6853 6854 list_add_tail_rcu(&data->list, &file->triggers); 6855 6856 update_cond_flag(file); 6857 6858 if (trace_event_trigger_enable_disable(file, 1) < 0) { 6859 list_del_rcu(&data->list); 6860 update_cond_flag(file); 6861 ret--; 6862 } 6863 6864 return ret; 6865 } 6866 6867 static bool have_hist_trigger_match(struct event_trigger_data *data, 6868 struct trace_event_file *file) 6869 { 6870 struct hist_trigger_data *hist_data = data->private_data; 6871 struct event_trigger_data *test, *named_data = NULL; 6872 bool match = false; 6873 6874 lockdep_assert_held(&event_mutex); 6875 6876 if (hist_data->attrs->name) 6877 named_data = find_named_trigger(hist_data->attrs->name); 6878 6879 list_for_each_entry(test, &file->triggers, list) { 6880 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 6881 if (hist_trigger_match(data, test, named_data, false)) { 6882 match = true; 6883 break; 6884 } 6885 } 6886 } 6887 6888 return match; 6889 } 6890 6891 static bool hist_trigger_check_refs(struct event_trigger_data *data, 6892 struct trace_event_file *file) 6893 { 6894 struct hist_trigger_data *hist_data = data->private_data; 6895 struct event_trigger_data *test, *named_data = NULL; 6896 6897 lockdep_assert_held(&event_mutex); 6898 6899 if (hist_data->attrs->name) 6900 named_data = find_named_trigger(hist_data->attrs->name); 6901 6902 list_for_each_entry(test, &file->triggers, list) { 6903 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 6904 if (!hist_trigger_match(data, test, named_data, false)) 6905 continue; 6906 hist_data = test->private_data; 6907 if (check_var_refs(hist_data)) 6908 return true; 6909 break; 6910 } 6911 } 6912 6913 return false; 6914 } 6915 6916 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops, 6917 struct event_trigger_data *data, 6918 struct trace_event_file *file) 6919 { 6920 struct hist_trigger_data *hist_data = data->private_data; 6921 struct event_trigger_data *test, *named_data = NULL; 6922 bool unregistered = false; 6923 6924 lockdep_assert_held(&event_mutex); 6925 6926 if (hist_data->attrs->name) 6927 named_data = find_named_trigger(hist_data->attrs->name); 6928 6929 list_for_each_entry(test, &file->triggers, list) { 6930 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 6931 if (!hist_trigger_match(data, test, named_data, false)) 6932 continue; 6933 unregistered = true; 6934 list_del_rcu(&test->list); 6935 trace_event_trigger_enable_disable(file, 0); 6936 update_cond_flag(file); 6937 break; 6938 } 6939 } 6940 6941 if (unregistered && test->ops->free) 6942 test->ops->free(test->ops, test); 6943 6944 if (hist_data->enable_timestamps) { 6945 if (!hist_data->remove || unregistered) 6946 tracing_set_time_stamp_abs(file->tr, false); 6947 } 6948 } 6949 6950 static bool hist_file_check_refs(struct trace_event_file *file) 6951 { 6952 struct hist_trigger_data *hist_data; 6953 struct event_trigger_data *test; 6954 6955 lockdep_assert_held(&event_mutex); 6956 6957 list_for_each_entry(test, &file->triggers, list) { 6958 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 6959 hist_data = test->private_data; 6960 if (check_var_refs(hist_data)) 6961 return true; 6962 } 6963 } 6964 6965 return false; 6966 } 6967 6968 static void hist_unreg_all(struct trace_event_file *file) 6969 { 6970 struct event_trigger_data *test, *n; 6971 struct hist_trigger_data *hist_data; 6972 struct synth_event *se; 6973 const char *se_name; 6974 6975 lockdep_assert_held(&event_mutex); 6976 6977 if (hist_file_check_refs(file)) 6978 return; 6979 6980 list_for_each_entry_safe(test, n, &file->triggers, list) { 6981 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 6982 hist_data = test->private_data; 6983 list_del_rcu(&test->list); 6984 trace_event_trigger_enable_disable(file, 0); 6985 6986 se_name = trace_event_name(file->event_call); 6987 se = find_synth_event(se_name); 6988 if (se) 6989 se->ref--; 6990 6991 update_cond_flag(file); 6992 if (hist_data->enable_timestamps) 6993 tracing_set_time_stamp_abs(file->tr, false); 6994 if (test->ops->free) 6995 test->ops->free(test->ops, test); 6996 } 6997 } 6998 } 6999 7000 static int event_hist_trigger_func(struct event_command *cmd_ops, 7001 struct trace_event_file *file, 7002 char *glob, char *cmd, char *param) 7003 { 7004 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT; 7005 struct event_trigger_data *trigger_data; 7006 struct hist_trigger_attrs *attrs; 7007 struct event_trigger_ops *trigger_ops; 7008 struct hist_trigger_data *hist_data; 7009 struct synth_event *se; 7010 const char *se_name; 7011 bool remove = false; 7012 char *trigger, *p; 7013 int ret = 0; 7014 7015 lockdep_assert_held(&event_mutex); 7016 7017 if (glob && strlen(glob)) { 7018 hist_err_clear(); 7019 last_cmd_set(file, param); 7020 } 7021 7022 if (!param) 7023 return -EINVAL; 7024 7025 if (glob[0] == '!') 7026 remove = true; 7027 7028 /* 7029 * separate the trigger from the filter (k:v [if filter]) 7030 * allowing for whitespace in the trigger 7031 */ 7032 p = trigger = param; 7033 do { 7034 p = strstr(p, "if"); 7035 if (!p) 7036 break; 7037 if (p == param) 7038 return -EINVAL; 7039 if (*(p - 1) != ' ' && *(p - 1) != '\t') { 7040 p++; 7041 continue; 7042 } 7043 if (p >= param + strlen(param) - (sizeof("if") - 1) - 1) 7044 return -EINVAL; 7045 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') { 7046 p++; 7047 continue; 7048 } 7049 break; 7050 } while (p); 7051 7052 if (!p) 7053 param = NULL; 7054 else { 7055 *(p - 1) = '\0'; 7056 param = strstrip(p); 7057 trigger = strstrip(trigger); 7058 } 7059 7060 attrs = parse_hist_trigger_attrs(file->tr, trigger); 7061 if (IS_ERR(attrs)) 7062 return PTR_ERR(attrs); 7063 7064 if (attrs->map_bits) 7065 hist_trigger_bits = attrs->map_bits; 7066 7067 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove); 7068 if (IS_ERR(hist_data)) { 7069 destroy_hist_trigger_attrs(attrs); 7070 return PTR_ERR(hist_data); 7071 } 7072 7073 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger); 7074 7075 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL); 7076 if (!trigger_data) { 7077 ret = -ENOMEM; 7078 goto out_free; 7079 } 7080 7081 trigger_data->count = -1; 7082 trigger_data->ops = trigger_ops; 7083 trigger_data->cmd_ops = cmd_ops; 7084 7085 INIT_LIST_HEAD(&trigger_data->list); 7086 RCU_INIT_POINTER(trigger_data->filter, NULL); 7087 7088 trigger_data->private_data = hist_data; 7089 7090 /* if param is non-empty, it's supposed to be a filter */ 7091 if (param && cmd_ops->set_filter) { 7092 ret = cmd_ops->set_filter(param, trigger_data, file); 7093 if (ret < 0) 7094 goto out_free; 7095 } 7096 7097 if (remove) { 7098 if (!have_hist_trigger_match(trigger_data, file)) 7099 goto out_free; 7100 7101 if (hist_trigger_check_refs(trigger_data, file)) { 7102 ret = -EBUSY; 7103 goto out_free; 7104 } 7105 7106 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file); 7107 se_name = trace_event_name(file->event_call); 7108 se = find_synth_event(se_name); 7109 if (se) 7110 se->ref--; 7111 ret = 0; 7112 goto out_free; 7113 } 7114 7115 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file); 7116 /* 7117 * The above returns on success the # of triggers registered, 7118 * but if it didn't register any it returns zero. Consider no 7119 * triggers registered a failure too. 7120 */ 7121 if (!ret) { 7122 if (!(attrs->pause || attrs->cont || attrs->clear)) 7123 ret = -ENOENT; 7124 goto out_free; 7125 } else if (ret < 0) 7126 goto out_free; 7127 7128 if (get_named_trigger_data(trigger_data)) 7129 goto enable; 7130 7131 if (has_hist_vars(hist_data)) 7132 save_hist_vars(hist_data); 7133 7134 ret = create_actions(hist_data); 7135 if (ret) 7136 goto out_unreg; 7137 7138 ret = tracing_map_init(hist_data->map); 7139 if (ret) 7140 goto out_unreg; 7141 enable: 7142 ret = hist_trigger_enable(trigger_data, file); 7143 if (ret) 7144 goto out_unreg; 7145 7146 se_name = trace_event_name(file->event_call); 7147 se = find_synth_event(se_name); 7148 if (se) 7149 se->ref++; 7150 /* Just return zero, not the number of registered triggers */ 7151 ret = 0; 7152 out: 7153 if (ret == 0) 7154 hist_err_clear(); 7155 7156 return ret; 7157 out_unreg: 7158 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file); 7159 out_free: 7160 if (cmd_ops->set_filter) 7161 cmd_ops->set_filter(NULL, trigger_data, NULL); 7162 7163 remove_hist_vars(hist_data); 7164 7165 kfree(trigger_data); 7166 7167 destroy_hist_data(hist_data); 7168 goto out; 7169 } 7170 7171 static struct event_command trigger_hist_cmd = { 7172 .name = "hist", 7173 .trigger_type = ETT_EVENT_HIST, 7174 .flags = EVENT_CMD_FL_NEEDS_REC, 7175 .func = event_hist_trigger_func, 7176 .reg = hist_register_trigger, 7177 .unreg = hist_unregister_trigger, 7178 .unreg_all = hist_unreg_all, 7179 .get_trigger_ops = event_hist_get_trigger_ops, 7180 .set_filter = set_trigger_filter, 7181 }; 7182 7183 __init int register_trigger_hist_cmd(void) 7184 { 7185 int ret; 7186 7187 ret = register_event_command(&trigger_hist_cmd); 7188 WARN_ON(ret < 0); 7189 7190 return ret; 7191 } 7192 7193 static void 7194 hist_enable_trigger(struct event_trigger_data *data, void *rec, 7195 struct ring_buffer_event *event) 7196 { 7197 struct enable_trigger_data *enable_data = data->private_data; 7198 struct event_trigger_data *test; 7199 7200 list_for_each_entry_rcu(test, &enable_data->file->triggers, list, 7201 lockdep_is_held(&event_mutex)) { 7202 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 7203 if (enable_data->enable) 7204 test->paused = false; 7205 else 7206 test->paused = true; 7207 } 7208 } 7209 } 7210 7211 static void 7212 hist_enable_count_trigger(struct event_trigger_data *data, void *rec, 7213 struct ring_buffer_event *event) 7214 { 7215 if (!data->count) 7216 return; 7217 7218 if (data->count != -1) 7219 (data->count)--; 7220 7221 hist_enable_trigger(data, rec, event); 7222 } 7223 7224 static struct event_trigger_ops hist_enable_trigger_ops = { 7225 .func = hist_enable_trigger, 7226 .print = event_enable_trigger_print, 7227 .init = event_trigger_init, 7228 .free = event_enable_trigger_free, 7229 }; 7230 7231 static struct event_trigger_ops hist_enable_count_trigger_ops = { 7232 .func = hist_enable_count_trigger, 7233 .print = event_enable_trigger_print, 7234 .init = event_trigger_init, 7235 .free = event_enable_trigger_free, 7236 }; 7237 7238 static struct event_trigger_ops hist_disable_trigger_ops = { 7239 .func = hist_enable_trigger, 7240 .print = event_enable_trigger_print, 7241 .init = event_trigger_init, 7242 .free = event_enable_trigger_free, 7243 }; 7244 7245 static struct event_trigger_ops hist_disable_count_trigger_ops = { 7246 .func = hist_enable_count_trigger, 7247 .print = event_enable_trigger_print, 7248 .init = event_trigger_init, 7249 .free = event_enable_trigger_free, 7250 }; 7251 7252 static struct event_trigger_ops * 7253 hist_enable_get_trigger_ops(char *cmd, char *param) 7254 { 7255 struct event_trigger_ops *ops; 7256 bool enable; 7257 7258 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0); 7259 7260 if (enable) 7261 ops = param ? &hist_enable_count_trigger_ops : 7262 &hist_enable_trigger_ops; 7263 else 7264 ops = param ? &hist_disable_count_trigger_ops : 7265 &hist_disable_trigger_ops; 7266 7267 return ops; 7268 } 7269 7270 static void hist_enable_unreg_all(struct trace_event_file *file) 7271 { 7272 struct event_trigger_data *test, *n; 7273 7274 list_for_each_entry_safe(test, n, &file->triggers, list) { 7275 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) { 7276 list_del_rcu(&test->list); 7277 update_cond_flag(file); 7278 trace_event_trigger_enable_disable(file, 0); 7279 if (test->ops->free) 7280 test->ops->free(test->ops, test); 7281 } 7282 } 7283 } 7284 7285 static struct event_command trigger_hist_enable_cmd = { 7286 .name = ENABLE_HIST_STR, 7287 .trigger_type = ETT_HIST_ENABLE, 7288 .func = event_enable_trigger_func, 7289 .reg = event_enable_register_trigger, 7290 .unreg = event_enable_unregister_trigger, 7291 .unreg_all = hist_enable_unreg_all, 7292 .get_trigger_ops = hist_enable_get_trigger_ops, 7293 .set_filter = set_trigger_filter, 7294 }; 7295 7296 static struct event_command trigger_hist_disable_cmd = { 7297 .name = DISABLE_HIST_STR, 7298 .trigger_type = ETT_HIST_ENABLE, 7299 .func = event_enable_trigger_func, 7300 .reg = event_enable_register_trigger, 7301 .unreg = event_enable_unregister_trigger, 7302 .unreg_all = hist_enable_unreg_all, 7303 .get_trigger_ops = hist_enable_get_trigger_ops, 7304 .set_filter = set_trigger_filter, 7305 }; 7306 7307 static __init void unregister_trigger_hist_enable_disable_cmds(void) 7308 { 7309 unregister_event_command(&trigger_hist_enable_cmd); 7310 unregister_event_command(&trigger_hist_disable_cmd); 7311 } 7312 7313 __init int register_trigger_hist_enable_disable_cmds(void) 7314 { 7315 int ret; 7316 7317 ret = register_event_command(&trigger_hist_enable_cmd); 7318 if (WARN_ON(ret < 0)) 7319 return ret; 7320 ret = register_event_command(&trigger_hist_disable_cmd); 7321 if (WARN_ON(ret < 0)) 7322 unregister_trigger_hist_enable_disable_cmds(); 7323 7324 return ret; 7325 } 7326 7327 static __init int trace_events_hist_init(void) 7328 { 7329 struct dentry *entry = NULL; 7330 struct dentry *d_tracer; 7331 int err = 0; 7332 7333 err = dyn_event_register(&synth_event_ops); 7334 if (err) { 7335 pr_warn("Could not register synth_event_ops\n"); 7336 return err; 7337 } 7338 7339 d_tracer = tracing_init_dentry(); 7340 if (IS_ERR(d_tracer)) { 7341 err = PTR_ERR(d_tracer); 7342 goto err; 7343 } 7344 7345 entry = tracefs_create_file("synthetic_events", 0644, d_tracer, 7346 NULL, &synth_events_fops); 7347 if (!entry) { 7348 err = -ENODEV; 7349 goto err; 7350 } 7351 7352 return err; 7353 err: 7354 pr_warn("Could not create tracefs 'synthetic_events' entry\n"); 7355 7356 return err; 7357 } 7358 7359 fs_initcall(trace_events_hist_init); 7360