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