1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * trace_events_synth - synthetic trace events 4 * 5 * Copyright (C) 2015, 2020 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 #include "trace_probe.h" 21 #include "trace_probe_kernel.h" 22 23 #include "trace_synth.h" 24 25 #undef ERRORS 26 #define ERRORS \ 27 C(BAD_NAME, "Illegal name"), \ 28 C(INVALID_CMD, "Command must be of the form: <name> field[;field] ..."),\ 29 C(INVALID_DYN_CMD, "Command must be of the form: s or -:[synthetic/]<name> field[;field] ..."),\ 30 C(EVENT_EXISTS, "Event already exists"), \ 31 C(TOO_MANY_FIELDS, "Too many fields"), \ 32 C(INCOMPLETE_TYPE, "Incomplete type"), \ 33 C(INVALID_TYPE, "Invalid type"), \ 34 C(INVALID_FIELD, "Invalid field"), \ 35 C(INVALID_ARRAY_SPEC, "Invalid array specification"), 36 37 #undef C 38 #define C(a, b) SYNTH_ERR_##a 39 40 enum { ERRORS }; 41 42 #undef C 43 #define C(a, b) b 44 45 static const char *err_text[] = { ERRORS }; 46 47 static DEFINE_MUTEX(lastcmd_mutex); 48 static char *last_cmd; 49 50 static int errpos(const char *str) 51 { 52 guard(mutex)(&lastcmd_mutex); 53 if (!str || !last_cmd) 54 return 0; 55 56 return err_pos(last_cmd, str); 57 } 58 59 static void last_cmd_set(const char *str) 60 { 61 if (!str) 62 return; 63 64 mutex_lock(&lastcmd_mutex); 65 kfree(last_cmd); 66 last_cmd = kstrdup(str, GFP_KERNEL); 67 mutex_unlock(&lastcmd_mutex); 68 } 69 70 static void synth_err(u8 err_type, u16 err_pos) 71 { 72 guard(mutex)(&lastcmd_mutex); 73 if (!last_cmd) 74 return; 75 76 tracing_log_err(NULL, "synthetic_events", last_cmd, err_text, 77 err_type, err_pos); 78 } 79 80 static int create_synth_event(const char *raw_command); 81 static int synth_event_show(struct seq_file *m, struct dyn_event *ev); 82 static int synth_event_release(struct dyn_event *ev); 83 static bool synth_event_is_busy(struct dyn_event *ev); 84 static bool synth_event_match(const char *system, const char *event, 85 int argc, const char **argv, struct dyn_event *ev); 86 87 static struct dyn_event_operations synth_event_ops = { 88 .create = create_synth_event, 89 .show = synth_event_show, 90 .is_busy = synth_event_is_busy, 91 .free = synth_event_release, 92 .match = synth_event_match, 93 }; 94 95 static bool is_synth_event(struct dyn_event *ev) 96 { 97 return ev->ops == &synth_event_ops; 98 } 99 100 static struct synth_event *to_synth_event(struct dyn_event *ev) 101 { 102 return container_of(ev, struct synth_event, devent); 103 } 104 105 static bool synth_event_is_busy(struct dyn_event *ev) 106 { 107 struct synth_event *event = to_synth_event(ev); 108 109 return event->ref != 0; 110 } 111 112 static bool synth_event_match(const char *system, const char *event, 113 int argc, const char **argv, struct dyn_event *ev) 114 { 115 struct synth_event *sev = to_synth_event(ev); 116 117 return strcmp(sev->name, event) == 0 && 118 (!system || strcmp(system, SYNTH_SYSTEM) == 0); 119 } 120 121 struct synth_trace_event { 122 struct trace_entry ent; 123 union trace_synth_field fields[]; 124 }; 125 126 static int synth_event_define_fields(struct trace_event_call *call) 127 { 128 struct synth_trace_event trace; 129 int offset = offsetof(typeof(trace), fields); 130 struct synth_event *event = call->data; 131 unsigned int i, size, n_u64; 132 char *name, *type; 133 bool is_signed; 134 int ret = 0; 135 136 for (i = 0, n_u64 = 0; i < event->n_fields; i++) { 137 size = event->fields[i]->size; 138 is_signed = event->fields[i]->is_signed; 139 type = event->fields[i]->type; 140 name = event->fields[i]->name; 141 ret = trace_define_field(call, type, name, offset, size, 142 is_signed, FILTER_OTHER); 143 if (ret) 144 break; 145 146 event->fields[i]->offset = n_u64; 147 148 if (event->fields[i]->is_string && !event->fields[i]->is_dynamic) { 149 offset += STR_VAR_LEN_MAX; 150 n_u64 += STR_VAR_LEN_MAX / sizeof(u64); 151 } else { 152 offset += sizeof(u64); 153 n_u64++; 154 } 155 } 156 157 event->n_u64 = n_u64; 158 159 return ret; 160 } 161 162 static bool synth_field_signed(char *type) 163 { 164 if (str_has_prefix(type, "u")) 165 return false; 166 if (strcmp(type, "gfp_t") == 0) 167 return false; 168 169 return true; 170 } 171 172 static int synth_field_is_string(char *type) 173 { 174 if (strstr(type, "char[") != NULL) 175 return true; 176 177 return false; 178 } 179 180 static int synth_field_is_stack(char *type) 181 { 182 if (strstr(type, "long[") != NULL) 183 return true; 184 185 return false; 186 } 187 188 static int synth_field_string_size(char *type) 189 { 190 char buf[4], *end, *start; 191 unsigned int len; 192 int size, err; 193 194 start = strstr(type, "char["); 195 if (start == NULL) 196 return -EINVAL; 197 start += sizeof("char[") - 1; 198 199 end = strchr(type, ']'); 200 if (!end || end < start || type + strlen(type) > end + 1) 201 return -EINVAL; 202 203 len = end - start; 204 if (len > 3) 205 return -EINVAL; 206 207 if (len == 0) 208 return 0; /* variable-length string */ 209 210 strncpy(buf, start, len); 211 buf[len] = '\0'; 212 213 err = kstrtouint(buf, 0, &size); 214 if (err) 215 return err; 216 217 if (size > STR_VAR_LEN_MAX) 218 return -EINVAL; 219 220 return size; 221 } 222 223 static int synth_field_size(char *type) 224 { 225 int size = 0; 226 227 if (strcmp(type, "s64") == 0) 228 size = sizeof(s64); 229 else if (strcmp(type, "u64") == 0) 230 size = sizeof(u64); 231 else if (strcmp(type, "s32") == 0) 232 size = sizeof(s32); 233 else if (strcmp(type, "u32") == 0) 234 size = sizeof(u32); 235 else if (strcmp(type, "s16") == 0) 236 size = sizeof(s16); 237 else if (strcmp(type, "u16") == 0) 238 size = sizeof(u16); 239 else if (strcmp(type, "s8") == 0) 240 size = sizeof(s8); 241 else if (strcmp(type, "u8") == 0) 242 size = sizeof(u8); 243 else if (strcmp(type, "char") == 0) 244 size = sizeof(char); 245 else if (strcmp(type, "unsigned char") == 0) 246 size = sizeof(unsigned char); 247 else if (strcmp(type, "int") == 0) 248 size = sizeof(int); 249 else if (strcmp(type, "unsigned int") == 0) 250 size = sizeof(unsigned int); 251 else if (strcmp(type, "long") == 0) 252 size = sizeof(long); 253 else if (strcmp(type, "unsigned long") == 0) 254 size = sizeof(unsigned long); 255 else if (strcmp(type, "bool") == 0) 256 size = sizeof(bool); 257 else if (strcmp(type, "pid_t") == 0) 258 size = sizeof(pid_t); 259 else if (strcmp(type, "gfp_t") == 0) 260 size = sizeof(gfp_t); 261 else if (synth_field_is_string(type)) 262 size = synth_field_string_size(type); 263 else if (synth_field_is_stack(type)) 264 size = 0; 265 266 return size; 267 } 268 269 static const char *synth_field_fmt(char *type) 270 { 271 const char *fmt = "%llu"; 272 273 if (strcmp(type, "s64") == 0) 274 fmt = "%lld"; 275 else if (strcmp(type, "u64") == 0) 276 fmt = "%llu"; 277 else if (strcmp(type, "s32") == 0) 278 fmt = "%d"; 279 else if (strcmp(type, "u32") == 0) 280 fmt = "%u"; 281 else if (strcmp(type, "s16") == 0) 282 fmt = "%d"; 283 else if (strcmp(type, "u16") == 0) 284 fmt = "%u"; 285 else if (strcmp(type, "s8") == 0) 286 fmt = "%d"; 287 else if (strcmp(type, "u8") == 0) 288 fmt = "%u"; 289 else if (strcmp(type, "char") == 0) 290 fmt = "%d"; 291 else if (strcmp(type, "unsigned char") == 0) 292 fmt = "%u"; 293 else if (strcmp(type, "int") == 0) 294 fmt = "%d"; 295 else if (strcmp(type, "unsigned int") == 0) 296 fmt = "%u"; 297 else if (strcmp(type, "long") == 0) 298 fmt = "%ld"; 299 else if (strcmp(type, "unsigned long") == 0) 300 fmt = "%lu"; 301 else if (strcmp(type, "bool") == 0) 302 fmt = "%d"; 303 else if (strcmp(type, "pid_t") == 0) 304 fmt = "%d"; 305 else if (strcmp(type, "gfp_t") == 0) 306 fmt = "%x"; 307 else if (synth_field_is_string(type)) 308 fmt = "%.*s"; 309 else if (synth_field_is_stack(type)) 310 fmt = "%s"; 311 312 return fmt; 313 } 314 315 static void print_synth_event_num_val(struct trace_seq *s, 316 char *print_fmt, char *name, 317 int size, union trace_synth_field *val, char *space) 318 { 319 switch (size) { 320 case 1: 321 trace_seq_printf(s, print_fmt, name, val->as_u8, space); 322 break; 323 324 case 2: 325 trace_seq_printf(s, print_fmt, name, val->as_u16, space); 326 break; 327 328 case 4: 329 trace_seq_printf(s, print_fmt, name, val->as_u32, space); 330 break; 331 332 default: 333 trace_seq_printf(s, print_fmt, name, val->as_u64, space); 334 break; 335 } 336 } 337 338 static enum print_line_t print_synth_event(struct trace_iterator *iter, 339 int flags, 340 struct trace_event *event) 341 { 342 struct trace_array *tr = iter->tr; 343 struct trace_seq *s = &iter->seq; 344 struct synth_trace_event *entry; 345 struct synth_event *se; 346 unsigned int i, j, n_u64; 347 char print_fmt[32]; 348 const char *fmt; 349 350 entry = (struct synth_trace_event *)iter->ent; 351 se = container_of(event, struct synth_event, call.event); 352 353 trace_seq_printf(s, "%s: ", se->name); 354 355 for (i = 0, n_u64 = 0; i < se->n_fields; i++) { 356 if (trace_seq_has_overflowed(s)) 357 goto end; 358 359 fmt = synth_field_fmt(se->fields[i]->type); 360 361 /* parameter types */ 362 if (tr && tr->trace_flags & TRACE_ITER_VERBOSE) 363 trace_seq_printf(s, "%s ", fmt); 364 365 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt); 366 367 /* parameter values */ 368 if (se->fields[i]->is_string) { 369 if (se->fields[i]->is_dynamic) { 370 union trace_synth_field *data = &entry->fields[n_u64]; 371 372 trace_seq_printf(s, print_fmt, se->fields[i]->name, 373 STR_VAR_LEN_MAX, 374 (char *)entry + data->as_dynamic.offset, 375 i == se->n_fields - 1 ? "" : " "); 376 n_u64++; 377 } else { 378 trace_seq_printf(s, print_fmt, se->fields[i]->name, 379 STR_VAR_LEN_MAX, 380 (char *)&entry->fields[n_u64].as_u64, 381 i == se->n_fields - 1 ? "" : " "); 382 n_u64 += STR_VAR_LEN_MAX / sizeof(u64); 383 } 384 } else if (se->fields[i]->is_stack) { 385 union trace_synth_field *data = &entry->fields[n_u64]; 386 unsigned long *p = (void *)entry + data->as_dynamic.offset; 387 388 trace_seq_printf(s, "%s=STACK:\n", se->fields[i]->name); 389 for (j = 1; j < data->as_dynamic.len / sizeof(long); j++) 390 trace_seq_printf(s, "=> %pS\n", (void *)p[j]); 391 n_u64++; 392 } else { 393 struct trace_print_flags __flags[] = { 394 __def_gfpflag_names, {-1, NULL} }; 395 char *space = (i == se->n_fields - 1 ? "" : " "); 396 397 print_synth_event_num_val(s, print_fmt, 398 se->fields[i]->name, 399 se->fields[i]->size, 400 &entry->fields[n_u64], 401 space); 402 403 if (strcmp(se->fields[i]->type, "gfp_t") == 0) { 404 trace_seq_puts(s, " ("); 405 trace_print_flags_seq(s, "|", 406 entry->fields[n_u64].as_u64, 407 __flags); 408 trace_seq_putc(s, ')'); 409 } 410 n_u64++; 411 } 412 } 413 end: 414 trace_seq_putc(s, '\n'); 415 416 return trace_handle_return(s); 417 } 418 419 static struct trace_event_functions synth_event_funcs = { 420 .trace = print_synth_event 421 }; 422 423 static unsigned int trace_string(struct synth_trace_event *entry, 424 struct synth_event *event, 425 char *str_val, 426 bool is_dynamic, 427 unsigned int data_size, 428 unsigned int *n_u64) 429 { 430 unsigned int len = 0; 431 char *str_field; 432 int ret; 433 434 if (is_dynamic) { 435 union trace_synth_field *data = &entry->fields[*n_u64]; 436 437 len = fetch_store_strlen((unsigned long)str_val); 438 data->as_dynamic.offset = struct_size(entry, fields, event->n_u64) + data_size; 439 data->as_dynamic.len = len; 440 441 ret = fetch_store_string((unsigned long)str_val, &entry->fields[*n_u64], entry); 442 443 (*n_u64)++; 444 } else { 445 str_field = (char *)&entry->fields[*n_u64].as_u64; 446 447 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE 448 if ((unsigned long)str_val < TASK_SIZE) 449 ret = strncpy_from_user_nofault(str_field, (const void __user *)str_val, STR_VAR_LEN_MAX); 450 else 451 #endif 452 ret = strncpy_from_kernel_nofault(str_field, str_val, STR_VAR_LEN_MAX); 453 454 if (ret < 0) 455 strcpy(str_field, FAULT_STRING); 456 457 (*n_u64) += STR_VAR_LEN_MAX / sizeof(u64); 458 } 459 460 return len; 461 } 462 463 static unsigned int trace_stack(struct synth_trace_event *entry, 464 struct synth_event *event, 465 long *stack, 466 unsigned int data_size, 467 unsigned int *n_u64) 468 { 469 union trace_synth_field *data = &entry->fields[*n_u64]; 470 unsigned int len; 471 u32 data_offset; 472 void *data_loc; 473 474 data_offset = struct_size(entry, fields, event->n_u64); 475 data_offset += data_size; 476 477 for (len = 0; len < HIST_STACKTRACE_DEPTH; len++) { 478 if (!stack[len]) 479 break; 480 } 481 482 len *= sizeof(long); 483 484 /* Find the dynamic section to copy the stack into. */ 485 data_loc = (void *)entry + data_offset; 486 memcpy(data_loc, stack, len); 487 488 /* Fill in the field that holds the offset/len combo */ 489 490 data->as_dynamic.offset = data_offset; 491 data->as_dynamic.len = len; 492 493 (*n_u64)++; 494 495 return len; 496 } 497 498 static notrace void trace_event_raw_event_synth(void *__data, 499 u64 *var_ref_vals, 500 unsigned int *var_ref_idx) 501 { 502 unsigned int i, n_u64, val_idx, len, data_size = 0; 503 struct trace_event_file *trace_file = __data; 504 struct synth_trace_event *entry; 505 struct trace_event_buffer fbuffer; 506 struct trace_buffer *buffer; 507 struct synth_event *event; 508 int fields_size = 0; 509 510 event = trace_file->event_call->data; 511 512 if (trace_trigger_soft_disabled(trace_file)) 513 return; 514 515 fields_size = event->n_u64 * sizeof(u64); 516 517 for (i = 0; i < event->n_dynamic_fields; i++) { 518 unsigned int field_pos = event->dynamic_fields[i]->field_pos; 519 char *str_val; 520 521 val_idx = var_ref_idx[field_pos]; 522 str_val = (char *)(long)var_ref_vals[val_idx]; 523 524 if (event->dynamic_fields[i]->is_stack) { 525 /* reserve one extra element for size */ 526 len = *((unsigned long *)str_val) + 1; 527 len *= sizeof(unsigned long); 528 } else { 529 len = fetch_store_strlen((unsigned long)str_val); 530 } 531 532 fields_size += len; 533 } 534 535 /* 536 * Avoid ring buffer recursion detection, as this event 537 * is being performed within another event. 538 */ 539 buffer = trace_file->tr->array_buffer.buffer; 540 ring_buffer_nest_start(buffer); 541 542 entry = trace_event_buffer_reserve(&fbuffer, trace_file, 543 sizeof(*entry) + fields_size); 544 if (!entry) 545 goto out; 546 547 for (i = 0, n_u64 = 0; i < event->n_fields; i++) { 548 val_idx = var_ref_idx[i]; 549 if (event->fields[i]->is_string) { 550 char *str_val = (char *)(long)var_ref_vals[val_idx]; 551 552 len = trace_string(entry, event, str_val, 553 event->fields[i]->is_dynamic, 554 data_size, &n_u64); 555 data_size += len; /* only dynamic string increments */ 556 } else if (event->fields[i]->is_stack) { 557 long *stack = (long *)(long)var_ref_vals[val_idx]; 558 559 len = trace_stack(entry, event, stack, 560 data_size, &n_u64); 561 data_size += len; 562 } else { 563 struct synth_field *field = event->fields[i]; 564 u64 val = var_ref_vals[val_idx]; 565 566 switch (field->size) { 567 case 1: 568 entry->fields[n_u64].as_u8 = (u8)val; 569 break; 570 571 case 2: 572 entry->fields[n_u64].as_u16 = (u16)val; 573 break; 574 575 case 4: 576 entry->fields[n_u64].as_u32 = (u32)val; 577 break; 578 579 default: 580 entry->fields[n_u64].as_u64 = val; 581 break; 582 } 583 n_u64++; 584 } 585 } 586 587 trace_event_buffer_commit(&fbuffer); 588 out: 589 ring_buffer_nest_end(buffer); 590 } 591 592 static void free_synth_event_print_fmt(struct trace_event_call *call) 593 { 594 if (call) { 595 kfree(call->print_fmt); 596 call->print_fmt = NULL; 597 } 598 } 599 600 static int __set_synth_event_print_fmt(struct synth_event *event, 601 char *buf, int len) 602 { 603 const char *fmt; 604 int pos = 0; 605 int i; 606 607 /* When len=0, we just calculate the needed length */ 608 #define LEN_OR_ZERO (len ? len - pos : 0) 609 610 pos += snprintf(buf + pos, LEN_OR_ZERO, "\""); 611 for (i = 0; i < event->n_fields; i++) { 612 fmt = synth_field_fmt(event->fields[i]->type); 613 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s", 614 event->fields[i]->name, fmt, 615 i == event->n_fields - 1 ? "" : ", "); 616 } 617 pos += snprintf(buf + pos, LEN_OR_ZERO, "\""); 618 619 for (i = 0; i < event->n_fields; i++) { 620 if (event->fields[i]->is_string && 621 event->fields[i]->is_dynamic) 622 pos += snprintf(buf + pos, LEN_OR_ZERO, 623 ", __get_str(%s)", event->fields[i]->name); 624 else if (event->fields[i]->is_stack) 625 pos += snprintf(buf + pos, LEN_OR_ZERO, 626 ", __get_stacktrace(%s)", event->fields[i]->name); 627 else 628 pos += snprintf(buf + pos, LEN_OR_ZERO, 629 ", REC->%s", event->fields[i]->name); 630 } 631 632 #undef LEN_OR_ZERO 633 634 /* return the length of print_fmt */ 635 return pos; 636 } 637 638 static int set_synth_event_print_fmt(struct trace_event_call *call) 639 { 640 struct synth_event *event = call->data; 641 char *print_fmt; 642 int len; 643 644 /* First: called with 0 length to calculate the needed length */ 645 len = __set_synth_event_print_fmt(event, NULL, 0); 646 647 print_fmt = kmalloc(len + 1, GFP_KERNEL); 648 if (!print_fmt) 649 return -ENOMEM; 650 651 /* Second: actually write the @print_fmt */ 652 __set_synth_event_print_fmt(event, print_fmt, len + 1); 653 call->print_fmt = print_fmt; 654 655 return 0; 656 } 657 658 static void free_synth_field(struct synth_field *field) 659 { 660 kfree(field->type); 661 kfree(field->name); 662 kfree(field); 663 } 664 665 static int check_field_version(const char *prefix, const char *field_type, 666 const char *field_name) 667 { 668 /* 669 * For backward compatibility, the old synthetic event command 670 * format did not require semicolons, and in order to not 671 * break user space, that old format must still work. If a new 672 * feature is added, then the format that uses the new feature 673 * will be required to have semicolons, as nothing that uses 674 * the old format would be using the new, yet to be created, 675 * feature. When a new feature is added, this will detect it, 676 * and return a number greater than 1, and require the format 677 * to use semicolons. 678 */ 679 return 1; 680 } 681 682 static struct synth_field *parse_synth_field(int argc, char **argv, 683 int *consumed, int *field_version) 684 { 685 const char *prefix = NULL, *field_type = argv[0], *field_name, *array; 686 struct synth_field *field; 687 int len, ret = -ENOMEM; 688 struct seq_buf s; 689 ssize_t size; 690 691 if (!strcmp(field_type, "unsigned")) { 692 if (argc < 3) { 693 synth_err(SYNTH_ERR_INCOMPLETE_TYPE, errpos(field_type)); 694 return ERR_PTR(-EINVAL); 695 } 696 prefix = "unsigned "; 697 field_type = argv[1]; 698 field_name = argv[2]; 699 *consumed += 3; 700 } else { 701 field_name = argv[1]; 702 *consumed += 2; 703 } 704 705 if (!field_name) { 706 synth_err(SYNTH_ERR_INVALID_FIELD, errpos(field_type)); 707 return ERR_PTR(-EINVAL); 708 } 709 710 *field_version = check_field_version(prefix, field_type, field_name); 711 712 field = kzalloc(sizeof(*field), GFP_KERNEL); 713 if (!field) 714 return ERR_PTR(-ENOMEM); 715 716 len = strlen(field_name); 717 array = strchr(field_name, '['); 718 if (array) 719 len -= strlen(array); 720 721 field->name = kmemdup_nul(field_name, len, GFP_KERNEL); 722 if (!field->name) 723 goto free; 724 725 if (!is_good_name(field->name)) { 726 synth_err(SYNTH_ERR_BAD_NAME, errpos(field_name)); 727 ret = -EINVAL; 728 goto free; 729 } 730 731 len = strlen(field_type) + 1; 732 733 if (array) 734 len += strlen(array); 735 736 if (prefix) 737 len += strlen(prefix); 738 739 field->type = kzalloc(len, GFP_KERNEL); 740 if (!field->type) 741 goto free; 742 743 seq_buf_init(&s, field->type, len); 744 if (prefix) 745 seq_buf_puts(&s, prefix); 746 seq_buf_puts(&s, field_type); 747 if (array) 748 seq_buf_puts(&s, array); 749 if (WARN_ON_ONCE(!seq_buf_buffer_left(&s))) 750 goto free; 751 752 s.buffer[s.len] = '\0'; 753 754 size = synth_field_size(field->type); 755 if (size < 0) { 756 if (array) 757 synth_err(SYNTH_ERR_INVALID_ARRAY_SPEC, errpos(field_name)); 758 else 759 synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type)); 760 ret = -EINVAL; 761 goto free; 762 } else if (size == 0) { 763 if (synth_field_is_string(field->type) || 764 synth_field_is_stack(field->type)) { 765 char *type; 766 767 len = sizeof("__data_loc ") + strlen(field->type) + 1; 768 type = kzalloc(len, GFP_KERNEL); 769 if (!type) 770 goto free; 771 772 seq_buf_init(&s, type, len); 773 seq_buf_puts(&s, "__data_loc "); 774 seq_buf_puts(&s, field->type); 775 776 if (WARN_ON_ONCE(!seq_buf_buffer_left(&s))) 777 goto free; 778 s.buffer[s.len] = '\0'; 779 780 kfree(field->type); 781 field->type = type; 782 783 field->is_dynamic = true; 784 size = sizeof(u64); 785 } else { 786 synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type)); 787 ret = -EINVAL; 788 goto free; 789 } 790 } 791 field->size = size; 792 793 if (synth_field_is_string(field->type)) 794 field->is_string = true; 795 else if (synth_field_is_stack(field->type)) 796 field->is_stack = true; 797 798 field->is_signed = synth_field_signed(field->type); 799 out: 800 return field; 801 free: 802 free_synth_field(field); 803 field = ERR_PTR(ret); 804 goto out; 805 } 806 807 static void free_synth_tracepoint(struct tracepoint *tp) 808 { 809 if (!tp) 810 return; 811 812 kfree(tp->name); 813 kfree(tp); 814 } 815 816 static struct tracepoint *alloc_synth_tracepoint(char *name) 817 { 818 struct tracepoint *tp; 819 820 tp = kzalloc(sizeof(*tp), GFP_KERNEL); 821 if (!tp) 822 return ERR_PTR(-ENOMEM); 823 824 tp->name = kstrdup(name, GFP_KERNEL); 825 if (!tp->name) { 826 kfree(tp); 827 return ERR_PTR(-ENOMEM); 828 } 829 830 return tp; 831 } 832 833 struct synth_event *find_synth_event(const char *name) 834 { 835 struct dyn_event *pos; 836 struct synth_event *event; 837 838 for_each_dyn_event(pos) { 839 if (!is_synth_event(pos)) 840 continue; 841 event = to_synth_event(pos); 842 if (strcmp(event->name, name) == 0) 843 return event; 844 } 845 846 return NULL; 847 } 848 849 static struct trace_event_fields synth_event_fields_array[] = { 850 { .type = TRACE_FUNCTION_TYPE, 851 .define_fields = synth_event_define_fields }, 852 {} 853 }; 854 855 static int register_synth_event(struct synth_event *event) 856 { 857 struct trace_event_call *call = &event->call; 858 int ret = 0; 859 860 event->call.class = &event->class; 861 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL); 862 if (!event->class.system) { 863 ret = -ENOMEM; 864 goto out; 865 } 866 867 event->tp = alloc_synth_tracepoint(event->name); 868 if (IS_ERR(event->tp)) { 869 ret = PTR_ERR(event->tp); 870 event->tp = NULL; 871 goto out; 872 } 873 874 INIT_LIST_HEAD(&call->class->fields); 875 call->event.funcs = &synth_event_funcs; 876 call->class->fields_array = synth_event_fields_array; 877 878 ret = register_trace_event(&call->event); 879 if (!ret) { 880 ret = -ENODEV; 881 goto out; 882 } 883 call->flags = TRACE_EVENT_FL_TRACEPOINT; 884 call->class->reg = trace_event_reg; 885 call->class->probe = trace_event_raw_event_synth; 886 call->data = event; 887 call->tp = event->tp; 888 889 ret = trace_add_event_call(call); 890 if (ret) { 891 pr_warn("Failed to register synthetic event: %s\n", 892 trace_event_name(call)); 893 goto err; 894 } 895 896 ret = set_synth_event_print_fmt(call); 897 /* unregister_trace_event() will be called inside */ 898 if (ret < 0) 899 trace_remove_event_call(call); 900 out: 901 return ret; 902 err: 903 unregister_trace_event(&call->event); 904 goto out; 905 } 906 907 static int unregister_synth_event(struct synth_event *event) 908 { 909 struct trace_event_call *call = &event->call; 910 int ret; 911 912 ret = trace_remove_event_call(call); 913 914 return ret; 915 } 916 917 static void free_synth_event(struct synth_event *event) 918 { 919 unsigned int i; 920 921 if (!event) 922 return; 923 924 for (i = 0; i < event->n_fields; i++) 925 free_synth_field(event->fields[i]); 926 927 kfree(event->fields); 928 kfree(event->dynamic_fields); 929 kfree(event->name); 930 kfree(event->class.system); 931 free_synth_tracepoint(event->tp); 932 free_synth_event_print_fmt(&event->call); 933 kfree(event); 934 } 935 936 static struct synth_event *alloc_synth_event(const char *name, int n_fields, 937 struct synth_field **fields) 938 { 939 unsigned int i, j, n_dynamic_fields = 0; 940 struct synth_event *event; 941 942 event = kzalloc(sizeof(*event), GFP_KERNEL); 943 if (!event) { 944 event = ERR_PTR(-ENOMEM); 945 goto out; 946 } 947 948 event->name = kstrdup(name, GFP_KERNEL); 949 if (!event->name) { 950 kfree(event); 951 event = ERR_PTR(-ENOMEM); 952 goto out; 953 } 954 955 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL); 956 if (!event->fields) { 957 free_synth_event(event); 958 event = ERR_PTR(-ENOMEM); 959 goto out; 960 } 961 962 for (i = 0; i < n_fields; i++) 963 if (fields[i]->is_dynamic) 964 n_dynamic_fields++; 965 966 if (n_dynamic_fields) { 967 event->dynamic_fields = kcalloc(n_dynamic_fields, 968 sizeof(*event->dynamic_fields), 969 GFP_KERNEL); 970 if (!event->dynamic_fields) { 971 free_synth_event(event); 972 event = ERR_PTR(-ENOMEM); 973 goto out; 974 } 975 } 976 977 dyn_event_init(&event->devent, &synth_event_ops); 978 979 for (i = 0, j = 0; i < n_fields; i++) { 980 fields[i]->field_pos = i; 981 event->fields[i] = fields[i]; 982 983 if (fields[i]->is_dynamic) 984 event->dynamic_fields[j++] = fields[i]; 985 } 986 event->n_dynamic_fields = j; 987 event->n_fields = n_fields; 988 out: 989 return event; 990 } 991 992 static int synth_event_check_arg_fn(void *data) 993 { 994 struct dynevent_arg_pair *arg_pair = data; 995 int size; 996 997 size = synth_field_size((char *)arg_pair->lhs); 998 if (size == 0) { 999 if (strstr((char *)arg_pair->lhs, "[")) 1000 return 0; 1001 } 1002 1003 return size ? 0 : -EINVAL; 1004 } 1005 1006 /** 1007 * synth_event_add_field - Add a new field to a synthetic event cmd 1008 * @cmd: A pointer to the dynevent_cmd struct representing the new event 1009 * @type: The type of the new field to add 1010 * @name: The name of the new field to add 1011 * 1012 * Add a new field to a synthetic event cmd object. Field ordering is in 1013 * the same order the fields are added. 1014 * 1015 * See synth_field_size() for available types. If field_name contains 1016 * [n] the field is considered to be an array. 1017 * 1018 * Return: 0 if successful, error otherwise. 1019 */ 1020 int synth_event_add_field(struct dynevent_cmd *cmd, const char *type, 1021 const char *name) 1022 { 1023 struct dynevent_arg_pair arg_pair; 1024 int ret; 1025 1026 if (cmd->type != DYNEVENT_TYPE_SYNTH) 1027 return -EINVAL; 1028 1029 if (!type || !name) 1030 return -EINVAL; 1031 1032 dynevent_arg_pair_init(&arg_pair, 0, ';'); 1033 1034 arg_pair.lhs = type; 1035 arg_pair.rhs = name; 1036 1037 ret = dynevent_arg_pair_add(cmd, &arg_pair, synth_event_check_arg_fn); 1038 if (ret) 1039 return ret; 1040 1041 if (++cmd->n_fields > SYNTH_FIELDS_MAX) 1042 ret = -EINVAL; 1043 1044 return ret; 1045 } 1046 EXPORT_SYMBOL_GPL(synth_event_add_field); 1047 1048 /** 1049 * synth_event_add_field_str - Add a new field to a synthetic event cmd 1050 * @cmd: A pointer to the dynevent_cmd struct representing the new event 1051 * @type_name: The type and name of the new field to add, as a single string 1052 * 1053 * Add a new field to a synthetic event cmd object, as a single 1054 * string. The @type_name string is expected to be of the form 'type 1055 * name', which will be appended by ';'. No sanity checking is done - 1056 * what's passed in is assumed to already be well-formed. Field 1057 * ordering is in the same order the fields are added. 1058 * 1059 * See synth_field_size() for available types. If field_name contains 1060 * [n] the field is considered to be an array. 1061 * 1062 * Return: 0 if successful, error otherwise. 1063 */ 1064 int synth_event_add_field_str(struct dynevent_cmd *cmd, const char *type_name) 1065 { 1066 struct dynevent_arg arg; 1067 int ret; 1068 1069 if (cmd->type != DYNEVENT_TYPE_SYNTH) 1070 return -EINVAL; 1071 1072 if (!type_name) 1073 return -EINVAL; 1074 1075 dynevent_arg_init(&arg, ';'); 1076 1077 arg.str = type_name; 1078 1079 ret = dynevent_arg_add(cmd, &arg, NULL); 1080 if (ret) 1081 return ret; 1082 1083 if (++cmd->n_fields > SYNTH_FIELDS_MAX) 1084 ret = -EINVAL; 1085 1086 return ret; 1087 } 1088 EXPORT_SYMBOL_GPL(synth_event_add_field_str); 1089 1090 /** 1091 * synth_event_add_fields - Add multiple fields to a synthetic event cmd 1092 * @cmd: A pointer to the dynevent_cmd struct representing the new event 1093 * @fields: An array of type/name field descriptions 1094 * @n_fields: The number of field descriptions contained in the fields array 1095 * 1096 * Add a new set of fields to a synthetic event cmd object. The event 1097 * fields that will be defined for the event should be passed in as an 1098 * array of struct synth_field_desc, and the number of elements in the 1099 * array passed in as n_fields. Field ordering will retain the 1100 * ordering given in the fields array. 1101 * 1102 * See synth_field_size() for available types. If field_name contains 1103 * [n] the field is considered to be an array. 1104 * 1105 * Return: 0 if successful, error otherwise. 1106 */ 1107 int synth_event_add_fields(struct dynevent_cmd *cmd, 1108 struct synth_field_desc *fields, 1109 unsigned int n_fields) 1110 { 1111 unsigned int i; 1112 int ret = 0; 1113 1114 for (i = 0; i < n_fields; i++) { 1115 if (fields[i].type == NULL || fields[i].name == NULL) { 1116 ret = -EINVAL; 1117 break; 1118 } 1119 1120 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name); 1121 if (ret) 1122 break; 1123 } 1124 1125 return ret; 1126 } 1127 EXPORT_SYMBOL_GPL(synth_event_add_fields); 1128 1129 /** 1130 * __synth_event_gen_cmd_start - Start a synthetic event command from arg list 1131 * @cmd: A pointer to the dynevent_cmd struct representing the new event 1132 * @name: The name of the synthetic event 1133 * @mod: The module creating the event, NULL if not created from a module 1134 * @...: Variable number of arg (pairs), one pair for each field 1135 * 1136 * NOTE: Users normally won't want to call this function directly, but 1137 * rather use the synth_event_gen_cmd_start() wrapper, which 1138 * automatically adds a NULL to the end of the arg list. If this 1139 * function is used directly, make sure the last arg in the variable 1140 * arg list is NULL. 1141 * 1142 * Generate a synthetic event command to be executed by 1143 * synth_event_gen_cmd_end(). This function can be used to generate 1144 * the complete command or only the first part of it; in the latter 1145 * case, synth_event_add_field(), synth_event_add_field_str(), or 1146 * synth_event_add_fields() can be used to add more fields following 1147 * this. 1148 * 1149 * There should be an even number variable args, each pair consisting 1150 * of a type followed by a field name. 1151 * 1152 * See synth_field_size() for available types. If field_name contains 1153 * [n] the field is considered to be an array. 1154 * 1155 * Return: 0 if successful, error otherwise. 1156 */ 1157 int __synth_event_gen_cmd_start(struct dynevent_cmd *cmd, const char *name, 1158 struct module *mod, ...) 1159 { 1160 struct dynevent_arg arg; 1161 va_list args; 1162 int ret; 1163 1164 cmd->event_name = name; 1165 cmd->private_data = mod; 1166 1167 if (cmd->type != DYNEVENT_TYPE_SYNTH) 1168 return -EINVAL; 1169 1170 dynevent_arg_init(&arg, 0); 1171 arg.str = name; 1172 ret = dynevent_arg_add(cmd, &arg, NULL); 1173 if (ret) 1174 return ret; 1175 1176 va_start(args, mod); 1177 for (;;) { 1178 const char *type, *name; 1179 1180 type = va_arg(args, const char *); 1181 if (!type) 1182 break; 1183 name = va_arg(args, const char *); 1184 if (!name) 1185 break; 1186 1187 if (++cmd->n_fields > SYNTH_FIELDS_MAX) { 1188 ret = -EINVAL; 1189 break; 1190 } 1191 1192 ret = synth_event_add_field(cmd, type, name); 1193 if (ret) 1194 break; 1195 } 1196 va_end(args); 1197 1198 return ret; 1199 } 1200 EXPORT_SYMBOL_GPL(__synth_event_gen_cmd_start); 1201 1202 /** 1203 * synth_event_gen_cmd_array_start - Start synthetic event command from an array 1204 * @cmd: A pointer to the dynevent_cmd struct representing the new event 1205 * @name: The name of the synthetic event 1206 * @mod: The module creating the event, NULL if not created from a module 1207 * @fields: An array of type/name field descriptions 1208 * @n_fields: The number of field descriptions contained in the fields array 1209 * 1210 * Generate a synthetic event command to be executed by 1211 * synth_event_gen_cmd_end(). This function can be used to generate 1212 * the complete command or only the first part of it; in the latter 1213 * case, synth_event_add_field(), synth_event_add_field_str(), or 1214 * synth_event_add_fields() can be used to add more fields following 1215 * this. 1216 * 1217 * The event fields that will be defined for the event should be 1218 * passed in as an array of struct synth_field_desc, and the number of 1219 * elements in the array passed in as n_fields. Field ordering will 1220 * retain the ordering given in the fields array. 1221 * 1222 * See synth_field_size() for available types. If field_name contains 1223 * [n] the field is considered to be an array. 1224 * 1225 * Return: 0 if successful, error otherwise. 1226 */ 1227 int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd, const char *name, 1228 struct module *mod, 1229 struct synth_field_desc *fields, 1230 unsigned int n_fields) 1231 { 1232 struct dynevent_arg arg; 1233 unsigned int i; 1234 int ret = 0; 1235 1236 cmd->event_name = name; 1237 cmd->private_data = mod; 1238 1239 if (cmd->type != DYNEVENT_TYPE_SYNTH) 1240 return -EINVAL; 1241 1242 if (n_fields > SYNTH_FIELDS_MAX) 1243 return -EINVAL; 1244 1245 dynevent_arg_init(&arg, 0); 1246 arg.str = name; 1247 ret = dynevent_arg_add(cmd, &arg, NULL); 1248 if (ret) 1249 return ret; 1250 1251 for (i = 0; i < n_fields; i++) { 1252 if (fields[i].type == NULL || fields[i].name == NULL) 1253 return -EINVAL; 1254 1255 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name); 1256 if (ret) 1257 break; 1258 } 1259 1260 return ret; 1261 } 1262 EXPORT_SYMBOL_GPL(synth_event_gen_cmd_array_start); 1263 1264 static int __create_synth_event(const char *name, const char *raw_fields) 1265 { 1266 char **argv, *field_str, *tmp_fields, *saved_fields = NULL; 1267 struct synth_field *field, *fields[SYNTH_FIELDS_MAX]; 1268 int consumed, cmd_version = 1, n_fields_this_loop; 1269 int i, argc, n_fields = 0, ret = 0; 1270 struct synth_event *event = NULL; 1271 1272 /* 1273 * Argument syntax: 1274 * - Add synthetic event: <event_name> field[;field] ... 1275 * - Remove synthetic event: !<event_name> field[;field] ... 1276 * where 'field' = type field_name 1277 */ 1278 1279 if (name[0] == '\0') { 1280 synth_err(SYNTH_ERR_INVALID_CMD, 0); 1281 return -EINVAL; 1282 } 1283 1284 if (!is_good_name(name)) { 1285 synth_err(SYNTH_ERR_BAD_NAME, errpos(name)); 1286 return -EINVAL; 1287 } 1288 1289 mutex_lock(&event_mutex); 1290 1291 event = find_synth_event(name); 1292 if (event) { 1293 synth_err(SYNTH_ERR_EVENT_EXISTS, errpos(name)); 1294 ret = -EEXIST; 1295 goto err; 1296 } 1297 1298 tmp_fields = saved_fields = kstrdup(raw_fields, GFP_KERNEL); 1299 if (!tmp_fields) { 1300 ret = -ENOMEM; 1301 goto err; 1302 } 1303 1304 while ((field_str = strsep(&tmp_fields, ";")) != NULL) { 1305 argv = argv_split(GFP_KERNEL, field_str, &argc); 1306 if (!argv) { 1307 ret = -ENOMEM; 1308 goto err; 1309 } 1310 1311 if (!argc) { 1312 argv_free(argv); 1313 continue; 1314 } 1315 1316 n_fields_this_loop = 0; 1317 consumed = 0; 1318 while (argc > consumed) { 1319 int field_version; 1320 1321 field = parse_synth_field(argc - consumed, 1322 argv + consumed, &consumed, 1323 &field_version); 1324 if (IS_ERR(field)) { 1325 ret = PTR_ERR(field); 1326 goto err_free_arg; 1327 } 1328 1329 /* 1330 * Track the highest version of any field we 1331 * found in the command. 1332 */ 1333 if (field_version > cmd_version) 1334 cmd_version = field_version; 1335 1336 /* 1337 * Now sort out what is and isn't valid for 1338 * each supported version. 1339 * 1340 * If we see more than 1 field per loop, it 1341 * means we have multiple fields between 1342 * semicolons, and that's something we no 1343 * longer support in a version 2 or greater 1344 * command. 1345 */ 1346 if (cmd_version > 1 && n_fields_this_loop >= 1) { 1347 synth_err(SYNTH_ERR_INVALID_CMD, errpos(field_str)); 1348 ret = -EINVAL; 1349 goto err_free_arg; 1350 } 1351 1352 if (n_fields == SYNTH_FIELDS_MAX) { 1353 synth_err(SYNTH_ERR_TOO_MANY_FIELDS, 0); 1354 ret = -EINVAL; 1355 goto err_free_arg; 1356 } 1357 fields[n_fields++] = field; 1358 1359 n_fields_this_loop++; 1360 } 1361 argv_free(argv); 1362 1363 if (consumed < argc) { 1364 synth_err(SYNTH_ERR_INVALID_CMD, 0); 1365 ret = -EINVAL; 1366 goto err; 1367 } 1368 1369 } 1370 1371 if (n_fields == 0) { 1372 synth_err(SYNTH_ERR_INVALID_CMD, 0); 1373 ret = -EINVAL; 1374 goto err; 1375 } 1376 1377 event = alloc_synth_event(name, n_fields, fields); 1378 if (IS_ERR(event)) { 1379 ret = PTR_ERR(event); 1380 event = NULL; 1381 goto err; 1382 } 1383 ret = register_synth_event(event); 1384 if (!ret) 1385 dyn_event_add(&event->devent, &event->call); 1386 else 1387 free_synth_event(event); 1388 out: 1389 mutex_unlock(&event_mutex); 1390 1391 kfree(saved_fields); 1392 1393 return ret; 1394 err_free_arg: 1395 argv_free(argv); 1396 err: 1397 for (i = 0; i < n_fields; i++) 1398 free_synth_field(fields[i]); 1399 1400 goto out; 1401 } 1402 1403 /** 1404 * synth_event_create - Create a new synthetic event 1405 * @name: The name of the new synthetic event 1406 * @fields: An array of type/name field descriptions 1407 * @n_fields: The number of field descriptions contained in the fields array 1408 * @mod: The module creating the event, NULL if not created from a module 1409 * 1410 * Create a new synthetic event with the given name under the 1411 * trace/events/synthetic/ directory. The event fields that will be 1412 * defined for the event should be passed in as an array of struct 1413 * synth_field_desc, and the number elements in the array passed in as 1414 * n_fields. Field ordering will retain the ordering given in the 1415 * fields array. 1416 * 1417 * If the new synthetic event is being created from a module, the mod 1418 * param must be non-NULL. This will ensure that the trace buffer 1419 * won't contain unreadable events. 1420 * 1421 * The new synth event should be deleted using synth_event_delete() 1422 * function. The new synthetic event can be generated from modules or 1423 * other kernel code using trace_synth_event() and related functions. 1424 * 1425 * Return: 0 if successful, error otherwise. 1426 */ 1427 int synth_event_create(const char *name, struct synth_field_desc *fields, 1428 unsigned int n_fields, struct module *mod) 1429 { 1430 struct dynevent_cmd cmd; 1431 char *buf; 1432 int ret; 1433 1434 buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL); 1435 if (!buf) 1436 return -ENOMEM; 1437 1438 synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN); 1439 1440 ret = synth_event_gen_cmd_array_start(&cmd, name, mod, 1441 fields, n_fields); 1442 if (ret) 1443 goto out; 1444 1445 ret = synth_event_gen_cmd_end(&cmd); 1446 out: 1447 kfree(buf); 1448 1449 return ret; 1450 } 1451 EXPORT_SYMBOL_GPL(synth_event_create); 1452 1453 static int destroy_synth_event(struct synth_event *se) 1454 { 1455 int ret; 1456 1457 if (se->ref) 1458 return -EBUSY; 1459 1460 if (trace_event_dyn_busy(&se->call)) 1461 return -EBUSY; 1462 1463 ret = unregister_synth_event(se); 1464 if (!ret) { 1465 dyn_event_remove(&se->devent); 1466 free_synth_event(se); 1467 } 1468 1469 return ret; 1470 } 1471 1472 /** 1473 * synth_event_delete - Delete a synthetic event 1474 * @event_name: The name of the new synthetic event 1475 * 1476 * Delete a synthetic event that was created with synth_event_create(). 1477 * 1478 * Return: 0 if successful, error otherwise. 1479 */ 1480 int synth_event_delete(const char *event_name) 1481 { 1482 struct synth_event *se = NULL; 1483 struct module *mod = NULL; 1484 int ret = -ENOENT; 1485 1486 mutex_lock(&event_mutex); 1487 se = find_synth_event(event_name); 1488 if (se) { 1489 mod = se->mod; 1490 ret = destroy_synth_event(se); 1491 } 1492 mutex_unlock(&event_mutex); 1493 1494 if (mod) { 1495 /* 1496 * It is safest to reset the ring buffer if the module 1497 * being unloaded registered any events that were 1498 * used. The only worry is if a new module gets 1499 * loaded, and takes on the same id as the events of 1500 * this module. When printing out the buffer, traced 1501 * events left over from this module may be passed to 1502 * the new module events and unexpected results may 1503 * occur. 1504 */ 1505 tracing_reset_all_online_cpus(); 1506 } 1507 1508 return ret; 1509 } 1510 EXPORT_SYMBOL_GPL(synth_event_delete); 1511 1512 static int check_command(const char *raw_command) 1513 { 1514 char **argv = NULL, *cmd, *saved_cmd, *name_and_field; 1515 int argc, ret = 0; 1516 1517 cmd = saved_cmd = kstrdup(raw_command, GFP_KERNEL); 1518 if (!cmd) 1519 return -ENOMEM; 1520 1521 name_and_field = strsep(&cmd, ";"); 1522 if (!name_and_field) { 1523 ret = -EINVAL; 1524 goto free; 1525 } 1526 1527 if (name_and_field[0] == '!') 1528 goto free; 1529 1530 argv = argv_split(GFP_KERNEL, name_and_field, &argc); 1531 if (!argv) { 1532 ret = -ENOMEM; 1533 goto free; 1534 } 1535 argv_free(argv); 1536 1537 if (argc < 3) 1538 ret = -EINVAL; 1539 free: 1540 kfree(saved_cmd); 1541 1542 return ret; 1543 } 1544 1545 static int create_or_delete_synth_event(const char *raw_command) 1546 { 1547 char *name = NULL, *fields, *p; 1548 int ret = 0; 1549 1550 raw_command = skip_spaces(raw_command); 1551 if (raw_command[0] == '\0') 1552 return ret; 1553 1554 last_cmd_set(raw_command); 1555 1556 ret = check_command(raw_command); 1557 if (ret) { 1558 synth_err(SYNTH_ERR_INVALID_CMD, 0); 1559 return ret; 1560 } 1561 1562 p = strpbrk(raw_command, " \t"); 1563 if (!p && raw_command[0] != '!') { 1564 synth_err(SYNTH_ERR_INVALID_CMD, 0); 1565 ret = -EINVAL; 1566 goto free; 1567 } 1568 1569 name = kmemdup_nul(raw_command, p ? p - raw_command : strlen(raw_command), GFP_KERNEL); 1570 if (!name) 1571 return -ENOMEM; 1572 1573 if (name[0] == '!') { 1574 ret = synth_event_delete(name + 1); 1575 goto free; 1576 } 1577 1578 fields = skip_spaces(p); 1579 1580 ret = __create_synth_event(name, fields); 1581 free: 1582 kfree(name); 1583 1584 return ret; 1585 } 1586 1587 static int synth_event_run_command(struct dynevent_cmd *cmd) 1588 { 1589 struct synth_event *se; 1590 int ret; 1591 1592 ret = create_or_delete_synth_event(cmd->seq.buffer); 1593 if (ret) 1594 return ret; 1595 1596 se = find_synth_event(cmd->event_name); 1597 if (WARN_ON(!se)) 1598 return -ENOENT; 1599 1600 se->mod = cmd->private_data; 1601 1602 return ret; 1603 } 1604 1605 /** 1606 * synth_event_cmd_init - Initialize a synthetic event command object 1607 * @cmd: A pointer to the dynevent_cmd struct representing the new event 1608 * @buf: A pointer to the buffer used to build the command 1609 * @maxlen: The length of the buffer passed in @buf 1610 * 1611 * Initialize a synthetic event command object. Use this before 1612 * calling any of the other dyenvent_cmd functions. 1613 */ 1614 void synth_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen) 1615 { 1616 dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_SYNTH, 1617 synth_event_run_command); 1618 } 1619 EXPORT_SYMBOL_GPL(synth_event_cmd_init); 1620 1621 static inline int 1622 __synth_event_trace_init(struct trace_event_file *file, 1623 struct synth_event_trace_state *trace_state) 1624 { 1625 int ret = 0; 1626 1627 memset(trace_state, '\0', sizeof(*trace_state)); 1628 1629 /* 1630 * Normal event tracing doesn't get called at all unless the 1631 * ENABLED bit is set (which attaches the probe thus allowing 1632 * this code to be called, etc). Because this is called 1633 * directly by the user, we don't have that but we still need 1634 * to honor not logging when disabled. For the iterated 1635 * trace case, we save the enabled state upon start and just 1636 * ignore the following data calls. 1637 */ 1638 if (!(file->flags & EVENT_FILE_FL_ENABLED) || 1639 trace_trigger_soft_disabled(file)) { 1640 trace_state->disabled = true; 1641 ret = -ENOENT; 1642 goto out; 1643 } 1644 1645 trace_state->event = file->event_call->data; 1646 out: 1647 return ret; 1648 } 1649 1650 static inline int 1651 __synth_event_trace_start(struct trace_event_file *file, 1652 struct synth_event_trace_state *trace_state, 1653 int dynamic_fields_size) 1654 { 1655 int entry_size, fields_size = 0; 1656 int ret = 0; 1657 1658 fields_size = trace_state->event->n_u64 * sizeof(u64); 1659 fields_size += dynamic_fields_size; 1660 1661 /* 1662 * Avoid ring buffer recursion detection, as this event 1663 * is being performed within another event. 1664 */ 1665 trace_state->buffer = file->tr->array_buffer.buffer; 1666 ring_buffer_nest_start(trace_state->buffer); 1667 1668 entry_size = sizeof(*trace_state->entry) + fields_size; 1669 trace_state->entry = trace_event_buffer_reserve(&trace_state->fbuffer, 1670 file, 1671 entry_size); 1672 if (!trace_state->entry) { 1673 ring_buffer_nest_end(trace_state->buffer); 1674 ret = -EINVAL; 1675 } 1676 1677 return ret; 1678 } 1679 1680 static inline void 1681 __synth_event_trace_end(struct synth_event_trace_state *trace_state) 1682 { 1683 trace_event_buffer_commit(&trace_state->fbuffer); 1684 1685 ring_buffer_nest_end(trace_state->buffer); 1686 } 1687 1688 /** 1689 * synth_event_trace - Trace a synthetic event 1690 * @file: The trace_event_file representing the synthetic event 1691 * @n_vals: The number of values in vals 1692 * @...: Variable number of args containing the event values 1693 * 1694 * Trace a synthetic event using the values passed in the variable 1695 * argument list. 1696 * 1697 * The argument list should be a list 'n_vals' u64 values. The number 1698 * of vals must match the number of field in the synthetic event, and 1699 * must be in the same order as the synthetic event fields. 1700 * 1701 * All vals should be cast to u64, and string vals are just pointers 1702 * to strings, cast to u64. Strings will be copied into space 1703 * reserved in the event for the string, using these pointers. 1704 * 1705 * Return: 0 on success, err otherwise. 1706 */ 1707 int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...) 1708 { 1709 unsigned int i, n_u64, len, data_size = 0; 1710 struct synth_event_trace_state state; 1711 va_list args; 1712 int ret; 1713 1714 ret = __synth_event_trace_init(file, &state); 1715 if (ret) { 1716 if (ret == -ENOENT) 1717 ret = 0; /* just disabled, not really an error */ 1718 return ret; 1719 } 1720 1721 if (state.event->n_dynamic_fields) { 1722 va_start(args, n_vals); 1723 1724 for (i = 0; i < state.event->n_fields; i++) { 1725 u64 val = va_arg(args, u64); 1726 1727 if (state.event->fields[i]->is_string && 1728 state.event->fields[i]->is_dynamic) { 1729 char *str_val = (char *)(long)val; 1730 1731 data_size += strlen(str_val) + 1; 1732 } 1733 } 1734 1735 va_end(args); 1736 } 1737 1738 ret = __synth_event_trace_start(file, &state, data_size); 1739 if (ret) 1740 return ret; 1741 1742 if (n_vals != state.event->n_fields) { 1743 ret = -EINVAL; 1744 goto out; 1745 } 1746 1747 data_size = 0; 1748 1749 va_start(args, n_vals); 1750 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) { 1751 u64 val; 1752 1753 val = va_arg(args, u64); 1754 1755 if (state.event->fields[i]->is_string) { 1756 char *str_val = (char *)(long)val; 1757 1758 len = trace_string(state.entry, state.event, str_val, 1759 state.event->fields[i]->is_dynamic, 1760 data_size, &n_u64); 1761 data_size += len; /* only dynamic string increments */ 1762 } else { 1763 struct synth_field *field = state.event->fields[i]; 1764 1765 switch (field->size) { 1766 case 1: 1767 state.entry->fields[n_u64].as_u8 = (u8)val; 1768 break; 1769 1770 case 2: 1771 state.entry->fields[n_u64].as_u16 = (u16)val; 1772 break; 1773 1774 case 4: 1775 state.entry->fields[n_u64].as_u32 = (u32)val; 1776 break; 1777 1778 default: 1779 state.entry->fields[n_u64].as_u64 = val; 1780 break; 1781 } 1782 n_u64++; 1783 } 1784 } 1785 va_end(args); 1786 out: 1787 __synth_event_trace_end(&state); 1788 1789 return ret; 1790 } 1791 EXPORT_SYMBOL_GPL(synth_event_trace); 1792 1793 /** 1794 * synth_event_trace_array - Trace a synthetic event from an array 1795 * @file: The trace_event_file representing the synthetic event 1796 * @vals: Array of values 1797 * @n_vals: The number of values in vals 1798 * 1799 * Trace a synthetic event using the values passed in as 'vals'. 1800 * 1801 * The 'vals' array is just an array of 'n_vals' u64. The number of 1802 * vals must match the number of field in the synthetic event, and 1803 * must be in the same order as the synthetic event fields. 1804 * 1805 * All vals should be cast to u64, and string vals are just pointers 1806 * to strings, cast to u64. Strings will be copied into space 1807 * reserved in the event for the string, using these pointers. 1808 * 1809 * Return: 0 on success, err otherwise. 1810 */ 1811 int synth_event_trace_array(struct trace_event_file *file, u64 *vals, 1812 unsigned int n_vals) 1813 { 1814 unsigned int i, n_u64, field_pos, len, data_size = 0; 1815 struct synth_event_trace_state state; 1816 char *str_val; 1817 int ret; 1818 1819 ret = __synth_event_trace_init(file, &state); 1820 if (ret) { 1821 if (ret == -ENOENT) 1822 ret = 0; /* just disabled, not really an error */ 1823 return ret; 1824 } 1825 1826 if (state.event->n_dynamic_fields) { 1827 for (i = 0; i < state.event->n_dynamic_fields; i++) { 1828 field_pos = state.event->dynamic_fields[i]->field_pos; 1829 str_val = (char *)(long)vals[field_pos]; 1830 len = strlen(str_val) + 1; 1831 data_size += len; 1832 } 1833 } 1834 1835 ret = __synth_event_trace_start(file, &state, data_size); 1836 if (ret) 1837 return ret; 1838 1839 if (n_vals != state.event->n_fields) { 1840 ret = -EINVAL; 1841 goto out; 1842 } 1843 1844 data_size = 0; 1845 1846 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) { 1847 if (state.event->fields[i]->is_string) { 1848 char *str_val = (char *)(long)vals[i]; 1849 1850 len = trace_string(state.entry, state.event, str_val, 1851 state.event->fields[i]->is_dynamic, 1852 data_size, &n_u64); 1853 data_size += len; /* only dynamic string increments */ 1854 } else { 1855 struct synth_field *field = state.event->fields[i]; 1856 u64 val = vals[i]; 1857 1858 switch (field->size) { 1859 case 1: 1860 state.entry->fields[n_u64].as_u8 = (u8)val; 1861 break; 1862 1863 case 2: 1864 state.entry->fields[n_u64].as_u16 = (u16)val; 1865 break; 1866 1867 case 4: 1868 state.entry->fields[n_u64].as_u32 = (u32)val; 1869 break; 1870 1871 default: 1872 state.entry->fields[n_u64].as_u64 = val; 1873 break; 1874 } 1875 n_u64++; 1876 } 1877 } 1878 out: 1879 __synth_event_trace_end(&state); 1880 1881 return ret; 1882 } 1883 EXPORT_SYMBOL_GPL(synth_event_trace_array); 1884 1885 /** 1886 * synth_event_trace_start - Start piecewise synthetic event trace 1887 * @file: The trace_event_file representing the synthetic event 1888 * @trace_state: A pointer to object tracking the piecewise trace state 1889 * 1890 * Start the trace of a synthetic event field-by-field rather than all 1891 * at once. 1892 * 1893 * This function 'opens' an event trace, which means space is reserved 1894 * for the event in the trace buffer, after which the event's 1895 * individual field values can be set through either 1896 * synth_event_add_next_val() or synth_event_add_val(). 1897 * 1898 * A pointer to a trace_state object is passed in, which will keep 1899 * track of the current event trace state until the event trace is 1900 * closed (and the event finally traced) using 1901 * synth_event_trace_end(). 1902 * 1903 * Note that synth_event_trace_end() must be called after all values 1904 * have been added for each event trace, regardless of whether adding 1905 * all field values succeeded or not. 1906 * 1907 * Note also that for a given event trace, all fields must be added 1908 * using either synth_event_add_next_val() or synth_event_add_val() 1909 * but not both together or interleaved. 1910 * 1911 * Return: 0 on success, err otherwise. 1912 */ 1913 int synth_event_trace_start(struct trace_event_file *file, 1914 struct synth_event_trace_state *trace_state) 1915 { 1916 int ret; 1917 1918 if (!trace_state) 1919 return -EINVAL; 1920 1921 ret = __synth_event_trace_init(file, trace_state); 1922 if (ret) { 1923 if (ret == -ENOENT) 1924 ret = 0; /* just disabled, not really an error */ 1925 return ret; 1926 } 1927 1928 if (trace_state->event->n_dynamic_fields) 1929 return -ENOTSUPP; 1930 1931 ret = __synth_event_trace_start(file, trace_state, 0); 1932 1933 return ret; 1934 } 1935 EXPORT_SYMBOL_GPL(synth_event_trace_start); 1936 1937 static int __synth_event_add_val(const char *field_name, u64 val, 1938 struct synth_event_trace_state *trace_state) 1939 { 1940 struct synth_field *field = NULL; 1941 struct synth_trace_event *entry; 1942 struct synth_event *event; 1943 int i, ret = 0; 1944 1945 if (!trace_state) { 1946 ret = -EINVAL; 1947 goto out; 1948 } 1949 1950 /* can't mix add_next_synth_val() with add_synth_val() */ 1951 if (field_name) { 1952 if (trace_state->add_next) { 1953 ret = -EINVAL; 1954 goto out; 1955 } 1956 trace_state->add_name = true; 1957 } else { 1958 if (trace_state->add_name) { 1959 ret = -EINVAL; 1960 goto out; 1961 } 1962 trace_state->add_next = true; 1963 } 1964 1965 if (trace_state->disabled) 1966 goto out; 1967 1968 event = trace_state->event; 1969 if (trace_state->add_name) { 1970 for (i = 0; i < event->n_fields; i++) { 1971 field = event->fields[i]; 1972 if (strcmp(field->name, field_name) == 0) 1973 break; 1974 } 1975 if (!field) { 1976 ret = -EINVAL; 1977 goto out; 1978 } 1979 } else { 1980 if (trace_state->cur_field >= event->n_fields) { 1981 ret = -EINVAL; 1982 goto out; 1983 } 1984 field = event->fields[trace_state->cur_field++]; 1985 } 1986 1987 entry = trace_state->entry; 1988 if (field->is_string) { 1989 char *str_val = (char *)(long)val; 1990 char *str_field; 1991 1992 if (field->is_dynamic) { /* add_val can't do dynamic strings */ 1993 ret = -EINVAL; 1994 goto out; 1995 } 1996 1997 if (!str_val) { 1998 ret = -EINVAL; 1999 goto out; 2000 } 2001 2002 str_field = (char *)&entry->fields[field->offset]; 2003 strscpy(str_field, str_val, STR_VAR_LEN_MAX); 2004 } else { 2005 switch (field->size) { 2006 case 1: 2007 trace_state->entry->fields[field->offset].as_u8 = (u8)val; 2008 break; 2009 2010 case 2: 2011 trace_state->entry->fields[field->offset].as_u16 = (u16)val; 2012 break; 2013 2014 case 4: 2015 trace_state->entry->fields[field->offset].as_u32 = (u32)val; 2016 break; 2017 2018 default: 2019 trace_state->entry->fields[field->offset].as_u64 = val; 2020 break; 2021 } 2022 } 2023 out: 2024 return ret; 2025 } 2026 2027 /** 2028 * synth_event_add_next_val - Add the next field's value to an open synth trace 2029 * @val: The value to set the next field to 2030 * @trace_state: A pointer to object tracking the piecewise trace state 2031 * 2032 * Set the value of the next field in an event that's been opened by 2033 * synth_event_trace_start(). 2034 * 2035 * The val param should be the value cast to u64. If the value points 2036 * to a string, the val param should be a char * cast to u64. 2037 * 2038 * This function assumes all the fields in an event are to be set one 2039 * after another - successive calls to this function are made, one for 2040 * each field, in the order of the fields in the event, until all 2041 * fields have been set. If you'd rather set each field individually 2042 * without regard to ordering, synth_event_add_val() can be used 2043 * instead. 2044 * 2045 * Note however that synth_event_add_next_val() and 2046 * synth_event_add_val() can't be intermixed for a given event trace - 2047 * one or the other but not both can be used at the same time. 2048 * 2049 * Note also that synth_event_trace_end() must be called after all 2050 * values have been added for each event trace, regardless of whether 2051 * adding all field values succeeded or not. 2052 * 2053 * Return: 0 on success, err otherwise. 2054 */ 2055 int synth_event_add_next_val(u64 val, 2056 struct synth_event_trace_state *trace_state) 2057 { 2058 return __synth_event_add_val(NULL, val, trace_state); 2059 } 2060 EXPORT_SYMBOL_GPL(synth_event_add_next_val); 2061 2062 /** 2063 * synth_event_add_val - Add a named field's value to an open synth trace 2064 * @field_name: The name of the synthetic event field value to set 2065 * @val: The value to set the named field to 2066 * @trace_state: A pointer to object tracking the piecewise trace state 2067 * 2068 * Set the value of the named field in an event that's been opened by 2069 * synth_event_trace_start(). 2070 * 2071 * The val param should be the value cast to u64. If the value points 2072 * to a string, the val param should be a char * cast to u64. 2073 * 2074 * This function looks up the field name, and if found, sets the field 2075 * to the specified value. This lookup makes this function more 2076 * expensive than synth_event_add_next_val(), so use that or the 2077 * none-piecewise synth_event_trace() instead if efficiency is more 2078 * important. 2079 * 2080 * Note however that synth_event_add_next_val() and 2081 * synth_event_add_val() can't be intermixed for a given event trace - 2082 * one or the other but not both can be used at the same time. 2083 * 2084 * Note also that synth_event_trace_end() must be called after all 2085 * values have been added for each event trace, regardless of whether 2086 * adding all field values succeeded or not. 2087 * 2088 * Return: 0 on success, err otherwise. 2089 */ 2090 int synth_event_add_val(const char *field_name, u64 val, 2091 struct synth_event_trace_state *trace_state) 2092 { 2093 return __synth_event_add_val(field_name, val, trace_state); 2094 } 2095 EXPORT_SYMBOL_GPL(synth_event_add_val); 2096 2097 /** 2098 * synth_event_trace_end - End piecewise synthetic event trace 2099 * @trace_state: A pointer to object tracking the piecewise trace state 2100 * 2101 * End the trace of a synthetic event opened by 2102 * synth_event_trace__start(). 2103 * 2104 * This function 'closes' an event trace, which basically means that 2105 * it commits the reserved event and cleans up other loose ends. 2106 * 2107 * A pointer to a trace_state object is passed in, which will keep 2108 * track of the current event trace state opened with 2109 * synth_event_trace_start(). 2110 * 2111 * Note that this function must be called after all values have been 2112 * added for each event trace, regardless of whether adding all field 2113 * values succeeded or not. 2114 * 2115 * Return: 0 on success, err otherwise. 2116 */ 2117 int synth_event_trace_end(struct synth_event_trace_state *trace_state) 2118 { 2119 if (!trace_state) 2120 return -EINVAL; 2121 2122 __synth_event_trace_end(trace_state); 2123 2124 return 0; 2125 } 2126 EXPORT_SYMBOL_GPL(synth_event_trace_end); 2127 2128 static int create_synth_event(const char *raw_command) 2129 { 2130 char *fields, *p; 2131 const char *name; 2132 int len, ret = 0; 2133 2134 raw_command = skip_spaces(raw_command); 2135 if (raw_command[0] == '\0') 2136 return ret; 2137 2138 last_cmd_set(raw_command); 2139 2140 name = raw_command; 2141 2142 /* Don't try to process if not our system */ 2143 if (name[0] != 's' || name[1] != ':') 2144 return -ECANCELED; 2145 name += 2; 2146 2147 p = strpbrk(raw_command, " \t"); 2148 if (!p) { 2149 synth_err(SYNTH_ERR_INVALID_CMD, 0); 2150 return -EINVAL; 2151 } 2152 2153 fields = skip_spaces(p); 2154 2155 /* This interface accepts group name prefix */ 2156 if (strchr(name, '/')) { 2157 len = str_has_prefix(name, SYNTH_SYSTEM "/"); 2158 if (len == 0) { 2159 synth_err(SYNTH_ERR_INVALID_DYN_CMD, 0); 2160 return -EINVAL; 2161 } 2162 name += len; 2163 } 2164 2165 len = name - raw_command; 2166 2167 ret = check_command(raw_command + len); 2168 if (ret) { 2169 synth_err(SYNTH_ERR_INVALID_CMD, 0); 2170 return ret; 2171 } 2172 2173 name = kmemdup_nul(raw_command + len, p - raw_command - len, GFP_KERNEL); 2174 if (!name) 2175 return -ENOMEM; 2176 2177 ret = __create_synth_event(name, fields); 2178 2179 kfree(name); 2180 2181 return ret; 2182 } 2183 2184 static int synth_event_release(struct dyn_event *ev) 2185 { 2186 struct synth_event *event = to_synth_event(ev); 2187 int ret; 2188 2189 if (event->ref) 2190 return -EBUSY; 2191 2192 if (trace_event_dyn_busy(&event->call)) 2193 return -EBUSY; 2194 2195 ret = unregister_synth_event(event); 2196 if (ret) 2197 return ret; 2198 2199 dyn_event_remove(ev); 2200 free_synth_event(event); 2201 return 0; 2202 } 2203 2204 static int __synth_event_show(struct seq_file *m, struct synth_event *event) 2205 { 2206 struct synth_field *field; 2207 unsigned int i; 2208 char *type, *t; 2209 2210 seq_printf(m, "%s\t", event->name); 2211 2212 for (i = 0; i < event->n_fields; i++) { 2213 field = event->fields[i]; 2214 2215 type = field->type; 2216 t = strstr(type, "__data_loc"); 2217 if (t) { /* __data_loc belongs in format but not event desc */ 2218 t += sizeof("__data_loc"); 2219 type = t; 2220 } 2221 2222 /* parameter values */ 2223 seq_printf(m, "%s %s%s", type, field->name, 2224 i == event->n_fields - 1 ? "" : "; "); 2225 } 2226 2227 seq_putc(m, '\n'); 2228 2229 return 0; 2230 } 2231 2232 static int synth_event_show(struct seq_file *m, struct dyn_event *ev) 2233 { 2234 struct synth_event *event = to_synth_event(ev); 2235 2236 seq_printf(m, "s:%s/", event->class.system); 2237 2238 return __synth_event_show(m, event); 2239 } 2240 2241 static int synth_events_seq_show(struct seq_file *m, void *v) 2242 { 2243 struct dyn_event *ev = v; 2244 2245 if (!is_synth_event(ev)) 2246 return 0; 2247 2248 return __synth_event_show(m, to_synth_event(ev)); 2249 } 2250 2251 static const struct seq_operations synth_events_seq_op = { 2252 .start = dyn_event_seq_start, 2253 .next = dyn_event_seq_next, 2254 .stop = dyn_event_seq_stop, 2255 .show = synth_events_seq_show, 2256 }; 2257 2258 static int synth_events_open(struct inode *inode, struct file *file) 2259 { 2260 int ret; 2261 2262 ret = security_locked_down(LOCKDOWN_TRACEFS); 2263 if (ret) 2264 return ret; 2265 2266 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { 2267 ret = dyn_events_release_all(&synth_event_ops); 2268 if (ret < 0) 2269 return ret; 2270 } 2271 2272 return seq_open(file, &synth_events_seq_op); 2273 } 2274 2275 static ssize_t synth_events_write(struct file *file, 2276 const char __user *buffer, 2277 size_t count, loff_t *ppos) 2278 { 2279 return trace_parse_run_command(file, buffer, count, ppos, 2280 create_or_delete_synth_event); 2281 } 2282 2283 static const struct file_operations synth_events_fops = { 2284 .open = synth_events_open, 2285 .write = synth_events_write, 2286 .read = seq_read, 2287 .llseek = seq_lseek, 2288 .release = seq_release, 2289 }; 2290 2291 /* 2292 * Register dynevent at core_initcall. This allows kernel to setup kprobe 2293 * events in postcore_initcall without tracefs. 2294 */ 2295 static __init int trace_events_synth_init_early(void) 2296 { 2297 int err = 0; 2298 2299 err = dyn_event_register(&synth_event_ops); 2300 if (err) 2301 pr_warn("Could not register synth_event_ops\n"); 2302 2303 return err; 2304 } 2305 core_initcall(trace_events_synth_init_early); 2306 2307 static __init int trace_events_synth_init(void) 2308 { 2309 struct dentry *entry = NULL; 2310 int err = 0; 2311 err = tracing_init_dentry(); 2312 if (err) 2313 goto err; 2314 2315 entry = tracefs_create_file("synthetic_events", TRACE_MODE_WRITE, 2316 NULL, NULL, &synth_events_fops); 2317 if (!entry) { 2318 err = -ENODEV; 2319 goto err; 2320 } 2321 2322 return err; 2323 err: 2324 pr_warn("Could not create tracefs 'synthetic_events' entry\n"); 2325 2326 return err; 2327 } 2328 2329 fs_initcall(trace_events_synth_init); 2330