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