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