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