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