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