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