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 3030 strscpy(str, val_str, val->size); 3031 var_val = (u64)(uintptr_t)str; 3032 } 3033 tracing_map_set_var(elt, var_idx, var_val); 3034 } 3035 } 3036 3037 static void update_field_vars(struct hist_trigger_data *hist_data, 3038 struct tracing_map_elt *elt, 3039 struct trace_buffer *buffer, 3040 struct ring_buffer_event *rbe, 3041 void *rec) 3042 { 3043 __update_field_vars(elt, buffer, rbe, rec, hist_data->field_vars, 3044 hist_data->n_field_vars, 0); 3045 } 3046 3047 static void save_track_data_vars(struct hist_trigger_data *hist_data, 3048 struct tracing_map_elt *elt, 3049 struct trace_buffer *buffer, void *rec, 3050 struct ring_buffer_event *rbe, void *key, 3051 struct action_data *data, u64 *var_ref_vals) 3052 { 3053 __update_field_vars(elt, buffer, rbe, rec, hist_data->save_vars, 3054 hist_data->n_save_vars, hist_data->n_field_var_str); 3055 } 3056 3057 static struct hist_field *create_var(struct hist_trigger_data *hist_data, 3058 struct trace_event_file *file, 3059 char *name, int size, const char *type) 3060 { 3061 struct hist_field *var; 3062 int idx; 3063 3064 if (find_var(hist_data, file, name) && !hist_data->remove) { 3065 var = ERR_PTR(-EINVAL); 3066 goto out; 3067 } 3068 3069 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL); 3070 if (!var) { 3071 var = ERR_PTR(-ENOMEM); 3072 goto out; 3073 } 3074 3075 idx = tracing_map_add_var(hist_data->map); 3076 if (idx < 0) { 3077 kfree(var); 3078 var = ERR_PTR(-EINVAL); 3079 goto out; 3080 } 3081 3082 var->ref = 1; 3083 var->flags = HIST_FIELD_FL_VAR; 3084 var->var.idx = idx; 3085 var->var.hist_data = var->hist_data = hist_data; 3086 var->size = size; 3087 var->var.name = kstrdup(name, GFP_KERNEL); 3088 var->type = kstrdup_const(type, GFP_KERNEL); 3089 if (!var->var.name || !var->type) { 3090 kfree_const(var->type); 3091 kfree(var->var.name); 3092 kfree(var); 3093 var = ERR_PTR(-ENOMEM); 3094 } 3095 out: 3096 return var; 3097 } 3098 3099 static struct field_var *create_field_var(struct hist_trigger_data *hist_data, 3100 struct trace_event_file *file, 3101 char *field_name) 3102 { 3103 struct hist_field *val = NULL, *var = NULL; 3104 unsigned long flags = HIST_FIELD_FL_VAR; 3105 struct trace_array *tr = file->tr; 3106 struct field_var *field_var; 3107 int ret = 0; 3108 3109 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) { 3110 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name)); 3111 ret = -EINVAL; 3112 goto err; 3113 } 3114 3115 val = parse_atom(hist_data, file, field_name, &flags, NULL); 3116 if (IS_ERR(val)) { 3117 hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name)); 3118 ret = PTR_ERR(val); 3119 goto err; 3120 } 3121 3122 var = create_var(hist_data, file, field_name, val->size, val->type); 3123 if (IS_ERR(var)) { 3124 hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name)); 3125 kfree(val); 3126 ret = PTR_ERR(var); 3127 goto err; 3128 } 3129 3130 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL); 3131 if (!field_var) { 3132 kfree(val); 3133 kfree(var); 3134 ret = -ENOMEM; 3135 goto err; 3136 } 3137 3138 field_var->var = var; 3139 field_var->val = val; 3140 out: 3141 return field_var; 3142 err: 3143 field_var = ERR_PTR(ret); 3144 goto out; 3145 } 3146 3147 /** 3148 * create_target_field_var - Automatically create a variable for a field 3149 * @target_hist_data: The target hist trigger 3150 * @subsys_name: Optional subsystem name 3151 * @event_name: Optional event name 3152 * @var_name: The name of the field (and the resulting variable) 3153 * 3154 * Hist trigger actions fetch data from variables, not directly from 3155 * events. However, for convenience, users are allowed to directly 3156 * specify an event field in an action, which will be automatically 3157 * converted into a variable on their behalf. 3158 3159 * This function creates a field variable with the name var_name on 3160 * the hist trigger currently being defined on the target event. If 3161 * subsys_name and event_name are specified, this function simply 3162 * verifies that they do in fact match the target event subsystem and 3163 * event name. 3164 * 3165 * Return: The variable created for the field. 3166 */ 3167 static struct field_var * 3168 create_target_field_var(struct hist_trigger_data *target_hist_data, 3169 char *subsys_name, char *event_name, char *var_name) 3170 { 3171 struct trace_event_file *file = target_hist_data->event_file; 3172 3173 if (subsys_name) { 3174 struct trace_event_call *call; 3175 3176 if (!event_name) 3177 return NULL; 3178 3179 call = file->event_call; 3180 3181 if (strcmp(subsys_name, call->class->system) != 0) 3182 return NULL; 3183 3184 if (strcmp(event_name, trace_event_name(call)) != 0) 3185 return NULL; 3186 } 3187 3188 return create_field_var(target_hist_data, file, var_name); 3189 } 3190 3191 static bool check_track_val_max(u64 track_val, u64 var_val) 3192 { 3193 if (var_val <= track_val) 3194 return false; 3195 3196 return true; 3197 } 3198 3199 static bool check_track_val_changed(u64 track_val, u64 var_val) 3200 { 3201 if (var_val == track_val) 3202 return false; 3203 3204 return true; 3205 } 3206 3207 static u64 get_track_val(struct hist_trigger_data *hist_data, 3208 struct tracing_map_elt *elt, 3209 struct action_data *data) 3210 { 3211 unsigned int track_var_idx = data->track_data.track_var->var.idx; 3212 u64 track_val; 3213 3214 track_val = tracing_map_read_var(elt, track_var_idx); 3215 3216 return track_val; 3217 } 3218 3219 static void save_track_val(struct hist_trigger_data *hist_data, 3220 struct tracing_map_elt *elt, 3221 struct action_data *data, u64 var_val) 3222 { 3223 unsigned int track_var_idx = data->track_data.track_var->var.idx; 3224 3225 tracing_map_set_var(elt, track_var_idx, var_val); 3226 } 3227 3228 static void save_track_data(struct hist_trigger_data *hist_data, 3229 struct tracing_map_elt *elt, 3230 struct trace_buffer *buffer, void *rec, 3231 struct ring_buffer_event *rbe, void *key, 3232 struct action_data *data, u64 *var_ref_vals) 3233 { 3234 if (data->track_data.save_data) 3235 data->track_data.save_data(hist_data, elt, buffer, rec, rbe, 3236 key, data, var_ref_vals); 3237 } 3238 3239 static bool check_track_val(struct tracing_map_elt *elt, 3240 struct action_data *data, 3241 u64 var_val) 3242 { 3243 struct hist_trigger_data *hist_data; 3244 u64 track_val; 3245 3246 hist_data = data->track_data.track_var->hist_data; 3247 track_val = get_track_val(hist_data, elt, data); 3248 3249 return data->track_data.check_val(track_val, var_val); 3250 } 3251 3252 #ifdef CONFIG_TRACER_SNAPSHOT 3253 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data) 3254 { 3255 /* called with tr->max_lock held */ 3256 struct track_data *track_data = tr->cond_snapshot->cond_data; 3257 struct hist_elt_data *elt_data, *track_elt_data; 3258 struct snapshot_context *context = cond_data; 3259 struct action_data *action; 3260 u64 track_val; 3261 3262 if (!track_data) 3263 return false; 3264 3265 action = track_data->action_data; 3266 3267 track_val = get_track_val(track_data->hist_data, context->elt, 3268 track_data->action_data); 3269 3270 if (!action->track_data.check_val(track_data->track_val, track_val)) 3271 return false; 3272 3273 track_data->track_val = track_val; 3274 memcpy(track_data->key, context->key, track_data->key_len); 3275 3276 elt_data = context->elt->private_data; 3277 track_elt_data = track_data->elt.private_data; 3278 if (elt_data->comm) 3279 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN); 3280 3281 track_data->updated = true; 3282 3283 return true; 3284 } 3285 3286 static void save_track_data_snapshot(struct hist_trigger_data *hist_data, 3287 struct tracing_map_elt *elt, 3288 struct trace_buffer *buffer, void *rec, 3289 struct ring_buffer_event *rbe, void *key, 3290 struct action_data *data, 3291 u64 *var_ref_vals) 3292 { 3293 struct trace_event_file *file = hist_data->event_file; 3294 struct snapshot_context context; 3295 3296 context.elt = elt; 3297 context.key = key; 3298 3299 tracing_snapshot_cond(file->tr, &context); 3300 } 3301 3302 static void hist_trigger_print_key(struct seq_file *m, 3303 struct hist_trigger_data *hist_data, 3304 void *key, 3305 struct tracing_map_elt *elt); 3306 3307 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data) 3308 { 3309 unsigned int i; 3310 3311 if (!hist_data->n_actions) 3312 return NULL; 3313 3314 for (i = 0; i < hist_data->n_actions; i++) { 3315 struct action_data *data = hist_data->actions[i]; 3316 3317 if (data->action == ACTION_SNAPSHOT) 3318 return data; 3319 } 3320 3321 return NULL; 3322 } 3323 3324 static void track_data_snapshot_print(struct seq_file *m, 3325 struct hist_trigger_data *hist_data) 3326 { 3327 struct trace_event_file *file = hist_data->event_file; 3328 struct track_data *track_data; 3329 struct action_data *action; 3330 3331 track_data = tracing_cond_snapshot_data(file->tr); 3332 if (!track_data) 3333 return; 3334 3335 if (!track_data->updated) 3336 return; 3337 3338 action = snapshot_action(hist_data); 3339 if (!action) 3340 return; 3341 3342 seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n"); 3343 seq_printf(m, "\ttriggering value { %s(%s) }: %10llu", 3344 action->handler == HANDLER_ONMAX ? "onmax" : "onchange", 3345 action->track_data.var_str, track_data->track_val); 3346 3347 seq_puts(m, "\ttriggered by event with key: "); 3348 hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt); 3349 seq_putc(m, '\n'); 3350 } 3351 #else 3352 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data) 3353 { 3354 return false; 3355 } 3356 static void save_track_data_snapshot(struct hist_trigger_data *hist_data, 3357 struct tracing_map_elt *elt, 3358 struct trace_buffer *buffer, void *rec, 3359 struct ring_buffer_event *rbe, void *key, 3360 struct action_data *data, 3361 u64 *var_ref_vals) {} 3362 static void track_data_snapshot_print(struct seq_file *m, 3363 struct hist_trigger_data *hist_data) {} 3364 #endif /* CONFIG_TRACER_SNAPSHOT */ 3365 3366 static void track_data_print(struct seq_file *m, 3367 struct hist_trigger_data *hist_data, 3368 struct tracing_map_elt *elt, 3369 struct action_data *data) 3370 { 3371 u64 track_val = get_track_val(hist_data, elt, data); 3372 unsigned int i, save_var_idx; 3373 3374 if (data->handler == HANDLER_ONMAX) 3375 seq_printf(m, "\n\tmax: %10llu", track_val); 3376 else if (data->handler == HANDLER_ONCHANGE) 3377 seq_printf(m, "\n\tchanged: %10llu", track_val); 3378 3379 if (data->action == ACTION_SNAPSHOT) 3380 return; 3381 3382 for (i = 0; i < hist_data->n_save_vars; i++) { 3383 struct hist_field *save_val = hist_data->save_vars[i]->val; 3384 struct hist_field *save_var = hist_data->save_vars[i]->var; 3385 u64 val; 3386 3387 save_var_idx = save_var->var.idx; 3388 3389 val = tracing_map_read_var(elt, save_var_idx); 3390 3391 if (save_val->flags & HIST_FIELD_FL_STRING) { 3392 seq_printf(m, " %s: %-32s", save_var->var.name, 3393 (char *)(uintptr_t)(val)); 3394 } else 3395 seq_printf(m, " %s: %10llu", save_var->var.name, val); 3396 } 3397 } 3398 3399 static void ontrack_action(struct hist_trigger_data *hist_data, 3400 struct tracing_map_elt *elt, 3401 struct trace_buffer *buffer, void *rec, 3402 struct ring_buffer_event *rbe, void *key, 3403 struct action_data *data, u64 *var_ref_vals) 3404 { 3405 u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx]; 3406 3407 if (check_track_val(elt, data, var_val)) { 3408 save_track_val(hist_data, elt, data, var_val); 3409 save_track_data(hist_data, elt, buffer, rec, rbe, 3410 key, data, var_ref_vals); 3411 } 3412 } 3413 3414 static void action_data_destroy(struct action_data *data) 3415 { 3416 unsigned int i; 3417 3418 lockdep_assert_held(&event_mutex); 3419 3420 kfree(data->action_name); 3421 3422 for (i = 0; i < data->n_params; i++) 3423 kfree(data->params[i]); 3424 3425 if (data->synth_event) 3426 data->synth_event->ref--; 3427 3428 kfree(data->synth_event_name); 3429 3430 kfree(data); 3431 } 3432 3433 static void track_data_destroy(struct hist_trigger_data *hist_data, 3434 struct action_data *data) 3435 { 3436 struct trace_event_file *file = hist_data->event_file; 3437 3438 destroy_hist_field(data->track_data.track_var, 0); 3439 3440 if (data->action == ACTION_SNAPSHOT) { 3441 struct track_data *track_data; 3442 3443 track_data = tracing_cond_snapshot_data(file->tr); 3444 if (track_data && track_data->hist_data == hist_data) { 3445 tracing_snapshot_cond_disable(file->tr); 3446 track_data_free(track_data); 3447 } 3448 } 3449 3450 kfree(data->track_data.var_str); 3451 3452 action_data_destroy(data); 3453 } 3454 3455 static int action_create(struct hist_trigger_data *hist_data, 3456 struct action_data *data); 3457 3458 static int track_data_create(struct hist_trigger_data *hist_data, 3459 struct action_data *data) 3460 { 3461 struct hist_field *var_field, *ref_field, *track_var = NULL; 3462 struct trace_event_file *file = hist_data->event_file; 3463 struct trace_array *tr = file->tr; 3464 char *track_data_var_str; 3465 int ret = 0; 3466 3467 track_data_var_str = data->track_data.var_str; 3468 if (track_data_var_str[0] != '$') { 3469 hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str)); 3470 return -EINVAL; 3471 } 3472 track_data_var_str++; 3473 3474 var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str); 3475 if (!var_field) { 3476 hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str)); 3477 return -EINVAL; 3478 } 3479 3480 ref_field = create_var_ref(hist_data, var_field, NULL, NULL); 3481 if (!ref_field) 3482 return -ENOMEM; 3483 3484 data->track_data.var_ref = ref_field; 3485 3486 if (data->handler == HANDLER_ONMAX) 3487 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64"); 3488 if (IS_ERR(track_var)) { 3489 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0); 3490 ret = PTR_ERR(track_var); 3491 goto out; 3492 } 3493 3494 if (data->handler == HANDLER_ONCHANGE) 3495 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64"); 3496 if (IS_ERR(track_var)) { 3497 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0); 3498 ret = PTR_ERR(track_var); 3499 goto out; 3500 } 3501 data->track_data.track_var = track_var; 3502 3503 ret = action_create(hist_data, data); 3504 out: 3505 return ret; 3506 } 3507 3508 static int parse_action_params(struct trace_array *tr, char *params, 3509 struct action_data *data) 3510 { 3511 char *param, *saved_param; 3512 bool first_param = true; 3513 int ret = 0; 3514 3515 while (params) { 3516 if (data->n_params >= SYNTH_FIELDS_MAX) { 3517 hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0); 3518 goto out; 3519 } 3520 3521 param = strsep(¶ms, ","); 3522 if (!param) { 3523 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0); 3524 ret = -EINVAL; 3525 goto out; 3526 } 3527 3528 param = strstrip(param); 3529 if (strlen(param) < 2) { 3530 hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param)); 3531 ret = -EINVAL; 3532 goto out; 3533 } 3534 3535 saved_param = kstrdup(param, GFP_KERNEL); 3536 if (!saved_param) { 3537 ret = -ENOMEM; 3538 goto out; 3539 } 3540 3541 if (first_param && data->use_trace_keyword) { 3542 data->synth_event_name = saved_param; 3543 first_param = false; 3544 continue; 3545 } 3546 first_param = false; 3547 3548 data->params[data->n_params++] = saved_param; 3549 } 3550 out: 3551 return ret; 3552 } 3553 3554 static int action_parse(struct trace_array *tr, char *str, struct action_data *data, 3555 enum handler_id handler) 3556 { 3557 char *action_name; 3558 int ret = 0; 3559 3560 strsep(&str, "."); 3561 if (!str) { 3562 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0); 3563 ret = -EINVAL; 3564 goto out; 3565 } 3566 3567 action_name = strsep(&str, "("); 3568 if (!action_name || !str) { 3569 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0); 3570 ret = -EINVAL; 3571 goto out; 3572 } 3573 3574 if (str_has_prefix(action_name, "save")) { 3575 char *params = strsep(&str, ")"); 3576 3577 if (!params) { 3578 hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0); 3579 ret = -EINVAL; 3580 goto out; 3581 } 3582 3583 ret = parse_action_params(tr, params, data); 3584 if (ret) 3585 goto out; 3586 3587 if (handler == HANDLER_ONMAX) 3588 data->track_data.check_val = check_track_val_max; 3589 else if (handler == HANDLER_ONCHANGE) 3590 data->track_data.check_val = check_track_val_changed; 3591 else { 3592 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name)); 3593 ret = -EINVAL; 3594 goto out; 3595 } 3596 3597 data->track_data.save_data = save_track_data_vars; 3598 data->fn = ontrack_action; 3599 data->action = ACTION_SAVE; 3600 } else if (str_has_prefix(action_name, "snapshot")) { 3601 char *params = strsep(&str, ")"); 3602 3603 if (!str) { 3604 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params)); 3605 ret = -EINVAL; 3606 goto out; 3607 } 3608 3609 if (handler == HANDLER_ONMAX) 3610 data->track_data.check_val = check_track_val_max; 3611 else if (handler == HANDLER_ONCHANGE) 3612 data->track_data.check_val = check_track_val_changed; 3613 else { 3614 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name)); 3615 ret = -EINVAL; 3616 goto out; 3617 } 3618 3619 data->track_data.save_data = save_track_data_snapshot; 3620 data->fn = ontrack_action; 3621 data->action = ACTION_SNAPSHOT; 3622 } else { 3623 char *params = strsep(&str, ")"); 3624 3625 if (str_has_prefix(action_name, "trace")) 3626 data->use_trace_keyword = true; 3627 3628 if (params) { 3629 ret = parse_action_params(tr, params, data); 3630 if (ret) 3631 goto out; 3632 } 3633 3634 if (handler == HANDLER_ONMAX) 3635 data->track_data.check_val = check_track_val_max; 3636 else if (handler == HANDLER_ONCHANGE) 3637 data->track_data.check_val = check_track_val_changed; 3638 3639 if (handler != HANDLER_ONMATCH) { 3640 data->track_data.save_data = action_trace; 3641 data->fn = ontrack_action; 3642 } else 3643 data->fn = action_trace; 3644 3645 data->action = ACTION_TRACE; 3646 } 3647 3648 data->action_name = kstrdup(action_name, GFP_KERNEL); 3649 if (!data->action_name) { 3650 ret = -ENOMEM; 3651 goto out; 3652 } 3653 3654 data->handler = handler; 3655 out: 3656 return ret; 3657 } 3658 3659 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data, 3660 char *str, enum handler_id handler) 3661 { 3662 struct action_data *data; 3663 int ret = -EINVAL; 3664 char *var_str; 3665 3666 data = kzalloc(sizeof(*data), GFP_KERNEL); 3667 if (!data) 3668 return ERR_PTR(-ENOMEM); 3669 3670 var_str = strsep(&str, ")"); 3671 if (!var_str || !str) { 3672 ret = -EINVAL; 3673 goto free; 3674 } 3675 3676 data->track_data.var_str = kstrdup(var_str, GFP_KERNEL); 3677 if (!data->track_data.var_str) { 3678 ret = -ENOMEM; 3679 goto free; 3680 } 3681 3682 ret = action_parse(hist_data->event_file->tr, str, data, handler); 3683 if (ret) 3684 goto free; 3685 out: 3686 return data; 3687 free: 3688 track_data_destroy(hist_data, data); 3689 data = ERR_PTR(ret); 3690 goto out; 3691 } 3692 3693 static void onmatch_destroy(struct action_data *data) 3694 { 3695 kfree(data->match_data.event); 3696 kfree(data->match_data.event_system); 3697 3698 action_data_destroy(data); 3699 } 3700 3701 static void destroy_field_var(struct field_var *field_var) 3702 { 3703 if (!field_var) 3704 return; 3705 3706 destroy_hist_field(field_var->var, 0); 3707 destroy_hist_field(field_var->val, 0); 3708 3709 kfree(field_var); 3710 } 3711 3712 static void destroy_field_vars(struct hist_trigger_data *hist_data) 3713 { 3714 unsigned int i; 3715 3716 for (i = 0; i < hist_data->n_field_vars; i++) 3717 destroy_field_var(hist_data->field_vars[i]); 3718 3719 for (i = 0; i < hist_data->n_save_vars; i++) 3720 destroy_field_var(hist_data->save_vars[i]); 3721 } 3722 3723 static void save_field_var(struct hist_trigger_data *hist_data, 3724 struct field_var *field_var) 3725 { 3726 hist_data->field_vars[hist_data->n_field_vars++] = field_var; 3727 3728 if (field_var->val->flags & HIST_FIELD_FL_STRING) 3729 hist_data->n_field_var_str++; 3730 } 3731 3732 3733 static int check_synth_field(struct synth_event *event, 3734 struct hist_field *hist_field, 3735 unsigned int field_pos) 3736 { 3737 struct synth_field *field; 3738 3739 if (field_pos >= event->n_fields) 3740 return -EINVAL; 3741 3742 field = event->fields[field_pos]; 3743 3744 /* 3745 * A dynamic string synth field can accept static or 3746 * dynamic. A static string synth field can only accept a 3747 * same-sized static string, which is checked for later. 3748 */ 3749 if (strstr(hist_field->type, "char[") && field->is_string 3750 && field->is_dynamic) 3751 return 0; 3752 3753 if (strcmp(field->type, hist_field->type) != 0) { 3754 if (field->size != hist_field->size || 3755 field->is_signed != hist_field->is_signed) 3756 return -EINVAL; 3757 } 3758 3759 return 0; 3760 } 3761 3762 static struct hist_field * 3763 trace_action_find_var(struct hist_trigger_data *hist_data, 3764 struct action_data *data, 3765 char *system, char *event, char *var) 3766 { 3767 struct trace_array *tr = hist_data->event_file->tr; 3768 struct hist_field *hist_field; 3769 3770 var++; /* skip '$' */ 3771 3772 hist_field = find_target_event_var(hist_data, system, event, var); 3773 if (!hist_field) { 3774 if (!system && data->handler == HANDLER_ONMATCH) { 3775 system = data->match_data.event_system; 3776 event = data->match_data.event; 3777 } 3778 3779 hist_field = find_event_var(hist_data, system, event, var); 3780 } 3781 3782 if (!hist_field) 3783 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var)); 3784 3785 return hist_field; 3786 } 3787 3788 static struct hist_field * 3789 trace_action_create_field_var(struct hist_trigger_data *hist_data, 3790 struct action_data *data, char *system, 3791 char *event, char *var) 3792 { 3793 struct hist_field *hist_field = NULL; 3794 struct field_var *field_var; 3795 3796 /* 3797 * First try to create a field var on the target event (the 3798 * currently being defined). This will create a variable for 3799 * unqualified fields on the target event, or if qualified, 3800 * target fields that have qualified names matching the target. 3801 */ 3802 field_var = create_target_field_var(hist_data, system, event, var); 3803 3804 if (field_var && !IS_ERR(field_var)) { 3805 save_field_var(hist_data, field_var); 3806 hist_field = field_var->var; 3807 } else { 3808 field_var = NULL; 3809 /* 3810 * If no explicit system.event is specified, default to 3811 * looking for fields on the onmatch(system.event.xxx) 3812 * event. 3813 */ 3814 if (!system && data->handler == HANDLER_ONMATCH) { 3815 system = data->match_data.event_system; 3816 event = data->match_data.event; 3817 } 3818 3819 if (!event) 3820 goto free; 3821 /* 3822 * At this point, we're looking at a field on another 3823 * event. Because we can't modify a hist trigger on 3824 * another event to add a variable for a field, we need 3825 * to create a new trigger on that event and create the 3826 * variable at the same time. 3827 */ 3828 hist_field = create_field_var_hist(hist_data, system, event, var); 3829 if (IS_ERR(hist_field)) 3830 goto free; 3831 } 3832 out: 3833 return hist_field; 3834 free: 3835 destroy_field_var(field_var); 3836 hist_field = NULL; 3837 goto out; 3838 } 3839 3840 static int trace_action_create(struct hist_trigger_data *hist_data, 3841 struct action_data *data) 3842 { 3843 struct trace_array *tr = hist_data->event_file->tr; 3844 char *event_name, *param, *system = NULL; 3845 struct hist_field *hist_field, *var_ref; 3846 unsigned int i; 3847 unsigned int field_pos = 0; 3848 struct synth_event *event; 3849 char *synth_event_name; 3850 int var_ref_idx, ret = 0; 3851 3852 lockdep_assert_held(&event_mutex); 3853 3854 if (data->use_trace_keyword) 3855 synth_event_name = data->synth_event_name; 3856 else 3857 synth_event_name = data->action_name; 3858 3859 event = find_synth_event(synth_event_name); 3860 if (!event) { 3861 hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name)); 3862 return -EINVAL; 3863 } 3864 3865 event->ref++; 3866 3867 for (i = 0; i < data->n_params; i++) { 3868 char *p; 3869 3870 p = param = kstrdup(data->params[i], GFP_KERNEL); 3871 if (!param) { 3872 ret = -ENOMEM; 3873 goto err; 3874 } 3875 3876 system = strsep(¶m, "."); 3877 if (!param) { 3878 param = (char *)system; 3879 system = event_name = NULL; 3880 } else { 3881 event_name = strsep(¶m, "."); 3882 if (!param) { 3883 kfree(p); 3884 ret = -EINVAL; 3885 goto err; 3886 } 3887 } 3888 3889 if (param[0] == '$') 3890 hist_field = trace_action_find_var(hist_data, data, 3891 system, event_name, 3892 param); 3893 else 3894 hist_field = trace_action_create_field_var(hist_data, 3895 data, 3896 system, 3897 event_name, 3898 param); 3899 3900 if (!hist_field) { 3901 kfree(p); 3902 ret = -EINVAL; 3903 goto err; 3904 } 3905 3906 if (check_synth_field(event, hist_field, field_pos) == 0) { 3907 var_ref = create_var_ref(hist_data, hist_field, 3908 system, event_name); 3909 if (!var_ref) { 3910 kfree(p); 3911 ret = -ENOMEM; 3912 goto err; 3913 } 3914 3915 var_ref_idx = find_var_ref_idx(hist_data, var_ref); 3916 if (WARN_ON(var_ref_idx < 0)) { 3917 ret = var_ref_idx; 3918 goto err; 3919 } 3920 3921 data->var_ref_idx[i] = var_ref_idx; 3922 3923 field_pos++; 3924 kfree(p); 3925 continue; 3926 } 3927 3928 hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param)); 3929 kfree(p); 3930 ret = -EINVAL; 3931 goto err; 3932 } 3933 3934 if (field_pos != event->n_fields) { 3935 hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name)); 3936 ret = -EINVAL; 3937 goto err; 3938 } 3939 3940 data->synth_event = event; 3941 out: 3942 return ret; 3943 err: 3944 event->ref--; 3945 3946 goto out; 3947 } 3948 3949 static int action_create(struct hist_trigger_data *hist_data, 3950 struct action_data *data) 3951 { 3952 struct trace_event_file *file = hist_data->event_file; 3953 struct trace_array *tr = file->tr; 3954 struct track_data *track_data; 3955 struct field_var *field_var; 3956 unsigned int i; 3957 char *param; 3958 int ret = 0; 3959 3960 if (data->action == ACTION_TRACE) 3961 return trace_action_create(hist_data, data); 3962 3963 if (data->action == ACTION_SNAPSHOT) { 3964 track_data = track_data_alloc(hist_data->key_size, data, hist_data); 3965 if (IS_ERR(track_data)) { 3966 ret = PTR_ERR(track_data); 3967 goto out; 3968 } 3969 3970 ret = tracing_snapshot_cond_enable(file->tr, track_data, 3971 cond_snapshot_update); 3972 if (ret) 3973 track_data_free(track_data); 3974 3975 goto out; 3976 } 3977 3978 if (data->action == ACTION_SAVE) { 3979 if (hist_data->n_save_vars) { 3980 ret = -EEXIST; 3981 hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0); 3982 goto out; 3983 } 3984 3985 for (i = 0; i < data->n_params; i++) { 3986 param = kstrdup(data->params[i], GFP_KERNEL); 3987 if (!param) { 3988 ret = -ENOMEM; 3989 goto out; 3990 } 3991 3992 field_var = create_target_field_var(hist_data, NULL, NULL, param); 3993 if (IS_ERR(field_var)) { 3994 hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL, 3995 errpos(param)); 3996 ret = PTR_ERR(field_var); 3997 kfree(param); 3998 goto out; 3999 } 4000 4001 hist_data->save_vars[hist_data->n_save_vars++] = field_var; 4002 if (field_var->val->flags & HIST_FIELD_FL_STRING) 4003 hist_data->n_save_var_str++; 4004 kfree(param); 4005 } 4006 } 4007 out: 4008 return ret; 4009 } 4010 4011 static int onmatch_create(struct hist_trigger_data *hist_data, 4012 struct action_data *data) 4013 { 4014 return action_create(hist_data, data); 4015 } 4016 4017 static struct action_data *onmatch_parse(struct trace_array *tr, char *str) 4018 { 4019 char *match_event, *match_event_system; 4020 struct action_data *data; 4021 int ret = -EINVAL; 4022 4023 data = kzalloc(sizeof(*data), GFP_KERNEL); 4024 if (!data) 4025 return ERR_PTR(-ENOMEM); 4026 4027 match_event = strsep(&str, ")"); 4028 if (!match_event || !str) { 4029 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event)); 4030 goto free; 4031 } 4032 4033 match_event_system = strsep(&match_event, "."); 4034 if (!match_event) { 4035 hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system)); 4036 goto free; 4037 } 4038 4039 if (IS_ERR(event_file(tr, match_event_system, match_event))) { 4040 hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event)); 4041 goto free; 4042 } 4043 4044 data->match_data.event = kstrdup(match_event, GFP_KERNEL); 4045 if (!data->match_data.event) { 4046 ret = -ENOMEM; 4047 goto free; 4048 } 4049 4050 data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL); 4051 if (!data->match_data.event_system) { 4052 ret = -ENOMEM; 4053 goto free; 4054 } 4055 4056 ret = action_parse(tr, str, data, HANDLER_ONMATCH); 4057 if (ret) 4058 goto free; 4059 out: 4060 return data; 4061 free: 4062 onmatch_destroy(data); 4063 data = ERR_PTR(ret); 4064 goto out; 4065 } 4066 4067 static int create_hitcount_val(struct hist_trigger_data *hist_data) 4068 { 4069 hist_data->fields[HITCOUNT_IDX] = 4070 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL); 4071 if (!hist_data->fields[HITCOUNT_IDX]) 4072 return -ENOMEM; 4073 4074 hist_data->n_vals++; 4075 hist_data->n_fields++; 4076 4077 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX)) 4078 return -EINVAL; 4079 4080 return 0; 4081 } 4082 4083 static int __create_val_field(struct hist_trigger_data *hist_data, 4084 unsigned int val_idx, 4085 struct trace_event_file *file, 4086 char *var_name, char *field_str, 4087 unsigned long flags) 4088 { 4089 struct hist_field *hist_field; 4090 int ret = 0, n_subexprs = 0; 4091 4092 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, &n_subexprs); 4093 if (IS_ERR(hist_field)) { 4094 ret = PTR_ERR(hist_field); 4095 goto out; 4096 } 4097 4098 hist_data->fields[val_idx] = hist_field; 4099 4100 ++hist_data->n_vals; 4101 ++hist_data->n_fields; 4102 4103 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX)) 4104 ret = -EINVAL; 4105 out: 4106 return ret; 4107 } 4108 4109 static int create_val_field(struct hist_trigger_data *hist_data, 4110 unsigned int val_idx, 4111 struct trace_event_file *file, 4112 char *field_str) 4113 { 4114 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX)) 4115 return -EINVAL; 4116 4117 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0); 4118 } 4119 4120 static const char *no_comm = "(no comm)"; 4121 4122 static u64 hist_field_execname(struct hist_field *hist_field, 4123 struct tracing_map_elt *elt, 4124 struct trace_buffer *buffer, 4125 struct ring_buffer_event *rbe, 4126 void *event) 4127 { 4128 struct hist_elt_data *elt_data; 4129 4130 if (WARN_ON_ONCE(!elt)) 4131 return (u64)(unsigned long)no_comm; 4132 4133 elt_data = elt->private_data; 4134 4135 if (WARN_ON_ONCE(!elt_data->comm)) 4136 return (u64)(unsigned long)no_comm; 4137 4138 return (u64)(unsigned long)(elt_data->comm); 4139 } 4140 4141 /* Convert a var that points to common_pid.execname to a string */ 4142 static void update_var_execname(struct hist_field *hist_field) 4143 { 4144 hist_field->flags = HIST_FIELD_FL_STRING | HIST_FIELD_FL_VAR | 4145 HIST_FIELD_FL_EXECNAME; 4146 hist_field->size = MAX_FILTER_STR_VAL; 4147 hist_field->is_signed = 0; 4148 4149 kfree_const(hist_field->type); 4150 hist_field->type = "char[]"; 4151 4152 hist_field->fn = hist_field_execname; 4153 } 4154 4155 static int create_var_field(struct hist_trigger_data *hist_data, 4156 unsigned int val_idx, 4157 struct trace_event_file *file, 4158 char *var_name, char *expr_str) 4159 { 4160 struct trace_array *tr = hist_data->event_file->tr; 4161 unsigned long flags = 0; 4162 int ret; 4163 4164 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX)) 4165 return -EINVAL; 4166 4167 if (find_var(hist_data, file, var_name) && !hist_data->remove) { 4168 hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name)); 4169 return -EINVAL; 4170 } 4171 4172 flags |= HIST_FIELD_FL_VAR; 4173 hist_data->n_vars++; 4174 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX)) 4175 return -EINVAL; 4176 4177 ret = __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags); 4178 4179 if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_EXECNAME) 4180 update_var_execname(hist_data->fields[val_idx]); 4181 4182 if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_STRING) 4183 hist_data->fields[val_idx]->var_str_idx = hist_data->n_var_str++; 4184 4185 return ret; 4186 } 4187 4188 static int create_val_fields(struct hist_trigger_data *hist_data, 4189 struct trace_event_file *file) 4190 { 4191 char *fields_str, *field_str; 4192 unsigned int i, j = 1; 4193 int ret; 4194 4195 ret = create_hitcount_val(hist_data); 4196 if (ret) 4197 goto out; 4198 4199 fields_str = hist_data->attrs->vals_str; 4200 if (!fields_str) 4201 goto out; 4202 4203 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX && 4204 j < TRACING_MAP_VALS_MAX; i++) { 4205 field_str = strsep(&fields_str, ","); 4206 if (!field_str) 4207 break; 4208 4209 if (strcmp(field_str, "hitcount") == 0) 4210 continue; 4211 4212 ret = create_val_field(hist_data, j++, file, field_str); 4213 if (ret) 4214 goto out; 4215 } 4216 4217 if (fields_str && (strcmp(fields_str, "hitcount") != 0)) 4218 ret = -EINVAL; 4219 out: 4220 return ret; 4221 } 4222 4223 static int create_key_field(struct hist_trigger_data *hist_data, 4224 unsigned int key_idx, 4225 unsigned int key_offset, 4226 struct trace_event_file *file, 4227 char *field_str) 4228 { 4229 struct trace_array *tr = hist_data->event_file->tr; 4230 struct hist_field *hist_field = NULL; 4231 unsigned long flags = 0; 4232 unsigned int key_size; 4233 int ret = 0, n_subexprs = 0; 4234 4235 if (WARN_ON(key_idx >= HIST_FIELDS_MAX)) 4236 return -EINVAL; 4237 4238 flags |= HIST_FIELD_FL_KEY; 4239 4240 if (strcmp(field_str, "stacktrace") == 0) { 4241 flags |= HIST_FIELD_FL_STACKTRACE; 4242 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH; 4243 hist_field = create_hist_field(hist_data, NULL, flags, NULL); 4244 } else { 4245 hist_field = parse_expr(hist_data, file, field_str, flags, 4246 NULL, &n_subexprs); 4247 if (IS_ERR(hist_field)) { 4248 ret = PTR_ERR(hist_field); 4249 goto out; 4250 } 4251 4252 if (field_has_hist_vars(hist_field, 0)) { 4253 hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str)); 4254 destroy_hist_field(hist_field, 0); 4255 ret = -EINVAL; 4256 goto out; 4257 } 4258 4259 key_size = hist_field->size; 4260 } 4261 4262 hist_data->fields[key_idx] = hist_field; 4263 4264 key_size = ALIGN(key_size, sizeof(u64)); 4265 hist_data->fields[key_idx]->size = key_size; 4266 hist_data->fields[key_idx]->offset = key_offset; 4267 4268 hist_data->key_size += key_size; 4269 4270 if (hist_data->key_size > HIST_KEY_SIZE_MAX) { 4271 ret = -EINVAL; 4272 goto out; 4273 } 4274 4275 hist_data->n_keys++; 4276 hist_data->n_fields++; 4277 4278 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX)) 4279 return -EINVAL; 4280 4281 ret = key_size; 4282 out: 4283 return ret; 4284 } 4285 4286 static int create_key_fields(struct hist_trigger_data *hist_data, 4287 struct trace_event_file *file) 4288 { 4289 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals; 4290 char *fields_str, *field_str; 4291 int ret = -EINVAL; 4292 4293 fields_str = hist_data->attrs->keys_str; 4294 if (!fields_str) 4295 goto out; 4296 4297 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) { 4298 field_str = strsep(&fields_str, ","); 4299 if (!field_str) 4300 break; 4301 ret = create_key_field(hist_data, i, key_offset, 4302 file, field_str); 4303 if (ret < 0) 4304 goto out; 4305 key_offset += ret; 4306 } 4307 if (fields_str) { 4308 ret = -EINVAL; 4309 goto out; 4310 } 4311 ret = 0; 4312 out: 4313 return ret; 4314 } 4315 4316 static int create_var_fields(struct hist_trigger_data *hist_data, 4317 struct trace_event_file *file) 4318 { 4319 unsigned int i, j = hist_data->n_vals; 4320 int ret = 0; 4321 4322 unsigned int n_vars = hist_data->attrs->var_defs.n_vars; 4323 4324 for (i = 0; i < n_vars; i++) { 4325 char *var_name = hist_data->attrs->var_defs.name[i]; 4326 char *expr = hist_data->attrs->var_defs.expr[i]; 4327 4328 ret = create_var_field(hist_data, j++, file, var_name, expr); 4329 if (ret) 4330 goto out; 4331 } 4332 out: 4333 return ret; 4334 } 4335 4336 static void free_var_defs(struct hist_trigger_data *hist_data) 4337 { 4338 unsigned int i; 4339 4340 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) { 4341 kfree(hist_data->attrs->var_defs.name[i]); 4342 kfree(hist_data->attrs->var_defs.expr[i]); 4343 } 4344 4345 hist_data->attrs->var_defs.n_vars = 0; 4346 } 4347 4348 static int parse_var_defs(struct hist_trigger_data *hist_data) 4349 { 4350 struct trace_array *tr = hist_data->event_file->tr; 4351 char *s, *str, *var_name, *field_str; 4352 unsigned int i, j, n_vars = 0; 4353 int ret = 0; 4354 4355 for (i = 0; i < hist_data->attrs->n_assignments; i++) { 4356 str = hist_data->attrs->assignment_str[i]; 4357 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) { 4358 field_str = strsep(&str, ","); 4359 if (!field_str) 4360 break; 4361 4362 var_name = strsep(&field_str, "="); 4363 if (!var_name || !field_str) { 4364 hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT, 4365 errpos(var_name)); 4366 ret = -EINVAL; 4367 goto free; 4368 } 4369 4370 if (n_vars == TRACING_MAP_VARS_MAX) { 4371 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name)); 4372 ret = -EINVAL; 4373 goto free; 4374 } 4375 4376 s = kstrdup(var_name, GFP_KERNEL); 4377 if (!s) { 4378 ret = -ENOMEM; 4379 goto free; 4380 } 4381 hist_data->attrs->var_defs.name[n_vars] = s; 4382 4383 s = kstrdup(field_str, GFP_KERNEL); 4384 if (!s) { 4385 ret = -ENOMEM; 4386 goto free; 4387 } 4388 hist_data->attrs->var_defs.expr[n_vars++] = s; 4389 4390 hist_data->attrs->var_defs.n_vars = n_vars; 4391 } 4392 } 4393 4394 return ret; 4395 free: 4396 free_var_defs(hist_data); 4397 4398 return ret; 4399 } 4400 4401 static int create_hist_fields(struct hist_trigger_data *hist_data, 4402 struct trace_event_file *file) 4403 { 4404 int ret; 4405 4406 ret = parse_var_defs(hist_data); 4407 if (ret) 4408 goto out; 4409 4410 ret = create_val_fields(hist_data, file); 4411 if (ret) 4412 goto out; 4413 4414 ret = create_var_fields(hist_data, file); 4415 if (ret) 4416 goto out; 4417 4418 ret = create_key_fields(hist_data, file); 4419 if (ret) 4420 goto out; 4421 out: 4422 free_var_defs(hist_data); 4423 4424 return ret; 4425 } 4426 4427 static int is_descending(struct trace_array *tr, const char *str) 4428 { 4429 if (!str) 4430 return 0; 4431 4432 if (strcmp(str, "descending") == 0) 4433 return 1; 4434 4435 if (strcmp(str, "ascending") == 0) 4436 return 0; 4437 4438 hist_err(tr, HIST_ERR_INVALID_SORT_MODIFIER, errpos((char *)str)); 4439 4440 return -EINVAL; 4441 } 4442 4443 static int create_sort_keys(struct hist_trigger_data *hist_data) 4444 { 4445 struct trace_array *tr = hist_data->event_file->tr; 4446 char *fields_str = hist_data->attrs->sort_key_str; 4447 struct tracing_map_sort_key *sort_key; 4448 int descending, ret = 0; 4449 unsigned int i, j, k; 4450 4451 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */ 4452 4453 if (!fields_str) 4454 goto out; 4455 4456 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) { 4457 struct hist_field *hist_field; 4458 char *field_str, *field_name; 4459 const char *test_name; 4460 4461 sort_key = &hist_data->sort_keys[i]; 4462 4463 field_str = strsep(&fields_str, ","); 4464 if (!field_str) 4465 break; 4466 4467 if (!*field_str) { 4468 ret = -EINVAL; 4469 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort=")); 4470 break; 4471 } 4472 4473 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) { 4474 hist_err(tr, HIST_ERR_TOO_MANY_SORT_FIELDS, errpos("sort=")); 4475 ret = -EINVAL; 4476 break; 4477 } 4478 4479 field_name = strsep(&field_str, "."); 4480 if (!field_name || !*field_name) { 4481 ret = -EINVAL; 4482 hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort=")); 4483 break; 4484 } 4485 4486 if (strcmp(field_name, "hitcount") == 0) { 4487 descending = is_descending(tr, field_str); 4488 if (descending < 0) { 4489 ret = descending; 4490 break; 4491 } 4492 sort_key->descending = descending; 4493 continue; 4494 } 4495 4496 for (j = 1, k = 1; j < hist_data->n_fields; j++) { 4497 unsigned int idx; 4498 4499 hist_field = hist_data->fields[j]; 4500 if (hist_field->flags & HIST_FIELD_FL_VAR) 4501 continue; 4502 4503 idx = k++; 4504 4505 test_name = hist_field_name(hist_field, 0); 4506 4507 if (strcmp(field_name, test_name) == 0) { 4508 sort_key->field_idx = idx; 4509 descending = is_descending(tr, field_str); 4510 if (descending < 0) { 4511 ret = descending; 4512 goto out; 4513 } 4514 sort_key->descending = descending; 4515 break; 4516 } 4517 } 4518 if (j == hist_data->n_fields) { 4519 ret = -EINVAL; 4520 hist_err(tr, HIST_ERR_INVALID_SORT_FIELD, errpos(field_name)); 4521 break; 4522 } 4523 } 4524 4525 hist_data->n_sort_keys = i; 4526 out: 4527 return ret; 4528 } 4529 4530 static void destroy_actions(struct hist_trigger_data *hist_data) 4531 { 4532 unsigned int i; 4533 4534 for (i = 0; i < hist_data->n_actions; i++) { 4535 struct action_data *data = hist_data->actions[i]; 4536 4537 if (data->handler == HANDLER_ONMATCH) 4538 onmatch_destroy(data); 4539 else if (data->handler == HANDLER_ONMAX || 4540 data->handler == HANDLER_ONCHANGE) 4541 track_data_destroy(hist_data, data); 4542 else 4543 kfree(data); 4544 } 4545 } 4546 4547 static int parse_actions(struct hist_trigger_data *hist_data) 4548 { 4549 struct trace_array *tr = hist_data->event_file->tr; 4550 struct action_data *data; 4551 unsigned int i; 4552 int ret = 0; 4553 char *str; 4554 int len; 4555 4556 for (i = 0; i < hist_data->attrs->n_actions; i++) { 4557 str = hist_data->attrs->action_str[i]; 4558 4559 if ((len = str_has_prefix(str, "onmatch("))) { 4560 char *action_str = str + len; 4561 4562 data = onmatch_parse(tr, action_str); 4563 if (IS_ERR(data)) { 4564 ret = PTR_ERR(data); 4565 break; 4566 } 4567 } else if ((len = str_has_prefix(str, "onmax("))) { 4568 char *action_str = str + len; 4569 4570 data = track_data_parse(hist_data, action_str, 4571 HANDLER_ONMAX); 4572 if (IS_ERR(data)) { 4573 ret = PTR_ERR(data); 4574 break; 4575 } 4576 } else if ((len = str_has_prefix(str, "onchange("))) { 4577 char *action_str = str + len; 4578 4579 data = track_data_parse(hist_data, action_str, 4580 HANDLER_ONCHANGE); 4581 if (IS_ERR(data)) { 4582 ret = PTR_ERR(data); 4583 break; 4584 } 4585 } else { 4586 ret = -EINVAL; 4587 break; 4588 } 4589 4590 hist_data->actions[hist_data->n_actions++] = data; 4591 } 4592 4593 return ret; 4594 } 4595 4596 static int create_actions(struct hist_trigger_data *hist_data) 4597 { 4598 struct action_data *data; 4599 unsigned int i; 4600 int ret = 0; 4601 4602 for (i = 0; i < hist_data->attrs->n_actions; i++) { 4603 data = hist_data->actions[i]; 4604 4605 if (data->handler == HANDLER_ONMATCH) { 4606 ret = onmatch_create(hist_data, data); 4607 if (ret) 4608 break; 4609 } else if (data->handler == HANDLER_ONMAX || 4610 data->handler == HANDLER_ONCHANGE) { 4611 ret = track_data_create(hist_data, data); 4612 if (ret) 4613 break; 4614 } else { 4615 ret = -EINVAL; 4616 break; 4617 } 4618 } 4619 4620 return ret; 4621 } 4622 4623 static void print_actions(struct seq_file *m, 4624 struct hist_trigger_data *hist_data, 4625 struct tracing_map_elt *elt) 4626 { 4627 unsigned int i; 4628 4629 for (i = 0; i < hist_data->n_actions; i++) { 4630 struct action_data *data = hist_data->actions[i]; 4631 4632 if (data->action == ACTION_SNAPSHOT) 4633 continue; 4634 4635 if (data->handler == HANDLER_ONMAX || 4636 data->handler == HANDLER_ONCHANGE) 4637 track_data_print(m, hist_data, elt, data); 4638 } 4639 } 4640 4641 static void print_action_spec(struct seq_file *m, 4642 struct hist_trigger_data *hist_data, 4643 struct action_data *data) 4644 { 4645 unsigned int i; 4646 4647 if (data->action == ACTION_SAVE) { 4648 for (i = 0; i < hist_data->n_save_vars; i++) { 4649 seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name); 4650 if (i < hist_data->n_save_vars - 1) 4651 seq_puts(m, ","); 4652 } 4653 } else if (data->action == ACTION_TRACE) { 4654 if (data->use_trace_keyword) 4655 seq_printf(m, "%s", data->synth_event_name); 4656 for (i = 0; i < data->n_params; i++) { 4657 if (i || data->use_trace_keyword) 4658 seq_puts(m, ","); 4659 seq_printf(m, "%s", data->params[i]); 4660 } 4661 } 4662 } 4663 4664 static void print_track_data_spec(struct seq_file *m, 4665 struct hist_trigger_data *hist_data, 4666 struct action_data *data) 4667 { 4668 if (data->handler == HANDLER_ONMAX) 4669 seq_puts(m, ":onmax("); 4670 else if (data->handler == HANDLER_ONCHANGE) 4671 seq_puts(m, ":onchange("); 4672 seq_printf(m, "%s", data->track_data.var_str); 4673 seq_printf(m, ").%s(", data->action_name); 4674 4675 print_action_spec(m, hist_data, data); 4676 4677 seq_puts(m, ")"); 4678 } 4679 4680 static void print_onmatch_spec(struct seq_file *m, 4681 struct hist_trigger_data *hist_data, 4682 struct action_data *data) 4683 { 4684 seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system, 4685 data->match_data.event); 4686 4687 seq_printf(m, "%s(", data->action_name); 4688 4689 print_action_spec(m, hist_data, data); 4690 4691 seq_puts(m, ")"); 4692 } 4693 4694 static bool actions_match(struct hist_trigger_data *hist_data, 4695 struct hist_trigger_data *hist_data_test) 4696 { 4697 unsigned int i, j; 4698 4699 if (hist_data->n_actions != hist_data_test->n_actions) 4700 return false; 4701 4702 for (i = 0; i < hist_data->n_actions; i++) { 4703 struct action_data *data = hist_data->actions[i]; 4704 struct action_data *data_test = hist_data_test->actions[i]; 4705 char *action_name, *action_name_test; 4706 4707 if (data->handler != data_test->handler) 4708 return false; 4709 if (data->action != data_test->action) 4710 return false; 4711 4712 if (data->n_params != data_test->n_params) 4713 return false; 4714 4715 for (j = 0; j < data->n_params; j++) { 4716 if (strcmp(data->params[j], data_test->params[j]) != 0) 4717 return false; 4718 } 4719 4720 if (data->use_trace_keyword) 4721 action_name = data->synth_event_name; 4722 else 4723 action_name = data->action_name; 4724 4725 if (data_test->use_trace_keyword) 4726 action_name_test = data_test->synth_event_name; 4727 else 4728 action_name_test = data_test->action_name; 4729 4730 if (strcmp(action_name, action_name_test) != 0) 4731 return false; 4732 4733 if (data->handler == HANDLER_ONMATCH) { 4734 if (strcmp(data->match_data.event_system, 4735 data_test->match_data.event_system) != 0) 4736 return false; 4737 if (strcmp(data->match_data.event, 4738 data_test->match_data.event) != 0) 4739 return false; 4740 } else if (data->handler == HANDLER_ONMAX || 4741 data->handler == HANDLER_ONCHANGE) { 4742 if (strcmp(data->track_data.var_str, 4743 data_test->track_data.var_str) != 0) 4744 return false; 4745 } 4746 } 4747 4748 return true; 4749 } 4750 4751 4752 static void print_actions_spec(struct seq_file *m, 4753 struct hist_trigger_data *hist_data) 4754 { 4755 unsigned int i; 4756 4757 for (i = 0; i < hist_data->n_actions; i++) { 4758 struct action_data *data = hist_data->actions[i]; 4759 4760 if (data->handler == HANDLER_ONMATCH) 4761 print_onmatch_spec(m, hist_data, data); 4762 else if (data->handler == HANDLER_ONMAX || 4763 data->handler == HANDLER_ONCHANGE) 4764 print_track_data_spec(m, hist_data, data); 4765 } 4766 } 4767 4768 static void destroy_field_var_hists(struct hist_trigger_data *hist_data) 4769 { 4770 unsigned int i; 4771 4772 for (i = 0; i < hist_data->n_field_var_hists; i++) { 4773 kfree(hist_data->field_var_hists[i]->cmd); 4774 kfree(hist_data->field_var_hists[i]); 4775 } 4776 } 4777 4778 static void destroy_hist_data(struct hist_trigger_data *hist_data) 4779 { 4780 if (!hist_data) 4781 return; 4782 4783 destroy_hist_trigger_attrs(hist_data->attrs); 4784 destroy_hist_fields(hist_data); 4785 tracing_map_destroy(hist_data->map); 4786 4787 destroy_actions(hist_data); 4788 destroy_field_vars(hist_data); 4789 destroy_field_var_hists(hist_data); 4790 4791 kfree(hist_data); 4792 } 4793 4794 static int create_tracing_map_fields(struct hist_trigger_data *hist_data) 4795 { 4796 struct tracing_map *map = hist_data->map; 4797 struct ftrace_event_field *field; 4798 struct hist_field *hist_field; 4799 int i, idx = 0; 4800 4801 for_each_hist_field(i, hist_data) { 4802 hist_field = hist_data->fields[i]; 4803 if (hist_field->flags & HIST_FIELD_FL_KEY) { 4804 tracing_map_cmp_fn_t cmp_fn; 4805 4806 field = hist_field->field; 4807 4808 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE) 4809 cmp_fn = tracing_map_cmp_none; 4810 else if (!field) 4811 cmp_fn = tracing_map_cmp_num(hist_field->size, 4812 hist_field->is_signed); 4813 else if (is_string_field(field)) 4814 cmp_fn = tracing_map_cmp_string; 4815 else 4816 cmp_fn = tracing_map_cmp_num(field->size, 4817 field->is_signed); 4818 idx = tracing_map_add_key_field(map, 4819 hist_field->offset, 4820 cmp_fn); 4821 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR)) 4822 idx = tracing_map_add_sum_field(map); 4823 4824 if (idx < 0) 4825 return idx; 4826 4827 if (hist_field->flags & HIST_FIELD_FL_VAR) { 4828 idx = tracing_map_add_var(map); 4829 if (idx < 0) 4830 return idx; 4831 hist_field->var.idx = idx; 4832 hist_field->var.hist_data = hist_data; 4833 } 4834 } 4835 4836 return 0; 4837 } 4838 4839 static struct hist_trigger_data * 4840 create_hist_data(unsigned int map_bits, 4841 struct hist_trigger_attrs *attrs, 4842 struct trace_event_file *file, 4843 bool remove) 4844 { 4845 const struct tracing_map_ops *map_ops = NULL; 4846 struct hist_trigger_data *hist_data; 4847 int ret = 0; 4848 4849 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL); 4850 if (!hist_data) 4851 return ERR_PTR(-ENOMEM); 4852 4853 hist_data->attrs = attrs; 4854 hist_data->remove = remove; 4855 hist_data->event_file = file; 4856 4857 ret = parse_actions(hist_data); 4858 if (ret) 4859 goto free; 4860 4861 ret = create_hist_fields(hist_data, file); 4862 if (ret) 4863 goto free; 4864 4865 ret = create_sort_keys(hist_data); 4866 if (ret) 4867 goto free; 4868 4869 map_ops = &hist_trigger_elt_data_ops; 4870 4871 hist_data->map = tracing_map_create(map_bits, hist_data->key_size, 4872 map_ops, hist_data); 4873 if (IS_ERR(hist_data->map)) { 4874 ret = PTR_ERR(hist_data->map); 4875 hist_data->map = NULL; 4876 goto free; 4877 } 4878 4879 ret = create_tracing_map_fields(hist_data); 4880 if (ret) 4881 goto free; 4882 out: 4883 return hist_data; 4884 free: 4885 hist_data->attrs = NULL; 4886 4887 destroy_hist_data(hist_data); 4888 4889 hist_data = ERR_PTR(ret); 4890 4891 goto out; 4892 } 4893 4894 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data, 4895 struct tracing_map_elt *elt, 4896 struct trace_buffer *buffer, void *rec, 4897 struct ring_buffer_event *rbe, 4898 u64 *var_ref_vals) 4899 { 4900 struct hist_elt_data *elt_data; 4901 struct hist_field *hist_field; 4902 unsigned int i, var_idx; 4903 u64 hist_val; 4904 4905 elt_data = elt->private_data; 4906 elt_data->var_ref_vals = var_ref_vals; 4907 4908 for_each_hist_val_field(i, hist_data) { 4909 hist_field = hist_data->fields[i]; 4910 hist_val = hist_field->fn(hist_field, elt, buffer, rbe, rec); 4911 if (hist_field->flags & HIST_FIELD_FL_VAR) { 4912 var_idx = hist_field->var.idx; 4913 4914 if (hist_field->flags & HIST_FIELD_FL_STRING) { 4915 unsigned int str_start, var_str_idx, idx; 4916 char *str, *val_str; 4917 4918 str_start = hist_data->n_field_var_str + 4919 hist_data->n_save_var_str; 4920 var_str_idx = hist_field->var_str_idx; 4921 idx = str_start + var_str_idx; 4922 4923 str = elt_data->field_var_str[idx]; 4924 val_str = (char *)(uintptr_t)hist_val; 4925 strscpy(str, val_str, hist_field->size); 4926 4927 hist_val = (u64)(uintptr_t)str; 4928 } 4929 tracing_map_set_var(elt, var_idx, hist_val); 4930 continue; 4931 } 4932 tracing_map_update_sum(elt, i, hist_val); 4933 } 4934 4935 for_each_hist_key_field(i, hist_data) { 4936 hist_field = hist_data->fields[i]; 4937 if (hist_field->flags & HIST_FIELD_FL_VAR) { 4938 hist_val = hist_field->fn(hist_field, elt, buffer, rbe, rec); 4939 var_idx = hist_field->var.idx; 4940 tracing_map_set_var(elt, var_idx, hist_val); 4941 } 4942 } 4943 4944 update_field_vars(hist_data, elt, buffer, rbe, rec); 4945 } 4946 4947 static inline void add_to_key(char *compound_key, void *key, 4948 struct hist_field *key_field, void *rec) 4949 { 4950 size_t size = key_field->size; 4951 4952 if (key_field->flags & HIST_FIELD_FL_STRING) { 4953 struct ftrace_event_field *field; 4954 4955 field = key_field->field; 4956 if (field->filter_type == FILTER_DYN_STRING) 4957 size = *(u32 *)(rec + field->offset) >> 16; 4958 else if (field->filter_type == FILTER_STATIC_STRING) 4959 size = field->size; 4960 4961 /* ensure NULL-termination */ 4962 if (size > key_field->size - 1) 4963 size = key_field->size - 1; 4964 4965 strncpy(compound_key + key_field->offset, (char *)key, size); 4966 } else 4967 memcpy(compound_key + key_field->offset, key, size); 4968 } 4969 4970 static void 4971 hist_trigger_actions(struct hist_trigger_data *hist_data, 4972 struct tracing_map_elt *elt, 4973 struct trace_buffer *buffer, void *rec, 4974 struct ring_buffer_event *rbe, void *key, 4975 u64 *var_ref_vals) 4976 { 4977 struct action_data *data; 4978 unsigned int i; 4979 4980 for (i = 0; i < hist_data->n_actions; i++) { 4981 data = hist_data->actions[i]; 4982 data->fn(hist_data, elt, buffer, rec, rbe, key, data, var_ref_vals); 4983 } 4984 } 4985 4986 static void event_hist_trigger(struct event_trigger_data *data, 4987 struct trace_buffer *buffer, void *rec, 4988 struct ring_buffer_event *rbe) 4989 { 4990 struct hist_trigger_data *hist_data = data->private_data; 4991 bool use_compound_key = (hist_data->n_keys > 1); 4992 unsigned long entries[HIST_STACKTRACE_DEPTH]; 4993 u64 var_ref_vals[TRACING_MAP_VARS_MAX]; 4994 char compound_key[HIST_KEY_SIZE_MAX]; 4995 struct tracing_map_elt *elt = NULL; 4996 struct hist_field *key_field; 4997 u64 field_contents; 4998 void *key = NULL; 4999 unsigned int i; 5000 5001 memset(compound_key, 0, hist_data->key_size); 5002 5003 for_each_hist_key_field(i, hist_data) { 5004 key_field = hist_data->fields[i]; 5005 5006 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) { 5007 memset(entries, 0, HIST_STACKTRACE_SIZE); 5008 stack_trace_save(entries, HIST_STACKTRACE_DEPTH, 5009 HIST_STACKTRACE_SKIP); 5010 key = entries; 5011 } else { 5012 field_contents = key_field->fn(key_field, elt, buffer, rbe, rec); 5013 if (key_field->flags & HIST_FIELD_FL_STRING) { 5014 key = (void *)(unsigned long)field_contents; 5015 use_compound_key = true; 5016 } else 5017 key = (void *)&field_contents; 5018 } 5019 5020 if (use_compound_key) 5021 add_to_key(compound_key, key, key_field, rec); 5022 } 5023 5024 if (use_compound_key) 5025 key = compound_key; 5026 5027 if (hist_data->n_var_refs && 5028 !resolve_var_refs(hist_data, key, var_ref_vals, false)) 5029 return; 5030 5031 elt = tracing_map_insert(hist_data->map, key); 5032 if (!elt) 5033 return; 5034 5035 hist_trigger_elt_update(hist_data, elt, buffer, rec, rbe, var_ref_vals); 5036 5037 if (resolve_var_refs(hist_data, key, var_ref_vals, true)) 5038 hist_trigger_actions(hist_data, elt, buffer, rec, rbe, key, var_ref_vals); 5039 } 5040 5041 static void hist_trigger_stacktrace_print(struct seq_file *m, 5042 unsigned long *stacktrace_entries, 5043 unsigned int max_entries) 5044 { 5045 unsigned int spaces = 8; 5046 unsigned int i; 5047 5048 for (i = 0; i < max_entries; i++) { 5049 if (!stacktrace_entries[i]) 5050 return; 5051 5052 seq_printf(m, "%*c", 1 + spaces, ' '); 5053 seq_printf(m, "%pS\n", (void*)stacktrace_entries[i]); 5054 } 5055 } 5056 5057 static void hist_trigger_print_key(struct seq_file *m, 5058 struct hist_trigger_data *hist_data, 5059 void *key, 5060 struct tracing_map_elt *elt) 5061 { 5062 struct hist_field *key_field; 5063 bool multiline = false; 5064 const char *field_name; 5065 unsigned int i; 5066 u64 uval; 5067 5068 seq_puts(m, "{ "); 5069 5070 for_each_hist_key_field(i, hist_data) { 5071 key_field = hist_data->fields[i]; 5072 5073 if (i > hist_data->n_vals) 5074 seq_puts(m, ", "); 5075 5076 field_name = hist_field_name(key_field, 0); 5077 5078 if (key_field->flags & HIST_FIELD_FL_HEX) { 5079 uval = *(u64 *)(key + key_field->offset); 5080 seq_printf(m, "%s: %llx", field_name, uval); 5081 } else if (key_field->flags & HIST_FIELD_FL_SYM) { 5082 uval = *(u64 *)(key + key_field->offset); 5083 seq_printf(m, "%s: [%llx] %-45ps", field_name, 5084 uval, (void *)(uintptr_t)uval); 5085 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) { 5086 uval = *(u64 *)(key + key_field->offset); 5087 seq_printf(m, "%s: [%llx] %-55pS", field_name, 5088 uval, (void *)(uintptr_t)uval); 5089 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) { 5090 struct hist_elt_data *elt_data = elt->private_data; 5091 char *comm; 5092 5093 if (WARN_ON_ONCE(!elt_data)) 5094 return; 5095 5096 comm = elt_data->comm; 5097 5098 uval = *(u64 *)(key + key_field->offset); 5099 seq_printf(m, "%s: %-16s[%10llu]", field_name, 5100 comm, uval); 5101 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) { 5102 const char *syscall_name; 5103 5104 uval = *(u64 *)(key + key_field->offset); 5105 syscall_name = get_syscall_name(uval); 5106 if (!syscall_name) 5107 syscall_name = "unknown_syscall"; 5108 5109 seq_printf(m, "%s: %-30s[%3llu]", field_name, 5110 syscall_name, uval); 5111 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) { 5112 seq_puts(m, "stacktrace:\n"); 5113 hist_trigger_stacktrace_print(m, 5114 key + key_field->offset, 5115 HIST_STACKTRACE_DEPTH); 5116 multiline = true; 5117 } else if (key_field->flags & HIST_FIELD_FL_LOG2) { 5118 seq_printf(m, "%s: ~ 2^%-2llu", field_name, 5119 *(u64 *)(key + key_field->offset)); 5120 } else if (key_field->flags & HIST_FIELD_FL_BUCKET) { 5121 unsigned long buckets = key_field->buckets; 5122 uval = *(u64 *)(key + key_field->offset); 5123 seq_printf(m, "%s: ~ %llu-%llu", field_name, 5124 uval, uval + buckets -1); 5125 } else if (key_field->flags & HIST_FIELD_FL_STRING) { 5126 seq_printf(m, "%s: %-50s", field_name, 5127 (char *)(key + key_field->offset)); 5128 } else { 5129 uval = *(u64 *)(key + key_field->offset); 5130 seq_printf(m, "%s: %10llu", field_name, uval); 5131 } 5132 } 5133 5134 if (!multiline) 5135 seq_puts(m, " "); 5136 5137 seq_puts(m, "}"); 5138 } 5139 5140 static void hist_trigger_entry_print(struct seq_file *m, 5141 struct hist_trigger_data *hist_data, 5142 void *key, 5143 struct tracing_map_elt *elt) 5144 { 5145 const char *field_name; 5146 unsigned int i; 5147 5148 hist_trigger_print_key(m, hist_data, key, elt); 5149 5150 seq_printf(m, " hitcount: %10llu", 5151 tracing_map_read_sum(elt, HITCOUNT_IDX)); 5152 5153 for (i = 1; i < hist_data->n_vals; i++) { 5154 field_name = hist_field_name(hist_data->fields[i], 0); 5155 5156 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR || 5157 hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR) 5158 continue; 5159 5160 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) { 5161 seq_printf(m, " %s: %10llx", field_name, 5162 tracing_map_read_sum(elt, i)); 5163 } else { 5164 seq_printf(m, " %s: %10llu", field_name, 5165 tracing_map_read_sum(elt, i)); 5166 } 5167 } 5168 5169 print_actions(m, hist_data, elt); 5170 5171 seq_puts(m, "\n"); 5172 } 5173 5174 static int print_entries(struct seq_file *m, 5175 struct hist_trigger_data *hist_data) 5176 { 5177 struct tracing_map_sort_entry **sort_entries = NULL; 5178 struct tracing_map *map = hist_data->map; 5179 int i, n_entries; 5180 5181 n_entries = tracing_map_sort_entries(map, hist_data->sort_keys, 5182 hist_data->n_sort_keys, 5183 &sort_entries); 5184 if (n_entries < 0) 5185 return n_entries; 5186 5187 for (i = 0; i < n_entries; i++) 5188 hist_trigger_entry_print(m, hist_data, 5189 sort_entries[i]->key, 5190 sort_entries[i]->elt); 5191 5192 tracing_map_destroy_sort_entries(sort_entries, n_entries); 5193 5194 return n_entries; 5195 } 5196 5197 static void hist_trigger_show(struct seq_file *m, 5198 struct event_trigger_data *data, int n) 5199 { 5200 struct hist_trigger_data *hist_data; 5201 int n_entries; 5202 5203 if (n > 0) 5204 seq_puts(m, "\n\n"); 5205 5206 seq_puts(m, "# event histogram\n#\n# trigger info: "); 5207 data->ops->print(m, data->ops, data); 5208 seq_puts(m, "#\n\n"); 5209 5210 hist_data = data->private_data; 5211 n_entries = print_entries(m, hist_data); 5212 if (n_entries < 0) 5213 n_entries = 0; 5214 5215 track_data_snapshot_print(m, hist_data); 5216 5217 seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n", 5218 (u64)atomic64_read(&hist_data->map->hits), 5219 n_entries, (u64)atomic64_read(&hist_data->map->drops)); 5220 } 5221 5222 static int hist_show(struct seq_file *m, void *v) 5223 { 5224 struct event_trigger_data *data; 5225 struct trace_event_file *event_file; 5226 int n = 0, ret = 0; 5227 5228 mutex_lock(&event_mutex); 5229 5230 event_file = event_file_data(m->private); 5231 if (unlikely(!event_file)) { 5232 ret = -ENODEV; 5233 goto out_unlock; 5234 } 5235 5236 list_for_each_entry(data, &event_file->triggers, list) { 5237 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST) 5238 hist_trigger_show(m, data, n++); 5239 } 5240 5241 out_unlock: 5242 mutex_unlock(&event_mutex); 5243 5244 return ret; 5245 } 5246 5247 static int event_hist_open(struct inode *inode, struct file *file) 5248 { 5249 int ret; 5250 5251 ret = security_locked_down(LOCKDOWN_TRACEFS); 5252 if (ret) 5253 return ret; 5254 5255 return single_open(file, hist_show, file); 5256 } 5257 5258 const struct file_operations event_hist_fops = { 5259 .open = event_hist_open, 5260 .read = seq_read, 5261 .llseek = seq_lseek, 5262 .release = single_release, 5263 }; 5264 5265 #ifdef CONFIG_HIST_TRIGGERS_DEBUG 5266 static void hist_field_debug_show_flags(struct seq_file *m, 5267 unsigned long flags) 5268 { 5269 seq_puts(m, " flags:\n"); 5270 5271 if (flags & HIST_FIELD_FL_KEY) 5272 seq_puts(m, " HIST_FIELD_FL_KEY\n"); 5273 else if (flags & HIST_FIELD_FL_HITCOUNT) 5274 seq_puts(m, " VAL: HIST_FIELD_FL_HITCOUNT\n"); 5275 else if (flags & HIST_FIELD_FL_VAR) 5276 seq_puts(m, " HIST_FIELD_FL_VAR\n"); 5277 else if (flags & HIST_FIELD_FL_VAR_REF) 5278 seq_puts(m, " HIST_FIELD_FL_VAR_REF\n"); 5279 else 5280 seq_puts(m, " VAL: normal u64 value\n"); 5281 5282 if (flags & HIST_FIELD_FL_ALIAS) 5283 seq_puts(m, " HIST_FIELD_FL_ALIAS\n"); 5284 else if (flags & HIST_FIELD_FL_CONST) 5285 seq_puts(m, " HIST_FIELD_FL_CONST\n"); 5286 } 5287 5288 static int hist_field_debug_show(struct seq_file *m, 5289 struct hist_field *field, unsigned long flags) 5290 { 5291 if ((field->flags & flags) != flags) { 5292 seq_printf(m, "ERROR: bad flags - %lx\n", flags); 5293 return -EINVAL; 5294 } 5295 5296 hist_field_debug_show_flags(m, field->flags); 5297 if (field->field) 5298 seq_printf(m, " ftrace_event_field name: %s\n", 5299 field->field->name); 5300 5301 if (field->flags & HIST_FIELD_FL_VAR) { 5302 seq_printf(m, " var.name: %s\n", field->var.name); 5303 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n", 5304 field->var.idx); 5305 } 5306 5307 if (field->flags & HIST_FIELD_FL_CONST) 5308 seq_printf(m, " constant: %llu\n", field->constant); 5309 5310 if (field->flags & HIST_FIELD_FL_ALIAS) 5311 seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n", 5312 field->var_ref_idx); 5313 5314 if (field->flags & HIST_FIELD_FL_VAR_REF) { 5315 seq_printf(m, " name: %s\n", field->name); 5316 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n", 5317 field->var.idx); 5318 seq_printf(m, " var.hist_data: %p\n", field->var.hist_data); 5319 seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n", 5320 field->var_ref_idx); 5321 if (field->system) 5322 seq_printf(m, " system: %s\n", field->system); 5323 if (field->event_name) 5324 seq_printf(m, " event_name: %s\n", field->event_name); 5325 } 5326 5327 seq_printf(m, " type: %s\n", field->type); 5328 seq_printf(m, " size: %u\n", field->size); 5329 seq_printf(m, " is_signed: %u\n", field->is_signed); 5330 5331 return 0; 5332 } 5333 5334 static int field_var_debug_show(struct seq_file *m, 5335 struct field_var *field_var, unsigned int i, 5336 bool save_vars) 5337 { 5338 const char *vars_name = save_vars ? "save_vars" : "field_vars"; 5339 struct hist_field *field; 5340 int ret = 0; 5341 5342 seq_printf(m, "\n hist_data->%s[%d]:\n", vars_name, i); 5343 5344 field = field_var->var; 5345 5346 seq_printf(m, "\n %s[%d].var:\n", vars_name, i); 5347 5348 hist_field_debug_show_flags(m, field->flags); 5349 seq_printf(m, " var.name: %s\n", field->var.name); 5350 seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n", 5351 field->var.idx); 5352 5353 field = field_var->val; 5354 5355 seq_printf(m, "\n %s[%d].val:\n", vars_name, i); 5356 if (field->field) 5357 seq_printf(m, " ftrace_event_field name: %s\n", 5358 field->field->name); 5359 else { 5360 ret = -EINVAL; 5361 goto out; 5362 } 5363 5364 seq_printf(m, " type: %s\n", field->type); 5365 seq_printf(m, " size: %u\n", field->size); 5366 seq_printf(m, " is_signed: %u\n", field->is_signed); 5367 out: 5368 return ret; 5369 } 5370 5371 static int hist_action_debug_show(struct seq_file *m, 5372 struct action_data *data, int i) 5373 { 5374 int ret = 0; 5375 5376 if (data->handler == HANDLER_ONMAX || 5377 data->handler == HANDLER_ONCHANGE) { 5378 seq_printf(m, "\n hist_data->actions[%d].track_data.var_ref:\n", i); 5379 ret = hist_field_debug_show(m, data->track_data.var_ref, 5380 HIST_FIELD_FL_VAR_REF); 5381 if (ret) 5382 goto out; 5383 5384 seq_printf(m, "\n hist_data->actions[%d].track_data.track_var:\n", i); 5385 ret = hist_field_debug_show(m, data->track_data.track_var, 5386 HIST_FIELD_FL_VAR); 5387 if (ret) 5388 goto out; 5389 } 5390 5391 if (data->handler == HANDLER_ONMATCH) { 5392 seq_printf(m, "\n hist_data->actions[%d].match_data.event_system: %s\n", 5393 i, data->match_data.event_system); 5394 seq_printf(m, " hist_data->actions[%d].match_data.event: %s\n", 5395 i, data->match_data.event); 5396 } 5397 out: 5398 return ret; 5399 } 5400 5401 static int hist_actions_debug_show(struct seq_file *m, 5402 struct hist_trigger_data *hist_data) 5403 { 5404 int i, ret = 0; 5405 5406 if (hist_data->n_actions) 5407 seq_puts(m, "\n action tracking variables (for onmax()/onchange()/onmatch()):\n"); 5408 5409 for (i = 0; i < hist_data->n_actions; i++) { 5410 struct action_data *action = hist_data->actions[i]; 5411 5412 ret = hist_action_debug_show(m, action, i); 5413 if (ret) 5414 goto out; 5415 } 5416 5417 if (hist_data->n_save_vars) 5418 seq_puts(m, "\n save action variables (save() params):\n"); 5419 5420 for (i = 0; i < hist_data->n_save_vars; i++) { 5421 ret = field_var_debug_show(m, hist_data->save_vars[i], i, true); 5422 if (ret) 5423 goto out; 5424 } 5425 out: 5426 return ret; 5427 } 5428 5429 static void hist_trigger_debug_show(struct seq_file *m, 5430 struct event_trigger_data *data, int n) 5431 { 5432 struct hist_trigger_data *hist_data; 5433 int i, ret; 5434 5435 if (n > 0) 5436 seq_puts(m, "\n\n"); 5437 5438 seq_puts(m, "# event histogram\n#\n# trigger info: "); 5439 data->ops->print(m, data->ops, data); 5440 seq_puts(m, "#\n\n"); 5441 5442 hist_data = data->private_data; 5443 5444 seq_printf(m, "hist_data: %p\n\n", hist_data); 5445 seq_printf(m, " n_vals: %u\n", hist_data->n_vals); 5446 seq_printf(m, " n_keys: %u\n", hist_data->n_keys); 5447 seq_printf(m, " n_fields: %u\n", hist_data->n_fields); 5448 5449 seq_puts(m, "\n val fields:\n\n"); 5450 5451 seq_puts(m, " hist_data->fields[0]:\n"); 5452 ret = hist_field_debug_show(m, hist_data->fields[0], 5453 HIST_FIELD_FL_HITCOUNT); 5454 if (ret) 5455 return; 5456 5457 for (i = 1; i < hist_data->n_vals; i++) { 5458 seq_printf(m, "\n hist_data->fields[%d]:\n", i); 5459 ret = hist_field_debug_show(m, hist_data->fields[i], 0); 5460 if (ret) 5461 return; 5462 } 5463 5464 seq_puts(m, "\n key fields:\n"); 5465 5466 for (i = hist_data->n_vals; i < hist_data->n_fields; i++) { 5467 seq_printf(m, "\n hist_data->fields[%d]:\n", i); 5468 ret = hist_field_debug_show(m, hist_data->fields[i], 5469 HIST_FIELD_FL_KEY); 5470 if (ret) 5471 return; 5472 } 5473 5474 if (hist_data->n_var_refs) 5475 seq_puts(m, "\n variable reference fields:\n"); 5476 5477 for (i = 0; i < hist_data->n_var_refs; i++) { 5478 seq_printf(m, "\n hist_data->var_refs[%d]:\n", i); 5479 ret = hist_field_debug_show(m, hist_data->var_refs[i], 5480 HIST_FIELD_FL_VAR_REF); 5481 if (ret) 5482 return; 5483 } 5484 5485 if (hist_data->n_field_vars) 5486 seq_puts(m, "\n field variables:\n"); 5487 5488 for (i = 0; i < hist_data->n_field_vars; i++) { 5489 ret = field_var_debug_show(m, hist_data->field_vars[i], i, false); 5490 if (ret) 5491 return; 5492 } 5493 5494 ret = hist_actions_debug_show(m, hist_data); 5495 if (ret) 5496 return; 5497 } 5498 5499 static int hist_debug_show(struct seq_file *m, void *v) 5500 { 5501 struct event_trigger_data *data; 5502 struct trace_event_file *event_file; 5503 int n = 0, ret = 0; 5504 5505 mutex_lock(&event_mutex); 5506 5507 event_file = event_file_data(m->private); 5508 if (unlikely(!event_file)) { 5509 ret = -ENODEV; 5510 goto out_unlock; 5511 } 5512 5513 list_for_each_entry(data, &event_file->triggers, list) { 5514 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST) 5515 hist_trigger_debug_show(m, data, n++); 5516 } 5517 5518 out_unlock: 5519 mutex_unlock(&event_mutex); 5520 5521 return ret; 5522 } 5523 5524 static int event_hist_debug_open(struct inode *inode, struct file *file) 5525 { 5526 int ret; 5527 5528 ret = security_locked_down(LOCKDOWN_TRACEFS); 5529 if (ret) 5530 return ret; 5531 5532 return single_open(file, hist_debug_show, file); 5533 } 5534 5535 const struct file_operations event_hist_debug_fops = { 5536 .open = event_hist_debug_open, 5537 .read = seq_read, 5538 .llseek = seq_lseek, 5539 .release = single_release, 5540 }; 5541 #endif 5542 5543 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field) 5544 { 5545 const char *field_name = hist_field_name(hist_field, 0); 5546 5547 if (hist_field->var.name) 5548 seq_printf(m, "%s=", hist_field->var.name); 5549 5550 if (hist_field->flags & HIST_FIELD_FL_CPU) 5551 seq_puts(m, "common_cpu"); 5552 else if (hist_field->flags & HIST_FIELD_FL_CONST) 5553 seq_printf(m, "%llu", hist_field->constant); 5554 else if (field_name) { 5555 if (hist_field->flags & HIST_FIELD_FL_VAR_REF || 5556 hist_field->flags & HIST_FIELD_FL_ALIAS) 5557 seq_putc(m, '$'); 5558 seq_printf(m, "%s", field_name); 5559 } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP) 5560 seq_puts(m, "common_timestamp"); 5561 5562 if (hist_field->flags) { 5563 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) && 5564 !(hist_field->flags & HIST_FIELD_FL_EXPR)) { 5565 const char *flags = get_hist_field_flags(hist_field); 5566 5567 if (flags) 5568 seq_printf(m, ".%s", flags); 5569 } 5570 } 5571 if (hist_field->buckets) 5572 seq_printf(m, "=%ld", hist_field->buckets); 5573 } 5574 5575 static int event_hist_trigger_print(struct seq_file *m, 5576 struct event_trigger_ops *ops, 5577 struct event_trigger_data *data) 5578 { 5579 struct hist_trigger_data *hist_data = data->private_data; 5580 struct hist_field *field; 5581 bool have_var = false; 5582 unsigned int i; 5583 5584 seq_puts(m, "hist:"); 5585 5586 if (data->name) 5587 seq_printf(m, "%s:", data->name); 5588 5589 seq_puts(m, "keys="); 5590 5591 for_each_hist_key_field(i, hist_data) { 5592 field = hist_data->fields[i]; 5593 5594 if (i > hist_data->n_vals) 5595 seq_puts(m, ","); 5596 5597 if (field->flags & HIST_FIELD_FL_STACKTRACE) 5598 seq_puts(m, "stacktrace"); 5599 else 5600 hist_field_print(m, field); 5601 } 5602 5603 seq_puts(m, ":vals="); 5604 5605 for_each_hist_val_field(i, hist_data) { 5606 field = hist_data->fields[i]; 5607 if (field->flags & HIST_FIELD_FL_VAR) { 5608 have_var = true; 5609 continue; 5610 } 5611 5612 if (i == HITCOUNT_IDX) 5613 seq_puts(m, "hitcount"); 5614 else { 5615 seq_puts(m, ","); 5616 hist_field_print(m, field); 5617 } 5618 } 5619 5620 if (have_var) { 5621 unsigned int n = 0; 5622 5623 seq_puts(m, ":"); 5624 5625 for_each_hist_val_field(i, hist_data) { 5626 field = hist_data->fields[i]; 5627 5628 if (field->flags & HIST_FIELD_FL_VAR) { 5629 if (n++) 5630 seq_puts(m, ","); 5631 hist_field_print(m, field); 5632 } 5633 } 5634 } 5635 5636 seq_puts(m, ":sort="); 5637 5638 for (i = 0; i < hist_data->n_sort_keys; i++) { 5639 struct tracing_map_sort_key *sort_key; 5640 unsigned int idx, first_key_idx; 5641 5642 /* skip VAR vals */ 5643 first_key_idx = hist_data->n_vals - hist_data->n_vars; 5644 5645 sort_key = &hist_data->sort_keys[i]; 5646 idx = sort_key->field_idx; 5647 5648 if (WARN_ON(idx >= HIST_FIELDS_MAX)) 5649 return -EINVAL; 5650 5651 if (i > 0) 5652 seq_puts(m, ","); 5653 5654 if (idx == HITCOUNT_IDX) 5655 seq_puts(m, "hitcount"); 5656 else { 5657 if (idx >= first_key_idx) 5658 idx += hist_data->n_vars; 5659 hist_field_print(m, hist_data->fields[idx]); 5660 } 5661 5662 if (sort_key->descending) 5663 seq_puts(m, ".descending"); 5664 } 5665 seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits)); 5666 if (hist_data->enable_timestamps) 5667 seq_printf(m, ":clock=%s", hist_data->attrs->clock); 5668 5669 print_actions_spec(m, hist_data); 5670 5671 if (data->filter_str) 5672 seq_printf(m, " if %s", data->filter_str); 5673 5674 if (data->paused) 5675 seq_puts(m, " [paused]"); 5676 else 5677 seq_puts(m, " [active]"); 5678 5679 seq_putc(m, '\n'); 5680 5681 return 0; 5682 } 5683 5684 static int event_hist_trigger_init(struct event_trigger_ops *ops, 5685 struct event_trigger_data *data) 5686 { 5687 struct hist_trigger_data *hist_data = data->private_data; 5688 5689 if (!data->ref && hist_data->attrs->name) 5690 save_named_trigger(hist_data->attrs->name, data); 5691 5692 data->ref++; 5693 5694 return 0; 5695 } 5696 5697 static void unregister_field_var_hists(struct hist_trigger_data *hist_data) 5698 { 5699 struct trace_event_file *file; 5700 unsigned int i; 5701 char *cmd; 5702 int ret; 5703 5704 for (i = 0; i < hist_data->n_field_var_hists; i++) { 5705 file = hist_data->field_var_hists[i]->hist_data->event_file; 5706 cmd = hist_data->field_var_hists[i]->cmd; 5707 ret = event_hist_trigger_func(&trigger_hist_cmd, file, 5708 "!hist", "hist", cmd); 5709 WARN_ON_ONCE(ret < 0); 5710 } 5711 } 5712 5713 static void event_hist_trigger_free(struct event_trigger_ops *ops, 5714 struct event_trigger_data *data) 5715 { 5716 struct hist_trigger_data *hist_data = data->private_data; 5717 5718 if (WARN_ON_ONCE(data->ref <= 0)) 5719 return; 5720 5721 data->ref--; 5722 if (!data->ref) { 5723 if (data->name) 5724 del_named_trigger(data); 5725 5726 trigger_data_free(data); 5727 5728 remove_hist_vars(hist_data); 5729 5730 unregister_field_var_hists(hist_data); 5731 5732 destroy_hist_data(hist_data); 5733 } 5734 } 5735 5736 static struct event_trigger_ops event_hist_trigger_ops = { 5737 .func = event_hist_trigger, 5738 .print = event_hist_trigger_print, 5739 .init = event_hist_trigger_init, 5740 .free = event_hist_trigger_free, 5741 }; 5742 5743 static int event_hist_trigger_named_init(struct event_trigger_ops *ops, 5744 struct event_trigger_data *data) 5745 { 5746 data->ref++; 5747 5748 save_named_trigger(data->named_data->name, data); 5749 5750 event_hist_trigger_init(ops, data->named_data); 5751 5752 return 0; 5753 } 5754 5755 static void event_hist_trigger_named_free(struct event_trigger_ops *ops, 5756 struct event_trigger_data *data) 5757 { 5758 if (WARN_ON_ONCE(data->ref <= 0)) 5759 return; 5760 5761 event_hist_trigger_free(ops, data->named_data); 5762 5763 data->ref--; 5764 if (!data->ref) { 5765 del_named_trigger(data); 5766 trigger_data_free(data); 5767 } 5768 } 5769 5770 static struct event_trigger_ops event_hist_trigger_named_ops = { 5771 .func = event_hist_trigger, 5772 .print = event_hist_trigger_print, 5773 .init = event_hist_trigger_named_init, 5774 .free = event_hist_trigger_named_free, 5775 }; 5776 5777 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd, 5778 char *param) 5779 { 5780 return &event_hist_trigger_ops; 5781 } 5782 5783 static void hist_clear(struct event_trigger_data *data) 5784 { 5785 struct hist_trigger_data *hist_data = data->private_data; 5786 5787 if (data->name) 5788 pause_named_trigger(data); 5789 5790 tracepoint_synchronize_unregister(); 5791 5792 tracing_map_clear(hist_data->map); 5793 5794 if (data->name) 5795 unpause_named_trigger(data); 5796 } 5797 5798 static bool compatible_field(struct ftrace_event_field *field, 5799 struct ftrace_event_field *test_field) 5800 { 5801 if (field == test_field) 5802 return true; 5803 if (field == NULL || test_field == NULL) 5804 return false; 5805 if (strcmp(field->name, test_field->name) != 0) 5806 return false; 5807 if (strcmp(field->type, test_field->type) != 0) 5808 return false; 5809 if (field->size != test_field->size) 5810 return false; 5811 if (field->is_signed != test_field->is_signed) 5812 return false; 5813 5814 return true; 5815 } 5816 5817 static bool hist_trigger_match(struct event_trigger_data *data, 5818 struct event_trigger_data *data_test, 5819 struct event_trigger_data *named_data, 5820 bool ignore_filter) 5821 { 5822 struct tracing_map_sort_key *sort_key, *sort_key_test; 5823 struct hist_trigger_data *hist_data, *hist_data_test; 5824 struct hist_field *key_field, *key_field_test; 5825 unsigned int i; 5826 5827 if (named_data && (named_data != data_test) && 5828 (named_data != data_test->named_data)) 5829 return false; 5830 5831 if (!named_data && is_named_trigger(data_test)) 5832 return false; 5833 5834 hist_data = data->private_data; 5835 hist_data_test = data_test->private_data; 5836 5837 if (hist_data->n_vals != hist_data_test->n_vals || 5838 hist_data->n_fields != hist_data_test->n_fields || 5839 hist_data->n_sort_keys != hist_data_test->n_sort_keys) 5840 return false; 5841 5842 if (!ignore_filter) { 5843 if ((data->filter_str && !data_test->filter_str) || 5844 (!data->filter_str && data_test->filter_str)) 5845 return false; 5846 } 5847 5848 for_each_hist_field(i, hist_data) { 5849 key_field = hist_data->fields[i]; 5850 key_field_test = hist_data_test->fields[i]; 5851 5852 if (key_field->flags != key_field_test->flags) 5853 return false; 5854 if (!compatible_field(key_field->field, key_field_test->field)) 5855 return false; 5856 if (key_field->offset != key_field_test->offset) 5857 return false; 5858 if (key_field->size != key_field_test->size) 5859 return false; 5860 if (key_field->is_signed != key_field_test->is_signed) 5861 return false; 5862 if (!!key_field->var.name != !!key_field_test->var.name) 5863 return false; 5864 if (key_field->var.name && 5865 strcmp(key_field->var.name, key_field_test->var.name) != 0) 5866 return false; 5867 } 5868 5869 for (i = 0; i < hist_data->n_sort_keys; i++) { 5870 sort_key = &hist_data->sort_keys[i]; 5871 sort_key_test = &hist_data_test->sort_keys[i]; 5872 5873 if (sort_key->field_idx != sort_key_test->field_idx || 5874 sort_key->descending != sort_key_test->descending) 5875 return false; 5876 } 5877 5878 if (!ignore_filter && data->filter_str && 5879 (strcmp(data->filter_str, data_test->filter_str) != 0)) 5880 return false; 5881 5882 if (!actions_match(hist_data, hist_data_test)) 5883 return false; 5884 5885 return true; 5886 } 5887 5888 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops, 5889 struct event_trigger_data *data, 5890 struct trace_event_file *file) 5891 { 5892 struct hist_trigger_data *hist_data = data->private_data; 5893 struct event_trigger_data *test, *named_data = NULL; 5894 struct trace_array *tr = file->tr; 5895 int ret = 0; 5896 5897 if (hist_data->attrs->name) { 5898 named_data = find_named_trigger(hist_data->attrs->name); 5899 if (named_data) { 5900 if (!hist_trigger_match(data, named_data, named_data, 5901 true)) { 5902 hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name)); 5903 ret = -EINVAL; 5904 goto out; 5905 } 5906 } 5907 } 5908 5909 if (hist_data->attrs->name && !named_data) 5910 goto new; 5911 5912 lockdep_assert_held(&event_mutex); 5913 5914 list_for_each_entry(test, &file->triggers, list) { 5915 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 5916 if (!hist_trigger_match(data, test, named_data, false)) 5917 continue; 5918 if (hist_data->attrs->pause) 5919 test->paused = true; 5920 else if (hist_data->attrs->cont) 5921 test->paused = false; 5922 else if (hist_data->attrs->clear) 5923 hist_clear(test); 5924 else { 5925 hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0); 5926 ret = -EEXIST; 5927 } 5928 goto out; 5929 } 5930 } 5931 new: 5932 if (hist_data->attrs->cont || hist_data->attrs->clear) { 5933 hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0); 5934 ret = -ENOENT; 5935 goto out; 5936 } 5937 5938 if (hist_data->attrs->pause) 5939 data->paused = true; 5940 5941 if (named_data) { 5942 data->private_data = named_data->private_data; 5943 set_named_trigger_data(data, named_data); 5944 data->ops = &event_hist_trigger_named_ops; 5945 } 5946 5947 if (data->ops->init) { 5948 ret = data->ops->init(data->ops, data); 5949 if (ret < 0) 5950 goto out; 5951 } 5952 5953 if (hist_data->enable_timestamps) { 5954 char *clock = hist_data->attrs->clock; 5955 5956 ret = tracing_set_clock(file->tr, hist_data->attrs->clock); 5957 if (ret) { 5958 hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock)); 5959 goto out; 5960 } 5961 5962 tracing_set_filter_buffering(file->tr, true); 5963 } 5964 5965 if (named_data) 5966 destroy_hist_data(hist_data); 5967 5968 ret++; 5969 out: 5970 return ret; 5971 } 5972 5973 static int hist_trigger_enable(struct event_trigger_data *data, 5974 struct trace_event_file *file) 5975 { 5976 int ret = 0; 5977 5978 list_add_tail_rcu(&data->list, &file->triggers); 5979 5980 update_cond_flag(file); 5981 5982 if (trace_event_trigger_enable_disable(file, 1) < 0) { 5983 list_del_rcu(&data->list); 5984 update_cond_flag(file); 5985 ret--; 5986 } 5987 5988 return ret; 5989 } 5990 5991 static bool have_hist_trigger_match(struct event_trigger_data *data, 5992 struct trace_event_file *file) 5993 { 5994 struct hist_trigger_data *hist_data = data->private_data; 5995 struct event_trigger_data *test, *named_data = NULL; 5996 bool match = false; 5997 5998 lockdep_assert_held(&event_mutex); 5999 6000 if (hist_data->attrs->name) 6001 named_data = find_named_trigger(hist_data->attrs->name); 6002 6003 list_for_each_entry(test, &file->triggers, list) { 6004 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 6005 if (hist_trigger_match(data, test, named_data, false)) { 6006 match = true; 6007 break; 6008 } 6009 } 6010 } 6011 6012 return match; 6013 } 6014 6015 static bool hist_trigger_check_refs(struct event_trigger_data *data, 6016 struct trace_event_file *file) 6017 { 6018 struct hist_trigger_data *hist_data = data->private_data; 6019 struct event_trigger_data *test, *named_data = NULL; 6020 6021 lockdep_assert_held(&event_mutex); 6022 6023 if (hist_data->attrs->name) 6024 named_data = find_named_trigger(hist_data->attrs->name); 6025 6026 list_for_each_entry(test, &file->triggers, list) { 6027 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 6028 if (!hist_trigger_match(data, test, named_data, false)) 6029 continue; 6030 hist_data = test->private_data; 6031 if (check_var_refs(hist_data)) 6032 return true; 6033 break; 6034 } 6035 } 6036 6037 return false; 6038 } 6039 6040 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops, 6041 struct event_trigger_data *data, 6042 struct trace_event_file *file) 6043 { 6044 struct hist_trigger_data *hist_data = data->private_data; 6045 struct event_trigger_data *test, *named_data = NULL; 6046 bool unregistered = false; 6047 6048 lockdep_assert_held(&event_mutex); 6049 6050 if (hist_data->attrs->name) 6051 named_data = find_named_trigger(hist_data->attrs->name); 6052 6053 list_for_each_entry(test, &file->triggers, list) { 6054 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 6055 if (!hist_trigger_match(data, test, named_data, false)) 6056 continue; 6057 unregistered = true; 6058 list_del_rcu(&test->list); 6059 trace_event_trigger_enable_disable(file, 0); 6060 update_cond_flag(file); 6061 break; 6062 } 6063 } 6064 6065 if (unregistered && test->ops->free) 6066 test->ops->free(test->ops, test); 6067 6068 if (hist_data->enable_timestamps) { 6069 if (!hist_data->remove || unregistered) 6070 tracing_set_filter_buffering(file->tr, false); 6071 } 6072 } 6073 6074 static bool hist_file_check_refs(struct trace_event_file *file) 6075 { 6076 struct hist_trigger_data *hist_data; 6077 struct event_trigger_data *test; 6078 6079 lockdep_assert_held(&event_mutex); 6080 6081 list_for_each_entry(test, &file->triggers, list) { 6082 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 6083 hist_data = test->private_data; 6084 if (check_var_refs(hist_data)) 6085 return true; 6086 } 6087 } 6088 6089 return false; 6090 } 6091 6092 static void hist_unreg_all(struct trace_event_file *file) 6093 { 6094 struct event_trigger_data *test, *n; 6095 struct hist_trigger_data *hist_data; 6096 struct synth_event *se; 6097 const char *se_name; 6098 6099 lockdep_assert_held(&event_mutex); 6100 6101 if (hist_file_check_refs(file)) 6102 return; 6103 6104 list_for_each_entry_safe(test, n, &file->triggers, list) { 6105 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 6106 hist_data = test->private_data; 6107 list_del_rcu(&test->list); 6108 trace_event_trigger_enable_disable(file, 0); 6109 6110 se_name = trace_event_name(file->event_call); 6111 se = find_synth_event(se_name); 6112 if (se) 6113 se->ref--; 6114 6115 update_cond_flag(file); 6116 if (hist_data->enable_timestamps) 6117 tracing_set_filter_buffering(file->tr, false); 6118 if (test->ops->free) 6119 test->ops->free(test->ops, test); 6120 } 6121 } 6122 } 6123 6124 static int event_hist_trigger_func(struct event_command *cmd_ops, 6125 struct trace_event_file *file, 6126 char *glob, char *cmd, char *param) 6127 { 6128 unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT; 6129 struct event_trigger_data *trigger_data; 6130 struct hist_trigger_attrs *attrs; 6131 struct event_trigger_ops *trigger_ops; 6132 struct hist_trigger_data *hist_data; 6133 struct synth_event *se; 6134 const char *se_name; 6135 bool remove = false; 6136 char *trigger, *p, *start; 6137 int ret = 0; 6138 6139 lockdep_assert_held(&event_mutex); 6140 6141 if (glob && strlen(glob)) { 6142 hist_err_clear(); 6143 last_cmd_set(file, param); 6144 } 6145 6146 if (!param) 6147 return -EINVAL; 6148 6149 if (glob[0] == '!') 6150 remove = true; 6151 6152 /* 6153 * separate the trigger from the filter (k:v [if filter]) 6154 * allowing for whitespace in the trigger 6155 */ 6156 p = trigger = param; 6157 do { 6158 p = strstr(p, "if"); 6159 if (!p) 6160 break; 6161 if (p == param) 6162 return -EINVAL; 6163 if (*(p - 1) != ' ' && *(p - 1) != '\t') { 6164 p++; 6165 continue; 6166 } 6167 if (p >= param + strlen(param) - (sizeof("if") - 1) - 1) 6168 return -EINVAL; 6169 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') { 6170 p++; 6171 continue; 6172 } 6173 break; 6174 } while (p); 6175 6176 if (!p) 6177 param = NULL; 6178 else { 6179 *(p - 1) = '\0'; 6180 param = strstrip(p); 6181 trigger = strstrip(trigger); 6182 } 6183 6184 /* 6185 * To simplify arithmetic expression parsing, replace occurrences of 6186 * '.sym-offset' modifier with '.symXoffset' 6187 */ 6188 start = strstr(trigger, ".sym-offset"); 6189 while (start) { 6190 *(start + 4) = 'X'; 6191 start = strstr(start + 11, ".sym-offset"); 6192 } 6193 6194 attrs = parse_hist_trigger_attrs(file->tr, trigger); 6195 if (IS_ERR(attrs)) 6196 return PTR_ERR(attrs); 6197 6198 if (attrs->map_bits) 6199 hist_trigger_bits = attrs->map_bits; 6200 6201 hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove); 6202 if (IS_ERR(hist_data)) { 6203 destroy_hist_trigger_attrs(attrs); 6204 return PTR_ERR(hist_data); 6205 } 6206 6207 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger); 6208 6209 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL); 6210 if (!trigger_data) { 6211 ret = -ENOMEM; 6212 goto out_free; 6213 } 6214 6215 trigger_data->count = -1; 6216 trigger_data->ops = trigger_ops; 6217 trigger_data->cmd_ops = cmd_ops; 6218 6219 INIT_LIST_HEAD(&trigger_data->list); 6220 RCU_INIT_POINTER(trigger_data->filter, NULL); 6221 6222 trigger_data->private_data = hist_data; 6223 6224 /* if param is non-empty, it's supposed to be a filter */ 6225 if (param && cmd_ops->set_filter) { 6226 ret = cmd_ops->set_filter(param, trigger_data, file); 6227 if (ret < 0) 6228 goto out_free; 6229 } 6230 6231 if (remove) { 6232 if (!have_hist_trigger_match(trigger_data, file)) 6233 goto out_free; 6234 6235 if (hist_trigger_check_refs(trigger_data, file)) { 6236 ret = -EBUSY; 6237 goto out_free; 6238 } 6239 6240 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file); 6241 se_name = trace_event_name(file->event_call); 6242 se = find_synth_event(se_name); 6243 if (se) 6244 se->ref--; 6245 ret = 0; 6246 goto out_free; 6247 } 6248 6249 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file); 6250 /* 6251 * The above returns on success the # of triggers registered, 6252 * but if it didn't register any it returns zero. Consider no 6253 * triggers registered a failure too. 6254 */ 6255 if (!ret) { 6256 if (!(attrs->pause || attrs->cont || attrs->clear)) 6257 ret = -ENOENT; 6258 goto out_free; 6259 } else if (ret < 0) 6260 goto out_free; 6261 6262 if (get_named_trigger_data(trigger_data)) 6263 goto enable; 6264 6265 if (has_hist_vars(hist_data)) 6266 save_hist_vars(hist_data); 6267 6268 ret = create_actions(hist_data); 6269 if (ret) 6270 goto out_unreg; 6271 6272 ret = tracing_map_init(hist_data->map); 6273 if (ret) 6274 goto out_unreg; 6275 enable: 6276 ret = hist_trigger_enable(trigger_data, file); 6277 if (ret) 6278 goto out_unreg; 6279 6280 se_name = trace_event_name(file->event_call); 6281 se = find_synth_event(se_name); 6282 if (se) 6283 se->ref++; 6284 /* Just return zero, not the number of registered triggers */ 6285 ret = 0; 6286 out: 6287 if (ret == 0) 6288 hist_err_clear(); 6289 6290 return ret; 6291 out_unreg: 6292 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file); 6293 out_free: 6294 if (cmd_ops->set_filter) 6295 cmd_ops->set_filter(NULL, trigger_data, NULL); 6296 6297 remove_hist_vars(hist_data); 6298 6299 kfree(trigger_data); 6300 6301 destroy_hist_data(hist_data); 6302 goto out; 6303 } 6304 6305 static struct event_command trigger_hist_cmd = { 6306 .name = "hist", 6307 .trigger_type = ETT_EVENT_HIST, 6308 .flags = EVENT_CMD_FL_NEEDS_REC, 6309 .func = event_hist_trigger_func, 6310 .reg = hist_register_trigger, 6311 .unreg = hist_unregister_trigger, 6312 .unreg_all = hist_unreg_all, 6313 .get_trigger_ops = event_hist_get_trigger_ops, 6314 .set_filter = set_trigger_filter, 6315 }; 6316 6317 __init int register_trigger_hist_cmd(void) 6318 { 6319 int ret; 6320 6321 ret = register_event_command(&trigger_hist_cmd); 6322 WARN_ON(ret < 0); 6323 6324 return ret; 6325 } 6326 6327 static void 6328 hist_enable_trigger(struct event_trigger_data *data, 6329 struct trace_buffer *buffer, void *rec, 6330 struct ring_buffer_event *event) 6331 { 6332 struct enable_trigger_data *enable_data = data->private_data; 6333 struct event_trigger_data *test; 6334 6335 list_for_each_entry_rcu(test, &enable_data->file->triggers, list, 6336 lockdep_is_held(&event_mutex)) { 6337 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 6338 if (enable_data->enable) 6339 test->paused = false; 6340 else 6341 test->paused = true; 6342 } 6343 } 6344 } 6345 6346 static void 6347 hist_enable_count_trigger(struct event_trigger_data *data, 6348 struct trace_buffer *buffer, void *rec, 6349 struct ring_buffer_event *event) 6350 { 6351 if (!data->count) 6352 return; 6353 6354 if (data->count != -1) 6355 (data->count)--; 6356 6357 hist_enable_trigger(data, buffer, rec, event); 6358 } 6359 6360 static struct event_trigger_ops hist_enable_trigger_ops = { 6361 .func = hist_enable_trigger, 6362 .print = event_enable_trigger_print, 6363 .init = event_trigger_init, 6364 .free = event_enable_trigger_free, 6365 }; 6366 6367 static struct event_trigger_ops hist_enable_count_trigger_ops = { 6368 .func = hist_enable_count_trigger, 6369 .print = event_enable_trigger_print, 6370 .init = event_trigger_init, 6371 .free = event_enable_trigger_free, 6372 }; 6373 6374 static struct event_trigger_ops hist_disable_trigger_ops = { 6375 .func = hist_enable_trigger, 6376 .print = event_enable_trigger_print, 6377 .init = event_trigger_init, 6378 .free = event_enable_trigger_free, 6379 }; 6380 6381 static struct event_trigger_ops hist_disable_count_trigger_ops = { 6382 .func = hist_enable_count_trigger, 6383 .print = event_enable_trigger_print, 6384 .init = event_trigger_init, 6385 .free = event_enable_trigger_free, 6386 }; 6387 6388 static struct event_trigger_ops * 6389 hist_enable_get_trigger_ops(char *cmd, char *param) 6390 { 6391 struct event_trigger_ops *ops; 6392 bool enable; 6393 6394 enable = (strcmp(cmd, ENABLE_HIST_STR) == 0); 6395 6396 if (enable) 6397 ops = param ? &hist_enable_count_trigger_ops : 6398 &hist_enable_trigger_ops; 6399 else 6400 ops = param ? &hist_disable_count_trigger_ops : 6401 &hist_disable_trigger_ops; 6402 6403 return ops; 6404 } 6405 6406 static void hist_enable_unreg_all(struct trace_event_file *file) 6407 { 6408 struct event_trigger_data *test, *n; 6409 6410 list_for_each_entry_safe(test, n, &file->triggers, list) { 6411 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) { 6412 list_del_rcu(&test->list); 6413 update_cond_flag(file); 6414 trace_event_trigger_enable_disable(file, 0); 6415 if (test->ops->free) 6416 test->ops->free(test->ops, test); 6417 } 6418 } 6419 } 6420 6421 static struct event_command trigger_hist_enable_cmd = { 6422 .name = ENABLE_HIST_STR, 6423 .trigger_type = ETT_HIST_ENABLE, 6424 .func = event_enable_trigger_func, 6425 .reg = event_enable_register_trigger, 6426 .unreg = event_enable_unregister_trigger, 6427 .unreg_all = hist_enable_unreg_all, 6428 .get_trigger_ops = hist_enable_get_trigger_ops, 6429 .set_filter = set_trigger_filter, 6430 }; 6431 6432 static struct event_command trigger_hist_disable_cmd = { 6433 .name = DISABLE_HIST_STR, 6434 .trigger_type = ETT_HIST_ENABLE, 6435 .func = event_enable_trigger_func, 6436 .reg = event_enable_register_trigger, 6437 .unreg = event_enable_unregister_trigger, 6438 .unreg_all = hist_enable_unreg_all, 6439 .get_trigger_ops = hist_enable_get_trigger_ops, 6440 .set_filter = set_trigger_filter, 6441 }; 6442 6443 static __init void unregister_trigger_hist_enable_disable_cmds(void) 6444 { 6445 unregister_event_command(&trigger_hist_enable_cmd); 6446 unregister_event_command(&trigger_hist_disable_cmd); 6447 } 6448 6449 __init int register_trigger_hist_enable_disable_cmds(void) 6450 { 6451 int ret; 6452 6453 ret = register_event_command(&trigger_hist_enable_cmd); 6454 if (WARN_ON(ret < 0)) 6455 return ret; 6456 ret = register_event_command(&trigger_hist_disable_cmd); 6457 if (WARN_ON(ret < 0)) 6458 unregister_trigger_hist_enable_disable_cmds(); 6459 6460 return ret; 6461 } 6462