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