1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * trace_events_hist - trace event hist triggers 4 * 5 * Copyright (C) 2015 Tom Zanussi <tom.zanussi@linux.intel.com> 6 */ 7 8 #include <linux/module.h> 9 #include <linux/kallsyms.h> 10 #include <linux/security.h> 11 #include <linux/mutex.h> 12 #include <linux/slab.h> 13 #include <linux/stacktrace.h> 14 #include <linux/rculist.h> 15 #include <linux/tracefs.h> 16 17 /* for gfp flag names */ 18 #include <linux/trace_events.h> 19 #include <trace/events/mmflags.h> 20 21 #include "tracing_map.h" 22 #include "trace.h" 23 #include "trace_dynevent.h" 24 25 #define SYNTH_SYSTEM "synthetic" 26 #define SYNTH_FIELDS_MAX 32 27 28 #define STR_VAR_LEN_MAX 32 /* must be multiple of sizeof(u64) */ 29 30 #define ERRORS \ 31 C(NONE, "No error"), \ 32 C(DUPLICATE_VAR, "Variable already defined"), \ 33 C(VAR_NOT_UNIQUE, "Variable name not unique, need to use fully qualified name (subsys.event.var) for variable"), \ 34 C(TOO_MANY_VARS, "Too many variables defined"), \ 35 C(MALFORMED_ASSIGNMENT, "Malformed assignment"), \ 36 C(NAMED_MISMATCH, "Named hist trigger doesn't match existing named trigger (includes variables)"), \ 37 C(TRIGGER_EEXIST, "Hist trigger already exists"), \ 38 C(TRIGGER_ENOENT_CLEAR, "Can't clear or continue a nonexistent hist trigger"), \ 39 C(SET_CLOCK_FAIL, "Couldn't set trace_clock"), \ 40 C(BAD_FIELD_MODIFIER, "Invalid field modifier"), \ 41 C(TOO_MANY_SUBEXPR, "Too many subexpressions (3 max)"), \ 42 C(TIMESTAMP_MISMATCH, "Timestamp units in expression don't match"), \ 43 C(TOO_MANY_FIELD_VARS, "Too many field variables defined"), \ 44 C(EVENT_FILE_NOT_FOUND, "Event file not found"), \ 45 C(HIST_NOT_FOUND, "Matching event histogram not found"), \ 46 C(HIST_CREATE_FAIL, "Couldn't create histogram for field"), \ 47 C(SYNTH_VAR_NOT_FOUND, "Couldn't find synthetic variable"), \ 48 C(SYNTH_EVENT_NOT_FOUND,"Couldn't find synthetic event"), \ 49 C(SYNTH_TYPE_MISMATCH, "Param type doesn't match synthetic event field type"), \ 50 C(SYNTH_COUNT_MISMATCH, "Param count doesn't match synthetic event field count"), \ 51 C(FIELD_VAR_PARSE_FAIL, "Couldn't parse field variable"), \ 52 C(VAR_CREATE_FIND_FAIL, "Couldn't create or find variable"), \ 53 C(ONX_NOT_VAR, "For onmax(x) or onchange(x), x must be a variable"), \ 54 C(ONX_VAR_NOT_FOUND, "Couldn't find onmax or onchange variable"), \ 55 C(ONX_VAR_CREATE_FAIL, "Couldn't create onmax or onchange variable"), \ 56 C(FIELD_VAR_CREATE_FAIL,"Couldn't create field variable"), \ 57 C(TOO_MANY_PARAMS, "Too many action params"), \ 58 C(PARAM_NOT_FOUND, "Couldn't find param"), \ 59 C(INVALID_PARAM, "Invalid action param"), \ 60 C(ACTION_NOT_FOUND, "No action found"), \ 61 C(NO_SAVE_PARAMS, "No params found for save()"), \ 62 C(TOO_MANY_SAVE_ACTIONS,"Can't have more than one save() action per hist"), \ 63 C(ACTION_MISMATCH, "Handler doesn't support action"), \ 64 C(NO_CLOSING_PAREN, "No closing paren found"), \ 65 C(SUBSYS_NOT_FOUND, "Missing subsystem"), \ 66 C(INVALID_SUBSYS_EVENT, "Invalid subsystem or event name"), \ 67 C(INVALID_REF_KEY, "Using variable references in keys not supported"), \ 68 C(VAR_NOT_FOUND, "Couldn't find variable"), \ 69 C(FIELD_NOT_FOUND, "Couldn't find field"), 70 71 #undef C 72 #define C(a, b) HIST_ERR_##a 73 74 enum { ERRORS }; 75 76 #undef C 77 #define C(a, b) b 78 79 static const char *err_text[] = { ERRORS }; 80 81 struct hist_field; 82 83 typedef u64 (*hist_field_fn_t) (struct hist_field *field, 84 struct tracing_map_elt *elt, 85 struct ring_buffer_event *rbe, 86 void *event); 87 88 #define HIST_FIELD_OPERANDS_MAX 2 89 #define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX) 90 #define HIST_ACTIONS_MAX 8 91 92 enum field_op_id { 93 FIELD_OP_NONE, 94 FIELD_OP_PLUS, 95 FIELD_OP_MINUS, 96 FIELD_OP_UNARY_MINUS, 97 }; 98 99 /* 100 * A hist_var (histogram variable) contains variable information for 101 * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF 102 * flag set. A hist_var has a variable name e.g. ts0, and is 103 * associated with a given histogram trigger, as specified by 104 * hist_data. The hist_var idx is the unique index assigned to the 105 * variable by the hist trigger's tracing_map. The idx is what is 106 * used to set a variable's value and, by a variable reference, to 107 * retrieve it. 108 */ 109 struct hist_var { 110 char *name; 111 struct hist_trigger_data *hist_data; 112 unsigned int idx; 113 }; 114 115 struct hist_field { 116 struct ftrace_event_field *field; 117 unsigned long flags; 118 hist_field_fn_t fn; 119 unsigned int ref; 120 unsigned int size; 121 unsigned int offset; 122 unsigned int is_signed; 123 const char *type; 124 struct hist_field *operands[HIST_FIELD_OPERANDS_MAX]; 125 struct hist_trigger_data *hist_data; 126 127 /* 128 * Variable fields contain variable-specific info in var. 129 */ 130 struct hist_var var; 131 enum field_op_id operator; 132 char *system; 133 char *event_name; 134 135 /* 136 * The name field is used for EXPR and VAR_REF fields. VAR 137 * fields contain the variable name in var.name. 138 */ 139 char *name; 140 141 /* 142 * When a histogram trigger is hit, if it has any references 143 * to variables, the values of those variables are collected 144 * into a var_ref_vals array by resolve_var_refs(). The 145 * current value of each variable is read from the tracing_map 146 * using the hist field's hist_var.idx and entered into the 147 * var_ref_idx entry i.e. var_ref_vals[var_ref_idx]. 148 */ 149 unsigned int var_ref_idx; 150 bool read_once; 151 }; 152 153 static u64 hist_field_none(struct hist_field *field, 154 struct tracing_map_elt *elt, 155 struct ring_buffer_event *rbe, 156 void *event) 157 { 158 return 0; 159 } 160 161 static u64 hist_field_counter(struct hist_field *field, 162 struct tracing_map_elt *elt, 163 struct ring_buffer_event *rbe, 164 void *event) 165 { 166 return 1; 167 } 168 169 static u64 hist_field_string(struct hist_field *hist_field, 170 struct tracing_map_elt *elt, 171 struct ring_buffer_event *rbe, 172 void *event) 173 { 174 char *addr = (char *)(event + hist_field->field->offset); 175 176 return (u64)(unsigned long)addr; 177 } 178 179 static u64 hist_field_dynstring(struct hist_field *hist_field, 180 struct tracing_map_elt *elt, 181 struct ring_buffer_event *rbe, 182 void *event) 183 { 184 u32 str_item = *(u32 *)(event + hist_field->field->offset); 185 int str_loc = str_item & 0xffff; 186 char *addr = (char *)(event + str_loc); 187 188 return (u64)(unsigned long)addr; 189 } 190 191 static u64 hist_field_pstring(struct hist_field *hist_field, 192 struct tracing_map_elt *elt, 193 struct ring_buffer_event *rbe, 194 void *event) 195 { 196 char **addr = (char **)(event + hist_field->field->offset); 197 198 return (u64)(unsigned long)*addr; 199 } 200 201 static u64 hist_field_log2(struct hist_field *hist_field, 202 struct tracing_map_elt *elt, 203 struct ring_buffer_event *rbe, 204 void *event) 205 { 206 struct hist_field *operand = hist_field->operands[0]; 207 208 u64 val = operand->fn(operand, elt, rbe, event); 209 210 return (u64) ilog2(roundup_pow_of_two(val)); 211 } 212 213 static u64 hist_field_plus(struct hist_field *hist_field, 214 struct tracing_map_elt *elt, 215 struct ring_buffer_event *rbe, 216 void *event) 217 { 218 struct hist_field *operand1 = hist_field->operands[0]; 219 struct hist_field *operand2 = hist_field->operands[1]; 220 221 u64 val1 = operand1->fn(operand1, elt, rbe, event); 222 u64 val2 = operand2->fn(operand2, elt, rbe, event); 223 224 return val1 + val2; 225 } 226 227 static u64 hist_field_minus(struct hist_field *hist_field, 228 struct tracing_map_elt *elt, 229 struct ring_buffer_event *rbe, 230 void *event) 231 { 232 struct hist_field *operand1 = hist_field->operands[0]; 233 struct hist_field *operand2 = hist_field->operands[1]; 234 235 u64 val1 = operand1->fn(operand1, elt, rbe, event); 236 u64 val2 = operand2->fn(operand2, elt, rbe, event); 237 238 return val1 - val2; 239 } 240 241 static u64 hist_field_unary_minus(struct hist_field *hist_field, 242 struct tracing_map_elt *elt, 243 struct ring_buffer_event *rbe, 244 void *event) 245 { 246 struct hist_field *operand = hist_field->operands[0]; 247 248 s64 sval = (s64)operand->fn(operand, elt, rbe, event); 249 u64 val = (u64)-sval; 250 251 return val; 252 } 253 254 #define DEFINE_HIST_FIELD_FN(type) \ 255 static u64 hist_field_##type(struct hist_field *hist_field, \ 256 struct tracing_map_elt *elt, \ 257 struct ring_buffer_event *rbe, \ 258 void *event) \ 259 { \ 260 type *addr = (type *)(event + hist_field->field->offset); \ 261 \ 262 return (u64)(unsigned long)*addr; \ 263 } 264 265 DEFINE_HIST_FIELD_FN(s64); 266 DEFINE_HIST_FIELD_FN(u64); 267 DEFINE_HIST_FIELD_FN(s32); 268 DEFINE_HIST_FIELD_FN(u32); 269 DEFINE_HIST_FIELD_FN(s16); 270 DEFINE_HIST_FIELD_FN(u16); 271 DEFINE_HIST_FIELD_FN(s8); 272 DEFINE_HIST_FIELD_FN(u8); 273 274 #define for_each_hist_field(i, hist_data) \ 275 for ((i) = 0; (i) < (hist_data)->n_fields; (i)++) 276 277 #define for_each_hist_val_field(i, hist_data) \ 278 for ((i) = 0; (i) < (hist_data)->n_vals; (i)++) 279 280 #define for_each_hist_key_field(i, hist_data) \ 281 for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++) 282 283 #define HIST_STACKTRACE_DEPTH 16 284 #define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long)) 285 #define HIST_STACKTRACE_SKIP 5 286 287 #define HITCOUNT_IDX 0 288 #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE) 289 290 enum hist_field_flags { 291 HIST_FIELD_FL_HITCOUNT = 1 << 0, 292 HIST_FIELD_FL_KEY = 1 << 1, 293 HIST_FIELD_FL_STRING = 1 << 2, 294 HIST_FIELD_FL_HEX = 1 << 3, 295 HIST_FIELD_FL_SYM = 1 << 4, 296 HIST_FIELD_FL_SYM_OFFSET = 1 << 5, 297 HIST_FIELD_FL_EXECNAME = 1 << 6, 298 HIST_FIELD_FL_SYSCALL = 1 << 7, 299 HIST_FIELD_FL_STACKTRACE = 1 << 8, 300 HIST_FIELD_FL_LOG2 = 1 << 9, 301 HIST_FIELD_FL_TIMESTAMP = 1 << 10, 302 HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11, 303 HIST_FIELD_FL_VAR = 1 << 12, 304 HIST_FIELD_FL_EXPR = 1 << 13, 305 HIST_FIELD_FL_VAR_REF = 1 << 14, 306 HIST_FIELD_FL_CPU = 1 << 15, 307 HIST_FIELD_FL_ALIAS = 1 << 16, 308 }; 309 310 struct var_defs { 311 unsigned int n_vars; 312 char *name[TRACING_MAP_VARS_MAX]; 313 char *expr[TRACING_MAP_VARS_MAX]; 314 }; 315 316 struct hist_trigger_attrs { 317 char *keys_str; 318 char *vals_str; 319 char *sort_key_str; 320 char *name; 321 char *clock; 322 bool pause; 323 bool cont; 324 bool clear; 325 bool ts_in_usecs; 326 unsigned int map_bits; 327 328 char *assignment_str[TRACING_MAP_VARS_MAX]; 329 unsigned int n_assignments; 330 331 char *action_str[HIST_ACTIONS_MAX]; 332 unsigned int n_actions; 333 334 struct var_defs var_defs; 335 }; 336 337 struct field_var { 338 struct hist_field *var; 339 struct hist_field *val; 340 }; 341 342 struct field_var_hist { 343 struct hist_trigger_data *hist_data; 344 char *cmd; 345 }; 346 347 struct hist_trigger_data { 348 struct hist_field *fields[HIST_FIELDS_MAX]; 349 unsigned int n_vals; 350 unsigned int n_keys; 351 unsigned int n_fields; 352 unsigned int n_vars; 353 unsigned int key_size; 354 struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX]; 355 unsigned int n_sort_keys; 356 struct trace_event_file *event_file; 357 struct hist_trigger_attrs *attrs; 358 struct tracing_map *map; 359 bool enable_timestamps; 360 bool remove; 361 struct hist_field *var_refs[TRACING_MAP_VARS_MAX]; 362 unsigned int n_var_refs; 363 364 struct action_data *actions[HIST_ACTIONS_MAX]; 365 unsigned int n_actions; 366 367 struct field_var *field_vars[SYNTH_FIELDS_MAX]; 368 unsigned int n_field_vars; 369 unsigned int n_field_var_str; 370 struct field_var_hist *field_var_hists[SYNTH_FIELDS_MAX]; 371 unsigned int n_field_var_hists; 372 373 struct field_var *save_vars[SYNTH_FIELDS_MAX]; 374 unsigned int n_save_vars; 375 unsigned int n_save_var_str; 376 }; 377 378 static int synth_event_create(int argc, const char **argv); 379 static int synth_event_show(struct seq_file *m, struct dyn_event *ev); 380 static int synth_event_release(struct dyn_event *ev); 381 static bool synth_event_is_busy(struct dyn_event *ev); 382 static bool synth_event_match(const char *system, const char *event, 383 int argc, const char **argv, struct dyn_event *ev); 384 385 static struct dyn_event_operations synth_event_ops = { 386 .create = synth_event_create, 387 .show = synth_event_show, 388 .is_busy = synth_event_is_busy, 389 .free = synth_event_release, 390 .match = synth_event_match, 391 }; 392 393 struct synth_field { 394 char *type; 395 char *name; 396 size_t size; 397 bool is_signed; 398 bool is_string; 399 }; 400 401 struct synth_event { 402 struct dyn_event devent; 403 int ref; 404 char *name; 405 struct synth_field **fields; 406 unsigned int n_fields; 407 unsigned int n_u64; 408 struct trace_event_class class; 409 struct trace_event_call call; 410 struct tracepoint *tp; 411 }; 412 413 static bool is_synth_event(struct dyn_event *ev) 414 { 415 return ev->ops == &synth_event_ops; 416 } 417 418 static struct synth_event *to_synth_event(struct dyn_event *ev) 419 { 420 return container_of(ev, struct synth_event, devent); 421 } 422 423 static bool synth_event_is_busy(struct dyn_event *ev) 424 { 425 struct synth_event *event = to_synth_event(ev); 426 427 return event->ref != 0; 428 } 429 430 static bool synth_event_match(const char *system, const char *event, 431 int argc, const char **argv, struct dyn_event *ev) 432 { 433 struct synth_event *sev = to_synth_event(ev); 434 435 return strcmp(sev->name, event) == 0 && 436 (!system || strcmp(system, SYNTH_SYSTEM) == 0); 437 } 438 439 struct action_data; 440 441 typedef void (*action_fn_t) (struct hist_trigger_data *hist_data, 442 struct tracing_map_elt *elt, void *rec, 443 struct ring_buffer_event *rbe, void *key, 444 struct action_data *data, u64 *var_ref_vals); 445 446 typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val); 447 448 enum handler_id { 449 HANDLER_ONMATCH = 1, 450 HANDLER_ONMAX, 451 HANDLER_ONCHANGE, 452 }; 453 454 enum action_id { 455 ACTION_SAVE = 1, 456 ACTION_TRACE, 457 ACTION_SNAPSHOT, 458 }; 459 460 struct action_data { 461 enum handler_id handler; 462 enum action_id action; 463 char *action_name; 464 action_fn_t fn; 465 466 unsigned int n_params; 467 char *params[SYNTH_FIELDS_MAX]; 468 469 /* 470 * When a histogram trigger is hit, the values of any 471 * references to variables, including variables being passed 472 * as parameters to synthetic events, are collected into a 473 * var_ref_vals array. This var_ref_idx array is an array of 474 * indices into the var_ref_vals array, one for each synthetic 475 * event param, and is passed to the synthetic event 476 * invocation. 477 */ 478 unsigned int var_ref_idx[TRACING_MAP_VARS_MAX]; 479 struct synth_event *synth_event; 480 bool use_trace_keyword; 481 char *synth_event_name; 482 483 union { 484 struct { 485 char *event; 486 char *event_system; 487 } match_data; 488 489 struct { 490 /* 491 * var_str contains the $-unstripped variable 492 * name referenced by var_ref, and used when 493 * printing the action. Because var_ref 494 * creation is deferred to create_actions(), 495 * we need a per-action way to save it until 496 * then, thus var_str. 497 */ 498 char *var_str; 499 500 /* 501 * var_ref refers to the variable being 502 * tracked e.g onmax($var). 503 */ 504 struct hist_field *var_ref; 505 506 /* 507 * track_var contains the 'invisible' tracking 508 * variable created to keep the current 509 * e.g. max value. 510 */ 511 struct hist_field *track_var; 512 513 check_track_val_fn_t check_val; 514 action_fn_t save_data; 515 } track_data; 516 }; 517 }; 518 519 struct track_data { 520 u64 track_val; 521 bool updated; 522 523 unsigned int key_len; 524 void *key; 525 struct tracing_map_elt elt; 526 527 struct action_data *action_data; 528 struct hist_trigger_data *hist_data; 529 }; 530 531 struct hist_elt_data { 532 char *comm; 533 u64 *var_ref_vals; 534 char *field_var_str[SYNTH_FIELDS_MAX]; 535 }; 536 537 struct snapshot_context { 538 struct tracing_map_elt *elt; 539 void *key; 540 }; 541 542 static void track_data_free(struct track_data *track_data) 543 { 544 struct hist_elt_data *elt_data; 545 546 if (!track_data) 547 return; 548 549 kfree(track_data->key); 550 551 elt_data = track_data->elt.private_data; 552 if (elt_data) { 553 kfree(elt_data->comm); 554 kfree(elt_data); 555 } 556 557 kfree(track_data); 558 } 559 560 static struct track_data *track_data_alloc(unsigned int key_len, 561 struct action_data *action_data, 562 struct hist_trigger_data *hist_data) 563 { 564 struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL); 565 struct hist_elt_data *elt_data; 566 567 if (!data) 568 return ERR_PTR(-ENOMEM); 569 570 data->key = kzalloc(key_len, GFP_KERNEL); 571 if (!data->key) { 572 track_data_free(data); 573 return ERR_PTR(-ENOMEM); 574 } 575 576 data->key_len = key_len; 577 data->action_data = action_data; 578 data->hist_data = hist_data; 579 580 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL); 581 if (!elt_data) { 582 track_data_free(data); 583 return ERR_PTR(-ENOMEM); 584 } 585 data->elt.private_data = elt_data; 586 587 elt_data->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL); 588 if (!elt_data->comm) { 589 track_data_free(data); 590 return ERR_PTR(-ENOMEM); 591 } 592 593 return data; 594 } 595 596 static char last_cmd[MAX_FILTER_STR_VAL]; 597 static char last_cmd_loc[MAX_FILTER_STR_VAL]; 598 599 static int errpos(char *str) 600 { 601 return err_pos(last_cmd, str); 602 } 603 604 static void last_cmd_set(struct trace_event_file *file, char *str) 605 { 606 const char *system = NULL, *name = NULL; 607 struct trace_event_call *call; 608 609 if (!str) 610 return; 611 612 strncpy(last_cmd, str, MAX_FILTER_STR_VAL - 1); 613 614 if (file) { 615 call = file->event_call; 616 617 system = call->class->system; 618 if (system) { 619 name = trace_event_name(call); 620 if (!name) 621 system = NULL; 622 } 623 } 624 625 if (system) 626 snprintf(last_cmd_loc, MAX_FILTER_STR_VAL, "hist:%s:%s", system, name); 627 } 628 629 static void hist_err(struct trace_array *tr, u8 err_type, u8 err_pos) 630 { 631 tracing_log_err(tr, last_cmd_loc, last_cmd, err_text, 632 err_type, err_pos); 633 } 634 635 static void hist_err_clear(void) 636 { 637 last_cmd[0] = '\0'; 638 last_cmd_loc[0] = '\0'; 639 } 640 641 struct synth_trace_event { 642 struct trace_entry ent; 643 u64 fields[]; 644 }; 645 646 static int synth_event_define_fields(struct trace_event_call *call) 647 { 648 struct synth_trace_event trace; 649 int offset = offsetof(typeof(trace), fields); 650 struct synth_event *event = call->data; 651 unsigned int i, size, n_u64; 652 char *name, *type; 653 bool is_signed; 654 int ret = 0; 655 656 for (i = 0, n_u64 = 0; i < event->n_fields; i++) { 657 size = event->fields[i]->size; 658 is_signed = event->fields[i]->is_signed; 659 type = event->fields[i]->type; 660 name = event->fields[i]->name; 661 ret = trace_define_field(call, type, name, offset, size, 662 is_signed, FILTER_OTHER); 663 if (ret) 664 break; 665 666 if (event->fields[i]->is_string) { 667 offset += STR_VAR_LEN_MAX; 668 n_u64 += STR_VAR_LEN_MAX / sizeof(u64); 669 } else { 670 offset += sizeof(u64); 671 n_u64++; 672 } 673 } 674 675 event->n_u64 = n_u64; 676 677 return ret; 678 } 679 680 static bool synth_field_signed(char *type) 681 { 682 if (str_has_prefix(type, "u")) 683 return false; 684 if (strcmp(type, "gfp_t") == 0) 685 return false; 686 687 return true; 688 } 689 690 static int synth_field_is_string(char *type) 691 { 692 if (strstr(type, "char[") != NULL) 693 return true; 694 695 return false; 696 } 697 698 static int synth_field_string_size(char *type) 699 { 700 char buf[4], *end, *start; 701 unsigned int len; 702 int size, err; 703 704 start = strstr(type, "char["); 705 if (start == NULL) 706 return -EINVAL; 707 start += sizeof("char[") - 1; 708 709 end = strchr(type, ']'); 710 if (!end || end < start) 711 return -EINVAL; 712 713 len = end - start; 714 if (len > 3) 715 return -EINVAL; 716 717 strncpy(buf, start, len); 718 buf[len] = '\0'; 719 720 err = kstrtouint(buf, 0, &size); 721 if (err) 722 return err; 723 724 if (size > STR_VAR_LEN_MAX) 725 return -EINVAL; 726 727 return size; 728 } 729 730 static int synth_field_size(char *type) 731 { 732 int size = 0; 733 734 if (strcmp(type, "s64") == 0) 735 size = sizeof(s64); 736 else if (strcmp(type, "u64") == 0) 737 size = sizeof(u64); 738 else if (strcmp(type, "s32") == 0) 739 size = sizeof(s32); 740 else if (strcmp(type, "u32") == 0) 741 size = sizeof(u32); 742 else if (strcmp(type, "s16") == 0) 743 size = sizeof(s16); 744 else if (strcmp(type, "u16") == 0) 745 size = sizeof(u16); 746 else if (strcmp(type, "s8") == 0) 747 size = sizeof(s8); 748 else if (strcmp(type, "u8") == 0) 749 size = sizeof(u8); 750 else if (strcmp(type, "char") == 0) 751 size = sizeof(char); 752 else if (strcmp(type, "unsigned char") == 0) 753 size = sizeof(unsigned char); 754 else if (strcmp(type, "int") == 0) 755 size = sizeof(int); 756 else if (strcmp(type, "unsigned int") == 0) 757 size = sizeof(unsigned int); 758 else if (strcmp(type, "long") == 0) 759 size = sizeof(long); 760 else if (strcmp(type, "unsigned long") == 0) 761 size = sizeof(unsigned long); 762 else if (strcmp(type, "pid_t") == 0) 763 size = sizeof(pid_t); 764 else if (strcmp(type, "gfp_t") == 0) 765 size = sizeof(gfp_t); 766 else if (synth_field_is_string(type)) 767 size = synth_field_string_size(type); 768 769 return size; 770 } 771 772 static const char *synth_field_fmt(char *type) 773 { 774 const char *fmt = "%llu"; 775 776 if (strcmp(type, "s64") == 0) 777 fmt = "%lld"; 778 else if (strcmp(type, "u64") == 0) 779 fmt = "%llu"; 780 else if (strcmp(type, "s32") == 0) 781 fmt = "%d"; 782 else if (strcmp(type, "u32") == 0) 783 fmt = "%u"; 784 else if (strcmp(type, "s16") == 0) 785 fmt = "%d"; 786 else if (strcmp(type, "u16") == 0) 787 fmt = "%u"; 788 else if (strcmp(type, "s8") == 0) 789 fmt = "%d"; 790 else if (strcmp(type, "u8") == 0) 791 fmt = "%u"; 792 else if (strcmp(type, "char") == 0) 793 fmt = "%d"; 794 else if (strcmp(type, "unsigned char") == 0) 795 fmt = "%u"; 796 else if (strcmp(type, "int") == 0) 797 fmt = "%d"; 798 else if (strcmp(type, "unsigned int") == 0) 799 fmt = "%u"; 800 else if (strcmp(type, "long") == 0) 801 fmt = "%ld"; 802 else if (strcmp(type, "unsigned long") == 0) 803 fmt = "%lu"; 804 else if (strcmp(type, "pid_t") == 0) 805 fmt = "%d"; 806 else if (strcmp(type, "gfp_t") == 0) 807 fmt = "%x"; 808 else if (synth_field_is_string(type)) 809 fmt = "%s"; 810 811 return fmt; 812 } 813 814 static void print_synth_event_num_val(struct trace_seq *s, 815 char *print_fmt, char *name, 816 int size, u64 val, char *space) 817 { 818 switch (size) { 819 case 1: 820 trace_seq_printf(s, print_fmt, name, (u8)val, space); 821 break; 822 823 case 2: 824 trace_seq_printf(s, print_fmt, name, (u16)val, space); 825 break; 826 827 case 4: 828 trace_seq_printf(s, print_fmt, name, (u32)val, space); 829 break; 830 831 default: 832 trace_seq_printf(s, print_fmt, name, val, space); 833 break; 834 } 835 } 836 837 static enum print_line_t print_synth_event(struct trace_iterator *iter, 838 int flags, 839 struct trace_event *event) 840 { 841 struct trace_array *tr = iter->tr; 842 struct trace_seq *s = &iter->seq; 843 struct synth_trace_event *entry; 844 struct synth_event *se; 845 unsigned int i, n_u64; 846 char print_fmt[32]; 847 const char *fmt; 848 849 entry = (struct synth_trace_event *)iter->ent; 850 se = container_of(event, struct synth_event, call.event); 851 852 trace_seq_printf(s, "%s: ", se->name); 853 854 for (i = 0, n_u64 = 0; i < se->n_fields; i++) { 855 if (trace_seq_has_overflowed(s)) 856 goto end; 857 858 fmt = synth_field_fmt(se->fields[i]->type); 859 860 /* parameter types */ 861 if (tr->trace_flags & TRACE_ITER_VERBOSE) 862 trace_seq_printf(s, "%s ", fmt); 863 864 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt); 865 866 /* parameter values */ 867 if (se->fields[i]->is_string) { 868 trace_seq_printf(s, print_fmt, se->fields[i]->name, 869 (char *)&entry->fields[n_u64], 870 i == se->n_fields - 1 ? "" : " "); 871 n_u64 += STR_VAR_LEN_MAX / sizeof(u64); 872 } else { 873 struct trace_print_flags __flags[] = { 874 __def_gfpflag_names, {-1, NULL} }; 875 char *space = (i == se->n_fields - 1 ? "" : " "); 876 877 print_synth_event_num_val(s, print_fmt, 878 se->fields[i]->name, 879 se->fields[i]->size, 880 entry->fields[n_u64], 881 space); 882 883 if (strcmp(se->fields[i]->type, "gfp_t") == 0) { 884 trace_seq_puts(s, " ("); 885 trace_print_flags_seq(s, "|", 886 entry->fields[n_u64], 887 __flags); 888 trace_seq_putc(s, ')'); 889 } 890 n_u64++; 891 } 892 } 893 end: 894 trace_seq_putc(s, '\n'); 895 896 return trace_handle_return(s); 897 } 898 899 static struct trace_event_functions synth_event_funcs = { 900 .trace = print_synth_event 901 }; 902 903 static notrace void trace_event_raw_event_synth(void *__data, 904 u64 *var_ref_vals, 905 unsigned int *var_ref_idx) 906 { 907 struct trace_event_file *trace_file = __data; 908 struct synth_trace_event *entry; 909 struct trace_event_buffer fbuffer; 910 struct ring_buffer *buffer; 911 struct synth_event *event; 912 unsigned int i, n_u64, val_idx; 913 int fields_size = 0; 914 915 event = trace_file->event_call->data; 916 917 if (trace_trigger_soft_disabled(trace_file)) 918 return; 919 920 fields_size = event->n_u64 * sizeof(u64); 921 922 /* 923 * Avoid ring buffer recursion detection, as this event 924 * is being performed within another event. 925 */ 926 buffer = trace_file->tr->trace_buffer.buffer; 927 ring_buffer_nest_start(buffer); 928 929 entry = trace_event_buffer_reserve(&fbuffer, trace_file, 930 sizeof(*entry) + fields_size); 931 if (!entry) 932 goto out; 933 934 for (i = 0, n_u64 = 0; i < event->n_fields; i++) { 935 val_idx = var_ref_idx[i]; 936 if (event->fields[i]->is_string) { 937 char *str_val = (char *)(long)var_ref_vals[val_idx]; 938 char *str_field = (char *)&entry->fields[n_u64]; 939 940 strscpy(str_field, str_val, STR_VAR_LEN_MAX); 941 n_u64 += STR_VAR_LEN_MAX / sizeof(u64); 942 } else { 943 struct synth_field *field = event->fields[i]; 944 u64 val = var_ref_vals[val_idx]; 945 946 switch (field->size) { 947 case 1: 948 *(u8 *)&entry->fields[n_u64] = (u8)val; 949 break; 950 951 case 2: 952 *(u16 *)&entry->fields[n_u64] = (u16)val; 953 break; 954 955 case 4: 956 *(u32 *)&entry->fields[n_u64] = (u32)val; 957 break; 958 959 default: 960 entry->fields[n_u64] = val; 961 break; 962 } 963 n_u64++; 964 } 965 } 966 967 trace_event_buffer_commit(&fbuffer); 968 out: 969 ring_buffer_nest_end(buffer); 970 } 971 972 static void free_synth_event_print_fmt(struct trace_event_call *call) 973 { 974 if (call) { 975 kfree(call->print_fmt); 976 call->print_fmt = NULL; 977 } 978 } 979 980 static int __set_synth_event_print_fmt(struct synth_event *event, 981 char *buf, int len) 982 { 983 const char *fmt; 984 int pos = 0; 985 int i; 986 987 /* When len=0, we just calculate the needed length */ 988 #define LEN_OR_ZERO (len ? len - pos : 0) 989 990 pos += snprintf(buf + pos, LEN_OR_ZERO, "\""); 991 for (i = 0; i < event->n_fields; i++) { 992 fmt = synth_field_fmt(event->fields[i]->type); 993 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s", 994 event->fields[i]->name, fmt, 995 i == event->n_fields - 1 ? "" : ", "); 996 } 997 pos += snprintf(buf + pos, LEN_OR_ZERO, "\""); 998 999 for (i = 0; i < event->n_fields; i++) { 1000 pos += snprintf(buf + pos, LEN_OR_ZERO, 1001 ", REC->%s", event->fields[i]->name); 1002 } 1003 1004 #undef LEN_OR_ZERO 1005 1006 /* return the length of print_fmt */ 1007 return pos; 1008 } 1009 1010 static int set_synth_event_print_fmt(struct trace_event_call *call) 1011 { 1012 struct synth_event *event = call->data; 1013 char *print_fmt; 1014 int len; 1015 1016 /* First: called with 0 length to calculate the needed length */ 1017 len = __set_synth_event_print_fmt(event, NULL, 0); 1018 1019 print_fmt = kmalloc(len + 1, GFP_KERNEL); 1020 if (!print_fmt) 1021 return -ENOMEM; 1022 1023 /* Second: actually write the @print_fmt */ 1024 __set_synth_event_print_fmt(event, print_fmt, len + 1); 1025 call->print_fmt = print_fmt; 1026 1027 return 0; 1028 } 1029 1030 static void free_synth_field(struct synth_field *field) 1031 { 1032 kfree(field->type); 1033 kfree(field->name); 1034 kfree(field); 1035 } 1036 1037 static struct synth_field *parse_synth_field(int argc, const char **argv, 1038 int *consumed) 1039 { 1040 struct synth_field *field; 1041 const char *prefix = NULL, *field_type = argv[0], *field_name, *array; 1042 int len, ret = 0; 1043 1044 if (field_type[0] == ';') 1045 field_type++; 1046 1047 if (!strcmp(field_type, "unsigned")) { 1048 if (argc < 3) 1049 return ERR_PTR(-EINVAL); 1050 prefix = "unsigned "; 1051 field_type = argv[1]; 1052 field_name = argv[2]; 1053 *consumed = 3; 1054 } else { 1055 field_name = argv[1]; 1056 *consumed = 2; 1057 } 1058 1059 field = kzalloc(sizeof(*field), GFP_KERNEL); 1060 if (!field) 1061 return ERR_PTR(-ENOMEM); 1062 1063 len = strlen(field_name); 1064 array = strchr(field_name, '['); 1065 if (array) 1066 len -= strlen(array); 1067 else if (field_name[len - 1] == ';') 1068 len--; 1069 1070 field->name = kmemdup_nul(field_name, len, GFP_KERNEL); 1071 if (!field->name) { 1072 ret = -ENOMEM; 1073 goto free; 1074 } 1075 1076 if (field_type[0] == ';') 1077 field_type++; 1078 len = strlen(field_type) + 1; 1079 if (array) 1080 len += strlen(array); 1081 if (prefix) 1082 len += strlen(prefix); 1083 1084 field->type = kzalloc(len, GFP_KERNEL); 1085 if (!field->type) { 1086 ret = -ENOMEM; 1087 goto free; 1088 } 1089 if (prefix) 1090 strcat(field->type, prefix); 1091 strcat(field->type, field_type); 1092 if (array) { 1093 strcat(field->type, array); 1094 if (field->type[len - 1] == ';') 1095 field->type[len - 1] = '\0'; 1096 } 1097 1098 field->size = synth_field_size(field->type); 1099 if (!field->size) { 1100 ret = -EINVAL; 1101 goto free; 1102 } 1103 1104 if (synth_field_is_string(field->type)) 1105 field->is_string = true; 1106 1107 field->is_signed = synth_field_signed(field->type); 1108 1109 out: 1110 return field; 1111 free: 1112 free_synth_field(field); 1113 field = ERR_PTR(ret); 1114 goto out; 1115 } 1116 1117 static void free_synth_tracepoint(struct tracepoint *tp) 1118 { 1119 if (!tp) 1120 return; 1121 1122 kfree(tp->name); 1123 kfree(tp); 1124 } 1125 1126 static struct tracepoint *alloc_synth_tracepoint(char *name) 1127 { 1128 struct tracepoint *tp; 1129 1130 tp = kzalloc(sizeof(*tp), GFP_KERNEL); 1131 if (!tp) 1132 return ERR_PTR(-ENOMEM); 1133 1134 tp->name = kstrdup(name, GFP_KERNEL); 1135 if (!tp->name) { 1136 kfree(tp); 1137 return ERR_PTR(-ENOMEM); 1138 } 1139 1140 return tp; 1141 } 1142 1143 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals, 1144 unsigned int *var_ref_idx); 1145 1146 static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals, 1147 unsigned int *var_ref_idx) 1148 { 1149 struct tracepoint *tp = event->tp; 1150 1151 if (unlikely(atomic_read(&tp->key.enabled) > 0)) { 1152 struct tracepoint_func *probe_func_ptr; 1153 synth_probe_func_t probe_func; 1154 void *__data; 1155 1156 if (!(cpu_online(raw_smp_processor_id()))) 1157 return; 1158 1159 probe_func_ptr = rcu_dereference_sched((tp)->funcs); 1160 if (probe_func_ptr) { 1161 do { 1162 probe_func = probe_func_ptr->func; 1163 __data = probe_func_ptr->data; 1164 probe_func(__data, var_ref_vals, var_ref_idx); 1165 } while ((++probe_func_ptr)->func); 1166 } 1167 } 1168 } 1169 1170 static struct synth_event *find_synth_event(const char *name) 1171 { 1172 struct dyn_event *pos; 1173 struct synth_event *event; 1174 1175 for_each_dyn_event(pos) { 1176 if (!is_synth_event(pos)) 1177 continue; 1178 event = to_synth_event(pos); 1179 if (strcmp(event->name, name) == 0) 1180 return event; 1181 } 1182 1183 return NULL; 1184 } 1185 1186 static int register_synth_event(struct synth_event *event) 1187 { 1188 struct trace_event_call *call = &event->call; 1189 int ret = 0; 1190 1191 event->call.class = &event->class; 1192 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL); 1193 if (!event->class.system) { 1194 ret = -ENOMEM; 1195 goto out; 1196 } 1197 1198 event->tp = alloc_synth_tracepoint(event->name); 1199 if (IS_ERR(event->tp)) { 1200 ret = PTR_ERR(event->tp); 1201 event->tp = NULL; 1202 goto out; 1203 } 1204 1205 INIT_LIST_HEAD(&call->class->fields); 1206 call->event.funcs = &synth_event_funcs; 1207 call->class->define_fields = synth_event_define_fields; 1208 1209 ret = register_trace_event(&call->event); 1210 if (!ret) { 1211 ret = -ENODEV; 1212 goto out; 1213 } 1214 call->flags = TRACE_EVENT_FL_TRACEPOINT; 1215 call->class->reg = trace_event_reg; 1216 call->class->probe = trace_event_raw_event_synth; 1217 call->data = event; 1218 call->tp = event->tp; 1219 1220 ret = trace_add_event_call(call); 1221 if (ret) { 1222 pr_warn("Failed to register synthetic event: %s\n", 1223 trace_event_name(call)); 1224 goto err; 1225 } 1226 1227 ret = set_synth_event_print_fmt(call); 1228 if (ret < 0) { 1229 trace_remove_event_call(call); 1230 goto err; 1231 } 1232 out: 1233 return ret; 1234 err: 1235 unregister_trace_event(&call->event); 1236 goto out; 1237 } 1238 1239 static int unregister_synth_event(struct synth_event *event) 1240 { 1241 struct trace_event_call *call = &event->call; 1242 int ret; 1243 1244 ret = trace_remove_event_call(call); 1245 1246 return ret; 1247 } 1248 1249 static void free_synth_event(struct synth_event *event) 1250 { 1251 unsigned int i; 1252 1253 if (!event) 1254 return; 1255 1256 for (i = 0; i < event->n_fields; i++) 1257 free_synth_field(event->fields[i]); 1258 1259 kfree(event->fields); 1260 kfree(event->name); 1261 kfree(event->class.system); 1262 free_synth_tracepoint(event->tp); 1263 free_synth_event_print_fmt(&event->call); 1264 kfree(event); 1265 } 1266 1267 static struct synth_event *alloc_synth_event(const char *name, int n_fields, 1268 struct synth_field **fields) 1269 { 1270 struct synth_event *event; 1271 unsigned int i; 1272 1273 event = kzalloc(sizeof(*event), GFP_KERNEL); 1274 if (!event) { 1275 event = ERR_PTR(-ENOMEM); 1276 goto out; 1277 } 1278 1279 event->name = kstrdup(name, GFP_KERNEL); 1280 if (!event->name) { 1281 kfree(event); 1282 event = ERR_PTR(-ENOMEM); 1283 goto out; 1284 } 1285 1286 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL); 1287 if (!event->fields) { 1288 free_synth_event(event); 1289 event = ERR_PTR(-ENOMEM); 1290 goto out; 1291 } 1292 1293 dyn_event_init(&event->devent, &synth_event_ops); 1294 1295 for (i = 0; i < n_fields; i++) 1296 event->fields[i] = fields[i]; 1297 1298 event->n_fields = n_fields; 1299 out: 1300 return event; 1301 } 1302 1303 static void action_trace(struct hist_trigger_data *hist_data, 1304 struct tracing_map_elt *elt, void *rec, 1305 struct ring_buffer_event *rbe, void *key, 1306 struct action_data *data, u64 *var_ref_vals) 1307 { 1308 struct synth_event *event = data->synth_event; 1309 1310 trace_synth(event, var_ref_vals, data->var_ref_idx); 1311 } 1312 1313 struct hist_var_data { 1314 struct list_head list; 1315 struct hist_trigger_data *hist_data; 1316 }; 1317 1318 static int __create_synth_event(int argc, const char *name, const char **argv) 1319 { 1320 struct synth_field *field, *fields[SYNTH_FIELDS_MAX]; 1321 struct synth_event *event = NULL; 1322 int i, consumed = 0, n_fields = 0, ret = 0; 1323 1324 /* 1325 * Argument syntax: 1326 * - Add synthetic event: <event_name> field[;field] ... 1327 * - Remove synthetic event: !<event_name> field[;field] ... 1328 * where 'field' = type field_name 1329 */ 1330 1331 if (name[0] == '\0' || argc < 1) 1332 return -EINVAL; 1333 1334 mutex_lock(&event_mutex); 1335 1336 event = find_synth_event(name); 1337 if (event) { 1338 ret = -EEXIST; 1339 goto out; 1340 } 1341 1342 for (i = 0; i < argc - 1; i++) { 1343 if (strcmp(argv[i], ";") == 0) 1344 continue; 1345 if (n_fields == SYNTH_FIELDS_MAX) { 1346 ret = -EINVAL; 1347 goto err; 1348 } 1349 1350 field = parse_synth_field(argc - i, &argv[i], &consumed); 1351 if (IS_ERR(field)) { 1352 ret = PTR_ERR(field); 1353 goto err; 1354 } 1355 fields[n_fields++] = field; 1356 i += consumed - 1; 1357 } 1358 1359 if (i < argc && strcmp(argv[i], ";") != 0) { 1360 ret = -EINVAL; 1361 goto err; 1362 } 1363 1364 event = alloc_synth_event(name, n_fields, fields); 1365 if (IS_ERR(event)) { 1366 ret = PTR_ERR(event); 1367 event = NULL; 1368 goto err; 1369 } 1370 ret = register_synth_event(event); 1371 if (!ret) 1372 dyn_event_add(&event->devent); 1373 else 1374 free_synth_event(event); 1375 out: 1376 mutex_unlock(&event_mutex); 1377 1378 return ret; 1379 err: 1380 for (i = 0; i < n_fields; i++) 1381 free_synth_field(fields[i]); 1382 1383 goto out; 1384 } 1385 1386 static int create_or_delete_synth_event(int argc, char **argv) 1387 { 1388 const char *name = argv[0]; 1389 struct synth_event *event = NULL; 1390 int ret; 1391 1392 /* trace_run_command() ensures argc != 0 */ 1393 if (name[0] == '!') { 1394 mutex_lock(&event_mutex); 1395 event = find_synth_event(name + 1); 1396 if (event) { 1397 if (event->ref) 1398 ret = -EBUSY; 1399 else { 1400 ret = unregister_synth_event(event); 1401 if (!ret) { 1402 dyn_event_remove(&event->devent); 1403 free_synth_event(event); 1404 } 1405 } 1406 } else 1407 ret = -ENOENT; 1408 mutex_unlock(&event_mutex); 1409 return ret; 1410 } 1411 1412 ret = __create_synth_event(argc - 1, name, (const char **)argv + 1); 1413 return ret == -ECANCELED ? -EINVAL : ret; 1414 } 1415 1416 static int synth_event_create(int argc, const char **argv) 1417 { 1418 const char *name = argv[0]; 1419 int len; 1420 1421 if (name[0] != 's' || name[1] != ':') 1422 return -ECANCELED; 1423 name += 2; 1424 1425 /* This interface accepts group name prefix */ 1426 if (strchr(name, '/')) { 1427 len = str_has_prefix(name, SYNTH_SYSTEM "/"); 1428 if (len == 0) 1429 return -EINVAL; 1430 name += len; 1431 } 1432 return __create_synth_event(argc - 1, name, argv + 1); 1433 } 1434 1435 static int synth_event_release(struct dyn_event *ev) 1436 { 1437 struct synth_event *event = to_synth_event(ev); 1438 int ret; 1439 1440 if (event->ref) 1441 return -EBUSY; 1442 1443 ret = unregister_synth_event(event); 1444 if (ret) 1445 return ret; 1446 1447 dyn_event_remove(ev); 1448 free_synth_event(event); 1449 return 0; 1450 } 1451 1452 static int __synth_event_show(struct seq_file *m, struct synth_event *event) 1453 { 1454 struct synth_field *field; 1455 unsigned int i; 1456 1457 seq_printf(m, "%s\t", event->name); 1458 1459 for (i = 0; i < event->n_fields; i++) { 1460 field = event->fields[i]; 1461 1462 /* parameter values */ 1463 seq_printf(m, "%s %s%s", field->type, field->name, 1464 i == event->n_fields - 1 ? "" : "; "); 1465 } 1466 1467 seq_putc(m, '\n'); 1468 1469 return 0; 1470 } 1471 1472 static int synth_event_show(struct seq_file *m, struct dyn_event *ev) 1473 { 1474 struct synth_event *event = to_synth_event(ev); 1475 1476 seq_printf(m, "s:%s/", event->class.system); 1477 1478 return __synth_event_show(m, event); 1479 } 1480 1481 static int synth_events_seq_show(struct seq_file *m, void *v) 1482 { 1483 struct dyn_event *ev = v; 1484 1485 if (!is_synth_event(ev)) 1486 return 0; 1487 1488 return __synth_event_show(m, to_synth_event(ev)); 1489 } 1490 1491 static const struct seq_operations synth_events_seq_op = { 1492 .start = dyn_event_seq_start, 1493 .next = dyn_event_seq_next, 1494 .stop = dyn_event_seq_stop, 1495 .show = synth_events_seq_show, 1496 }; 1497 1498 static int synth_events_open(struct inode *inode, struct file *file) 1499 { 1500 int ret; 1501 1502 ret = security_locked_down(LOCKDOWN_TRACEFS); 1503 if (ret) 1504 return ret; 1505 1506 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { 1507 ret = dyn_events_release_all(&synth_event_ops); 1508 if (ret < 0) 1509 return ret; 1510 } 1511 1512 return seq_open(file, &synth_events_seq_op); 1513 } 1514 1515 static ssize_t synth_events_write(struct file *file, 1516 const char __user *buffer, 1517 size_t count, loff_t *ppos) 1518 { 1519 return trace_parse_run_command(file, buffer, count, ppos, 1520 create_or_delete_synth_event); 1521 } 1522 1523 static const struct file_operations synth_events_fops = { 1524 .open = synth_events_open, 1525 .write = synth_events_write, 1526 .read = seq_read, 1527 .llseek = seq_lseek, 1528 .release = seq_release, 1529 }; 1530 1531 static u64 hist_field_timestamp(struct hist_field *hist_field, 1532 struct tracing_map_elt *elt, 1533 struct ring_buffer_event *rbe, 1534 void *event) 1535 { 1536 struct hist_trigger_data *hist_data = hist_field->hist_data; 1537 struct trace_array *tr = hist_data->event_file->tr; 1538 1539 u64 ts = ring_buffer_event_time_stamp(rbe); 1540 1541 if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr)) 1542 ts = ns2usecs(ts); 1543 1544 return ts; 1545 } 1546 1547 static u64 hist_field_cpu(struct hist_field *hist_field, 1548 struct tracing_map_elt *elt, 1549 struct ring_buffer_event *rbe, 1550 void *event) 1551 { 1552 int cpu = smp_processor_id(); 1553 1554 return cpu; 1555 } 1556 1557 /** 1558 * check_field_for_var_ref - Check if a VAR_REF field references a variable 1559 * @hist_field: The VAR_REF field to check 1560 * @var_data: The hist trigger that owns the variable 1561 * @var_idx: The trigger variable identifier 1562 * 1563 * Check the given VAR_REF field to see whether or not it references 1564 * the given variable associated with the given trigger. 1565 * 1566 * Return: The VAR_REF field if it does reference the variable, NULL if not 1567 */ 1568 static struct hist_field * 1569 check_field_for_var_ref(struct hist_field *hist_field, 1570 struct hist_trigger_data *var_data, 1571 unsigned int var_idx) 1572 { 1573 WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF)); 1574 1575 if (hist_field && hist_field->var.idx == var_idx && 1576 hist_field->var.hist_data == var_data) 1577 return hist_field; 1578 1579 return NULL; 1580 } 1581 1582 /** 1583 * find_var_ref - Check if a trigger has a reference to a trigger variable 1584 * @hist_data: The hist trigger that might have a reference to the variable 1585 * @var_data: The hist trigger that owns the variable 1586 * @var_idx: The trigger variable identifier 1587 * 1588 * Check the list of var_refs[] on the first hist trigger to see 1589 * whether any of them are references to the variable on the second 1590 * trigger. 1591 * 1592 * Return: The VAR_REF field referencing the variable if so, NULL if not 1593 */ 1594 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data, 1595 struct hist_trigger_data *var_data, 1596 unsigned int var_idx) 1597 { 1598 struct hist_field *hist_field; 1599 unsigned int i; 1600 1601 for (i = 0; i < hist_data->n_var_refs; i++) { 1602 hist_field = hist_data->var_refs[i]; 1603 if (check_field_for_var_ref(hist_field, var_data, var_idx)) 1604 return hist_field; 1605 } 1606 1607 return NULL; 1608 } 1609 1610 /** 1611 * find_any_var_ref - Check if there is a reference to a given trigger variable 1612 * @hist_data: The hist trigger 1613 * @var_idx: The trigger variable identifier 1614 * 1615 * Check to see whether the given variable is currently referenced by 1616 * any other trigger. 1617 * 1618 * The trigger the variable is defined on is explicitly excluded - the 1619 * assumption being that a self-reference doesn't prevent a trigger 1620 * from being removed. 1621 * 1622 * Return: The VAR_REF field referencing the variable if so, NULL if not 1623 */ 1624 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data, 1625 unsigned int var_idx) 1626 { 1627 struct trace_array *tr = hist_data->event_file->tr; 1628 struct hist_field *found = NULL; 1629 struct hist_var_data *var_data; 1630 1631 list_for_each_entry(var_data, &tr->hist_vars, list) { 1632 if (var_data->hist_data == hist_data) 1633 continue; 1634 found = find_var_ref(var_data->hist_data, hist_data, var_idx); 1635 if (found) 1636 break; 1637 } 1638 1639 return found; 1640 } 1641 1642 /** 1643 * check_var_refs - Check if there is a reference to any of trigger's variables 1644 * @hist_data: The hist trigger 1645 * 1646 * A trigger can define one or more variables. If any one of them is 1647 * currently referenced by any other trigger, this function will 1648 * determine that. 1649 1650 * Typically used to determine whether or not a trigger can be removed 1651 * - if there are any references to a trigger's variables, it cannot. 1652 * 1653 * Return: True if there is a reference to any of trigger's variables 1654 */ 1655 static bool check_var_refs(struct hist_trigger_data *hist_data) 1656 { 1657 struct hist_field *field; 1658 bool found = false; 1659 int i; 1660 1661 for_each_hist_field(i, hist_data) { 1662 field = hist_data->fields[i]; 1663 if (field && field->flags & HIST_FIELD_FL_VAR) { 1664 if (find_any_var_ref(hist_data, field->var.idx)) { 1665 found = true; 1666 break; 1667 } 1668 } 1669 } 1670 1671 return found; 1672 } 1673 1674 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data) 1675 { 1676 struct trace_array *tr = hist_data->event_file->tr; 1677 struct hist_var_data *var_data, *found = NULL; 1678 1679 list_for_each_entry(var_data, &tr->hist_vars, list) { 1680 if (var_data->hist_data == hist_data) { 1681 found = var_data; 1682 break; 1683 } 1684 } 1685 1686 return found; 1687 } 1688 1689 static bool field_has_hist_vars(struct hist_field *hist_field, 1690 unsigned int level) 1691 { 1692 int i; 1693 1694 if (level > 3) 1695 return false; 1696 1697 if (!hist_field) 1698 return false; 1699 1700 if (hist_field->flags & HIST_FIELD_FL_VAR || 1701 hist_field->flags & HIST_FIELD_FL_VAR_REF) 1702 return true; 1703 1704 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) { 1705 struct hist_field *operand; 1706 1707 operand = hist_field->operands[i]; 1708 if (field_has_hist_vars(operand, level + 1)) 1709 return true; 1710 } 1711 1712 return false; 1713 } 1714 1715 static bool has_hist_vars(struct hist_trigger_data *hist_data) 1716 { 1717 struct hist_field *hist_field; 1718 int i; 1719 1720 for_each_hist_field(i, hist_data) { 1721 hist_field = hist_data->fields[i]; 1722 if (field_has_hist_vars(hist_field, 0)) 1723 return true; 1724 } 1725 1726 return false; 1727 } 1728 1729 static int save_hist_vars(struct hist_trigger_data *hist_data) 1730 { 1731 struct trace_array *tr = hist_data->event_file->tr; 1732 struct hist_var_data *var_data; 1733 1734 var_data = find_hist_vars(hist_data); 1735 if (var_data) 1736 return 0; 1737 1738 if (tracing_check_open_get_tr(tr)) 1739 return -ENODEV; 1740 1741 var_data = kzalloc(sizeof(*var_data), GFP_KERNEL); 1742 if (!var_data) { 1743 trace_array_put(tr); 1744 return -ENOMEM; 1745 } 1746 1747 var_data->hist_data = hist_data; 1748 list_add(&var_data->list, &tr->hist_vars); 1749 1750 return 0; 1751 } 1752 1753 static void remove_hist_vars(struct hist_trigger_data *hist_data) 1754 { 1755 struct trace_array *tr = hist_data->event_file->tr; 1756 struct hist_var_data *var_data; 1757 1758 var_data = find_hist_vars(hist_data); 1759 if (!var_data) 1760 return; 1761 1762 if (WARN_ON(check_var_refs(hist_data))) 1763 return; 1764 1765 list_del(&var_data->list); 1766 1767 kfree(var_data); 1768 1769 trace_array_put(tr); 1770 } 1771 1772 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data, 1773 const char *var_name) 1774 { 1775 struct hist_field *hist_field, *found = NULL; 1776 int i; 1777 1778 for_each_hist_field(i, hist_data) { 1779 hist_field = hist_data->fields[i]; 1780 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR && 1781 strcmp(hist_field->var.name, var_name) == 0) { 1782 found = hist_field; 1783 break; 1784 } 1785 } 1786 1787 return found; 1788 } 1789 1790 static struct hist_field *find_var(struct hist_trigger_data *hist_data, 1791 struct trace_event_file *file, 1792 const char *var_name) 1793 { 1794 struct hist_trigger_data *test_data; 1795 struct event_trigger_data *test; 1796 struct hist_field *hist_field; 1797 1798 lockdep_assert_held(&event_mutex); 1799 1800 hist_field = find_var_field(hist_data, var_name); 1801 if (hist_field) 1802 return hist_field; 1803 1804 list_for_each_entry(test, &file->triggers, list) { 1805 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 1806 test_data = test->private_data; 1807 hist_field = find_var_field(test_data, var_name); 1808 if (hist_field) 1809 return hist_field; 1810 } 1811 } 1812 1813 return NULL; 1814 } 1815 1816 static struct trace_event_file *find_var_file(struct trace_array *tr, 1817 char *system, 1818 char *event_name, 1819 char *var_name) 1820 { 1821 struct hist_trigger_data *var_hist_data; 1822 struct hist_var_data *var_data; 1823 struct trace_event_file *file, *found = NULL; 1824 1825 if (system) 1826 return find_event_file(tr, system, event_name); 1827 1828 list_for_each_entry(var_data, &tr->hist_vars, list) { 1829 var_hist_data = var_data->hist_data; 1830 file = var_hist_data->event_file; 1831 if (file == found) 1832 continue; 1833 1834 if (find_var_field(var_hist_data, var_name)) { 1835 if (found) { 1836 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name)); 1837 return NULL; 1838 } 1839 1840 found = file; 1841 } 1842 } 1843 1844 return found; 1845 } 1846 1847 static struct hist_field *find_file_var(struct trace_event_file *file, 1848 const char *var_name) 1849 { 1850 struct hist_trigger_data *test_data; 1851 struct event_trigger_data *test; 1852 struct hist_field *hist_field; 1853 1854 lockdep_assert_held(&event_mutex); 1855 1856 list_for_each_entry(test, &file->triggers, list) { 1857 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 1858 test_data = test->private_data; 1859 hist_field = find_var_field(test_data, var_name); 1860 if (hist_field) 1861 return hist_field; 1862 } 1863 } 1864 1865 return NULL; 1866 } 1867 1868 static struct hist_field * 1869 find_match_var(struct hist_trigger_data *hist_data, char *var_name) 1870 { 1871 struct trace_array *tr = hist_data->event_file->tr; 1872 struct hist_field *hist_field, *found = NULL; 1873 struct trace_event_file *file; 1874 unsigned int i; 1875 1876 for (i = 0; i < hist_data->n_actions; i++) { 1877 struct action_data *data = hist_data->actions[i]; 1878 1879 if (data->handler == HANDLER_ONMATCH) { 1880 char *system = data->match_data.event_system; 1881 char *event_name = data->match_data.event; 1882 1883 file = find_var_file(tr, system, event_name, var_name); 1884 if (!file) 1885 continue; 1886 hist_field = find_file_var(file, var_name); 1887 if (hist_field) { 1888 if (found) { 1889 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, 1890 errpos(var_name)); 1891 return ERR_PTR(-EINVAL); 1892 } 1893 1894 found = hist_field; 1895 } 1896 } 1897 } 1898 return found; 1899 } 1900 1901 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data, 1902 char *system, 1903 char *event_name, 1904 char *var_name) 1905 { 1906 struct trace_array *tr = hist_data->event_file->tr; 1907 struct hist_field *hist_field = NULL; 1908 struct trace_event_file *file; 1909 1910 if (!system || !event_name) { 1911 hist_field = find_match_var(hist_data, var_name); 1912 if (IS_ERR(hist_field)) 1913 return NULL; 1914 if (hist_field) 1915 return hist_field; 1916 } 1917 1918 file = find_var_file(tr, system, event_name, var_name); 1919 if (!file) 1920 return NULL; 1921 1922 hist_field = find_file_var(file, var_name); 1923 1924 return hist_field; 1925 } 1926 1927 static u64 hist_field_var_ref(struct hist_field *hist_field, 1928 struct tracing_map_elt *elt, 1929 struct ring_buffer_event *rbe, 1930 void *event) 1931 { 1932 struct hist_elt_data *elt_data; 1933 u64 var_val = 0; 1934 1935 if (WARN_ON_ONCE(!elt)) 1936 return var_val; 1937 1938 elt_data = elt->private_data; 1939 var_val = elt_data->var_ref_vals[hist_field->var_ref_idx]; 1940 1941 return var_val; 1942 } 1943 1944 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key, 1945 u64 *var_ref_vals, bool self) 1946 { 1947 struct hist_trigger_data *var_data; 1948 struct tracing_map_elt *var_elt; 1949 struct hist_field *hist_field; 1950 unsigned int i, var_idx; 1951 bool resolved = true; 1952 u64 var_val = 0; 1953 1954 for (i = 0; i < hist_data->n_var_refs; i++) { 1955 hist_field = hist_data->var_refs[i]; 1956 var_idx = hist_field->var.idx; 1957 var_data = hist_field->var.hist_data; 1958 1959 if (var_data == NULL) { 1960 resolved = false; 1961 break; 1962 } 1963 1964 if ((self && var_data != hist_data) || 1965 (!self && var_data == hist_data)) 1966 continue; 1967 1968 var_elt = tracing_map_lookup(var_data->map, key); 1969 if (!var_elt) { 1970 resolved = false; 1971 break; 1972 } 1973 1974 if (!tracing_map_var_set(var_elt, var_idx)) { 1975 resolved = false; 1976 break; 1977 } 1978 1979 if (self || !hist_field->read_once) 1980 var_val = tracing_map_read_var(var_elt, var_idx); 1981 else 1982 var_val = tracing_map_read_var_once(var_elt, var_idx); 1983 1984 var_ref_vals[i] = var_val; 1985 } 1986 1987 return resolved; 1988 } 1989 1990 static const char *hist_field_name(struct hist_field *field, 1991 unsigned int level) 1992 { 1993 const char *field_name = ""; 1994 1995 if (level > 1) 1996 return field_name; 1997 1998 if (field->field) 1999 field_name = field->field->name; 2000 else if (field->flags & HIST_FIELD_FL_LOG2 || 2001 field->flags & HIST_FIELD_FL_ALIAS) 2002 field_name = hist_field_name(field->operands[0], ++level); 2003 else if (field->flags & HIST_FIELD_FL_CPU) 2004 field_name = "cpu"; 2005 else if (field->flags & HIST_FIELD_FL_EXPR || 2006 field->flags & HIST_FIELD_FL_VAR_REF) { 2007 if (field->system) { 2008 static char full_name[MAX_FILTER_STR_VAL]; 2009 2010 strcat(full_name, field->system); 2011 strcat(full_name, "."); 2012 strcat(full_name, field->event_name); 2013 strcat(full_name, "."); 2014 strcat(full_name, field->name); 2015 field_name = full_name; 2016 } else 2017 field_name = field->name; 2018 } else if (field->flags & HIST_FIELD_FL_TIMESTAMP) 2019 field_name = "common_timestamp"; 2020 2021 if (field_name == NULL) 2022 field_name = ""; 2023 2024 return field_name; 2025 } 2026 2027 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed) 2028 { 2029 hist_field_fn_t fn = NULL; 2030 2031 switch (field_size) { 2032 case 8: 2033 if (field_is_signed) 2034 fn = hist_field_s64; 2035 else 2036 fn = hist_field_u64; 2037 break; 2038 case 4: 2039 if (field_is_signed) 2040 fn = hist_field_s32; 2041 else 2042 fn = hist_field_u32; 2043 break; 2044 case 2: 2045 if (field_is_signed) 2046 fn = hist_field_s16; 2047 else 2048 fn = hist_field_u16; 2049 break; 2050 case 1: 2051 if (field_is_signed) 2052 fn = hist_field_s8; 2053 else 2054 fn = hist_field_u8; 2055 break; 2056 } 2057 2058 return fn; 2059 } 2060 2061 static int parse_map_size(char *str) 2062 { 2063 unsigned long size, map_bits; 2064 int ret; 2065 2066 ret = kstrtoul(str, 0, &size); 2067 if (ret) 2068 goto out; 2069 2070 map_bits = ilog2(roundup_pow_of_two(size)); 2071 if (map_bits < TRACING_MAP_BITS_MIN || 2072 map_bits > TRACING_MAP_BITS_MAX) 2073 ret = -EINVAL; 2074 else 2075 ret = map_bits; 2076 out: 2077 return ret; 2078 } 2079 2080 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs) 2081 { 2082 unsigned int i; 2083 2084 if (!attrs) 2085 return; 2086 2087 for (i = 0; i < attrs->n_assignments; i++) 2088 kfree(attrs->assignment_str[i]); 2089 2090 for (i = 0; i < attrs->n_actions; i++) 2091 kfree(attrs->action_str[i]); 2092 2093 kfree(attrs->name); 2094 kfree(attrs->sort_key_str); 2095 kfree(attrs->keys_str); 2096 kfree(attrs->vals_str); 2097 kfree(attrs->clock); 2098 kfree(attrs); 2099 } 2100 2101 static int parse_action(char *str, struct hist_trigger_attrs *attrs) 2102 { 2103 int ret = -EINVAL; 2104 2105 if (attrs->n_actions >= HIST_ACTIONS_MAX) 2106 return ret; 2107 2108 if ((str_has_prefix(str, "onmatch(")) || 2109 (str_has_prefix(str, "onmax(")) || 2110 (str_has_prefix(str, "onchange("))) { 2111 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL); 2112 if (!attrs->action_str[attrs->n_actions]) { 2113 ret = -ENOMEM; 2114 return ret; 2115 } 2116 attrs->n_actions++; 2117 ret = 0; 2118 } 2119 return ret; 2120 } 2121 2122 static int parse_assignment(struct trace_array *tr, 2123 char *str, struct hist_trigger_attrs *attrs) 2124 { 2125 int len, ret = 0; 2126 2127 if ((len = str_has_prefix(str, "key=")) || 2128 (len = str_has_prefix(str, "keys="))) { 2129 attrs->keys_str = kstrdup(str + len, GFP_KERNEL); 2130 if (!attrs->keys_str) { 2131 ret = -ENOMEM; 2132 goto out; 2133 } 2134 } else if ((len = str_has_prefix(str, "val=")) || 2135 (len = str_has_prefix(str, "vals=")) || 2136 (len = str_has_prefix(str, "values="))) { 2137 attrs->vals_str = kstrdup(str + len, GFP_KERNEL); 2138 if (!attrs->vals_str) { 2139 ret = -ENOMEM; 2140 goto out; 2141 } 2142 } else if ((len = str_has_prefix(str, "sort="))) { 2143 attrs->sort_key_str = kstrdup(str + len, GFP_KERNEL); 2144 if (!attrs->sort_key_str) { 2145 ret = -ENOMEM; 2146 goto out; 2147 } 2148 } else if (str_has_prefix(str, "name=")) { 2149 attrs->name = kstrdup(str, GFP_KERNEL); 2150 if (!attrs->name) { 2151 ret = -ENOMEM; 2152 goto out; 2153 } 2154 } else if ((len = str_has_prefix(str, "clock="))) { 2155 str += len; 2156 2157 str = strstrip(str); 2158 attrs->clock = kstrdup(str, GFP_KERNEL); 2159 if (!attrs->clock) { 2160 ret = -ENOMEM; 2161 goto out; 2162 } 2163 } else if ((len = str_has_prefix(str, "size="))) { 2164 int map_bits = parse_map_size(str + len); 2165 2166 if (map_bits < 0) { 2167 ret = map_bits; 2168 goto out; 2169 } 2170 attrs->map_bits = map_bits; 2171 } else { 2172 char *assignment; 2173 2174 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) { 2175 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str)); 2176 ret = -EINVAL; 2177 goto out; 2178 } 2179 2180 assignment = kstrdup(str, GFP_KERNEL); 2181 if (!assignment) { 2182 ret = -ENOMEM; 2183 goto out; 2184 } 2185 2186 attrs->assignment_str[attrs->n_assignments++] = assignment; 2187 } 2188 out: 2189 return ret; 2190 } 2191 2192 static struct hist_trigger_attrs * 2193 parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str) 2194 { 2195 struct hist_trigger_attrs *attrs; 2196 int ret = 0; 2197 2198 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 2199 if (!attrs) 2200 return ERR_PTR(-ENOMEM); 2201 2202 while (trigger_str) { 2203 char *str = strsep(&trigger_str, ":"); 2204 char *rhs; 2205 2206 rhs = strchr(str, '='); 2207 if (rhs) { 2208 if (!strlen(++rhs)) { 2209 ret = -EINVAL; 2210 goto free; 2211 } 2212 ret = parse_assignment(tr, str, attrs); 2213 if (ret) 2214 goto free; 2215 } else if (strcmp(str, "pause") == 0) 2216 attrs->pause = true; 2217 else if ((strcmp(str, "cont") == 0) || 2218 (strcmp(str, "continue") == 0)) 2219 attrs->cont = true; 2220 else if (strcmp(str, "clear") == 0) 2221 attrs->clear = true; 2222 else { 2223 ret = parse_action(str, attrs); 2224 if (ret) 2225 goto free; 2226 } 2227 } 2228 2229 if (!attrs->keys_str) { 2230 ret = -EINVAL; 2231 goto free; 2232 } 2233 2234 if (!attrs->clock) { 2235 attrs->clock = kstrdup("global", GFP_KERNEL); 2236 if (!attrs->clock) { 2237 ret = -ENOMEM; 2238 goto free; 2239 } 2240 } 2241 2242 return attrs; 2243 free: 2244 destroy_hist_trigger_attrs(attrs); 2245 2246 return ERR_PTR(ret); 2247 } 2248 2249 static inline void save_comm(char *comm, struct task_struct *task) 2250 { 2251 if (!task->pid) { 2252 strcpy(comm, "<idle>"); 2253 return; 2254 } 2255 2256 if (WARN_ON_ONCE(task->pid < 0)) { 2257 strcpy(comm, "<XXX>"); 2258 return; 2259 } 2260 2261 strncpy(comm, task->comm, TASK_COMM_LEN); 2262 } 2263 2264 static void hist_elt_data_free(struct hist_elt_data *elt_data) 2265 { 2266 unsigned int i; 2267 2268 for (i = 0; i < SYNTH_FIELDS_MAX; i++) 2269 kfree(elt_data->field_var_str[i]); 2270 2271 kfree(elt_data->comm); 2272 kfree(elt_data); 2273 } 2274 2275 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt) 2276 { 2277 struct hist_elt_data *elt_data = elt->private_data; 2278 2279 hist_elt_data_free(elt_data); 2280 } 2281 2282 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt) 2283 { 2284 struct hist_trigger_data *hist_data = elt->map->private_data; 2285 unsigned int size = TASK_COMM_LEN; 2286 struct hist_elt_data *elt_data; 2287 struct hist_field *key_field; 2288 unsigned int i, n_str; 2289 2290 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL); 2291 if (!elt_data) 2292 return -ENOMEM; 2293 2294 for_each_hist_key_field(i, hist_data) { 2295 key_field = hist_data->fields[i]; 2296 2297 if (key_field->flags & HIST_FIELD_FL_EXECNAME) { 2298 elt_data->comm = kzalloc(size, GFP_KERNEL); 2299 if (!elt_data->comm) { 2300 kfree(elt_data); 2301 return -ENOMEM; 2302 } 2303 break; 2304 } 2305 } 2306 2307 n_str = hist_data->n_field_var_str + hist_data->n_save_var_str; 2308 2309 size = STR_VAR_LEN_MAX; 2310 2311 for (i = 0; i < n_str; i++) { 2312 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL); 2313 if (!elt_data->field_var_str[i]) { 2314 hist_elt_data_free(elt_data); 2315 return -ENOMEM; 2316 } 2317 } 2318 2319 elt->private_data = elt_data; 2320 2321 return 0; 2322 } 2323 2324 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt) 2325 { 2326 struct hist_elt_data *elt_data = elt->private_data; 2327 2328 if (elt_data->comm) 2329 save_comm(elt_data->comm, current); 2330 } 2331 2332 static const struct tracing_map_ops hist_trigger_elt_data_ops = { 2333 .elt_alloc = hist_trigger_elt_data_alloc, 2334 .elt_free = hist_trigger_elt_data_free, 2335 .elt_init = hist_trigger_elt_data_init, 2336 }; 2337 2338 static const char *get_hist_field_flags(struct hist_field *hist_field) 2339 { 2340 const char *flags_str = NULL; 2341 2342 if (hist_field->flags & HIST_FIELD_FL_HEX) 2343 flags_str = "hex"; 2344 else if (hist_field->flags & HIST_FIELD_FL_SYM) 2345 flags_str = "sym"; 2346 else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET) 2347 flags_str = "sym-offset"; 2348 else if (hist_field->flags & HIST_FIELD_FL_EXECNAME) 2349 flags_str = "execname"; 2350 else if (hist_field->flags & HIST_FIELD_FL_SYSCALL) 2351 flags_str = "syscall"; 2352 else if (hist_field->flags & HIST_FIELD_FL_LOG2) 2353 flags_str = "log2"; 2354 else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS) 2355 flags_str = "usecs"; 2356 2357 return flags_str; 2358 } 2359 2360 static void expr_field_str(struct hist_field *field, char *expr) 2361 { 2362 if (field->flags & HIST_FIELD_FL_VAR_REF) 2363 strcat(expr, "$"); 2364 2365 strcat(expr, hist_field_name(field, 0)); 2366 2367 if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) { 2368 const char *flags_str = get_hist_field_flags(field); 2369 2370 if (flags_str) { 2371 strcat(expr, "."); 2372 strcat(expr, flags_str); 2373 } 2374 } 2375 } 2376 2377 static char *expr_str(struct hist_field *field, unsigned int level) 2378 { 2379 char *expr; 2380 2381 if (level > 1) 2382 return NULL; 2383 2384 expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL); 2385 if (!expr) 2386 return NULL; 2387 2388 if (!field->operands[0]) { 2389 expr_field_str(field, expr); 2390 return expr; 2391 } 2392 2393 if (field->operator == FIELD_OP_UNARY_MINUS) { 2394 char *subexpr; 2395 2396 strcat(expr, "-("); 2397 subexpr = expr_str(field->operands[0], ++level); 2398 if (!subexpr) { 2399 kfree(expr); 2400 return NULL; 2401 } 2402 strcat(expr, subexpr); 2403 strcat(expr, ")"); 2404 2405 kfree(subexpr); 2406 2407 return expr; 2408 } 2409 2410 expr_field_str(field->operands[0], expr); 2411 2412 switch (field->operator) { 2413 case FIELD_OP_MINUS: 2414 strcat(expr, "-"); 2415 break; 2416 case FIELD_OP_PLUS: 2417 strcat(expr, "+"); 2418 break; 2419 default: 2420 kfree(expr); 2421 return NULL; 2422 } 2423 2424 expr_field_str(field->operands[1], expr); 2425 2426 return expr; 2427 } 2428 2429 static int contains_operator(char *str) 2430 { 2431 enum field_op_id field_op = FIELD_OP_NONE; 2432 char *op; 2433 2434 op = strpbrk(str, "+-"); 2435 if (!op) 2436 return FIELD_OP_NONE; 2437 2438 switch (*op) { 2439 case '-': 2440 if (*str == '-') 2441 field_op = FIELD_OP_UNARY_MINUS; 2442 else 2443 field_op = FIELD_OP_MINUS; 2444 break; 2445 case '+': 2446 field_op = FIELD_OP_PLUS; 2447 break; 2448 default: 2449 break; 2450 } 2451 2452 return field_op; 2453 } 2454 2455 static void get_hist_field(struct hist_field *hist_field) 2456 { 2457 hist_field->ref++; 2458 } 2459 2460 static void __destroy_hist_field(struct hist_field *hist_field) 2461 { 2462 if (--hist_field->ref > 1) 2463 return; 2464 2465 kfree(hist_field->var.name); 2466 kfree(hist_field->name); 2467 kfree(hist_field->type); 2468 2469 kfree(hist_field); 2470 } 2471 2472 static void destroy_hist_field(struct hist_field *hist_field, 2473 unsigned int level) 2474 { 2475 unsigned int i; 2476 2477 if (level > 3) 2478 return; 2479 2480 if (!hist_field) 2481 return; 2482 2483 if (hist_field->flags & HIST_FIELD_FL_VAR_REF) 2484 return; /* var refs will be destroyed separately */ 2485 2486 for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) 2487 destroy_hist_field(hist_field->operands[i], level + 1); 2488 2489 __destroy_hist_field(hist_field); 2490 } 2491 2492 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data, 2493 struct ftrace_event_field *field, 2494 unsigned long flags, 2495 char *var_name) 2496 { 2497 struct hist_field *hist_field; 2498 2499 if (field && is_function_field(field)) 2500 return NULL; 2501 2502 hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL); 2503 if (!hist_field) 2504 return NULL; 2505 2506 hist_field->ref = 1; 2507 2508 hist_field->hist_data = hist_data; 2509 2510 if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS) 2511 goto out; /* caller will populate */ 2512 2513 if (flags & HIST_FIELD_FL_VAR_REF) { 2514 hist_field->fn = hist_field_var_ref; 2515 goto out; 2516 } 2517 2518 if (flags & HIST_FIELD_FL_HITCOUNT) { 2519 hist_field->fn = hist_field_counter; 2520 hist_field->size = sizeof(u64); 2521 hist_field->type = kstrdup("u64", GFP_KERNEL); 2522 if (!hist_field->type) 2523 goto free; 2524 goto out; 2525 } 2526 2527 if (flags & HIST_FIELD_FL_STACKTRACE) { 2528 hist_field->fn = hist_field_none; 2529 goto out; 2530 } 2531 2532 if (flags & HIST_FIELD_FL_LOG2) { 2533 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2; 2534 hist_field->fn = hist_field_log2; 2535 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL); 2536 hist_field->size = hist_field->operands[0]->size; 2537 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL); 2538 if (!hist_field->type) 2539 goto free; 2540 goto out; 2541 } 2542 2543 if (flags & HIST_FIELD_FL_TIMESTAMP) { 2544 hist_field->fn = hist_field_timestamp; 2545 hist_field->size = sizeof(u64); 2546 hist_field->type = kstrdup("u64", GFP_KERNEL); 2547 if (!hist_field->type) 2548 goto free; 2549 goto out; 2550 } 2551 2552 if (flags & HIST_FIELD_FL_CPU) { 2553 hist_field->fn = hist_field_cpu; 2554 hist_field->size = sizeof(int); 2555 hist_field->type = kstrdup("unsigned int", GFP_KERNEL); 2556 if (!hist_field->type) 2557 goto free; 2558 goto out; 2559 } 2560 2561 if (WARN_ON_ONCE(!field)) 2562 goto out; 2563 2564 if (is_string_field(field)) { 2565 flags |= HIST_FIELD_FL_STRING; 2566 2567 hist_field->size = MAX_FILTER_STR_VAL; 2568 hist_field->type = kstrdup(field->type, GFP_KERNEL); 2569 if (!hist_field->type) 2570 goto free; 2571 2572 if (field->filter_type == FILTER_STATIC_STRING) 2573 hist_field->fn = hist_field_string; 2574 else if (field->filter_type == FILTER_DYN_STRING) 2575 hist_field->fn = hist_field_dynstring; 2576 else 2577 hist_field->fn = hist_field_pstring; 2578 } else { 2579 hist_field->size = field->size; 2580 hist_field->is_signed = field->is_signed; 2581 hist_field->type = kstrdup(field->type, GFP_KERNEL); 2582 if (!hist_field->type) 2583 goto free; 2584 2585 hist_field->fn = select_value_fn(field->size, 2586 field->is_signed); 2587 if (!hist_field->fn) { 2588 destroy_hist_field(hist_field, 0); 2589 return NULL; 2590 } 2591 } 2592 out: 2593 hist_field->field = field; 2594 hist_field->flags = flags; 2595 2596 if (var_name) { 2597 hist_field->var.name = kstrdup(var_name, GFP_KERNEL); 2598 if (!hist_field->var.name) 2599 goto free; 2600 } 2601 2602 return hist_field; 2603 free: 2604 destroy_hist_field(hist_field, 0); 2605 return NULL; 2606 } 2607 2608 static void destroy_hist_fields(struct hist_trigger_data *hist_data) 2609 { 2610 unsigned int i; 2611 2612 for (i = 0; i < HIST_FIELDS_MAX; i++) { 2613 if (hist_data->fields[i]) { 2614 destroy_hist_field(hist_data->fields[i], 0); 2615 hist_data->fields[i] = NULL; 2616 } 2617 } 2618 2619 for (i = 0; i < hist_data->n_var_refs; i++) { 2620 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF)); 2621 __destroy_hist_field(hist_data->var_refs[i]); 2622 hist_data->var_refs[i] = NULL; 2623 } 2624 } 2625 2626 static int init_var_ref(struct hist_field *ref_field, 2627 struct hist_field *var_field, 2628 char *system, char *event_name) 2629 { 2630 int err = 0; 2631 2632 ref_field->var.idx = var_field->var.idx; 2633 ref_field->var.hist_data = var_field->hist_data; 2634 ref_field->size = var_field->size; 2635 ref_field->is_signed = var_field->is_signed; 2636 ref_field->flags |= var_field->flags & 2637 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS); 2638 2639 if (system) { 2640 ref_field->system = kstrdup(system, GFP_KERNEL); 2641 if (!ref_field->system) 2642 return -ENOMEM; 2643 } 2644 2645 if (event_name) { 2646 ref_field->event_name = kstrdup(event_name, GFP_KERNEL); 2647 if (!ref_field->event_name) { 2648 err = -ENOMEM; 2649 goto free; 2650 } 2651 } 2652 2653 if (var_field->var.name) { 2654 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL); 2655 if (!ref_field->name) { 2656 err = -ENOMEM; 2657 goto free; 2658 } 2659 } else if (var_field->name) { 2660 ref_field->name = kstrdup(var_field->name, GFP_KERNEL); 2661 if (!ref_field->name) { 2662 err = -ENOMEM; 2663 goto free; 2664 } 2665 } 2666 2667 ref_field->type = kstrdup(var_field->type, GFP_KERNEL); 2668 if (!ref_field->type) { 2669 err = -ENOMEM; 2670 goto free; 2671 } 2672 out: 2673 return err; 2674 free: 2675 kfree(ref_field->system); 2676 kfree(ref_field->event_name); 2677 kfree(ref_field->name); 2678 2679 goto out; 2680 } 2681 2682 static int find_var_ref_idx(struct hist_trigger_data *hist_data, 2683 struct hist_field *var_field) 2684 { 2685 struct hist_field *ref_field; 2686 int i; 2687 2688 for (i = 0; i < hist_data->n_var_refs; i++) { 2689 ref_field = hist_data->var_refs[i]; 2690 if (ref_field->var.idx == var_field->var.idx && 2691 ref_field->var.hist_data == var_field->hist_data) 2692 return i; 2693 } 2694 2695 return -ENOENT; 2696 } 2697 2698 /** 2699 * create_var_ref - Create a variable reference and attach it to trigger 2700 * @hist_data: The trigger that will be referencing the variable 2701 * @var_field: The VAR field to create a reference to 2702 * @system: The optional system string 2703 * @event_name: The optional event_name string 2704 * 2705 * Given a variable hist_field, create a VAR_REF hist_field that 2706 * represents a reference to it. 2707 * 2708 * This function also adds the reference to the trigger that 2709 * now references the variable. 2710 * 2711 * Return: The VAR_REF field if successful, NULL if not 2712 */ 2713 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data, 2714 struct hist_field *var_field, 2715 char *system, char *event_name) 2716 { 2717 unsigned long flags = HIST_FIELD_FL_VAR_REF; 2718 struct hist_field *ref_field; 2719 int i; 2720 2721 /* Check if the variable already exists */ 2722 for (i = 0; i < hist_data->n_var_refs; i++) { 2723 ref_field = hist_data->var_refs[i]; 2724 if (ref_field->var.idx == var_field->var.idx && 2725 ref_field->var.hist_data == var_field->hist_data) { 2726 get_hist_field(ref_field); 2727 return ref_field; 2728 } 2729 } 2730 2731 ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL); 2732 if (ref_field) { 2733 if (init_var_ref(ref_field, var_field, system, event_name)) { 2734 destroy_hist_field(ref_field, 0); 2735 return NULL; 2736 } 2737 2738 hist_data->var_refs[hist_data->n_var_refs] = ref_field; 2739 ref_field->var_ref_idx = hist_data->n_var_refs++; 2740 } 2741 2742 return ref_field; 2743 } 2744 2745 static bool is_var_ref(char *var_name) 2746 { 2747 if (!var_name || strlen(var_name) < 2 || var_name[0] != '$') 2748 return false; 2749 2750 return true; 2751 } 2752 2753 static char *field_name_from_var(struct hist_trigger_data *hist_data, 2754 char *var_name) 2755 { 2756 char *name, *field; 2757 unsigned int i; 2758 2759 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) { 2760 name = hist_data->attrs->var_defs.name[i]; 2761 2762 if (strcmp(var_name, name) == 0) { 2763 field = hist_data->attrs->var_defs.expr[i]; 2764 if (contains_operator(field) || is_var_ref(field)) 2765 continue; 2766 return field; 2767 } 2768 } 2769 2770 return NULL; 2771 } 2772 2773 static char *local_field_var_ref(struct hist_trigger_data *hist_data, 2774 char *system, char *event_name, 2775 char *var_name) 2776 { 2777 struct trace_event_call *call; 2778 2779 if (system && event_name) { 2780 call = hist_data->event_file->event_call; 2781 2782 if (strcmp(system, call->class->system) != 0) 2783 return NULL; 2784 2785 if (strcmp(event_name, trace_event_name(call)) != 0) 2786 return NULL; 2787 } 2788 2789 if (!!system != !!event_name) 2790 return NULL; 2791 2792 if (!is_var_ref(var_name)) 2793 return NULL; 2794 2795 var_name++; 2796 2797 return field_name_from_var(hist_data, var_name); 2798 } 2799 2800 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data, 2801 char *system, char *event_name, 2802 char *var_name) 2803 { 2804 struct hist_field *var_field = NULL, *ref_field = NULL; 2805 struct trace_array *tr = hist_data->event_file->tr; 2806 2807 if (!is_var_ref(var_name)) 2808 return NULL; 2809 2810 var_name++; 2811 2812 var_field = find_event_var(hist_data, system, event_name, var_name); 2813 if (var_field) 2814 ref_field = create_var_ref(hist_data, var_field, 2815 system, event_name); 2816 2817 if (!ref_field) 2818 hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name)); 2819 2820 return ref_field; 2821 } 2822 2823 static struct ftrace_event_field * 2824 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file, 2825 char *field_str, unsigned long *flags) 2826 { 2827 struct ftrace_event_field *field = NULL; 2828 char *field_name, *modifier, *str; 2829 struct trace_array *tr = file->tr; 2830 2831 modifier = str = kstrdup(field_str, GFP_KERNEL); 2832 if (!modifier) 2833 return ERR_PTR(-ENOMEM); 2834 2835 field_name = strsep(&modifier, "."); 2836 if (modifier) { 2837 if (strcmp(modifier, "hex") == 0) 2838 *flags |= HIST_FIELD_FL_HEX; 2839 else if (strcmp(modifier, "sym") == 0) 2840 *flags |= HIST_FIELD_FL_SYM; 2841 else if (strcmp(modifier, "sym-offset") == 0) 2842 *flags |= HIST_FIELD_FL_SYM_OFFSET; 2843 else if ((strcmp(modifier, "execname") == 0) && 2844 (strcmp(field_name, "common_pid") == 0)) 2845 *flags |= HIST_FIELD_FL_EXECNAME; 2846 else if (strcmp(modifier, "syscall") == 0) 2847 *flags |= HIST_FIELD_FL_SYSCALL; 2848 else if (strcmp(modifier, "log2") == 0) 2849 *flags |= HIST_FIELD_FL_LOG2; 2850 else if (strcmp(modifier, "usecs") == 0) 2851 *flags |= HIST_FIELD_FL_TIMESTAMP_USECS; 2852 else { 2853 hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier)); 2854 field = ERR_PTR(-EINVAL); 2855 goto out; 2856 } 2857 } 2858 2859 if (strcmp(field_name, "common_timestamp") == 0) { 2860 *flags |= HIST_FIELD_FL_TIMESTAMP; 2861 hist_data->enable_timestamps = true; 2862 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS) 2863 hist_data->attrs->ts_in_usecs = true; 2864 } else if (strcmp(field_name, "cpu") == 0) 2865 *flags |= HIST_FIELD_FL_CPU; 2866 else { 2867 field = trace_find_event_field(file->event_call, field_name); 2868 if (!field || !field->size) { 2869 hist_err(tr, HIST_ERR_FIELD_NOT_FOUND, errpos(field_name)); 2870 field = ERR_PTR(-EINVAL); 2871 goto out; 2872 } 2873 } 2874 out: 2875 kfree(str); 2876 2877 return field; 2878 } 2879 2880 static struct hist_field *create_alias(struct hist_trigger_data *hist_data, 2881 struct hist_field *var_ref, 2882 char *var_name) 2883 { 2884 struct hist_field *alias = NULL; 2885 unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR; 2886 2887 alias = create_hist_field(hist_data, NULL, flags, var_name); 2888 if (!alias) 2889 return NULL; 2890 2891 alias->fn = var_ref->fn; 2892 alias->operands[0] = var_ref; 2893 2894 if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) { 2895 destroy_hist_field(alias, 0); 2896 return NULL; 2897 } 2898 2899 alias->var_ref_idx = var_ref->var_ref_idx; 2900 2901 return alias; 2902 } 2903 2904 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data, 2905 struct trace_event_file *file, char *str, 2906 unsigned long *flags, char *var_name) 2907 { 2908 char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str; 2909 struct ftrace_event_field *field = NULL; 2910 struct hist_field *hist_field = NULL; 2911 int ret = 0; 2912 2913 s = strchr(str, '.'); 2914 if (s) { 2915 s = strchr(++s, '.'); 2916 if (s) { 2917 ref_system = strsep(&str, "."); 2918 if (!str) { 2919 ret = -EINVAL; 2920 goto out; 2921 } 2922 ref_event = strsep(&str, "."); 2923 if (!str) { 2924 ret = -EINVAL; 2925 goto out; 2926 } 2927 ref_var = str; 2928 } 2929 } 2930 2931 s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var); 2932 if (!s) { 2933 hist_field = parse_var_ref(hist_data, ref_system, 2934 ref_event, ref_var); 2935 if (hist_field) { 2936 if (var_name) { 2937 hist_field = create_alias(hist_data, hist_field, var_name); 2938 if (!hist_field) { 2939 ret = -ENOMEM; 2940 goto out; 2941 } 2942 } 2943 return hist_field; 2944 } 2945 } else 2946 str = s; 2947 2948 field = parse_field(hist_data, file, str, flags); 2949 if (IS_ERR(field)) { 2950 ret = PTR_ERR(field); 2951 goto out; 2952 } 2953 2954 hist_field = create_hist_field(hist_data, field, *flags, var_name); 2955 if (!hist_field) { 2956 ret = -ENOMEM; 2957 goto out; 2958 } 2959 2960 return hist_field; 2961 out: 2962 return ERR_PTR(ret); 2963 } 2964 2965 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data, 2966 struct trace_event_file *file, 2967 char *str, unsigned long flags, 2968 char *var_name, unsigned int level); 2969 2970 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data, 2971 struct trace_event_file *file, 2972 char *str, unsigned long flags, 2973 char *var_name, unsigned int level) 2974 { 2975 struct hist_field *operand1, *expr = NULL; 2976 unsigned long operand_flags; 2977 int ret = 0; 2978 char *s; 2979 2980 /* we support only -(xxx) i.e. explicit parens required */ 2981 2982 if (level > 3) { 2983 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str)); 2984 ret = -EINVAL; 2985 goto free; 2986 } 2987 2988 str++; /* skip leading '-' */ 2989 2990 s = strchr(str, '('); 2991 if (s) 2992 str++; 2993 else { 2994 ret = -EINVAL; 2995 goto free; 2996 } 2997 2998 s = strrchr(str, ')'); 2999 if (s) 3000 *s = '\0'; 3001 else { 3002 ret = -EINVAL; /* no closing ')' */ 3003 goto free; 3004 } 3005 3006 flags |= HIST_FIELD_FL_EXPR; 3007 expr = create_hist_field(hist_data, NULL, flags, var_name); 3008 if (!expr) { 3009 ret = -ENOMEM; 3010 goto free; 3011 } 3012 3013 operand_flags = 0; 3014 operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level); 3015 if (IS_ERR(operand1)) { 3016 ret = PTR_ERR(operand1); 3017 goto free; 3018 } 3019 3020 expr->flags |= operand1->flags & 3021 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS); 3022 expr->fn = hist_field_unary_minus; 3023 expr->operands[0] = operand1; 3024 expr->operator = FIELD_OP_UNARY_MINUS; 3025 expr->name = expr_str(expr, 0); 3026 expr->type = kstrdup(operand1->type, GFP_KERNEL); 3027 if (!expr->type) { 3028 ret = -ENOMEM; 3029 goto free; 3030 } 3031 3032 return expr; 3033 free: 3034 destroy_hist_field(expr, 0); 3035 return ERR_PTR(ret); 3036 } 3037 3038 static int check_expr_operands(struct trace_array *tr, 3039 struct hist_field *operand1, 3040 struct hist_field *operand2) 3041 { 3042 unsigned long operand1_flags = operand1->flags; 3043 unsigned long operand2_flags = operand2->flags; 3044 3045 if ((operand1_flags & HIST_FIELD_FL_VAR_REF) || 3046 (operand1_flags & HIST_FIELD_FL_ALIAS)) { 3047 struct hist_field *var; 3048 3049 var = find_var_field(operand1->var.hist_data, operand1->name); 3050 if (!var) 3051 return -EINVAL; 3052 operand1_flags = var->flags; 3053 } 3054 3055 if ((operand2_flags & HIST_FIELD_FL_VAR_REF) || 3056 (operand2_flags & HIST_FIELD_FL_ALIAS)) { 3057 struct hist_field *var; 3058 3059 var = find_var_field(operand2->var.hist_data, operand2->name); 3060 if (!var) 3061 return -EINVAL; 3062 operand2_flags = var->flags; 3063 } 3064 3065 if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) != 3066 (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) { 3067 hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0); 3068 return -EINVAL; 3069 } 3070 3071 return 0; 3072 } 3073 3074 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data, 3075 struct trace_event_file *file, 3076 char *str, unsigned long flags, 3077 char *var_name, unsigned int level) 3078 { 3079 struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL; 3080 unsigned long operand_flags; 3081 int field_op, ret = -EINVAL; 3082 char *sep, *operand1_str; 3083 3084 if (level > 3) { 3085 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str)); 3086 return ERR_PTR(-EINVAL); 3087 } 3088 3089 field_op = contains_operator(str); 3090 3091 if (field_op == FIELD_OP_NONE) 3092 return parse_atom(hist_data, file, str, &flags, var_name); 3093 3094 if (field_op == FIELD_OP_UNARY_MINUS) 3095 return parse_unary(hist_data, file, str, flags, var_name, ++level); 3096 3097 switch (field_op) { 3098 case FIELD_OP_MINUS: 3099 sep = "-"; 3100 break; 3101 case FIELD_OP_PLUS: 3102 sep = "+"; 3103 break; 3104 default: 3105 goto free; 3106 } 3107 3108 operand1_str = strsep(&str, sep); 3109 if (!operand1_str || !str) 3110 goto free; 3111 3112 operand_flags = 0; 3113 operand1 = parse_atom(hist_data, file, operand1_str, 3114 &operand_flags, NULL); 3115 if (IS_ERR(operand1)) { 3116 ret = PTR_ERR(operand1); 3117 operand1 = NULL; 3118 goto free; 3119 } 3120 3121 /* rest of string could be another expression e.g. b+c in a+b+c */ 3122 operand_flags = 0; 3123 operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level); 3124 if (IS_ERR(operand2)) { 3125 ret = PTR_ERR(operand2); 3126 operand2 = NULL; 3127 goto free; 3128 } 3129 3130 ret = check_expr_operands(file->tr, operand1, operand2); 3131 if (ret) 3132 goto free; 3133 3134 flags |= HIST_FIELD_FL_EXPR; 3135 3136 flags |= operand1->flags & 3137 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS); 3138 3139 expr = create_hist_field(hist_data, NULL, flags, var_name); 3140 if (!expr) { 3141 ret = -ENOMEM; 3142 goto free; 3143 } 3144 3145 operand1->read_once = true; 3146 operand2->read_once = true; 3147 3148 expr->operands[0] = operand1; 3149 expr->operands[1] = operand2; 3150 expr->operator = field_op; 3151 expr->name = expr_str(expr, 0); 3152 expr->type = kstrdup(operand1->type, GFP_KERNEL); 3153 if (!expr->type) { 3154 ret = -ENOMEM; 3155 goto free; 3156 } 3157 3158 switch (field_op) { 3159 case FIELD_OP_MINUS: 3160 expr->fn = hist_field_minus; 3161 break; 3162 case FIELD_OP_PLUS: 3163 expr->fn = hist_field_plus; 3164 break; 3165 default: 3166 ret = -EINVAL; 3167 goto free; 3168 } 3169 3170 return expr; 3171 free: 3172 destroy_hist_field(operand1, 0); 3173 destroy_hist_field(operand2, 0); 3174 destroy_hist_field(expr, 0); 3175 3176 return ERR_PTR(ret); 3177 } 3178 3179 static char *find_trigger_filter(struct hist_trigger_data *hist_data, 3180 struct trace_event_file *file) 3181 { 3182 struct event_trigger_data *test; 3183 3184 lockdep_assert_held(&event_mutex); 3185 3186 list_for_each_entry(test, &file->triggers, list) { 3187 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 3188 if (test->private_data == hist_data) 3189 return test->filter_str; 3190 } 3191 } 3192 3193 return NULL; 3194 } 3195 3196 static struct event_command trigger_hist_cmd; 3197 static int event_hist_trigger_func(struct event_command *cmd_ops, 3198 struct trace_event_file *file, 3199 char *glob, char *cmd, char *param); 3200 3201 static bool compatible_keys(struct hist_trigger_data *target_hist_data, 3202 struct hist_trigger_data *hist_data, 3203 unsigned int n_keys) 3204 { 3205 struct hist_field *target_hist_field, *hist_field; 3206 unsigned int n, i, j; 3207 3208 if (hist_data->n_fields - hist_data->n_vals != n_keys) 3209 return false; 3210 3211 i = hist_data->n_vals; 3212 j = target_hist_data->n_vals; 3213 3214 for (n = 0; n < n_keys; n++) { 3215 hist_field = hist_data->fields[i + n]; 3216 target_hist_field = target_hist_data->fields[j + n]; 3217 3218 if (strcmp(hist_field->type, target_hist_field->type) != 0) 3219 return false; 3220 if (hist_field->size != target_hist_field->size) 3221 return false; 3222 if (hist_field->is_signed != target_hist_field->is_signed) 3223 return false; 3224 } 3225 3226 return true; 3227 } 3228 3229 static struct hist_trigger_data * 3230 find_compatible_hist(struct hist_trigger_data *target_hist_data, 3231 struct trace_event_file *file) 3232 { 3233 struct hist_trigger_data *hist_data; 3234 struct event_trigger_data *test; 3235 unsigned int n_keys; 3236 3237 lockdep_assert_held(&event_mutex); 3238 3239 n_keys = target_hist_data->n_fields - target_hist_data->n_vals; 3240 3241 list_for_each_entry(test, &file->triggers, list) { 3242 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { 3243 hist_data = test->private_data; 3244 3245 if (compatible_keys(target_hist_data, hist_data, n_keys)) 3246 return hist_data; 3247 } 3248 } 3249 3250 return NULL; 3251 } 3252 3253 static struct trace_event_file *event_file(struct trace_array *tr, 3254 char *system, char *event_name) 3255 { 3256 struct trace_event_file *file; 3257 3258 file = __find_event_file(tr, system, event_name); 3259 if (!file) 3260 return ERR_PTR(-EINVAL); 3261 3262 return file; 3263 } 3264 3265 static struct hist_field * 3266 find_synthetic_field_var(struct hist_trigger_data *target_hist_data, 3267 char *system, char *event_name, char *field_name) 3268 { 3269 struct hist_field *event_var; 3270 char *synthetic_name; 3271 3272 synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL); 3273 if (!synthetic_name) 3274 return ERR_PTR(-ENOMEM); 3275 3276 strcpy(synthetic_name, "synthetic_"); 3277 strcat(synthetic_name, field_name); 3278 3279 event_var = find_event_var(target_hist_data, system, event_name, synthetic_name); 3280 3281 kfree(synthetic_name); 3282 3283 return event_var; 3284 } 3285 3286 /** 3287 * create_field_var_hist - Automatically create a histogram and var for a field 3288 * @target_hist_data: The target hist trigger 3289 * @subsys_name: Optional subsystem name 3290 * @event_name: Optional event name 3291 * @field_name: The name of the field (and the resulting variable) 3292 * 3293 * Hist trigger actions fetch data from variables, not directly from 3294 * events. However, for convenience, users are allowed to directly 3295 * specify an event field in an action, which will be automatically 3296 * converted into a variable on their behalf. 3297 3298 * If a user specifies a field on an event that isn't the event the 3299 * histogram currently being defined (the target event histogram), the 3300 * only way that can be accomplished is if a new hist trigger is 3301 * created and the field variable defined on that. 3302 * 3303 * This function creates a new histogram compatible with the target 3304 * event (meaning a histogram with the same key as the target 3305 * histogram), and creates a variable for the specified field, but 3306 * with 'synthetic_' prepended to the variable name in order to avoid 3307 * collision with normal field variables. 3308 * 3309 * Return: The variable created for the field. 3310 */ 3311 static struct hist_field * 3312 create_field_var_hist(struct hist_trigger_data *target_hist_data, 3313 char *subsys_name, char *event_name, char *field_name) 3314 { 3315 struct trace_array *tr = target_hist_data->event_file->tr; 3316 struct hist_field *event_var = ERR_PTR(-EINVAL); 3317 struct hist_trigger_data *hist_data; 3318 unsigned int i, n, first = true; 3319 struct field_var_hist *var_hist; 3320 struct trace_event_file *file; 3321 struct hist_field *key_field; 3322 char *saved_filter; 3323 char *cmd; 3324 int ret; 3325 3326 if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) { 3327 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name)); 3328 return ERR_PTR(-EINVAL); 3329 } 3330 3331 file = event_file(tr, subsys_name, event_name); 3332 3333 if (IS_ERR(file)) { 3334 hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name)); 3335 ret = PTR_ERR(file); 3336 return ERR_PTR(ret); 3337 } 3338 3339 /* 3340 * Look for a histogram compatible with target. We'll use the 3341 * found histogram specification to create a new matching 3342 * histogram with our variable on it. target_hist_data is not 3343 * yet a registered histogram so we can't use that. 3344 */ 3345 hist_data = find_compatible_hist(target_hist_data, file); 3346 if (!hist_data) { 3347 hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name)); 3348 return ERR_PTR(-EINVAL); 3349 } 3350 3351 /* See if a synthetic field variable has already been created */ 3352 event_var = find_synthetic_field_var(target_hist_data, subsys_name, 3353 event_name, field_name); 3354 if (!IS_ERR_OR_NULL(event_var)) 3355 return event_var; 3356 3357 var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL); 3358 if (!var_hist) 3359 return ERR_PTR(-ENOMEM); 3360 3361 cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL); 3362 if (!cmd) { 3363 kfree(var_hist); 3364 return ERR_PTR(-ENOMEM); 3365 } 3366 3367 /* Use the same keys as the compatible histogram */ 3368 strcat(cmd, "keys="); 3369 3370 for_each_hist_key_field(i, hist_data) { 3371 key_field = hist_data->fields[i]; 3372 if (!first) 3373 strcat(cmd, ","); 3374 strcat(cmd, key_field->field->name); 3375 first = false; 3376 } 3377 3378 /* Create the synthetic field variable specification */ 3379 strcat(cmd, ":synthetic_"); 3380 strcat(cmd, field_name); 3381 strcat(cmd, "="); 3382 strcat(cmd, field_name); 3383 3384 /* Use the same filter as the compatible histogram */ 3385 saved_filter = find_trigger_filter(hist_data, file); 3386 if (saved_filter) { 3387 strcat(cmd, " if "); 3388 strcat(cmd, saved_filter); 3389 } 3390 3391 var_hist->cmd = kstrdup(cmd, GFP_KERNEL); 3392 if (!var_hist->cmd) { 3393 kfree(cmd); 3394 kfree(var_hist); 3395 return ERR_PTR(-ENOMEM); 3396 } 3397 3398 /* Save the compatible histogram information */ 3399 var_hist->hist_data = hist_data; 3400 3401 /* Create the new histogram with our variable */ 3402 ret = event_hist_trigger_func(&trigger_hist_cmd, file, 3403 "", "hist", cmd); 3404 if (ret) { 3405 kfree(cmd); 3406 kfree(var_hist->cmd); 3407 kfree(var_hist); 3408 hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name)); 3409 return ERR_PTR(ret); 3410 } 3411 3412 kfree(cmd); 3413 3414 /* If we can't find the variable, something went wrong */ 3415 event_var = find_synthetic_field_var(target_hist_data, subsys_name, 3416 event_name, field_name); 3417 if (IS_ERR_OR_NULL(event_var)) { 3418 kfree(var_hist->cmd); 3419 kfree(var_hist); 3420 hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name)); 3421 return ERR_PTR(-EINVAL); 3422 } 3423 3424 n = target_hist_data->n_field_var_hists; 3425 target_hist_data->field_var_hists[n] = var_hist; 3426 target_hist_data->n_field_var_hists++; 3427 3428 return event_var; 3429 } 3430 3431 static struct hist_field * 3432 find_target_event_var(struct hist_trigger_data *hist_data, 3433 char *subsys_name, char *event_name, char *var_name) 3434 { 3435 struct trace_event_file *file = hist_data->event_file; 3436 struct hist_field *hist_field = NULL; 3437 3438 if (subsys_name) { 3439 struct trace_event_call *call; 3440 3441 if (!event_name) 3442 return NULL; 3443 3444 call = file->event_call; 3445 3446 if (strcmp(subsys_name, call->class->system) != 0) 3447 return NULL; 3448 3449 if (strcmp(event_name, trace_event_name(call)) != 0) 3450 return NULL; 3451 } 3452 3453 hist_field = find_var_field(hist_data, var_name); 3454 3455 return hist_field; 3456 } 3457 3458 static inline void __update_field_vars(struct tracing_map_elt *elt, 3459 struct ring_buffer_event *rbe, 3460 void *rec, 3461 struct field_var **field_vars, 3462 unsigned int n_field_vars, 3463 unsigned int field_var_str_start) 3464 { 3465 struct hist_elt_data *elt_data = elt->private_data; 3466 unsigned int i, j, var_idx; 3467 u64 var_val; 3468 3469 for (i = 0, j = field_var_str_start; i < n_field_vars; i++) { 3470 struct field_var *field_var = field_vars[i]; 3471 struct hist_field *var = field_var->var; 3472 struct hist_field *val = field_var->val; 3473 3474 var_val = val->fn(val, elt, rbe, rec); 3475 var_idx = var->var.idx; 3476 3477 if (val->flags & HIST_FIELD_FL_STRING) { 3478 char *str = elt_data->field_var_str[j++]; 3479 char *val_str = (char *)(uintptr_t)var_val; 3480 3481 strscpy(str, val_str, STR_VAR_LEN_MAX); 3482 var_val = (u64)(uintptr_t)str; 3483 } 3484 tracing_map_set_var(elt, var_idx, var_val); 3485 } 3486 } 3487 3488 static void update_field_vars(struct hist_trigger_data *hist_data, 3489 struct tracing_map_elt *elt, 3490 struct ring_buffer_event *rbe, 3491 void *rec) 3492 { 3493 __update_field_vars(elt, rbe, rec, hist_data->field_vars, 3494 hist_data->n_field_vars, 0); 3495 } 3496 3497 static void save_track_data_vars(struct hist_trigger_data *hist_data, 3498 struct tracing_map_elt *elt, void *rec, 3499 struct ring_buffer_event *rbe, void *key, 3500 struct action_data *data, u64 *var_ref_vals) 3501 { 3502 __update_field_vars(elt, rbe, rec, hist_data->save_vars, 3503 hist_data->n_save_vars, hist_data->n_field_var_str); 3504 } 3505 3506 static struct hist_field *create_var(struct hist_trigger_data *hist_data, 3507 struct trace_event_file *file, 3508 char *name, int size, const char *type) 3509 { 3510 struct hist_field *var; 3511 int idx; 3512 3513 if (find_var(hist_data, file, name) && !hist_data->remove) { 3514 var = ERR_PTR(-EINVAL); 3515 goto out; 3516 } 3517 3518 var = kzalloc(sizeof(struct hist_field), GFP_KERNEL); 3519 if (!var) { 3520 var = ERR_PTR(-ENOMEM); 3521 goto out; 3522 } 3523 3524 idx = tracing_map_add_var(hist_data->map); 3525 if (idx < 0) { 3526 kfree(var); 3527 var = ERR_PTR(-EINVAL); 3528 goto out; 3529 } 3530 3531 var->flags = HIST_FIELD_FL_VAR; 3532 var->var.idx = idx; 3533 var->var.hist_data = var->hist_data = hist_data; 3534 var->size = size; 3535 var->var.name = kstrdup(name, GFP_KERNEL); 3536 var->type = kstrdup(type, GFP_KERNEL); 3537 if (!var->var.name || !var->type) { 3538 kfree(var->var.name); 3539 kfree(var->type); 3540 kfree(var); 3541 var = ERR_PTR(-ENOMEM); 3542 } 3543 out: 3544 return var; 3545 } 3546 3547 static struct field_var *create_field_var(struct hist_trigger_data *hist_data, 3548 struct trace_event_file *file, 3549 char *field_name) 3550 { 3551 struct hist_field *val = NULL, *var = NULL; 3552 unsigned long flags = HIST_FIELD_FL_VAR; 3553 struct trace_array *tr = file->tr; 3554 struct field_var *field_var; 3555 int ret = 0; 3556 3557 if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) { 3558 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name)); 3559 ret = -EINVAL; 3560 goto err; 3561 } 3562 3563 val = parse_atom(hist_data, file, field_name, &flags, NULL); 3564 if (IS_ERR(val)) { 3565 hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name)); 3566 ret = PTR_ERR(val); 3567 goto err; 3568 } 3569 3570 var = create_var(hist_data, file, field_name, val->size, val->type); 3571 if (IS_ERR(var)) { 3572 hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name)); 3573 kfree(val); 3574 ret = PTR_ERR(var); 3575 goto err; 3576 } 3577 3578 field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL); 3579 if (!field_var) { 3580 kfree(val); 3581 kfree(var); 3582 ret = -ENOMEM; 3583 goto err; 3584 } 3585 3586 field_var->var = var; 3587 field_var->val = val; 3588 out: 3589 return field_var; 3590 err: 3591 field_var = ERR_PTR(ret); 3592 goto out; 3593 } 3594 3595 /** 3596 * create_target_field_var - Automatically create a variable for a field 3597 * @target_hist_data: The target hist trigger 3598 * @subsys_name: Optional subsystem name 3599 * @event_name: Optional event name 3600 * @var_name: The name of the field (and the resulting variable) 3601 * 3602 * Hist trigger actions fetch data from variables, not directly from 3603 * events. However, for convenience, users are allowed to directly 3604 * specify an event field in an action, which will be automatically 3605 * converted into a variable on their behalf. 3606 3607 * This function creates a field variable with the name var_name on 3608 * the hist trigger currently being defined on the target event. If 3609 * subsys_name and event_name are specified, this function simply 3610 * verifies that they do in fact match the target event subsystem and 3611 * event name. 3612 * 3613 * Return: The variable created for the field. 3614 */ 3615 static struct field_var * 3616 create_target_field_var(struct hist_trigger_data *target_hist_data, 3617 char *subsys_name, char *event_name, char *var_name) 3618 { 3619 struct trace_event_file *file = target_hist_data->event_file; 3620 3621 if (subsys_name) { 3622 struct trace_event_call *call; 3623 3624 if (!event_name) 3625 return NULL; 3626 3627 call = file->event_call; 3628 3629 if (strcmp(subsys_name, call->class->system) != 0) 3630 return NULL; 3631 3632 if (strcmp(event_name, trace_event_name(call)) != 0) 3633 return NULL; 3634 } 3635 3636 return create_field_var(target_hist_data, file, var_name); 3637 } 3638 3639 static bool check_track_val_max(u64 track_val, u64 var_val) 3640 { 3641 if (var_val <= track_val) 3642 return false; 3643 3644 return true; 3645 } 3646 3647 static bool check_track_val_changed(u64 track_val, u64 var_val) 3648 { 3649 if (var_val == track_val) 3650 return false; 3651 3652 return true; 3653 } 3654 3655 static u64 get_track_val(struct hist_trigger_data *hist_data, 3656 struct tracing_map_elt *elt, 3657 struct action_data *data) 3658 { 3659 unsigned int track_var_idx = data->track_data.track_var->var.idx; 3660 u64 track_val; 3661 3662 track_val = tracing_map_read_var(elt, track_var_idx); 3663 3664 return track_val; 3665 } 3666 3667 static void save_track_val(struct hist_trigger_data *hist_data, 3668 struct tracing_map_elt *elt, 3669 struct action_data *data, u64 var_val) 3670 { 3671 unsigned int track_var_idx = data->track_data.track_var->var.idx; 3672 3673 tracing_map_set_var(elt, track_var_idx, var_val); 3674 } 3675 3676 static void save_track_data(struct hist_trigger_data *hist_data, 3677 struct tracing_map_elt *elt, void *rec, 3678 struct ring_buffer_event *rbe, void *key, 3679 struct action_data *data, u64 *var_ref_vals) 3680 { 3681 if (data->track_data.save_data) 3682 data->track_data.save_data(hist_data, elt, rec, rbe, key, data, var_ref_vals); 3683 } 3684 3685 static bool check_track_val(struct tracing_map_elt *elt, 3686 struct action_data *data, 3687 u64 var_val) 3688 { 3689 struct hist_trigger_data *hist_data; 3690 u64 track_val; 3691 3692 hist_data = data->track_data.track_var->hist_data; 3693 track_val = get_track_val(hist_data, elt, data); 3694 3695 return data->track_data.check_val(track_val, var_val); 3696 } 3697 3698 #ifdef CONFIG_TRACER_SNAPSHOT 3699 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data) 3700 { 3701 /* called with tr->max_lock held */ 3702 struct track_data *track_data = tr->cond_snapshot->cond_data; 3703 struct hist_elt_data *elt_data, *track_elt_data; 3704 struct snapshot_context *context = cond_data; 3705 struct action_data *action; 3706 u64 track_val; 3707 3708 if (!track_data) 3709 return false; 3710 3711 action = track_data->action_data; 3712 3713 track_val = get_track_val(track_data->hist_data, context->elt, 3714 track_data->action_data); 3715 3716 if (!action->track_data.check_val(track_data->track_val, track_val)) 3717 return false; 3718 3719 track_data->track_val = track_val; 3720 memcpy(track_data->key, context->key, track_data->key_len); 3721 3722 elt_data = context->elt->private_data; 3723 track_elt_data = track_data->elt.private_data; 3724 if (elt_data->comm) 3725 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN); 3726 3727 track_data->updated = true; 3728 3729 return true; 3730 } 3731 3732 static void save_track_data_snapshot(struct hist_trigger_data *hist_data, 3733 struct tracing_map_elt *elt, void *rec, 3734 struct ring_buffer_event *rbe, void *key, 3735 struct action_data *data, 3736 u64 *var_ref_vals) 3737 { 3738 struct trace_event_file *file = hist_data->event_file; 3739 struct snapshot_context context; 3740 3741 context.elt = elt; 3742 context.key = key; 3743 3744 tracing_snapshot_cond(file->tr, &context); 3745 } 3746 3747 static void hist_trigger_print_key(struct seq_file *m, 3748 struct hist_trigger_data *hist_data, 3749 void *key, 3750 struct tracing_map_elt *elt); 3751 3752 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data) 3753 { 3754 unsigned int i; 3755 3756 if (!hist_data->n_actions) 3757 return NULL; 3758 3759 for (i = 0; i < hist_data->n_actions; i++) { 3760 struct action_data *data = hist_data->actions[i]; 3761 3762 if (data->action == ACTION_SNAPSHOT) 3763 return data; 3764 } 3765 3766 return NULL; 3767 } 3768 3769 static void track_data_snapshot_print(struct seq_file *m, 3770 struct hist_trigger_data *hist_data) 3771 { 3772 struct trace_event_file *file = hist_data->event_file; 3773 struct track_data *track_data; 3774 struct action_data *action; 3775 3776 track_data = tracing_cond_snapshot_data(file->tr); 3777 if (!track_data) 3778 return; 3779 3780 if (!track_data->updated) 3781 return; 3782 3783 action = snapshot_action(hist_data); 3784 if (!action) 3785 return; 3786 3787 seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n"); 3788 seq_printf(m, "\ttriggering value { %s(%s) }: %10llu", 3789 action->handler == HANDLER_ONMAX ? "onmax" : "onchange", 3790 action->track_data.var_str, track_data->track_val); 3791 3792 seq_puts(m, "\ttriggered by event with key: "); 3793 hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt); 3794 seq_putc(m, '\n'); 3795 } 3796 #else 3797 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data) 3798 { 3799 return false; 3800 } 3801 static void save_track_data_snapshot(struct hist_trigger_data *hist_data, 3802 struct tracing_map_elt *elt, void *rec, 3803 struct ring_buffer_event *rbe, void *key, 3804 struct action_data *data, 3805 u64 *var_ref_vals) {} 3806 static void track_data_snapshot_print(struct seq_file *m, 3807 struct hist_trigger_data *hist_data) {} 3808 #endif /* CONFIG_TRACER_SNAPSHOT */ 3809 3810 static void track_data_print(struct seq_file *m, 3811 struct hist_trigger_data *hist_data, 3812 struct tracing_map_elt *elt, 3813 struct action_data *data) 3814 { 3815 u64 track_val = get_track_val(hist_data, elt, data); 3816 unsigned int i, save_var_idx; 3817 3818 if (data->handler == HANDLER_ONMAX) 3819 seq_printf(m, "\n\tmax: %10llu", track_val); 3820 else if (data->handler == HANDLER_ONCHANGE) 3821 seq_printf(m, "\n\tchanged: %10llu", track_val); 3822 3823 if (data->action == ACTION_SNAPSHOT) 3824 return; 3825 3826 for (i = 0; i < hist_data->n_save_vars; i++) { 3827 struct hist_field *save_val = hist_data->save_vars[i]->val; 3828 struct hist_field *save_var = hist_data->save_vars[i]->var; 3829 u64 val; 3830 3831 save_var_idx = save_var->var.idx; 3832 3833 val = tracing_map_read_var(elt, save_var_idx); 3834 3835 if (save_val->flags & HIST_FIELD_FL_STRING) { 3836 seq_printf(m, " %s: %-32s", save_var->var.name, 3837 (char *)(uintptr_t)(val)); 3838 } else 3839 seq_printf(m, " %s: %10llu", save_var->var.name, val); 3840 } 3841 } 3842 3843 static void ontrack_action(struct hist_trigger_data *hist_data, 3844 struct tracing_map_elt *elt, void *rec, 3845 struct ring_buffer_event *rbe, void *key, 3846 struct action_data *data, u64 *var_ref_vals) 3847 { 3848 u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx]; 3849 3850 if (check_track_val(elt, data, var_val)) { 3851 save_track_val(hist_data, elt, data, var_val); 3852 save_track_data(hist_data, elt, rec, rbe, key, data, var_ref_vals); 3853 } 3854 } 3855 3856 static void action_data_destroy(struct action_data *data) 3857 { 3858 unsigned int i; 3859 3860 lockdep_assert_held(&event_mutex); 3861 3862 kfree(data->action_name); 3863 3864 for (i = 0; i < data->n_params; i++) 3865 kfree(data->params[i]); 3866 3867 if (data->synth_event) 3868 data->synth_event->ref--; 3869 3870 kfree(data->synth_event_name); 3871 3872 kfree(data); 3873 } 3874 3875 static void track_data_destroy(struct hist_trigger_data *hist_data, 3876 struct action_data *data) 3877 { 3878 struct trace_event_file *file = hist_data->event_file; 3879 3880 destroy_hist_field(data->track_data.track_var, 0); 3881 3882 if (data->action == ACTION_SNAPSHOT) { 3883 struct track_data *track_data; 3884 3885 track_data = tracing_cond_snapshot_data(file->tr); 3886 if (track_data && track_data->hist_data == hist_data) { 3887 tracing_snapshot_cond_disable(file->tr); 3888 track_data_free(track_data); 3889 } 3890 } 3891 3892 kfree(data->track_data.var_str); 3893 3894 action_data_destroy(data); 3895 } 3896 3897 static int action_create(struct hist_trigger_data *hist_data, 3898 struct action_data *data); 3899 3900 static int track_data_create(struct hist_trigger_data *hist_data, 3901 struct action_data *data) 3902 { 3903 struct hist_field *var_field, *ref_field, *track_var = NULL; 3904 struct trace_event_file *file = hist_data->event_file; 3905 struct trace_array *tr = file->tr; 3906 char *track_data_var_str; 3907 int ret = 0; 3908 3909 track_data_var_str = data->track_data.var_str; 3910 if (track_data_var_str[0] != '$') { 3911 hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str)); 3912 return -EINVAL; 3913 } 3914 track_data_var_str++; 3915 3916 var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str); 3917 if (!var_field) { 3918 hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str)); 3919 return -EINVAL; 3920 } 3921 3922 ref_field = create_var_ref(hist_data, var_field, NULL, NULL); 3923 if (!ref_field) 3924 return -ENOMEM; 3925 3926 data->track_data.var_ref = ref_field; 3927 3928 if (data->handler == HANDLER_ONMAX) 3929 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64"); 3930 if (IS_ERR(track_var)) { 3931 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0); 3932 ret = PTR_ERR(track_var); 3933 goto out; 3934 } 3935 3936 if (data->handler == HANDLER_ONCHANGE) 3937 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64"); 3938 if (IS_ERR(track_var)) { 3939 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0); 3940 ret = PTR_ERR(track_var); 3941 goto out; 3942 } 3943 data->track_data.track_var = track_var; 3944 3945 ret = action_create(hist_data, data); 3946 out: 3947 return ret; 3948 } 3949 3950 static int parse_action_params(struct trace_array *tr, char *params, 3951 struct action_data *data) 3952 { 3953 char *param, *saved_param; 3954 bool first_param = true; 3955 int ret = 0; 3956 3957 while (params) { 3958 if (data->n_params >= SYNTH_FIELDS_MAX) { 3959 hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0); 3960 goto out; 3961 } 3962 3963 param = strsep(¶ms, ","); 3964 if (!param) { 3965 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0); 3966 ret = -EINVAL; 3967 goto out; 3968 } 3969 3970 param = strstrip(param); 3971 if (strlen(param) < 2) { 3972 hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param)); 3973 ret = -EINVAL; 3974 goto out; 3975 } 3976 3977 saved_param = kstrdup(param, GFP_KERNEL); 3978 if (!saved_param) { 3979 ret = -ENOMEM; 3980 goto out; 3981 } 3982 3983 if (first_param && data->use_trace_keyword) { 3984 data->synth_event_name = saved_param; 3985 first_param = false; 3986 continue; 3987 } 3988 first_param = false; 3989 3990 data->params[data->n_params++] = saved_param; 3991 } 3992 out: 3993 return ret; 3994 } 3995 3996 static int action_parse(struct trace_array *tr, char *str, struct action_data *data, 3997 enum handler_id handler) 3998 { 3999 char *action_name; 4000 int ret = 0; 4001 4002 strsep(&str, "."); 4003 if (!str) { 4004 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0); 4005 ret = -EINVAL; 4006 goto out; 4007 } 4008 4009 action_name = strsep(&str, "("); 4010 if (!action_name || !str) { 4011 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0); 4012 ret = -EINVAL; 4013 goto out; 4014 } 4015 4016 if (str_has_prefix(action_name, "save")) { 4017 char *params = strsep(&str, ")"); 4018 4019 if (!params) { 4020 hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0); 4021 ret = -EINVAL; 4022 goto out; 4023 } 4024 4025 ret = parse_action_params(tr, params, data); 4026 if (ret) 4027 goto out; 4028 4029 if (handler == HANDLER_ONMAX) 4030 data->track_data.check_val = check_track_val_max; 4031 else if (handler == HANDLER_ONCHANGE) 4032 data->track_data.check_val = check_track_val_changed; 4033 else { 4034 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name)); 4035 ret = -EINVAL; 4036 goto out; 4037 } 4038 4039 data->track_data.save_data = save_track_data_vars; 4040 data->fn = ontrack_action; 4041 data->action = ACTION_SAVE; 4042 } else if (str_has_prefix(action_name, "snapshot")) { 4043 char *params = strsep(&str, ")"); 4044 4045 if (!str) { 4046 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params)); 4047 ret = -EINVAL; 4048 goto out; 4049 } 4050 4051 if (handler == HANDLER_ONMAX) 4052 data->track_data.check_val = check_track_val_max; 4053 else if (handler == HANDLER_ONCHANGE) 4054 data->track_data.check_val = check_track_val_changed; 4055 else { 4056 hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name)); 4057 ret = -EINVAL; 4058 goto out; 4059 } 4060 4061 data->track_data.save_data = save_track_data_snapshot; 4062 data->fn = ontrack_action; 4063 data->action = ACTION_SNAPSHOT; 4064 } else { 4065 char *params = strsep(&str, ")"); 4066 4067 if (str_has_prefix(action_name, "trace")) 4068 data->use_trace_keyword = true; 4069 4070 if (params) { 4071 ret = parse_action_params(tr, params, data); 4072 if (ret) 4073 goto out; 4074 } 4075 4076 if (handler == HANDLER_ONMAX) 4077 data->track_data.check_val = check_track_val_max; 4078 else if (handler == HANDLER_ONCHANGE) 4079 data->track_data.check_val = check_track_val_changed; 4080 4081 if (handler != HANDLER_ONMATCH) { 4082 data->track_data.save_data = action_trace; 4083 data->fn = ontrack_action; 4084 } else 4085 data->fn = action_trace; 4086 4087 data->action = ACTION_TRACE; 4088 } 4089 4090 data->action_name = kstrdup(action_name, GFP_KERNEL); 4091 if (!data->action_name) { 4092 ret = -ENOMEM; 4093 goto out; 4094 } 4095 4096 data->handler = handler; 4097 out: 4098 return ret; 4099 } 4100 4101 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data, 4102 char *str, enum handler_id handler) 4103 { 4104 struct action_data *data; 4105 int ret = -EINVAL; 4106 char *var_str; 4107 4108 data = kzalloc(sizeof(*data), GFP_KERNEL); 4109 if (!data) 4110 return ERR_PTR(-ENOMEM); 4111 4112 var_str = strsep(&str, ")"); 4113 if (!var_str || !str) { 4114 ret = -EINVAL; 4115 goto free; 4116 } 4117 4118 data->track_data.var_str = kstrdup(var_str, GFP_KERNEL); 4119 if (!data->track_data.var_str) { 4120 ret = -ENOMEM; 4121 goto free; 4122 } 4123 4124 ret = action_parse(hist_data->event_file->tr, str, data, handler); 4125 if (ret) 4126 goto free; 4127 out: 4128 return data; 4129 free: 4130 track_data_destroy(hist_data, data); 4131 data = ERR_PTR(ret); 4132 goto out; 4133 } 4134 4135 static void onmatch_destroy(struct action_data *data) 4136 { 4137 kfree(data->match_data.event); 4138 kfree(data->match_data.event_system); 4139 4140 action_data_destroy(data); 4141 } 4142 4143 static void destroy_field_var(struct field_var *field_var) 4144 { 4145 if (!field_var) 4146 return; 4147 4148 destroy_hist_field(field_var->var, 0); 4149 destroy_hist_field(field_var->val, 0); 4150 4151 kfree(field_var); 4152 } 4153 4154 static void destroy_field_vars(struct hist_trigger_data *hist_data) 4155 { 4156 unsigned int i; 4157 4158 for (i = 0; i < hist_data->n_field_vars; i++) 4159 destroy_field_var(hist_data->field_vars[i]); 4160 } 4161 4162 static void save_field_var(struct hist_trigger_data *hist_data, 4163 struct field_var *field_var) 4164 { 4165 hist_data->field_vars[hist_data->n_field_vars++] = field_var; 4166 4167 if (field_var->val->flags & HIST_FIELD_FL_STRING) 4168 hist_data->n_field_var_str++; 4169 } 4170 4171 4172 static int check_synth_field(struct synth_event *event, 4173 struct hist_field *hist_field, 4174 unsigned int field_pos) 4175 { 4176 struct synth_field *field; 4177 4178 if (field_pos >= event->n_fields) 4179 return -EINVAL; 4180 4181 field = event->fields[field_pos]; 4182 4183 if (strcmp(field->type, hist_field->type) != 0) 4184 return -EINVAL; 4185 4186 return 0; 4187 } 4188 4189 static struct hist_field * 4190 trace_action_find_var(struct hist_trigger_data *hist_data, 4191 struct action_data *data, 4192 char *system, char *event, char *var) 4193 { 4194 struct trace_array *tr = hist_data->event_file->tr; 4195 struct hist_field *hist_field; 4196 4197 var++; /* skip '$' */ 4198 4199 hist_field = find_target_event_var(hist_data, system, event, var); 4200 if (!hist_field) { 4201 if (!system && data->handler == HANDLER_ONMATCH) { 4202 system = data->match_data.event_system; 4203 event = data->match_data.event; 4204 } 4205 4206 hist_field = find_event_var(hist_data, system, event, var); 4207 } 4208 4209 if (!hist_field) 4210 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var)); 4211 4212 return hist_field; 4213 } 4214 4215 static struct hist_field * 4216 trace_action_create_field_var(struct hist_trigger_data *hist_data, 4217 struct action_data *data, char *system, 4218 char *event, char *var) 4219 { 4220 struct hist_field *hist_field = NULL; 4221 struct field_var *field_var; 4222 4223 /* 4224 * First try to create a field var on the target event (the 4225 * currently being defined). This will create a variable for 4226 * unqualified fields on the target event, or if qualified, 4227 * target fields that have qualified names matching the target. 4228 */ 4229 field_var = create_target_field_var(hist_data, system, event, var); 4230 4231 if (field_var && !IS_ERR(field_var)) { 4232 save_field_var(hist_data, field_var); 4233 hist_field = field_var->var; 4234 } else { 4235 field_var = NULL; 4236 /* 4237 * If no explicit system.event is specfied, default to 4238 * looking for fields on the onmatch(system.event.xxx) 4239 * event. 4240 */ 4241 if (!system && data->handler == HANDLER_ONMATCH) { 4242 system = data->match_data.event_system; 4243 event = data->match_data.event; 4244 } 4245 4246 /* 4247 * At this point, we're looking at a field on another 4248 * event. Because we can't modify a hist trigger on 4249 * another event to add a variable for a field, we need 4250 * to create a new trigger on that event and create the 4251 * variable at the same time. 4252 */ 4253 hist_field = create_field_var_hist(hist_data, system, event, var); 4254 if (IS_ERR(hist_field)) 4255 goto free; 4256 } 4257 out: 4258 return hist_field; 4259 free: 4260 destroy_field_var(field_var); 4261 hist_field = NULL; 4262 goto out; 4263 } 4264 4265 static int trace_action_create(struct hist_trigger_data *hist_data, 4266 struct action_data *data) 4267 { 4268 struct trace_array *tr = hist_data->event_file->tr; 4269 char *event_name, *param, *system = NULL; 4270 struct hist_field *hist_field, *var_ref; 4271 unsigned int i; 4272 unsigned int field_pos = 0; 4273 struct synth_event *event; 4274 char *synth_event_name; 4275 int var_ref_idx, ret = 0; 4276 4277 lockdep_assert_held(&event_mutex); 4278 4279 if (data->use_trace_keyword) 4280 synth_event_name = data->synth_event_name; 4281 else 4282 synth_event_name = data->action_name; 4283 4284 event = find_synth_event(synth_event_name); 4285 if (!event) { 4286 hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name)); 4287 return -EINVAL; 4288 } 4289 4290 event->ref++; 4291 4292 for (i = 0; i < data->n_params; i++) { 4293 char *p; 4294 4295 p = param = kstrdup(data->params[i], GFP_KERNEL); 4296 if (!param) { 4297 ret = -ENOMEM; 4298 goto err; 4299 } 4300 4301 system = strsep(¶m, "."); 4302 if (!param) { 4303 param = (char *)system; 4304 system = event_name = NULL; 4305 } else { 4306 event_name = strsep(¶m, "."); 4307 if (!param) { 4308 kfree(p); 4309 ret = -EINVAL; 4310 goto err; 4311 } 4312 } 4313 4314 if (param[0] == '$') 4315 hist_field = trace_action_find_var(hist_data, data, 4316 system, event_name, 4317 param); 4318 else 4319 hist_field = trace_action_create_field_var(hist_data, 4320 data, 4321 system, 4322 event_name, 4323 param); 4324 4325 if (!hist_field) { 4326 kfree(p); 4327 ret = -EINVAL; 4328 goto err; 4329 } 4330 4331 if (check_synth_field(event, hist_field, field_pos) == 0) { 4332 var_ref = create_var_ref(hist_data, hist_field, 4333 system, event_name); 4334 if (!var_ref) { 4335 kfree(p); 4336 ret = -ENOMEM; 4337 goto err; 4338 } 4339 4340 var_ref_idx = find_var_ref_idx(hist_data, var_ref); 4341 if (WARN_ON(var_ref_idx < 0)) { 4342 ret = var_ref_idx; 4343 goto err; 4344 } 4345 4346 data->var_ref_idx[i] = var_ref_idx; 4347 4348 field_pos++; 4349 kfree(p); 4350 continue; 4351 } 4352 4353 hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param)); 4354 kfree(p); 4355 ret = -EINVAL; 4356 goto err; 4357 } 4358 4359 if (field_pos != event->n_fields) { 4360 hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name)); 4361 ret = -EINVAL; 4362 goto err; 4363 } 4364 4365 data->synth_event = event; 4366 out: 4367 return ret; 4368 err: 4369 event->ref--; 4370 4371 goto out; 4372 } 4373 4374 static int action_create(struct hist_trigger_data *hist_data, 4375 struct action_data *data) 4376 { 4377 struct trace_event_file *file = hist_data->event_file; 4378 struct trace_array *tr = file->tr; 4379 struct track_data *track_data; 4380 struct field_var *field_var; 4381 unsigned int i; 4382 char *param; 4383 int ret = 0; 4384 4385 if (data->action == ACTION_TRACE) 4386 return trace_action_create(hist_data, data); 4387 4388 if (data->action == ACTION_SNAPSHOT) { 4389 track_data = track_data_alloc(hist_data->key_size, data, hist_data); 4390 if (IS_ERR(track_data)) { 4391 ret = PTR_ERR(track_data); 4392 goto out; 4393 } 4394 4395 ret = tracing_snapshot_cond_enable(file->tr, track_data, 4396 cond_snapshot_update); 4397 if (ret) 4398 track_data_free(track_data); 4399 4400 goto out; 4401 } 4402 4403 if (data->action == ACTION_SAVE) { 4404 if (hist_data->n_save_vars) { 4405 ret = -EEXIST; 4406 hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0); 4407 goto out; 4408 } 4409 4410 for (i = 0; i < data->n_params; i++) { 4411 param = kstrdup(data->params[i], GFP_KERNEL); 4412 if (!param) { 4413 ret = -ENOMEM; 4414 goto out; 4415 } 4416 4417 field_var = create_target_field_var(hist_data, NULL, NULL, param); 4418 if (IS_ERR(field_var)) { 4419 hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL, 4420 errpos(param)); 4421 ret = PTR_ERR(field_var); 4422 kfree(param); 4423 goto out; 4424 } 4425 4426 hist_data->save_vars[hist_data->n_save_vars++] = field_var; 4427 if (field_var->val->flags & HIST_FIELD_FL_STRING) 4428 hist_data->n_save_var_str++; 4429 kfree(param); 4430 } 4431 } 4432 out: 4433 return ret; 4434 } 4435 4436 static int onmatch_create(struct hist_trigger_data *hist_data, 4437 struct action_data *data) 4438 { 4439 return action_create(hist_data, data); 4440 } 4441 4442 static struct action_data *onmatch_parse(struct trace_array *tr, char *str) 4443 { 4444 char *match_event, *match_event_system; 4445 struct action_data *data; 4446 int ret = -EINVAL; 4447 4448 data = kzalloc(sizeof(*data), GFP_KERNEL); 4449 if (!data) 4450 return ERR_PTR(-ENOMEM); 4451 4452 match_event = strsep(&str, ")"); 4453 if (!match_event || !str) { 4454 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event)); 4455 goto free; 4456 } 4457 4458 match_event_system = strsep(&match_event, "."); 4459 if (!match_event) { 4460 hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system)); 4461 goto free; 4462 } 4463 4464 if (IS_ERR(event_file(tr, match_event_system, match_event))) { 4465 hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event)); 4466 goto free; 4467 } 4468 4469 data->match_data.event = kstrdup(match_event, GFP_KERNEL); 4470 if (!data->match_data.event) { 4471 ret = -ENOMEM; 4472 goto free; 4473 } 4474 4475 data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL); 4476 if (!data->match_data.event_system) { 4477 ret = -ENOMEM; 4478 goto free; 4479 } 4480 4481 ret = action_parse(tr, str, data, HANDLER_ONMATCH); 4482 if (ret) 4483 goto free; 4484 out: 4485 return data; 4486 free: 4487 onmatch_destroy(data); 4488 data = ERR_PTR(ret); 4489 goto out; 4490 } 4491 4492 static int create_hitcount_val(struct hist_trigger_data *hist_data) 4493 { 4494 hist_data->fields[HITCOUNT_IDX] = 4495 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL); 4496 if (!hist_data->fields[HITCOUNT_IDX]) 4497 return -ENOMEM; 4498 4499 hist_data->n_vals++; 4500 hist_data->n_fields++; 4501 4502 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX)) 4503 return -EINVAL; 4504 4505 return 0; 4506 } 4507 4508 static int __create_val_field(struct hist_trigger_data *hist_data, 4509 unsigned int val_idx, 4510 struct trace_event_file *file, 4511 char *var_name, char *field_str, 4512 unsigned long flags) 4513 { 4514 struct hist_field *hist_field; 4515 int ret = 0; 4516 4517 hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0); 4518 if (IS_ERR(hist_field)) { 4519 ret = PTR_ERR(hist_field); 4520 goto out; 4521 } 4522 4523 hist_data->fields[val_idx] = hist_field; 4524 4525 ++hist_data->n_vals; 4526 ++hist_data->n_fields; 4527 4528 if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX)) 4529 ret = -EINVAL; 4530 out: 4531 return ret; 4532 } 4533 4534 static int create_val_field(struct hist_trigger_data *hist_data, 4535 unsigned int val_idx, 4536 struct trace_event_file *file, 4537 char *field_str) 4538 { 4539 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX)) 4540 return -EINVAL; 4541 4542 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0); 4543 } 4544 4545 static int create_var_field(struct hist_trigger_data *hist_data, 4546 unsigned int val_idx, 4547 struct trace_event_file *file, 4548 char *var_name, char *expr_str) 4549 { 4550 struct trace_array *tr = hist_data->event_file->tr; 4551 unsigned long flags = 0; 4552 4553 if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX)) 4554 return -EINVAL; 4555 4556 if (find_var(hist_data, file, var_name) && !hist_data->remove) { 4557 hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name)); 4558 return -EINVAL; 4559 } 4560 4561 flags |= HIST_FIELD_FL_VAR; 4562 hist_data->n_vars++; 4563 if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX)) 4564 return -EINVAL; 4565 4566 return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags); 4567 } 4568 4569 static int create_val_fields(struct hist_trigger_data *hist_data, 4570 struct trace_event_file *file) 4571 { 4572 char *fields_str, *field_str; 4573 unsigned int i, j = 1; 4574 int ret; 4575 4576 ret = create_hitcount_val(hist_data); 4577 if (ret) 4578 goto out; 4579 4580 fields_str = hist_data->attrs->vals_str; 4581 if (!fields_str) 4582 goto out; 4583 4584 for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX && 4585 j < TRACING_MAP_VALS_MAX; i++) { 4586 field_str = strsep(&fields_str, ","); 4587 if (!field_str) 4588 break; 4589 4590 if (strcmp(field_str, "hitcount") == 0) 4591 continue; 4592 4593 ret = create_val_field(hist_data, j++, file, field_str); 4594 if (ret) 4595 goto out; 4596 } 4597 4598 if (fields_str && (strcmp(fields_str, "hitcount") != 0)) 4599 ret = -EINVAL; 4600 out: 4601 return ret; 4602 } 4603 4604 static int create_key_field(struct hist_trigger_data *hist_data, 4605 unsigned int key_idx, 4606 unsigned int key_offset, 4607 struct trace_event_file *file, 4608 char *field_str) 4609 { 4610 struct trace_array *tr = hist_data->event_file->tr; 4611 struct hist_field *hist_field = NULL; 4612 unsigned long flags = 0; 4613 unsigned int key_size; 4614 int ret = 0; 4615 4616 if (WARN_ON(key_idx >= HIST_FIELDS_MAX)) 4617 return -EINVAL; 4618 4619 flags |= HIST_FIELD_FL_KEY; 4620 4621 if (strcmp(field_str, "stacktrace") == 0) { 4622 flags |= HIST_FIELD_FL_STACKTRACE; 4623 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH; 4624 hist_field = create_hist_field(hist_data, NULL, flags, NULL); 4625 } else { 4626 hist_field = parse_expr(hist_data, file, field_str, flags, 4627 NULL, 0); 4628 if (IS_ERR(hist_field)) { 4629 ret = PTR_ERR(hist_field); 4630 goto out; 4631 } 4632 4633 if (field_has_hist_vars(hist_field, 0)) { 4634 hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str)); 4635 destroy_hist_field(hist_field, 0); 4636 ret = -EINVAL; 4637 goto out; 4638 } 4639 4640 key_size = hist_field->size; 4641 } 4642 4643 hist_data->fields[key_idx] = hist_field; 4644 4645 key_size = ALIGN(key_size, sizeof(u64)); 4646 hist_data->fields[key_idx]->size = key_size; 4647 hist_data->fields[key_idx]->offset = key_offset; 4648 4649 hist_data->key_size += key_size; 4650 4651 if (hist_data->key_size > HIST_KEY_SIZE_MAX) { 4652 ret = -EINVAL; 4653 goto out; 4654 } 4655 4656 hist_data->n_keys++; 4657 hist_data->n_fields++; 4658 4659 if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX)) 4660 return -EINVAL; 4661 4662 ret = key_size; 4663 out: 4664 return ret; 4665 } 4666 4667 static int create_key_fields(struct hist_trigger_data *hist_data, 4668 struct trace_event_file *file) 4669 { 4670 unsigned int i, key_offset = 0, n_vals = hist_data->n_vals; 4671 char *fields_str, *field_str; 4672 int ret = -EINVAL; 4673 4674 fields_str = hist_data->attrs->keys_str; 4675 if (!fields_str) 4676 goto out; 4677 4678 for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) { 4679 field_str = strsep(&fields_str, ","); 4680 if (!field_str) 4681 break; 4682 ret = create_key_field(hist_data, i, key_offset, 4683 file, field_str); 4684 if (ret < 0) 4685 goto out; 4686 key_offset += ret; 4687 } 4688 if (fields_str) { 4689 ret = -EINVAL; 4690 goto out; 4691 } 4692 ret = 0; 4693 out: 4694 return ret; 4695 } 4696 4697 static int create_var_fields(struct hist_trigger_data *hist_data, 4698 struct trace_event_file *file) 4699 { 4700 unsigned int i, j = hist_data->n_vals; 4701 int ret = 0; 4702 4703 unsigned int n_vars = hist_data->attrs->var_defs.n_vars; 4704 4705 for (i = 0; i < n_vars; i++) { 4706 char *var_name = hist_data->attrs->var_defs.name[i]; 4707 char *expr = hist_data->attrs->var_defs.expr[i]; 4708 4709 ret = create_var_field(hist_data, j++, file, var_name, expr); 4710 if (ret) 4711 goto out; 4712 } 4713 out: 4714 return ret; 4715 } 4716 4717 static void free_var_defs(struct hist_trigger_data *hist_data) 4718 { 4719 unsigned int i; 4720 4721 for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) { 4722 kfree(hist_data->attrs->var_defs.name[i]); 4723 kfree(hist_data->attrs->var_defs.expr[i]); 4724 } 4725 4726 hist_data->attrs->var_defs.n_vars = 0; 4727 } 4728 4729 static int parse_var_defs(struct hist_trigger_data *hist_data) 4730 { 4731 struct trace_array *tr = hist_data->event_file->tr; 4732 char *s, *str, *var_name, *field_str; 4733 unsigned int i, j, n_vars = 0; 4734 int ret = 0; 4735 4736 for (i = 0; i < hist_data->attrs->n_assignments; i++) { 4737 str = hist_data->attrs->assignment_str[i]; 4738 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) { 4739 field_str = strsep(&str, ","); 4740 if (!field_str) 4741 break; 4742 4743 var_name = strsep(&field_str, "="); 4744 if (!var_name || !field_str) { 4745 hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT, 4746 errpos(var_name)); 4747 ret = -EINVAL; 4748 goto free; 4749 } 4750 4751 if (n_vars == TRACING_MAP_VARS_MAX) { 4752 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name)); 4753 ret = -EINVAL; 4754 goto free; 4755 } 4756 4757 s = kstrdup(var_name, GFP_KERNEL); 4758 if (!s) { 4759 ret = -ENOMEM; 4760 goto free; 4761 } 4762 hist_data->attrs->var_defs.name[n_vars] = s; 4763 4764 s = kstrdup(field_str, GFP_KERNEL); 4765 if (!s) { 4766 kfree(hist_data->attrs->var_defs.name[n_vars]); 4767 ret = -ENOMEM; 4768 goto free; 4769 } 4770 hist_data->attrs->var_defs.expr[n_vars++] = s; 4771 4772 hist_data->attrs->var_defs.n_vars = n_vars; 4773 } 4774 } 4775 4776 return ret; 4777 free: 4778 free_var_defs(hist_data); 4779 4780 return ret; 4781 } 4782 4783 static int create_hist_fields(struct hist_trigger_data *hist_data, 4784 struct trace_event_file *file) 4785 { 4786 int ret; 4787 4788 ret = parse_var_defs(hist_data); 4789 if (ret) 4790 goto out; 4791 4792 ret = create_val_fields(hist_data, file); 4793 if (ret) 4794 goto out; 4795 4796 ret = create_var_fields(hist_data, file); 4797 if (ret) 4798 goto out; 4799 4800 ret = create_key_fields(hist_data, file); 4801 if (ret) 4802 goto out; 4803 out: 4804 free_var_defs(hist_data); 4805 4806 return ret; 4807 } 4808 4809 static int is_descending(const char *str) 4810 { 4811 if (!str) 4812 return 0; 4813 4814 if (strcmp(str, "descending") == 0) 4815 return 1; 4816 4817 if (strcmp(str, "ascending") == 0) 4818 return 0; 4819 4820 return -EINVAL; 4821 } 4822 4823 static int create_sort_keys(struct hist_trigger_data *hist_data) 4824 { 4825 char *fields_str = hist_data->attrs->sort_key_str; 4826 struct tracing_map_sort_key *sort_key; 4827 int descending, ret = 0; 4828 unsigned int i, j, k; 4829 4830 hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */ 4831 4832 if (!fields_str) 4833 goto out; 4834 4835 for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) { 4836 struct hist_field *hist_field; 4837 char *field_str, *field_name; 4838 const char *test_name; 4839 4840 sort_key = &hist_data->sort_keys[i]; 4841 4842 field_str = strsep(&fields_str, ","); 4843 if (!field_str) 4844 break; 4845 4846 if (!*field_str) { 4847 ret = -EINVAL; 4848 break; 4849 } 4850 4851 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) { 4852 ret = -EINVAL; 4853 break; 4854 } 4855 4856 field_name = strsep(&field_str, "."); 4857 if (!field_name || !*field_name) { 4858 ret = -EINVAL; 4859 break; 4860 } 4861 4862 if (strcmp(field_name, "hitcount") == 0) { 4863 descending = is_descending(field_str); 4864 if (descending < 0) { 4865 ret = descending; 4866 break; 4867 } 4868 sort_key->descending = descending; 4869 continue; 4870 } 4871 4872 for (j = 1, k = 1; j < hist_data->n_fields; j++) { 4873 unsigned int idx; 4874 4875 hist_field = hist_data->fields[j]; 4876 if (hist_field->flags & HIST_FIELD_FL_VAR) 4877 continue; 4878 4879 idx = k++; 4880 4881 test_name = hist_field_name(hist_field, 0); 4882 4883 if (strcmp(field_name, test_name) == 0) { 4884 sort_key->field_idx = idx; 4885 descending = is_descending(field_str); 4886 if (descending < 0) { 4887 ret = descending; 4888 goto out; 4889 } 4890 sort_key->descending = descending; 4891 break; 4892 } 4893 } 4894 if (j == hist_data->n_fields) { 4895 ret = -EINVAL; 4896 break; 4897 } 4898 } 4899 4900 hist_data->n_sort_keys = i; 4901 out: 4902 return ret; 4903 } 4904 4905 static void destroy_actions(struct hist_trigger_data *hist_data) 4906 { 4907 unsigned int i; 4908 4909 for (i = 0; i < hist_data->n_actions; i++) { 4910 struct action_data *data = hist_data->actions[i]; 4911 4912 if (data->handler == HANDLER_ONMATCH) 4913 onmatch_destroy(data); 4914 else if (data->handler == HANDLER_ONMAX || 4915 data->handler == HANDLER_ONCHANGE) 4916 track_data_destroy(hist_data, data); 4917 else 4918 kfree(data); 4919 } 4920 } 4921 4922 static int parse_actions(struct hist_trigger_data *hist_data) 4923 { 4924 struct trace_array *tr = hist_data->event_file->tr; 4925 struct action_data *data; 4926 unsigned int i; 4927 int ret = 0; 4928 char *str; 4929 int len; 4930 4931 for (i = 0; i < hist_data->attrs->n_actions; i++) { 4932 str = hist_data->attrs->action_str[i]; 4933 4934 if ((len = str_has_prefix(str, "onmatch("))) { 4935 char *action_str = str + len; 4936 4937 data = onmatch_parse(tr, action_str); 4938 if (IS_ERR(data)) { 4939 ret = PTR_ERR(data); 4940 break; 4941 } 4942 } else if ((len = str_has_prefix(str, "onmax("))) { 4943 char *action_str = str + len; 4944 4945 data = track_data_parse(hist_data, action_str, 4946 HANDLER_ONMAX); 4947 if (IS_ERR(data)) { 4948 ret = PTR_ERR(data); 4949 break; 4950 } 4951 } else if ((len = str_has_prefix(str, "onchange("))) { 4952 char *action_str = str + len; 4953 4954 data = track_data_parse(hist_data, action_str, 4955 HANDLER_ONCHANGE); 4956 if (IS_ERR(data)) { 4957 ret = PTR_ERR(data); 4958 break; 4959 } 4960 } else { 4961 ret = -EINVAL; 4962 break; 4963 } 4964 4965 hist_data->actions[hist_data->n_actions++] = data; 4966 } 4967 4968 return ret; 4969 } 4970 4971 static int create_actions(struct hist_trigger_data *hist_data) 4972 { 4973 struct action_data *data; 4974 unsigned int i; 4975 int ret = 0; 4976 4977 for (i = 0; i < hist_data->attrs->n_actions; i++) { 4978 data = hist_data->actions[i]; 4979 4980 if (data->handler == HANDLER_ONMATCH) { 4981 ret = onmatch_create(hist_data, data); 4982 if (ret) 4983 break; 4984 } else if (data->handler == HANDLER_ONMAX || 4985 data->handler == HANDLER_ONCHANGE) { 4986 ret = track_data_create(hist_data, data); 4987 if (ret) 4988 break; 4989 } else { 4990 ret = -EINVAL; 4991 break; 4992 } 4993 } 4994 4995 return ret; 4996 } 4997 4998 static void print_actions(struct seq_file *m, 4999 struct hist_trigger_data *hist_data, 5000 struct tracing_map_elt *elt) 5001 { 5002 unsigned int i; 5003 5004 for (i = 0; i < hist_data->n_actions; i++) { 5005 struct action_data *data = hist_data->actions[i]; 5006 5007 if (data->action == ACTION_SNAPSHOT) 5008 continue; 5009 5010 if (data->handler == HANDLER_ONMAX || 5011 data->handler == HANDLER_ONCHANGE) 5012 track_data_print(m, hist_data, elt, data); 5013 } 5014 } 5015 5016 static void print_action_spec(struct seq_file *m, 5017 struct hist_trigger_data *hist_data, 5018 struct action_data *data) 5019 { 5020 unsigned int i; 5021 5022 if (data->action == ACTION_SAVE) { 5023 for (i = 0; i < hist_data->n_save_vars; i++) { 5024 seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name); 5025 if (i < hist_data->n_save_vars - 1) 5026 seq_puts(m, ","); 5027 } 5028 } else if (data->action == ACTION_TRACE) { 5029 if (data->use_trace_keyword) 5030 seq_printf(m, "%s", data->synth_event_name); 5031 for (i = 0; i < data->n_params; i++) { 5032 if (i || data->use_trace_keyword) 5033 seq_puts(m, ","); 5034 seq_printf(m, "%s", data->params[i]); 5035 } 5036 } 5037 } 5038 5039 static void print_track_data_spec(struct seq_file *m, 5040 struct hist_trigger_data *hist_data, 5041 struct action_data *data) 5042 { 5043 if (data->handler == HANDLER_ONMAX) 5044 seq_puts(m, ":onmax("); 5045 else if (data->handler == HANDLER_ONCHANGE) 5046 seq_puts(m, ":onchange("); 5047 seq_printf(m, "%s", data->track_data.var_str); 5048 seq_printf(m, ").%s(", data->action_name); 5049 5050 print_action_spec(m, hist_data, data); 5051 5052 seq_puts(m, ")"); 5053 } 5054 5055 static void print_onmatch_spec(struct seq_file *m, 5056 struct hist_trigger_data *hist_data, 5057 struct action_data *data) 5058 { 5059 seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system, 5060 data->match_data.event); 5061 5062 seq_printf(m, "%s(", data->action_name); 5063 5064 print_action_spec(m, hist_data, data); 5065 5066 seq_puts(m, ")"); 5067 } 5068 5069 static bool actions_match(struct hist_trigger_data *hist_data, 5070 struct hist_trigger_data *hist_data_test) 5071 { 5072 unsigned int i, j; 5073 5074 if (hist_data->n_actions != hist_data_test->n_actions) 5075 return false; 5076 5077 for (i = 0; i < hist_data->n_actions; i++) { 5078 struct action_data *data = hist_data->actions[i]; 5079 struct action_data *data_test = hist_data_test->actions[i]; 5080 char *action_name, *action_name_test; 5081 5082 if (data->handler != data_test->handler) 5083 return false; 5084 if (data->action != data_test->action) 5085 return false; 5086 5087 if (data->n_params != data_test->n_params) 5088 return false; 5089 5090 for (j = 0; j < data->n_params; j++) { 5091 if (strcmp(data->params[j], data_test->params[j]) != 0) 5092 return false; 5093 } 5094 5095 if (data->use_trace_keyword) 5096 action_name = data->synth_event_name; 5097 else 5098 action_name = data->action_name; 5099 5100 if (data_test->use_trace_keyword) 5101 action_name_test = data_test->synth_event_name; 5102 else 5103 action_name_test = data_test->action_name; 5104 5105 if (strcmp(action_name, action_name_test) != 0) 5106 return false; 5107 5108 if (data->handler == HANDLER_ONMATCH) { 5109 if (strcmp(data->match_data.event_system, 5110 data_test->match_data.event_system) != 0) 5111 return false; 5112 if (strcmp(data->match_data.event, 5113 data_test->match_data.event) != 0) 5114 return false; 5115 } else if (data->handler == HANDLER_ONMAX || 5116 data->handler == HANDLER_ONCHANGE) { 5117 if (strcmp(data->track_data.var_str, 5118 data_test->track_data.var_str) != 0) 5119 return false; 5120 } 5121 } 5122 5123 return true; 5124 } 5125 5126 5127 static void print_actions_spec(struct seq_file *m, 5128 struct hist_trigger_data *hist_data) 5129 { 5130 unsigned int i; 5131 5132 for (i = 0; i < hist_data->n_actions; i++) { 5133 struct action_data *data = hist_data->actions[i]; 5134 5135 if (data->handler == HANDLER_ONMATCH) 5136 print_onmatch_spec(m, hist_data, data); 5137 else if (data->handler == HANDLER_ONMAX || 5138 data->handler == HANDLER_ONCHANGE) 5139 print_track_data_spec(m, hist_data, data); 5140 } 5141 } 5142 5143 static void destroy_field_var_hists(struct hist_trigger_data *hist_data) 5144 { 5145 unsigned int i; 5146 5147 for (i = 0; i < hist_data->n_field_var_hists; i++) { 5148 kfree(hist_data->field_var_hists[i]->cmd); 5149 kfree(hist_data->field_var_hists[i]); 5150 } 5151 } 5152 5153 static void destroy_hist_data(struct hist_trigger_data *hist_data) 5154 { 5155 if (!hist_data) 5156 return; 5157 5158 destroy_hist_trigger_attrs(hist_data->attrs); 5159 destroy_hist_fields(hist_data); 5160 tracing_map_destroy(hist_data->map); 5161 5162 destroy_actions(hist_data); 5163 destroy_field_vars(hist_data); 5164 destroy_field_var_hists(hist_data); 5165 5166 kfree(hist_data); 5167 } 5168 5169 static int create_tracing_map_fields(struct hist_trigger_data *hist_data) 5170 { 5171 struct tracing_map *map = hist_data->map; 5172 struct ftrace_event_field *field; 5173 struct hist_field *hist_field; 5174 int i, idx = 0; 5175 5176 for_each_hist_field(i, hist_data) { 5177 hist_field = hist_data->fields[i]; 5178 if (hist_field->flags & HIST_FIELD_FL_KEY) { 5179 tracing_map_cmp_fn_t cmp_fn; 5180 5181 field = hist_field->field; 5182 5183 if (hist_field->flags & HIST_FIELD_FL_STACKTRACE) 5184 cmp_fn = tracing_map_cmp_none; 5185 else if (!field) 5186 cmp_fn = tracing_map_cmp_num(hist_field->size, 5187 hist_field->is_signed); 5188 else if (is_string_field(field)) 5189 cmp_fn = tracing_map_cmp_string; 5190 else 5191 cmp_fn = tracing_map_cmp_num(field->size, 5192 field->is_signed); 5193 idx = tracing_map_add_key_field(map, 5194 hist_field->offset, 5195 cmp_fn); 5196 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR)) 5197 idx = tracing_map_add_sum_field(map); 5198 5199 if (idx < 0) 5200 return idx; 5201 5202 if (hist_field->flags & HIST_FIELD_FL_VAR) { 5203 idx = tracing_map_add_var(map); 5204 if (idx < 0) 5205 return idx; 5206 hist_field->var.idx = idx; 5207 hist_field->var.hist_data = hist_data; 5208 } 5209 } 5210 5211 return 0; 5212 } 5213 5214 static struct hist_trigger_data * 5215 create_hist_data(unsigned int map_bits, 5216 struct hist_trigger_attrs *attrs, 5217 struct trace_event_file *file, 5218 bool remove) 5219 { 5220 const struct tracing_map_ops *map_ops = NULL; 5221 struct hist_trigger_data *hist_data; 5222 int ret = 0; 5223 5224 hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL); 5225 if (!hist_data) 5226 return ERR_PTR(-ENOMEM); 5227 5228 hist_data->attrs = attrs; 5229 hist_data->remove = remove; 5230 hist_data->event_file = file; 5231 5232 ret = parse_actions(hist_data); 5233 if (ret) 5234 goto free; 5235 5236 ret = create_hist_fields(hist_data, file); 5237 if (ret) 5238 goto free; 5239 5240 ret = create_sort_keys(hist_data); 5241 if (ret) 5242 goto free; 5243 5244 map_ops = &hist_trigger_elt_data_ops; 5245 5246 hist_data->map = tracing_map_create(map_bits, hist_data->key_size, 5247 map_ops, hist_data); 5248 if (IS_ERR(hist_data->map)) { 5249 ret = PTR_ERR(hist_data->map); 5250 hist_data->map = NULL; 5251 goto free; 5252 } 5253 5254 ret = create_tracing_map_fields(hist_data); 5255 if (ret) 5256 goto free; 5257 out: 5258 return hist_data; 5259 free: 5260 hist_data->attrs = NULL; 5261 5262 destroy_hist_data(hist_data); 5263 5264 hist_data = ERR_PTR(ret); 5265 5266 goto out;