1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * event tracer 4 * 5 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> 6 * 7 * - Added format output of fields of the trace point. 8 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>. 9 * 10 */ 11 12 #define pr_fmt(fmt) fmt 13 14 #include <linux/workqueue.h> 15 #include <linux/security.h> 16 #include <linux/spinlock.h> 17 #include <linux/kthread.h> 18 #include <linux/tracefs.h> 19 #include <linux/uaccess.h> 20 #include <linux/module.h> 21 #include <linux/ctype.h> 22 #include <linux/sort.h> 23 #include <linux/slab.h> 24 #include <linux/delay.h> 25 26 #include <trace/events/sched.h> 27 #include <trace/syscall.h> 28 29 #include <asm/setup.h> 30 31 #include "trace_output.h" 32 33 #undef TRACE_SYSTEM 34 #define TRACE_SYSTEM "TRACE_SYSTEM" 35 36 DEFINE_MUTEX(event_mutex); 37 38 LIST_HEAD(ftrace_events); 39 static LIST_HEAD(ftrace_generic_fields); 40 static LIST_HEAD(ftrace_common_fields); 41 static bool eventdir_initialized; 42 43 static LIST_HEAD(module_strings); 44 45 struct module_string { 46 struct list_head next; 47 struct module *module; 48 char *str; 49 }; 50 51 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO) 52 53 static struct kmem_cache *field_cachep; 54 static struct kmem_cache *file_cachep; 55 56 static inline int system_refcount(struct event_subsystem *system) 57 { 58 return system->ref_count; 59 } 60 61 static int system_refcount_inc(struct event_subsystem *system) 62 { 63 return system->ref_count++; 64 } 65 66 static int system_refcount_dec(struct event_subsystem *system) 67 { 68 return --system->ref_count; 69 } 70 71 /* Double loops, do not use break, only goto's work */ 72 #define do_for_each_event_file(tr, file) \ 73 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \ 74 list_for_each_entry(file, &tr->events, list) 75 76 #define do_for_each_event_file_safe(tr, file) \ 77 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \ 78 struct trace_event_file *___n; \ 79 list_for_each_entry_safe(file, ___n, &tr->events, list) 80 81 #define while_for_each_event_file() \ 82 } 83 84 static struct ftrace_event_field * 85 __find_event_field(struct list_head *head, const char *name) 86 { 87 struct ftrace_event_field *field; 88 89 list_for_each_entry(field, head, link) { 90 if (!strcmp(field->name, name)) 91 return field; 92 } 93 94 return NULL; 95 } 96 97 struct ftrace_event_field * 98 trace_find_event_field(struct trace_event_call *call, char *name) 99 { 100 struct ftrace_event_field *field; 101 struct list_head *head; 102 103 head = trace_get_fields(call); 104 field = __find_event_field(head, name); 105 if (field) 106 return field; 107 108 field = __find_event_field(&ftrace_generic_fields, name); 109 if (field) 110 return field; 111 112 return __find_event_field(&ftrace_common_fields, name); 113 } 114 115 static int __trace_define_field(struct list_head *head, const char *type, 116 const char *name, int offset, int size, 117 int is_signed, int filter_type, int len, 118 int need_test) 119 { 120 struct ftrace_event_field *field; 121 122 field = kmem_cache_alloc(field_cachep, GFP_TRACE); 123 if (!field) 124 return -ENOMEM; 125 126 field->name = name; 127 field->type = type; 128 129 if (filter_type == FILTER_OTHER) 130 field->filter_type = filter_assign_type(type); 131 else 132 field->filter_type = filter_type; 133 134 field->offset = offset; 135 field->size = size; 136 field->is_signed = is_signed; 137 field->needs_test = need_test; 138 field->len = len; 139 140 list_add(&field->link, head); 141 142 return 0; 143 } 144 145 int trace_define_field(struct trace_event_call *call, const char *type, 146 const char *name, int offset, int size, int is_signed, 147 int filter_type) 148 { 149 struct list_head *head; 150 151 if (WARN_ON(!call->class)) 152 return 0; 153 154 head = trace_get_fields(call); 155 return __trace_define_field(head, type, name, offset, size, 156 is_signed, filter_type, 0, 0); 157 } 158 EXPORT_SYMBOL_GPL(trace_define_field); 159 160 static int trace_define_field_ext(struct trace_event_call *call, const char *type, 161 const char *name, int offset, int size, int is_signed, 162 int filter_type, int len, int need_test) 163 { 164 struct list_head *head; 165 166 if (WARN_ON(!call->class)) 167 return 0; 168 169 head = trace_get_fields(call); 170 return __trace_define_field(head, type, name, offset, size, 171 is_signed, filter_type, len, need_test); 172 } 173 174 #define __generic_field(type, item, filter_type) \ 175 ret = __trace_define_field(&ftrace_generic_fields, #type, \ 176 #item, 0, 0, is_signed_type(type), \ 177 filter_type, 0, 0); \ 178 if (ret) \ 179 return ret; 180 181 #define __common_field(type, item) \ 182 ret = __trace_define_field(&ftrace_common_fields, #type, \ 183 "common_" #item, \ 184 offsetof(typeof(ent), item), \ 185 sizeof(ent.item), \ 186 is_signed_type(type), FILTER_OTHER, \ 187 0, 0); \ 188 if (ret) \ 189 return ret; 190 191 static int trace_define_generic_fields(void) 192 { 193 int ret; 194 195 __generic_field(int, CPU, FILTER_CPU); 196 __generic_field(int, cpu, FILTER_CPU); 197 __generic_field(int, common_cpu, FILTER_CPU); 198 __generic_field(char *, COMM, FILTER_COMM); 199 __generic_field(char *, comm, FILTER_COMM); 200 __generic_field(char *, stacktrace, FILTER_STACKTRACE); 201 __generic_field(char *, STACKTRACE, FILTER_STACKTRACE); 202 203 return ret; 204 } 205 206 static int trace_define_common_fields(void) 207 { 208 int ret; 209 struct trace_entry ent; 210 211 __common_field(unsigned short, type); 212 __common_field(unsigned char, flags); 213 /* Holds both preempt_count and migrate_disable */ 214 __common_field(unsigned char, preempt_count); 215 __common_field(int, pid); 216 217 return ret; 218 } 219 220 static void trace_destroy_fields(struct trace_event_call *call) 221 { 222 struct ftrace_event_field *field, *next; 223 struct list_head *head; 224 225 head = trace_get_fields(call); 226 list_for_each_entry_safe(field, next, head, link) { 227 list_del(&field->link); 228 kmem_cache_free(field_cachep, field); 229 } 230 } 231 232 /* 233 * run-time version of trace_event_get_offsets_<call>() that returns the last 234 * accessible offset of trace fields excluding __dynamic_array bytes 235 */ 236 int trace_event_get_offsets(struct trace_event_call *call) 237 { 238 struct ftrace_event_field *tail; 239 struct list_head *head; 240 241 head = trace_get_fields(call); 242 /* 243 * head->next points to the last field with the largest offset, 244 * since it was added last by trace_define_field() 245 */ 246 tail = list_first_entry(head, struct ftrace_event_field, link); 247 return tail->offset + tail->size; 248 } 249 250 251 static struct trace_event_fields *find_event_field(const char *fmt, 252 struct trace_event_call *call) 253 { 254 struct trace_event_fields *field = call->class->fields_array; 255 const char *p = fmt; 256 int len; 257 258 if (!(len = str_has_prefix(fmt, "REC->"))) 259 return NULL; 260 fmt += len; 261 for (p = fmt; *p; p++) { 262 if (!isalnum(*p) && *p != '_') 263 break; 264 } 265 len = p - fmt; 266 267 for (; field->type; field++) { 268 if (strncmp(field->name, fmt, len) || field->name[len]) 269 continue; 270 271 return field; 272 } 273 return NULL; 274 } 275 276 /* 277 * Check if the referenced field is an array and return true, 278 * as arrays are OK to dereference. 279 */ 280 static bool test_field(const char *fmt, struct trace_event_call *call) 281 { 282 struct trace_event_fields *field; 283 284 field = find_event_field(fmt, call); 285 if (!field) 286 return false; 287 288 /* This is an array and is OK to dereference. */ 289 return strchr(field->type, '[') != NULL; 290 } 291 292 /* Look for a string within an argument */ 293 static bool find_print_string(const char *arg, const char *str, const char *end) 294 { 295 const char *r; 296 297 r = strstr(arg, str); 298 return r && r < end; 299 } 300 301 /* Return true if the argument pointer is safe */ 302 static bool process_pointer(const char *fmt, int len, struct trace_event_call *call) 303 { 304 const char *r, *e, *a; 305 306 e = fmt + len; 307 308 /* Find the REC-> in the argument */ 309 r = strstr(fmt, "REC->"); 310 if (r && r < e) { 311 /* 312 * Addresses of events on the buffer, or an array on the buffer is 313 * OK to dereference. There's ways to fool this, but 314 * this is to catch common mistakes, not malicious code. 315 */ 316 a = strchr(fmt, '&'); 317 if ((a && (a < r)) || test_field(r, call)) 318 return true; 319 } else if (find_print_string(fmt, "__get_dynamic_array(", e)) { 320 return true; 321 } else if (find_print_string(fmt, "__get_rel_dynamic_array(", e)) { 322 return true; 323 } else if (find_print_string(fmt, "__get_dynamic_array_len(", e)) { 324 return true; 325 } else if (find_print_string(fmt, "__get_rel_dynamic_array_len(", e)) { 326 return true; 327 } else if (find_print_string(fmt, "__get_sockaddr(", e)) { 328 return true; 329 } else if (find_print_string(fmt, "__get_rel_sockaddr(", e)) { 330 return true; 331 } 332 return false; 333 } 334 335 /* Return true if the string is safe */ 336 static bool process_string(const char *fmt, int len, struct trace_event_call *call) 337 { 338 struct trace_event_fields *field; 339 const char *r, *e, *s; 340 341 e = fmt + len; 342 343 /* 344 * There are several helper functions that return strings. 345 * If the argument contains a function, then assume its field is valid. 346 * It is considered that the argument has a function if it has: 347 * alphanumeric or '_' before a parenthesis. 348 */ 349 s = fmt; 350 do { 351 r = strstr(s, "("); 352 if (!r || r >= e) 353 break; 354 for (int i = 1; r - i >= s; i++) { 355 char ch = *(r - i); 356 if (isspace(ch)) 357 continue; 358 if (isalnum(ch) || ch == '_') 359 return true; 360 /* Anything else, this isn't a function */ 361 break; 362 } 363 /* A function could be wrapped in parenthesis, try the next one */ 364 s = r + 1; 365 } while (s < e); 366 367 /* 368 * Check for arrays. If the argument has: foo[REC->val] 369 * then it is very likely that foo is an array of strings 370 * that are safe to use. 371 */ 372 r = strstr(s, "["); 373 if (r && r < e) { 374 r = strstr(r, "REC->"); 375 if (r && r < e) 376 return true; 377 } 378 379 /* 380 * If there's any strings in the argument consider this arg OK as it 381 * could be: REC->field ? "foo" : "bar" and we don't want to get into 382 * verifying that logic here. 383 */ 384 if (find_print_string(fmt, "\"", e)) 385 return true; 386 387 /* Dereferenced strings are also valid like any other pointer */ 388 if (process_pointer(fmt, len, call)) 389 return true; 390 391 /* Make sure the field is found */ 392 field = find_event_field(fmt, call); 393 if (!field) 394 return false; 395 396 /* Test this field's string before printing the event */ 397 call->flags |= TRACE_EVENT_FL_TEST_STR; 398 field->needs_test = 1; 399 400 return true; 401 } 402 403 static void handle_dereference_arg(const char *arg_str, u64 string_flags, int len, 404 u64 *dereference_flags, int arg, 405 struct trace_event_call *call) 406 { 407 if (string_flags & (1ULL << arg)) { 408 if (process_string(arg_str, len, call)) 409 *dereference_flags &= ~(1ULL << arg); 410 } else if (process_pointer(arg_str, len, call)) 411 *dereference_flags &= ~(1ULL << arg); 412 else 413 pr_warn("TRACE EVENT ERROR: Bad dereference argument: '%.*s'\n", 414 len, arg_str); 415 } 416 417 /* 418 * Examine the print fmt of the event looking for unsafe dereference 419 * pointers using %p* that could be recorded in the trace event and 420 * much later referenced after the pointer was freed. Dereferencing 421 * pointers are OK, if it is dereferenced into the event itself. 422 */ 423 static void test_event_printk(struct trace_event_call *call) 424 { 425 u64 dereference_flags = 0; 426 u64 string_flags = 0; 427 bool first = true; 428 const char *fmt; 429 int parens = 0; 430 char in_quote = 0; 431 int start_arg = 0; 432 int arg = 0; 433 int i, e; 434 435 fmt = call->print_fmt; 436 437 if (!fmt) 438 return; 439 440 for (i = 0; fmt[i]; i++) { 441 switch (fmt[i]) { 442 case '\\': 443 i++; 444 if (!fmt[i]) 445 return; 446 continue; 447 case '"': 448 case '\'': 449 /* 450 * The print fmt starts with a string that 451 * is processed first to find %p* usage, 452 * then after the first string, the print fmt 453 * contains arguments that are used to check 454 * if the dereferenced %p* usage is safe. 455 */ 456 if (first) { 457 if (fmt[i] == '\'') 458 continue; 459 if (in_quote) { 460 arg = 0; 461 first = false; 462 /* 463 * If there was no %p* uses 464 * the fmt is OK. 465 */ 466 if (!dereference_flags) 467 return; 468 } 469 } 470 if (in_quote) { 471 if (in_quote == fmt[i]) 472 in_quote = 0; 473 } else { 474 in_quote = fmt[i]; 475 } 476 continue; 477 case '%': 478 if (!first || !in_quote) 479 continue; 480 i++; 481 if (!fmt[i]) 482 return; 483 switch (fmt[i]) { 484 case '%': 485 continue; 486 case 'p': 487 do_pointer: 488 /* Find dereferencing fields */ 489 switch (fmt[i + 1]) { 490 case 'B': case 'R': case 'r': 491 case 'b': case 'M': case 'm': 492 case 'I': case 'i': case 'E': 493 case 'U': case 'V': case 'N': 494 case 'a': case 'd': case 'D': 495 case 'g': case 't': case 'C': 496 case 'O': case 'f': 497 if (WARN_ONCE(arg == 63, 498 "Too many args for event: %s", 499 trace_event_name(call))) 500 return; 501 dereference_flags |= 1ULL << arg; 502 } 503 break; 504 default: 505 { 506 bool star = false; 507 int j; 508 509 /* Increment arg if %*s exists. */ 510 for (j = 0; fmt[i + j]; j++) { 511 if (isdigit(fmt[i + j]) || 512 fmt[i + j] == '.') 513 continue; 514 if (fmt[i + j] == '*') { 515 star = true; 516 /* Handle %*pbl case */ 517 if (!j && fmt[i + 1] == 'p') { 518 arg++; 519 i++; 520 goto do_pointer; 521 } 522 continue; 523 } 524 if ((fmt[i + j] == 's')) { 525 if (star) 526 arg++; 527 if (WARN_ONCE(arg == 63, 528 "Too many args for event: %s", 529 trace_event_name(call))) 530 return; 531 dereference_flags |= 1ULL << arg; 532 string_flags |= 1ULL << arg; 533 } 534 break; 535 } 536 break; 537 } /* default */ 538 539 } /* switch */ 540 arg++; 541 continue; 542 case '(': 543 if (in_quote) 544 continue; 545 parens++; 546 continue; 547 case ')': 548 if (in_quote) 549 continue; 550 parens--; 551 if (WARN_ONCE(parens < 0, 552 "Paren mismatch for event: %s\narg='%s'\n%*s", 553 trace_event_name(call), 554 fmt + start_arg, 555 (i - start_arg) + 5, "^")) 556 return; 557 continue; 558 case ',': 559 if (in_quote || parens) 560 continue; 561 e = i; 562 i++; 563 while (isspace(fmt[i])) 564 i++; 565 566 /* 567 * If start_arg is zero, then this is the start of the 568 * first argument. The processing of the argument happens 569 * when the end of the argument is found, as it needs to 570 * handle parenthesis and such. 571 */ 572 if (!start_arg) { 573 start_arg = i; 574 /* Balance out the i++ in the for loop */ 575 i--; 576 continue; 577 } 578 579 if (dereference_flags & (1ULL << arg)) { 580 handle_dereference_arg(fmt + start_arg, string_flags, 581 e - start_arg, 582 &dereference_flags, arg, call); 583 } 584 585 start_arg = i; 586 arg++; 587 /* Balance out the i++ in the for loop */ 588 i--; 589 } 590 } 591 592 if (dereference_flags & (1ULL << arg)) { 593 handle_dereference_arg(fmt + start_arg, string_flags, 594 i - start_arg, 595 &dereference_flags, arg, call); 596 } 597 598 /* 599 * If you triggered the below warning, the trace event reported 600 * uses an unsafe dereference pointer %p*. As the data stored 601 * at the trace event time may no longer exist when the trace 602 * event is printed, dereferencing to the original source is 603 * unsafe. The source of the dereference must be copied into the 604 * event itself, and the dereference must access the copy instead. 605 */ 606 if (WARN_ON_ONCE(dereference_flags)) { 607 arg = 1; 608 while (!(dereference_flags & 1)) { 609 dereference_flags >>= 1; 610 arg++; 611 } 612 pr_warn("event %s has unsafe dereference of argument %d\n", 613 trace_event_name(call), arg); 614 pr_warn("print_fmt: %s\n", fmt); 615 } 616 } 617 618 int trace_event_raw_init(struct trace_event_call *call) 619 { 620 int id; 621 622 id = register_trace_event(&call->event); 623 if (!id) 624 return -ENODEV; 625 626 test_event_printk(call); 627 628 return 0; 629 } 630 EXPORT_SYMBOL_GPL(trace_event_raw_init); 631 632 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file) 633 { 634 struct trace_array *tr = trace_file->tr; 635 struct trace_pid_list *no_pid_list; 636 struct trace_pid_list *pid_list; 637 638 pid_list = rcu_dereference_raw(tr->filtered_pids); 639 no_pid_list = rcu_dereference_raw(tr->filtered_no_pids); 640 641 if (!pid_list && !no_pid_list) 642 return false; 643 644 /* 645 * This is recorded at every sched_switch for this task. 646 * Thus, even if the task migrates the ignore value will be the same. 647 */ 648 return this_cpu_read(tr->array_buffer.data->ignore_pid) != 0; 649 } 650 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid); 651 652 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer, 653 struct trace_event_file *trace_file, 654 unsigned long len) 655 { 656 struct trace_event_call *event_call = trace_file->event_call; 657 658 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) && 659 trace_event_ignore_this_pid(trace_file)) 660 return NULL; 661 662 /* 663 * If CONFIG_PREEMPTION is enabled, then the tracepoint itself disables 664 * preemption (adding one to the preempt_count). Since we are 665 * interested in the preempt_count at the time the tracepoint was 666 * hit, we need to subtract one to offset the increment. 667 */ 668 fbuffer->trace_ctx = tracing_gen_ctx_dec(); 669 fbuffer->trace_file = trace_file; 670 671 fbuffer->event = 672 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file, 673 event_call->event.type, len, 674 fbuffer->trace_ctx); 675 if (!fbuffer->event) 676 return NULL; 677 678 fbuffer->regs = NULL; 679 fbuffer->entry = ring_buffer_event_data(fbuffer->event); 680 return fbuffer->entry; 681 } 682 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve); 683 684 int trace_event_reg(struct trace_event_call *call, 685 enum trace_reg type, void *data) 686 { 687 struct trace_event_file *file = data; 688 689 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT)); 690 switch (type) { 691 case TRACE_REG_REGISTER: 692 return tracepoint_probe_register(call->tp, 693 call->class->probe, 694 file); 695 case TRACE_REG_UNREGISTER: 696 tracepoint_probe_unregister(call->tp, 697 call->class->probe, 698 file); 699 return 0; 700 701 #ifdef CONFIG_PERF_EVENTS 702 case TRACE_REG_PERF_REGISTER: 703 if (!call->class->perf_probe) 704 return -ENODEV; 705 return tracepoint_probe_register(call->tp, 706 call->class->perf_probe, 707 call); 708 case TRACE_REG_PERF_UNREGISTER: 709 tracepoint_probe_unregister(call->tp, 710 call->class->perf_probe, 711 call); 712 return 0; 713 case TRACE_REG_PERF_OPEN: 714 case TRACE_REG_PERF_CLOSE: 715 case TRACE_REG_PERF_ADD: 716 case TRACE_REG_PERF_DEL: 717 return 0; 718 #endif 719 } 720 return 0; 721 } 722 EXPORT_SYMBOL_GPL(trace_event_reg); 723 724 void trace_event_enable_cmd_record(bool enable) 725 { 726 struct trace_event_file *file; 727 struct trace_array *tr; 728 729 lockdep_assert_held(&event_mutex); 730 731 do_for_each_event_file(tr, file) { 732 733 if (!(file->flags & EVENT_FILE_FL_ENABLED)) 734 continue; 735 736 if (enable) { 737 tracing_start_cmdline_record(); 738 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags); 739 } else { 740 tracing_stop_cmdline_record(); 741 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags); 742 } 743 } while_for_each_event_file(); 744 } 745 746 void trace_event_enable_tgid_record(bool enable) 747 { 748 struct trace_event_file *file; 749 struct trace_array *tr; 750 751 lockdep_assert_held(&event_mutex); 752 753 do_for_each_event_file(tr, file) { 754 if (!(file->flags & EVENT_FILE_FL_ENABLED)) 755 continue; 756 757 if (enable) { 758 tracing_start_tgid_record(); 759 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags); 760 } else { 761 tracing_stop_tgid_record(); 762 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, 763 &file->flags); 764 } 765 } while_for_each_event_file(); 766 } 767 768 static int __ftrace_event_enable_disable(struct trace_event_file *file, 769 int enable, int soft_disable) 770 { 771 struct trace_event_call *call = file->event_call; 772 struct trace_array *tr = file->tr; 773 bool soft_mode = atomic_read(&file->sm_ref) != 0; 774 int ret = 0; 775 int disable; 776 777 switch (enable) { 778 case 0: 779 /* 780 * When soft_disable is set and enable is cleared, the sm_ref 781 * reference counter is decremented. If it reaches 0, we want 782 * to clear the SOFT_DISABLED flag but leave the event in the 783 * state that it was. That is, if the event was enabled and 784 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED 785 * is set we do not want the event to be enabled before we 786 * clear the bit. 787 * 788 * When soft_disable is not set but the soft_mode is, 789 * we do nothing. Do not disable the tracepoint, otherwise 790 * "soft enable"s (clearing the SOFT_DISABLED bit) won't work. 791 */ 792 if (soft_disable) { 793 if (atomic_dec_return(&file->sm_ref) > 0) 794 break; 795 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED; 796 soft_mode = false; 797 /* Disable use of trace_buffered_event */ 798 trace_buffered_event_disable(); 799 } else 800 disable = !soft_mode; 801 802 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) { 803 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags); 804 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) { 805 tracing_stop_cmdline_record(); 806 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags); 807 } 808 809 if (file->flags & EVENT_FILE_FL_RECORDED_TGID) { 810 tracing_stop_tgid_record(); 811 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags); 812 } 813 814 ret = call->class->reg(call, TRACE_REG_UNREGISTER, file); 815 816 WARN_ON_ONCE(ret); 817 } 818 /* If in soft mode, just set the SOFT_DISABLE_BIT, else clear it */ 819 if (soft_mode) 820 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags); 821 else 822 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags); 823 break; 824 case 1: 825 /* 826 * When soft_disable is set and enable is set, we want to 827 * register the tracepoint for the event, but leave the event 828 * as is. That means, if the event was already enabled, we do 829 * nothing (but set soft_mode). If the event is disabled, we 830 * set SOFT_DISABLED before enabling the event tracepoint, so 831 * it still seems to be disabled. 832 */ 833 if (!soft_disable) 834 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags); 835 else { 836 if (atomic_inc_return(&file->sm_ref) > 1) 837 break; 838 soft_mode = true; 839 /* Enable use of trace_buffered_event */ 840 trace_buffered_event_enable(); 841 } 842 843 if (!(file->flags & EVENT_FILE_FL_ENABLED)) { 844 bool cmd = false, tgid = false; 845 846 /* Keep the event disabled, when going to soft mode. */ 847 if (soft_disable) 848 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags); 849 850 if (tr->trace_flags & TRACE_ITER(RECORD_CMD)) { 851 cmd = true; 852 tracing_start_cmdline_record(); 853 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags); 854 } 855 856 if (tr->trace_flags & TRACE_ITER(RECORD_TGID)) { 857 tgid = true; 858 tracing_start_tgid_record(); 859 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags); 860 } 861 862 ret = call->class->reg(call, TRACE_REG_REGISTER, file); 863 if (ret) { 864 if (cmd) 865 tracing_stop_cmdline_record(); 866 if (tgid) 867 tracing_stop_tgid_record(); 868 pr_info("event trace: Could not enable event " 869 "%s\n", trace_event_name(call)); 870 break; 871 } 872 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags); 873 874 /* WAS_ENABLED gets set but never cleared. */ 875 set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT, &file->flags); 876 } 877 break; 878 } 879 880 return ret; 881 } 882 883 int trace_event_enable_disable(struct trace_event_file *file, 884 int enable, int soft_disable) 885 { 886 return __ftrace_event_enable_disable(file, enable, soft_disable); 887 } 888 889 static int ftrace_event_enable_disable(struct trace_event_file *file, 890 int enable) 891 { 892 return __ftrace_event_enable_disable(file, enable, 0); 893 } 894 895 #ifdef CONFIG_MODULES 896 struct event_mod_load { 897 struct list_head list; 898 char *module; 899 char *match; 900 char *system; 901 char *event; 902 }; 903 904 static void free_event_mod(struct event_mod_load *event_mod) 905 { 906 list_del(&event_mod->list); 907 kfree(event_mod->module); 908 kfree(event_mod->match); 909 kfree(event_mod->system); 910 kfree(event_mod->event); 911 kfree(event_mod); 912 } 913 914 static void clear_mod_events(struct trace_array *tr) 915 { 916 struct event_mod_load *event_mod, *n; 917 918 list_for_each_entry_safe(event_mod, n, &tr->mod_events, list) { 919 free_event_mod(event_mod); 920 } 921 } 922 923 static int remove_cache_mod(struct trace_array *tr, const char *mod, 924 const char *match, const char *system, const char *event) 925 { 926 struct event_mod_load *event_mod, *n; 927 int ret = -EINVAL; 928 929 list_for_each_entry_safe(event_mod, n, &tr->mod_events, list) { 930 if (strcmp(event_mod->module, mod) != 0) 931 continue; 932 933 if (match && strcmp(event_mod->match, match) != 0) 934 continue; 935 936 if (system && 937 (!event_mod->system || strcmp(event_mod->system, system) != 0)) 938 continue; 939 940 if (event && 941 (!event_mod->event || strcmp(event_mod->event, event) != 0)) 942 continue; 943 944 free_event_mod(event_mod); 945 ret = 0; 946 } 947 948 return ret; 949 } 950 951 static int cache_mod(struct trace_array *tr, const char *mod, int set, 952 const char *match, const char *system, const char *event) 953 { 954 struct event_mod_load *event_mod; 955 956 /* If the module exists, then this just failed to find an event */ 957 if (module_exists(mod)) 958 return -EINVAL; 959 960 /* See if this is to remove a cached filter */ 961 if (!set) 962 return remove_cache_mod(tr, mod, match, system, event); 963 964 event_mod = kzalloc(sizeof(*event_mod), GFP_KERNEL); 965 if (!event_mod) 966 return -ENOMEM; 967 968 INIT_LIST_HEAD(&event_mod->list); 969 event_mod->module = kstrdup(mod, GFP_KERNEL); 970 if (!event_mod->module) 971 goto out_free; 972 973 if (match) { 974 event_mod->match = kstrdup(match, GFP_KERNEL); 975 if (!event_mod->match) 976 goto out_free; 977 } 978 979 if (system) { 980 event_mod->system = kstrdup(system, GFP_KERNEL); 981 if (!event_mod->system) 982 goto out_free; 983 } 984 985 if (event) { 986 event_mod->event = kstrdup(event, GFP_KERNEL); 987 if (!event_mod->event) 988 goto out_free; 989 } 990 991 list_add(&event_mod->list, &tr->mod_events); 992 993 return 0; 994 995 out_free: 996 free_event_mod(event_mod); 997 998 return -ENOMEM; 999 } 1000 #else /* CONFIG_MODULES */ 1001 static inline void clear_mod_events(struct trace_array *tr) { } 1002 static int cache_mod(struct trace_array *tr, const char *mod, int set, 1003 const char *match, const char *system, const char *event) 1004 { 1005 return -EINVAL; 1006 } 1007 #endif 1008 1009 static void ftrace_clear_events(struct trace_array *tr) 1010 { 1011 struct trace_event_file *file; 1012 1013 mutex_lock(&event_mutex); 1014 list_for_each_entry(file, &tr->events, list) { 1015 ftrace_event_enable_disable(file, 0); 1016 } 1017 clear_mod_events(tr); 1018 mutex_unlock(&event_mutex); 1019 } 1020 1021 static void 1022 event_filter_pid_sched_process_exit(void *data, struct task_struct *task) 1023 { 1024 struct trace_pid_list *pid_list; 1025 struct trace_array *tr = data; 1026 1027 pid_list = rcu_dereference_raw(tr->filtered_pids); 1028 trace_filter_add_remove_task(pid_list, NULL, task); 1029 1030 pid_list = rcu_dereference_raw(tr->filtered_no_pids); 1031 trace_filter_add_remove_task(pid_list, NULL, task); 1032 } 1033 1034 static void 1035 event_filter_pid_sched_process_fork(void *data, 1036 struct task_struct *self, 1037 struct task_struct *task) 1038 { 1039 struct trace_pid_list *pid_list; 1040 struct trace_array *tr = data; 1041 1042 pid_list = rcu_dereference_sched(tr->filtered_pids); 1043 trace_filter_add_remove_task(pid_list, self, task); 1044 1045 pid_list = rcu_dereference_sched(tr->filtered_no_pids); 1046 trace_filter_add_remove_task(pid_list, self, task); 1047 } 1048 1049 void trace_event_follow_fork(struct trace_array *tr, bool enable) 1050 { 1051 if (enable) { 1052 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork, 1053 tr, INT_MIN); 1054 register_trace_prio_sched_process_free(event_filter_pid_sched_process_exit, 1055 tr, INT_MAX); 1056 } else { 1057 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork, 1058 tr); 1059 unregister_trace_sched_process_free(event_filter_pid_sched_process_exit, 1060 tr); 1061 } 1062 } 1063 1064 static void 1065 event_filter_pid_sched_switch_probe_pre(void *data, bool preempt, 1066 struct task_struct *prev, 1067 struct task_struct *next, 1068 unsigned int prev_state) 1069 { 1070 struct trace_array *tr = data; 1071 struct trace_pid_list *no_pid_list; 1072 struct trace_pid_list *pid_list; 1073 bool ret; 1074 1075 pid_list = rcu_dereference_sched(tr->filtered_pids); 1076 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids); 1077 1078 /* 1079 * Sched switch is funny, as we only want to ignore it 1080 * in the notrace case if both prev and next should be ignored. 1081 */ 1082 ret = trace_ignore_this_task(NULL, no_pid_list, prev) && 1083 trace_ignore_this_task(NULL, no_pid_list, next); 1084 1085 this_cpu_write(tr->array_buffer.data->ignore_pid, ret || 1086 (trace_ignore_this_task(pid_list, NULL, prev) && 1087 trace_ignore_this_task(pid_list, NULL, next))); 1088 } 1089 1090 static void 1091 event_filter_pid_sched_switch_probe_post(void *data, bool preempt, 1092 struct task_struct *prev, 1093 struct task_struct *next, 1094 unsigned int prev_state) 1095 { 1096 struct trace_array *tr = data; 1097 struct trace_pid_list *no_pid_list; 1098 struct trace_pid_list *pid_list; 1099 1100 pid_list = rcu_dereference_sched(tr->filtered_pids); 1101 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids); 1102 1103 this_cpu_write(tr->array_buffer.data->ignore_pid, 1104 trace_ignore_this_task(pid_list, no_pid_list, next)); 1105 } 1106 1107 static void 1108 event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task) 1109 { 1110 struct trace_array *tr = data; 1111 struct trace_pid_list *no_pid_list; 1112 struct trace_pid_list *pid_list; 1113 1114 /* Nothing to do if we are already tracing */ 1115 if (!this_cpu_read(tr->array_buffer.data->ignore_pid)) 1116 return; 1117 1118 pid_list = rcu_dereference_sched(tr->filtered_pids); 1119 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids); 1120 1121 this_cpu_write(tr->array_buffer.data->ignore_pid, 1122 trace_ignore_this_task(pid_list, no_pid_list, task)); 1123 } 1124 1125 static void 1126 event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task) 1127 { 1128 struct trace_array *tr = data; 1129 struct trace_pid_list *no_pid_list; 1130 struct trace_pid_list *pid_list; 1131 1132 /* Nothing to do if we are not tracing */ 1133 if (this_cpu_read(tr->array_buffer.data->ignore_pid)) 1134 return; 1135 1136 pid_list = rcu_dereference_sched(tr->filtered_pids); 1137 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids); 1138 1139 /* Set tracing if current is enabled */ 1140 this_cpu_write(tr->array_buffer.data->ignore_pid, 1141 trace_ignore_this_task(pid_list, no_pid_list, current)); 1142 } 1143 1144 static void unregister_pid_events(struct trace_array *tr) 1145 { 1146 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr); 1147 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr); 1148 1149 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr); 1150 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr); 1151 1152 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr); 1153 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr); 1154 1155 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr); 1156 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr); 1157 } 1158 1159 static void __ftrace_clear_event_pids(struct trace_array *tr, int type) 1160 { 1161 struct trace_pid_list *pid_list; 1162 struct trace_pid_list *no_pid_list; 1163 struct trace_event_file *file; 1164 int cpu; 1165 1166 pid_list = rcu_dereference_protected(tr->filtered_pids, 1167 lockdep_is_held(&event_mutex)); 1168 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids, 1169 lockdep_is_held(&event_mutex)); 1170 1171 /* Make sure there's something to do */ 1172 if (!pid_type_enabled(type, pid_list, no_pid_list)) 1173 return; 1174 1175 if (!still_need_pid_events(type, pid_list, no_pid_list)) { 1176 unregister_pid_events(tr); 1177 1178 list_for_each_entry(file, &tr->events, list) { 1179 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags); 1180 } 1181 1182 for_each_possible_cpu(cpu) 1183 per_cpu_ptr(tr->array_buffer.data, cpu)->ignore_pid = false; 1184 } 1185 1186 if (type & TRACE_PIDS) 1187 rcu_assign_pointer(tr->filtered_pids, NULL); 1188 1189 if (type & TRACE_NO_PIDS) 1190 rcu_assign_pointer(tr->filtered_no_pids, NULL); 1191 1192 /* Wait till all users are no longer using pid filtering */ 1193 tracepoint_synchronize_unregister(); 1194 1195 if ((type & TRACE_PIDS) && pid_list) 1196 trace_pid_list_free(pid_list); 1197 1198 if ((type & TRACE_NO_PIDS) && no_pid_list) 1199 trace_pid_list_free(no_pid_list); 1200 } 1201 1202 static void ftrace_clear_event_pids(struct trace_array *tr, int type) 1203 { 1204 mutex_lock(&event_mutex); 1205 __ftrace_clear_event_pids(tr, type); 1206 mutex_unlock(&event_mutex); 1207 } 1208 1209 static void __put_system(struct event_subsystem *system) 1210 { 1211 struct event_filter *filter = system->filter; 1212 1213 WARN_ON_ONCE(system_refcount(system) == 0); 1214 if (system_refcount_dec(system)) 1215 return; 1216 1217 list_del(&system->list); 1218 1219 if (filter) { 1220 kfree(filter->filter_string); 1221 kfree(filter); 1222 } 1223 kfree_const(system->name); 1224 kfree(system); 1225 } 1226 1227 static void __get_system(struct event_subsystem *system) 1228 { 1229 WARN_ON_ONCE(system_refcount(system) == 0); 1230 system_refcount_inc(system); 1231 } 1232 1233 static void __get_system_dir(struct trace_subsystem_dir *dir) 1234 { 1235 WARN_ON_ONCE(dir->ref_count == 0); 1236 dir->ref_count++; 1237 __get_system(dir->subsystem); 1238 } 1239 1240 static void __put_system_dir(struct trace_subsystem_dir *dir) 1241 { 1242 WARN_ON_ONCE(dir->ref_count == 0); 1243 /* If the subsystem is about to be freed, the dir must be too */ 1244 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1); 1245 1246 __put_system(dir->subsystem); 1247 if (!--dir->ref_count) 1248 kfree(dir); 1249 } 1250 1251 static void put_system(struct trace_subsystem_dir *dir) 1252 { 1253 mutex_lock(&event_mutex); 1254 __put_system_dir(dir); 1255 mutex_unlock(&event_mutex); 1256 } 1257 1258 static void remove_subsystem(struct trace_subsystem_dir *dir) 1259 { 1260 if (!dir) 1261 return; 1262 1263 if (!--dir->nr_events) { 1264 eventfs_remove_dir(dir->ei); 1265 list_del(&dir->list); 1266 __put_system_dir(dir); 1267 } 1268 } 1269 1270 void event_file_get(struct trace_event_file *file) 1271 { 1272 refcount_inc(&file->ref); 1273 } 1274 1275 void event_file_put(struct trace_event_file *file) 1276 { 1277 if (WARN_ON_ONCE(!refcount_read(&file->ref))) { 1278 if (file->flags & EVENT_FILE_FL_FREED) 1279 kmem_cache_free(file_cachep, file); 1280 return; 1281 } 1282 1283 if (refcount_dec_and_test(&file->ref)) { 1284 /* Count should only go to zero when it is freed */ 1285 if (WARN_ON_ONCE(!(file->flags & EVENT_FILE_FL_FREED))) 1286 return; 1287 kmem_cache_free(file_cachep, file); 1288 } 1289 } 1290 1291 static void remove_event_file_dir(struct trace_event_file *file) 1292 { 1293 eventfs_remove_dir(file->ei); 1294 list_del(&file->list); 1295 remove_subsystem(file->system); 1296 free_event_filter(file->filter); 1297 file->flags |= EVENT_FILE_FL_FREED; 1298 event_file_put(file); 1299 } 1300 1301 /* 1302 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events. 1303 */ 1304 static int 1305 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match, 1306 const char *sub, const char *event, int set, 1307 const char *mod) 1308 { 1309 struct trace_event_file *file; 1310 struct trace_event_call *call; 1311 char *module __free(kfree) = NULL; 1312 const char *name; 1313 int ret = -EINVAL; 1314 int eret = 0; 1315 1316 if (mod) { 1317 char *p; 1318 1319 module = kstrdup(mod, GFP_KERNEL); 1320 if (!module) 1321 return -ENOMEM; 1322 1323 /* Replace all '-' with '_' as that's what modules do */ 1324 for (p = strchr(module, '-'); p; p = strchr(p + 1, '-')) 1325 *p = '_'; 1326 } 1327 1328 list_for_each_entry(file, &tr->events, list) { 1329 1330 call = file->event_call; 1331 1332 /* If a module is specified, skip events that are not that module */ 1333 if (module && (!call->module || strcmp(module_name(call->module), module))) 1334 continue; 1335 1336 name = trace_event_name(call); 1337 1338 if (!name || !call->class || !call->class->reg) 1339 continue; 1340 1341 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) 1342 continue; 1343 1344 if (match && 1345 strcmp(match, name) != 0 && 1346 strcmp(match, call->class->system) != 0) 1347 continue; 1348 1349 if (sub && strcmp(sub, call->class->system) != 0) 1350 continue; 1351 1352 if (event && strcmp(event, name) != 0) 1353 continue; 1354 1355 ret = ftrace_event_enable_disable(file, set); 1356 1357 /* 1358 * Save the first error and return that. Some events 1359 * may still have been enabled, but let the user 1360 * know that something went wrong. 1361 */ 1362 if (ret && !eret) 1363 eret = ret; 1364 1365 ret = eret; 1366 } 1367 1368 /* 1369 * If this is a module setting and nothing was found, 1370 * check if the module was loaded. If it wasn't cache it. 1371 */ 1372 if (module && ret == -EINVAL && !eret) 1373 ret = cache_mod(tr, module, set, match, sub, event); 1374 1375 return ret; 1376 } 1377 1378 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match, 1379 const char *sub, const char *event, int set, 1380 const char *mod) 1381 { 1382 int ret; 1383 1384 mutex_lock(&event_mutex); 1385 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set, mod); 1386 mutex_unlock(&event_mutex); 1387 1388 return ret; 1389 } 1390 1391 int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set) 1392 { 1393 char *event = NULL, *sub = NULL, *match, *mod; 1394 int ret; 1395 1396 if (!tr) 1397 return -ENOENT; 1398 1399 /* Modules events can be appended with :mod:<module> */ 1400 mod = strstr(buf, ":mod:"); 1401 if (mod) { 1402 *mod = '\0'; 1403 /* move to the module name */ 1404 mod += 5; 1405 } 1406 1407 /* 1408 * The buf format can be <subsystem>:<event-name> 1409 * *:<event-name> means any event by that name. 1410 * :<event-name> is the same. 1411 * 1412 * <subsystem>:* means all events in that subsystem 1413 * <subsystem>: means the same. 1414 * 1415 * <name> (no ':') means all events in a subsystem with 1416 * the name <name> or any event that matches <name> 1417 */ 1418 1419 match = strsep(&buf, ":"); 1420 if (buf) { 1421 sub = match; 1422 event = buf; 1423 match = NULL; 1424 1425 if (!strlen(sub) || strcmp(sub, "*") == 0) 1426 sub = NULL; 1427 if (!strlen(event) || strcmp(event, "*") == 0) 1428 event = NULL; 1429 } else if (mod) { 1430 /* Allow wildcard for no length or star */ 1431 if (!strlen(match) || strcmp(match, "*") == 0) 1432 match = NULL; 1433 } 1434 1435 ret = __ftrace_set_clr_event(tr, match, sub, event, set, mod); 1436 1437 /* Put back the colon to allow this to be called again */ 1438 if (buf) 1439 *(buf - 1) = ':'; 1440 1441 return ret; 1442 } 1443 1444 /** 1445 * trace_set_clr_event - enable or disable an event 1446 * @system: system name to match (NULL for any system) 1447 * @event: event name to match (NULL for all events, within system) 1448 * @set: 1 to enable, 0 to disable 1449 * 1450 * This is a way for other parts of the kernel to enable or disable 1451 * event recording. 1452 * 1453 * Returns 0 on success, -EINVAL if the parameters do not match any 1454 * registered events. 1455 */ 1456 int trace_set_clr_event(const char *system, const char *event, int set) 1457 { 1458 struct trace_array *tr = top_trace_array(); 1459 1460 if (!tr) 1461 return -ENODEV; 1462 1463 return __ftrace_set_clr_event(tr, NULL, system, event, set, NULL); 1464 } 1465 EXPORT_SYMBOL_GPL(trace_set_clr_event); 1466 1467 /** 1468 * trace_array_set_clr_event - enable or disable an event for a trace array. 1469 * @tr: concerned trace array. 1470 * @system: system name to match (NULL for any system) 1471 * @event: event name to match (NULL for all events, within system) 1472 * @enable: true to enable, false to disable 1473 * 1474 * This is a way for other parts of the kernel to enable or disable 1475 * event recording. 1476 * 1477 * Returns 0 on success, -EINVAL if the parameters do not match any 1478 * registered events. 1479 */ 1480 int trace_array_set_clr_event(struct trace_array *tr, const char *system, 1481 const char *event, bool enable) 1482 { 1483 int set; 1484 1485 if (!tr) 1486 return -ENOENT; 1487 1488 set = (enable == true) ? 1 : 0; 1489 return __ftrace_set_clr_event(tr, NULL, system, event, set, NULL); 1490 } 1491 EXPORT_SYMBOL_GPL(trace_array_set_clr_event); 1492 1493 /* 128 should be much more than enough */ 1494 #define EVENT_BUF_SIZE 127 1495 1496 static ssize_t 1497 ftrace_event_write(struct file *file, const char __user *ubuf, 1498 size_t cnt, loff_t *ppos) 1499 { 1500 struct trace_parser parser; 1501 struct seq_file *m = file->private_data; 1502 struct trace_array *tr = m->private; 1503 ssize_t read, ret; 1504 1505 if (!cnt) 1506 return 0; 1507 1508 ret = tracing_update_buffers(tr); 1509 if (ret < 0) 1510 return ret; 1511 1512 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1)) 1513 return -ENOMEM; 1514 1515 read = trace_get_user(&parser, ubuf, cnt, ppos); 1516 1517 if (read >= 0 && trace_parser_loaded((&parser))) { 1518 int set = 1; 1519 1520 if (*parser.buffer == '!') 1521 set = 0; 1522 1523 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set); 1524 if (ret) 1525 goto out_put; 1526 } 1527 1528 ret = read; 1529 1530 out_put: 1531 trace_parser_put(&parser); 1532 1533 return ret; 1534 } 1535 1536 static void * 1537 t_next(struct seq_file *m, void *v, loff_t *pos) 1538 { 1539 struct trace_event_file *file = v; 1540 struct trace_event_call *call; 1541 struct trace_array *tr = m->private; 1542 1543 (*pos)++; 1544 1545 list_for_each_entry_continue(file, &tr->events, list) { 1546 call = file->event_call; 1547 /* 1548 * The ftrace subsystem is for showing formats only. 1549 * They can not be enabled or disabled via the event files. 1550 */ 1551 if (call->class && call->class->reg && 1552 !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) 1553 return file; 1554 } 1555 1556 return NULL; 1557 } 1558 1559 static void *t_start(struct seq_file *m, loff_t *pos) 1560 { 1561 struct trace_event_file *file; 1562 struct trace_array *tr = m->private; 1563 loff_t l; 1564 1565 mutex_lock(&event_mutex); 1566 1567 file = list_entry(&tr->events, struct trace_event_file, list); 1568 for (l = 0; l <= *pos; ) { 1569 file = t_next(m, file, &l); 1570 if (!file) 1571 break; 1572 } 1573 return file; 1574 } 1575 1576 enum set_event_iter_type { 1577 SET_EVENT_FILE, 1578 SET_EVENT_MOD, 1579 }; 1580 1581 struct set_event_iter { 1582 enum set_event_iter_type type; 1583 union { 1584 struct trace_event_file *file; 1585 struct event_mod_load *event_mod; 1586 }; 1587 }; 1588 1589 static void * 1590 s_next(struct seq_file *m, void *v, loff_t *pos) 1591 { 1592 struct set_event_iter *iter = v; 1593 struct trace_event_file *file; 1594 struct trace_array *tr = m->private; 1595 1596 (*pos)++; 1597 1598 if (iter->type == SET_EVENT_FILE) { 1599 file = iter->file; 1600 list_for_each_entry_continue(file, &tr->events, list) { 1601 if (file->flags & EVENT_FILE_FL_ENABLED) { 1602 iter->file = file; 1603 return iter; 1604 } 1605 } 1606 #ifdef CONFIG_MODULES 1607 iter->type = SET_EVENT_MOD; 1608 iter->event_mod = list_entry(&tr->mod_events, struct event_mod_load, list); 1609 #endif 1610 } 1611 1612 #ifdef CONFIG_MODULES 1613 list_for_each_entry_continue(iter->event_mod, &tr->mod_events, list) 1614 return iter; 1615 #endif 1616 1617 /* 1618 * The iter is allocated in s_start() and passed via the 'v' 1619 * parameter. To stop the iterator, NULL must be returned. But 1620 * the return value is what the 'v' parameter in s_stop() receives 1621 * and frees. Free iter here as it will no longer be used. 1622 */ 1623 kfree(iter); 1624 return NULL; 1625 } 1626 1627 static void *s_start(struct seq_file *m, loff_t *pos) 1628 { 1629 struct trace_array *tr = m->private; 1630 struct set_event_iter *iter; 1631 loff_t l; 1632 1633 iter = kzalloc(sizeof(*iter), GFP_KERNEL); 1634 mutex_lock(&event_mutex); 1635 if (!iter) 1636 return NULL; 1637 1638 iter->type = SET_EVENT_FILE; 1639 iter->file = list_entry(&tr->events, struct trace_event_file, list); 1640 1641 for (l = 0; l <= *pos; ) { 1642 iter = s_next(m, iter, &l); 1643 if (!iter) 1644 break; 1645 } 1646 return iter; 1647 } 1648 1649 static int t_show(struct seq_file *m, void *v) 1650 { 1651 struct trace_event_file *file = v; 1652 struct trace_event_call *call = file->event_call; 1653 1654 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) 1655 seq_printf(m, "%s:", call->class->system); 1656 seq_printf(m, "%s\n", trace_event_name(call)); 1657 1658 return 0; 1659 } 1660 1661 static void t_stop(struct seq_file *m, void *p) 1662 { 1663 mutex_unlock(&event_mutex); 1664 } 1665 1666 #ifdef CONFIG_MODULES 1667 static int s_show(struct seq_file *m, void *v) 1668 { 1669 struct set_event_iter *iter = v; 1670 const char *system; 1671 const char *event; 1672 1673 if (iter->type == SET_EVENT_FILE) 1674 return t_show(m, iter->file); 1675 1676 /* When match is set, system and event are not */ 1677 if (iter->event_mod->match) { 1678 seq_printf(m, "%s:mod:%s\n", iter->event_mod->match, 1679 iter->event_mod->module); 1680 return 0; 1681 } 1682 1683 system = iter->event_mod->system ? : "*"; 1684 event = iter->event_mod->event ? : "*"; 1685 1686 seq_printf(m, "%s:%s:mod:%s\n", system, event, iter->event_mod->module); 1687 1688 return 0; 1689 } 1690 #else /* CONFIG_MODULES */ 1691 static int s_show(struct seq_file *m, void *v) 1692 { 1693 struct set_event_iter *iter = v; 1694 1695 return t_show(m, iter->file); 1696 } 1697 #endif 1698 1699 static void s_stop(struct seq_file *m, void *v) 1700 { 1701 kfree(v); 1702 t_stop(m, NULL); 1703 } 1704 1705 static void * 1706 __next(struct seq_file *m, void *v, loff_t *pos, int type) 1707 { 1708 struct trace_array *tr = m->private; 1709 struct trace_pid_list *pid_list; 1710 1711 if (type == TRACE_PIDS) 1712 pid_list = rcu_dereference_sched(tr->filtered_pids); 1713 else 1714 pid_list = rcu_dereference_sched(tr->filtered_no_pids); 1715 1716 return trace_pid_next(pid_list, v, pos); 1717 } 1718 1719 static void * 1720 p_next(struct seq_file *m, void *v, loff_t *pos) 1721 { 1722 return __next(m, v, pos, TRACE_PIDS); 1723 } 1724 1725 static void * 1726 np_next(struct seq_file *m, void *v, loff_t *pos) 1727 { 1728 return __next(m, v, pos, TRACE_NO_PIDS); 1729 } 1730 1731 static void *__start(struct seq_file *m, loff_t *pos, int type) 1732 __acquires(RCU) 1733 { 1734 struct trace_pid_list *pid_list; 1735 struct trace_array *tr = m->private; 1736 1737 /* 1738 * Grab the mutex, to keep calls to p_next() having the same 1739 * tr->filtered_pids as p_start() has. 1740 * If we just passed the tr->filtered_pids around, then RCU would 1741 * have been enough, but doing that makes things more complex. 1742 */ 1743 mutex_lock(&event_mutex); 1744 rcu_read_lock_sched(); 1745 1746 if (type == TRACE_PIDS) 1747 pid_list = rcu_dereference_sched(tr->filtered_pids); 1748 else 1749 pid_list = rcu_dereference_sched(tr->filtered_no_pids); 1750 1751 if (!pid_list) 1752 return NULL; 1753 1754 return trace_pid_start(pid_list, pos); 1755 } 1756 1757 static void *p_start(struct seq_file *m, loff_t *pos) 1758 __acquires(RCU) 1759 { 1760 return __start(m, pos, TRACE_PIDS); 1761 } 1762 1763 static void *np_start(struct seq_file *m, loff_t *pos) 1764 __acquires(RCU) 1765 { 1766 return __start(m, pos, TRACE_NO_PIDS); 1767 } 1768 1769 static void p_stop(struct seq_file *m, void *p) 1770 __releases(RCU) 1771 { 1772 rcu_read_unlock_sched(); 1773 mutex_unlock(&event_mutex); 1774 } 1775 1776 static ssize_t 1777 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt, 1778 loff_t *ppos) 1779 { 1780 struct trace_event_file *file; 1781 unsigned long flags; 1782 char buf[4] = "0"; 1783 1784 mutex_lock(&event_mutex); 1785 file = event_file_file(filp); 1786 if (likely(file)) 1787 flags = file->flags; 1788 mutex_unlock(&event_mutex); 1789 1790 if (!file) 1791 return -ENODEV; 1792 1793 if (flags & EVENT_FILE_FL_ENABLED && 1794 !(flags & EVENT_FILE_FL_SOFT_DISABLED)) 1795 strcpy(buf, "1"); 1796 1797 if (atomic_read(&file->sm_ref) != 0) 1798 strcat(buf, "*"); 1799 1800 strcat(buf, "\n"); 1801 1802 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf)); 1803 } 1804 1805 static ssize_t 1806 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt, 1807 loff_t *ppos) 1808 { 1809 struct trace_event_file *file; 1810 unsigned long val; 1811 int ret; 1812 1813 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 1814 if (ret) 1815 return ret; 1816 1817 guard(mutex)(&event_mutex); 1818 1819 switch (val) { 1820 case 0: 1821 case 1: 1822 file = event_file_file(filp); 1823 if (!file) 1824 return -ENODEV; 1825 ret = tracing_update_buffers(file->tr); 1826 if (ret < 0) 1827 return ret; 1828 ret = ftrace_event_enable_disable(file, val); 1829 if (ret < 0) 1830 return ret; 1831 break; 1832 1833 default: 1834 return -EINVAL; 1835 } 1836 1837 *ppos += cnt; 1838 1839 return cnt; 1840 } 1841 1842 /* 1843 * Returns: 1844 * 0 : no events exist? 1845 * 1 : all events are disabled 1846 * 2 : all events are enabled 1847 * 3 : some events are enabled and some are enabled 1848 */ 1849 int trace_events_enabled(struct trace_array *tr, const char *system) 1850 { 1851 struct trace_event_call *call; 1852 struct trace_event_file *file; 1853 int set = 0; 1854 1855 guard(mutex)(&event_mutex); 1856 1857 list_for_each_entry(file, &tr->events, list) { 1858 call = file->event_call; 1859 if ((call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) || 1860 !trace_event_name(call) || !call->class || !call->class->reg) 1861 continue; 1862 1863 if (system && strcmp(call->class->system, system) != 0) 1864 continue; 1865 1866 /* 1867 * We need to find out if all the events are set 1868 * or if all events or cleared, or if we have 1869 * a mixture. 1870 */ 1871 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED)); 1872 1873 /* 1874 * If we have a mixture, no need to look further. 1875 */ 1876 if (set == 3) 1877 break; 1878 } 1879 1880 return set; 1881 } 1882 1883 static ssize_t 1884 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt, 1885 loff_t *ppos) 1886 { 1887 const char set_to_char[4] = { '?', '0', '1', 'X' }; 1888 struct trace_subsystem_dir *dir = filp->private_data; 1889 struct event_subsystem *system = dir->subsystem; 1890 struct trace_array *tr = dir->tr; 1891 char buf[2]; 1892 int set; 1893 int ret; 1894 1895 set = trace_events_enabled(tr, system ? system->name : NULL); 1896 1897 buf[0] = set_to_char[set]; 1898 buf[1] = '\n'; 1899 1900 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); 1901 1902 return ret; 1903 } 1904 1905 static ssize_t 1906 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt, 1907 loff_t *ppos) 1908 { 1909 struct trace_subsystem_dir *dir = filp->private_data; 1910 struct event_subsystem *system = dir->subsystem; 1911 const char *name = NULL; 1912 unsigned long val; 1913 ssize_t ret; 1914 1915 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 1916 if (ret) 1917 return ret; 1918 1919 ret = tracing_update_buffers(dir->tr); 1920 if (ret < 0) 1921 return ret; 1922 1923 if (val != 0 && val != 1) 1924 return -EINVAL; 1925 1926 /* 1927 * Opening of "enable" adds a ref count to system, 1928 * so the name is safe to use. 1929 */ 1930 if (system) 1931 name = system->name; 1932 1933 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val, NULL); 1934 if (ret) 1935 goto out; 1936 1937 ret = cnt; 1938 1939 out: 1940 *ppos += cnt; 1941 1942 return ret; 1943 } 1944 1945 enum { 1946 FORMAT_HEADER = 1, 1947 FORMAT_FIELD_SEPERATOR = 2, 1948 FORMAT_PRINTFMT = 3, 1949 }; 1950 1951 static void *f_next(struct seq_file *m, void *v, loff_t *pos) 1952 { 1953 struct trace_event_file *file = event_file_data(m->private); 1954 struct trace_event_call *call = file->event_call; 1955 struct list_head *common_head = &ftrace_common_fields; 1956 struct list_head *head = trace_get_fields(call); 1957 struct list_head *node = v; 1958 1959 (*pos)++; 1960 1961 switch ((unsigned long)v) { 1962 case FORMAT_HEADER: 1963 node = common_head; 1964 break; 1965 1966 case FORMAT_FIELD_SEPERATOR: 1967 node = head; 1968 break; 1969 1970 case FORMAT_PRINTFMT: 1971 /* all done */ 1972 return NULL; 1973 } 1974 1975 node = node->prev; 1976 if (node == common_head) 1977 return (void *)FORMAT_FIELD_SEPERATOR; 1978 else if (node == head) 1979 return (void *)FORMAT_PRINTFMT; 1980 else 1981 return node; 1982 } 1983 1984 static int f_show(struct seq_file *m, void *v) 1985 { 1986 struct trace_event_file *file = event_file_data(m->private); 1987 struct trace_event_call *call = file->event_call; 1988 struct ftrace_event_field *field; 1989 const char *array_descriptor; 1990 1991 switch ((unsigned long)v) { 1992 case FORMAT_HEADER: 1993 seq_printf(m, "name: %s\n", trace_event_name(call)); 1994 seq_printf(m, "ID: %d\n", call->event.type); 1995 seq_puts(m, "format:\n"); 1996 return 0; 1997 1998 case FORMAT_FIELD_SEPERATOR: 1999 seq_putc(m, '\n'); 2000 return 0; 2001 2002 case FORMAT_PRINTFMT: 2003 seq_printf(m, "\nprint fmt: %s\n", 2004 call->print_fmt); 2005 return 0; 2006 } 2007 2008 field = list_entry(v, struct ftrace_event_field, link); 2009 /* 2010 * Smartly shows the array type(except dynamic array). 2011 * Normal: 2012 * field:TYPE VAR 2013 * If TYPE := TYPE[LEN], it is shown: 2014 * field:TYPE VAR[LEN] 2015 */ 2016 array_descriptor = strchr(field->type, '['); 2017 2018 if (str_has_prefix(field->type, "__data_loc")) 2019 array_descriptor = NULL; 2020 2021 if (!array_descriptor) 2022 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n", 2023 field->type, field->name, field->offset, 2024 field->size, !!field->is_signed); 2025 else if (field->len) 2026 seq_printf(m, "\tfield:%.*s %s[%d];\toffset:%u;\tsize:%u;\tsigned:%d;\n", 2027 (int)(array_descriptor - field->type), 2028 field->type, field->name, 2029 field->len, field->offset, 2030 field->size, !!field->is_signed); 2031 else 2032 seq_printf(m, "\tfield:%.*s %s[];\toffset:%u;\tsize:%u;\tsigned:%d;\n", 2033 (int)(array_descriptor - field->type), 2034 field->type, field->name, 2035 field->offset, field->size, !!field->is_signed); 2036 2037 return 0; 2038 } 2039 2040 static void *f_start(struct seq_file *m, loff_t *pos) 2041 { 2042 struct trace_event_file *file; 2043 void *p = (void *)FORMAT_HEADER; 2044 loff_t l = 0; 2045 2046 /* ->stop() is called even if ->start() fails */ 2047 mutex_lock(&event_mutex); 2048 file = event_file_file(m->private); 2049 if (!file) 2050 return ERR_PTR(-ENODEV); 2051 2052 while (l < *pos && p) 2053 p = f_next(m, p, &l); 2054 2055 return p; 2056 } 2057 2058 static void f_stop(struct seq_file *m, void *p) 2059 { 2060 mutex_unlock(&event_mutex); 2061 } 2062 2063 static const struct seq_operations trace_format_seq_ops = { 2064 .start = f_start, 2065 .next = f_next, 2066 .stop = f_stop, 2067 .show = f_show, 2068 }; 2069 2070 static int trace_format_open(struct inode *inode, struct file *file) 2071 { 2072 struct seq_file *m; 2073 int ret; 2074 2075 /* Do we want to hide event format files on tracefs lockdown? */ 2076 2077 ret = seq_open(file, &trace_format_seq_ops); 2078 if (ret < 0) 2079 return ret; 2080 2081 m = file->private_data; 2082 m->private = file; 2083 2084 return 0; 2085 } 2086 2087 #ifdef CONFIG_PERF_EVENTS 2088 static ssize_t 2089 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) 2090 { 2091 int id = (long)event_file_data(filp); 2092 char buf[32]; 2093 int len; 2094 2095 if (unlikely(!id)) 2096 return -ENODEV; 2097 2098 len = sprintf(buf, "%d\n", id); 2099 2100 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len); 2101 } 2102 #endif 2103 2104 static ssize_t 2105 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt, 2106 loff_t *ppos) 2107 { 2108 struct trace_event_file *file; 2109 struct trace_seq *s; 2110 int r = -ENODEV; 2111 2112 if (*ppos) 2113 return 0; 2114 2115 s = kmalloc(sizeof(*s), GFP_KERNEL); 2116 2117 if (!s) 2118 return -ENOMEM; 2119 2120 trace_seq_init(s); 2121 2122 mutex_lock(&event_mutex); 2123 file = event_file_file(filp); 2124 if (file) 2125 print_event_filter(file, s); 2126 mutex_unlock(&event_mutex); 2127 2128 if (file) 2129 r = simple_read_from_buffer(ubuf, cnt, ppos, 2130 s->buffer, trace_seq_used(s)); 2131 2132 kfree(s); 2133 2134 return r; 2135 } 2136 2137 static ssize_t 2138 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt, 2139 loff_t *ppos) 2140 { 2141 struct trace_event_file *file; 2142 char *buf; 2143 int err = -ENODEV; 2144 2145 if (cnt >= PAGE_SIZE) 2146 return -EINVAL; 2147 2148 buf = memdup_user_nul(ubuf, cnt); 2149 if (IS_ERR(buf)) 2150 return PTR_ERR(buf); 2151 2152 mutex_lock(&event_mutex); 2153 file = event_file_file(filp); 2154 if (file) { 2155 if (file->flags & EVENT_FILE_FL_FREED) 2156 err = -ENODEV; 2157 else 2158 err = apply_event_filter(file, buf); 2159 } 2160 mutex_unlock(&event_mutex); 2161 2162 kfree(buf); 2163 if (err < 0) 2164 return err; 2165 2166 *ppos += cnt; 2167 2168 return cnt; 2169 } 2170 2171 static LIST_HEAD(event_subsystems); 2172 2173 static int subsystem_open(struct inode *inode, struct file *filp) 2174 { 2175 struct trace_subsystem_dir *dir = NULL, *iter_dir; 2176 struct trace_array *tr = NULL, *iter_tr; 2177 struct event_subsystem *system = NULL; 2178 int ret; 2179 2180 if (tracing_is_disabled()) 2181 return -ENODEV; 2182 2183 /* Make sure the system still exists */ 2184 mutex_lock(&event_mutex); 2185 mutex_lock(&trace_types_lock); 2186 list_for_each_entry(iter_tr, &ftrace_trace_arrays, list) { 2187 list_for_each_entry(iter_dir, &iter_tr->systems, list) { 2188 if (iter_dir == inode->i_private) { 2189 /* Don't open systems with no events */ 2190 tr = iter_tr; 2191 dir = iter_dir; 2192 if (dir->nr_events) { 2193 __get_system_dir(dir); 2194 system = dir->subsystem; 2195 } 2196 goto exit_loop; 2197 } 2198 } 2199 } 2200 exit_loop: 2201 mutex_unlock(&trace_types_lock); 2202 mutex_unlock(&event_mutex); 2203 2204 if (!system) 2205 return -ENODEV; 2206 2207 /* Still need to increment the ref count of the system */ 2208 if (trace_array_get(tr) < 0) { 2209 put_system(dir); 2210 return -ENODEV; 2211 } 2212 2213 ret = tracing_open_generic(inode, filp); 2214 if (ret < 0) { 2215 trace_array_put(tr); 2216 put_system(dir); 2217 } 2218 2219 return ret; 2220 } 2221 2222 static int system_tr_open(struct inode *inode, struct file *filp) 2223 { 2224 struct trace_subsystem_dir *dir; 2225 struct trace_array *tr = inode->i_private; 2226 int ret; 2227 2228 /* Make a temporary dir that has no system but points to tr */ 2229 dir = kzalloc(sizeof(*dir), GFP_KERNEL); 2230 if (!dir) 2231 return -ENOMEM; 2232 2233 ret = tracing_open_generic_tr(inode, filp); 2234 if (ret < 0) { 2235 kfree(dir); 2236 return ret; 2237 } 2238 dir->tr = tr; 2239 filp->private_data = dir; 2240 2241 return 0; 2242 } 2243 2244 static int subsystem_release(struct inode *inode, struct file *file) 2245 { 2246 struct trace_subsystem_dir *dir = file->private_data; 2247 2248 trace_array_put(dir->tr); 2249 2250 /* 2251 * If dir->subsystem is NULL, then this is a temporary 2252 * descriptor that was made for a trace_array to enable 2253 * all subsystems. 2254 */ 2255 if (dir->subsystem) 2256 put_system(dir); 2257 else 2258 kfree(dir); 2259 2260 return 0; 2261 } 2262 2263 static ssize_t 2264 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt, 2265 loff_t *ppos) 2266 { 2267 struct trace_subsystem_dir *dir = filp->private_data; 2268 struct event_subsystem *system = dir->subsystem; 2269 struct trace_seq *s; 2270 int r; 2271 2272 if (*ppos) 2273 return 0; 2274 2275 s = kmalloc(sizeof(*s), GFP_KERNEL); 2276 if (!s) 2277 return -ENOMEM; 2278 2279 trace_seq_init(s); 2280 2281 print_subsystem_event_filter(system, s); 2282 r = simple_read_from_buffer(ubuf, cnt, ppos, 2283 s->buffer, trace_seq_used(s)); 2284 2285 kfree(s); 2286 2287 return r; 2288 } 2289 2290 static ssize_t 2291 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt, 2292 loff_t *ppos) 2293 { 2294 struct trace_subsystem_dir *dir = filp->private_data; 2295 char *buf; 2296 int err; 2297 2298 if (cnt >= PAGE_SIZE) 2299 return -EINVAL; 2300 2301 buf = memdup_user_nul(ubuf, cnt); 2302 if (IS_ERR(buf)) 2303 return PTR_ERR(buf); 2304 2305 err = apply_subsystem_event_filter(dir, buf); 2306 kfree(buf); 2307 if (err < 0) 2308 return err; 2309 2310 *ppos += cnt; 2311 2312 return cnt; 2313 } 2314 2315 static ssize_t 2316 show_header_page_file(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) 2317 { 2318 struct trace_array *tr = filp->private_data; 2319 struct trace_seq *s; 2320 int r; 2321 2322 if (*ppos) 2323 return 0; 2324 2325 s = kmalloc(sizeof(*s), GFP_KERNEL); 2326 if (!s) 2327 return -ENOMEM; 2328 2329 trace_seq_init(s); 2330 2331 ring_buffer_print_page_header(tr->array_buffer.buffer, s); 2332 r = simple_read_from_buffer(ubuf, cnt, ppos, 2333 s->buffer, trace_seq_used(s)); 2334 2335 kfree(s); 2336 2337 return r; 2338 } 2339 2340 static ssize_t 2341 show_header_event_file(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) 2342 { 2343 struct trace_seq *s; 2344 int r; 2345 2346 if (*ppos) 2347 return 0; 2348 2349 s = kmalloc(sizeof(*s), GFP_KERNEL); 2350 if (!s) 2351 return -ENOMEM; 2352 2353 trace_seq_init(s); 2354 2355 ring_buffer_print_entry_header(s); 2356 r = simple_read_from_buffer(ubuf, cnt, ppos, 2357 s->buffer, trace_seq_used(s)); 2358 2359 kfree(s); 2360 2361 return r; 2362 } 2363 2364 static void ignore_task_cpu(void *data) 2365 { 2366 struct trace_array *tr = data; 2367 struct trace_pid_list *pid_list; 2368 struct trace_pid_list *no_pid_list; 2369 2370 /* 2371 * This function is called by on_each_cpu() while the 2372 * event_mutex is held. 2373 */ 2374 pid_list = rcu_dereference_protected(tr->filtered_pids, 2375 mutex_is_locked(&event_mutex)); 2376 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids, 2377 mutex_is_locked(&event_mutex)); 2378 2379 this_cpu_write(tr->array_buffer.data->ignore_pid, 2380 trace_ignore_this_task(pid_list, no_pid_list, current)); 2381 } 2382 2383 static void register_pid_events(struct trace_array *tr) 2384 { 2385 /* 2386 * Register a probe that is called before all other probes 2387 * to set ignore_pid if next or prev do not match. 2388 * Register a probe this is called after all other probes 2389 * to only keep ignore_pid set if next pid matches. 2390 */ 2391 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre, 2392 tr, INT_MAX); 2393 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post, 2394 tr, 0); 2395 2396 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, 2397 tr, INT_MAX); 2398 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, 2399 tr, 0); 2400 2401 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, 2402 tr, INT_MAX); 2403 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, 2404 tr, 0); 2405 2406 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre, 2407 tr, INT_MAX); 2408 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post, 2409 tr, 0); 2410 } 2411 2412 static ssize_t 2413 event_pid_write(struct file *filp, const char __user *ubuf, 2414 size_t cnt, loff_t *ppos, int type) 2415 { 2416 struct seq_file *m = filp->private_data; 2417 struct trace_array *tr = m->private; 2418 struct trace_pid_list *filtered_pids = NULL; 2419 struct trace_pid_list *other_pids = NULL; 2420 struct trace_pid_list *pid_list; 2421 struct trace_event_file *file; 2422 ssize_t ret; 2423 2424 if (!cnt) 2425 return 0; 2426 2427 ret = tracing_update_buffers(tr); 2428 if (ret < 0) 2429 return ret; 2430 2431 guard(mutex)(&event_mutex); 2432 2433 if (type == TRACE_PIDS) { 2434 filtered_pids = rcu_dereference_protected(tr->filtered_pids, 2435 lockdep_is_held(&event_mutex)); 2436 other_pids = rcu_dereference_protected(tr->filtered_no_pids, 2437 lockdep_is_held(&event_mutex)); 2438 } else { 2439 filtered_pids = rcu_dereference_protected(tr->filtered_no_pids, 2440 lockdep_is_held(&event_mutex)); 2441 other_pids = rcu_dereference_protected(tr->filtered_pids, 2442 lockdep_is_held(&event_mutex)); 2443 } 2444 2445 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt); 2446 if (ret < 0) 2447 return ret; 2448 2449 if (type == TRACE_PIDS) 2450 rcu_assign_pointer(tr->filtered_pids, pid_list); 2451 else 2452 rcu_assign_pointer(tr->filtered_no_pids, pid_list); 2453 2454 list_for_each_entry(file, &tr->events, list) { 2455 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags); 2456 } 2457 2458 if (filtered_pids) { 2459 tracepoint_synchronize_unregister(); 2460 trace_pid_list_free(filtered_pids); 2461 } else if (pid_list && !other_pids) { 2462 register_pid_events(tr); 2463 } 2464 2465 /* 2466 * Ignoring of pids is done at task switch. But we have to 2467 * check for those tasks that are currently running. 2468 * Always do this in case a pid was appended or removed. 2469 */ 2470 on_each_cpu(ignore_task_cpu, tr, 1); 2471 2472 *ppos += ret; 2473 2474 return ret; 2475 } 2476 2477 static ssize_t 2478 ftrace_event_pid_write(struct file *filp, const char __user *ubuf, 2479 size_t cnt, loff_t *ppos) 2480 { 2481 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS); 2482 } 2483 2484 static ssize_t 2485 ftrace_event_npid_write(struct file *filp, const char __user *ubuf, 2486 size_t cnt, loff_t *ppos) 2487 { 2488 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS); 2489 } 2490 2491 static int ftrace_event_avail_open(struct inode *inode, struct file *file); 2492 static int ftrace_event_set_open(struct inode *inode, struct file *file); 2493 static int ftrace_event_set_pid_open(struct inode *inode, struct file *file); 2494 static int ftrace_event_set_npid_open(struct inode *inode, struct file *file); 2495 static int ftrace_event_release(struct inode *inode, struct file *file); 2496 2497 static const struct seq_operations show_event_seq_ops = { 2498 .start = t_start, 2499 .next = t_next, 2500 .show = t_show, 2501 .stop = t_stop, 2502 }; 2503 2504 static const struct seq_operations show_set_event_seq_ops = { 2505 .start = s_start, 2506 .next = s_next, 2507 .show = s_show, 2508 .stop = s_stop, 2509 }; 2510 2511 static const struct seq_operations show_set_pid_seq_ops = { 2512 .start = p_start, 2513 .next = p_next, 2514 .show = trace_pid_show, 2515 .stop = p_stop, 2516 }; 2517 2518 static const struct seq_operations show_set_no_pid_seq_ops = { 2519 .start = np_start, 2520 .next = np_next, 2521 .show = trace_pid_show, 2522 .stop = p_stop, 2523 }; 2524 2525 static const struct file_operations ftrace_avail_fops = { 2526 .open = ftrace_event_avail_open, 2527 .read = seq_read, 2528 .llseek = seq_lseek, 2529 .release = seq_release, 2530 }; 2531 2532 static const struct file_operations ftrace_set_event_fops = { 2533 .open = ftrace_event_set_open, 2534 .read = seq_read, 2535 .write = ftrace_event_write, 2536 .llseek = seq_lseek, 2537 .release = ftrace_event_release, 2538 }; 2539 2540 static const struct file_operations ftrace_set_event_pid_fops = { 2541 .open = ftrace_event_set_pid_open, 2542 .read = seq_read, 2543 .write = ftrace_event_pid_write, 2544 .llseek = seq_lseek, 2545 .release = ftrace_event_release, 2546 }; 2547 2548 static const struct file_operations ftrace_set_event_notrace_pid_fops = { 2549 .open = ftrace_event_set_npid_open, 2550 .read = seq_read, 2551 .write = ftrace_event_npid_write, 2552 .llseek = seq_lseek, 2553 .release = ftrace_event_release, 2554 }; 2555 2556 static const struct file_operations ftrace_enable_fops = { 2557 .open = tracing_open_file_tr, 2558 .read = event_enable_read, 2559 .write = event_enable_write, 2560 .release = tracing_release_file_tr, 2561 .llseek = default_llseek, 2562 }; 2563 2564 static const struct file_operations ftrace_event_format_fops = { 2565 .open = trace_format_open, 2566 .read = seq_read, 2567 .llseek = seq_lseek, 2568 .release = seq_release, 2569 }; 2570 2571 #ifdef CONFIG_PERF_EVENTS 2572 static const struct file_operations ftrace_event_id_fops = { 2573 .read = event_id_read, 2574 .llseek = default_llseek, 2575 }; 2576 #endif 2577 2578 static const struct file_operations ftrace_event_filter_fops = { 2579 .open = tracing_open_file_tr, 2580 .read = event_filter_read, 2581 .write = event_filter_write, 2582 .release = tracing_release_file_tr, 2583 .llseek = default_llseek, 2584 }; 2585 2586 static const struct file_operations ftrace_subsystem_filter_fops = { 2587 .open = subsystem_open, 2588 .read = subsystem_filter_read, 2589 .write = subsystem_filter_write, 2590 .llseek = default_llseek, 2591 .release = subsystem_release, 2592 }; 2593 2594 static const struct file_operations ftrace_system_enable_fops = { 2595 .open = subsystem_open, 2596 .read = system_enable_read, 2597 .write = system_enable_write, 2598 .llseek = default_llseek, 2599 .release = subsystem_release, 2600 }; 2601 2602 static const struct file_operations ftrace_tr_enable_fops = { 2603 .open = system_tr_open, 2604 .read = system_enable_read, 2605 .write = system_enable_write, 2606 .llseek = default_llseek, 2607 .release = subsystem_release, 2608 }; 2609 2610 static const struct file_operations ftrace_show_header_page_fops = { 2611 .open = tracing_open_generic_tr, 2612 .read = show_header_page_file, 2613 .llseek = default_llseek, 2614 .release = tracing_release_generic_tr, 2615 }; 2616 2617 static const struct file_operations ftrace_show_header_event_fops = { 2618 .open = tracing_open_generic_tr, 2619 .read = show_header_event_file, 2620 .llseek = default_llseek, 2621 .release = tracing_release_generic_tr, 2622 }; 2623 2624 static int 2625 ftrace_event_open(struct inode *inode, struct file *file, 2626 const struct seq_operations *seq_ops) 2627 { 2628 struct seq_file *m; 2629 int ret; 2630 2631 ret = security_locked_down(LOCKDOWN_TRACEFS); 2632 if (ret) 2633 return ret; 2634 2635 ret = seq_open(file, seq_ops); 2636 if (ret < 0) 2637 return ret; 2638 m = file->private_data; 2639 /* copy tr over to seq ops */ 2640 m->private = inode->i_private; 2641 2642 return ret; 2643 } 2644 2645 static int ftrace_event_release(struct inode *inode, struct file *file) 2646 { 2647 struct trace_array *tr = inode->i_private; 2648 2649 trace_array_put(tr); 2650 2651 return seq_release(inode, file); 2652 } 2653 2654 static int 2655 ftrace_event_avail_open(struct inode *inode, struct file *file) 2656 { 2657 const struct seq_operations *seq_ops = &show_event_seq_ops; 2658 2659 /* Checks for tracefs lockdown */ 2660 return ftrace_event_open(inode, file, seq_ops); 2661 } 2662 2663 static int 2664 ftrace_event_set_open(struct inode *inode, struct file *file) 2665 { 2666 const struct seq_operations *seq_ops = &show_set_event_seq_ops; 2667 struct trace_array *tr = inode->i_private; 2668 int ret; 2669 2670 ret = tracing_check_open_get_tr(tr); 2671 if (ret) 2672 return ret; 2673 2674 if ((file->f_mode & FMODE_WRITE) && 2675 (file->f_flags & O_TRUNC)) 2676 ftrace_clear_events(tr); 2677 2678 ret = ftrace_event_open(inode, file, seq_ops); 2679 if (ret < 0) 2680 trace_array_put(tr); 2681 return ret; 2682 } 2683 2684 static int 2685 ftrace_event_set_pid_open(struct inode *inode, struct file *file) 2686 { 2687 const struct seq_operations *seq_ops = &show_set_pid_seq_ops; 2688 struct trace_array *tr = inode->i_private; 2689 int ret; 2690 2691 ret = tracing_check_open_get_tr(tr); 2692 if (ret) 2693 return ret; 2694 2695 if ((file->f_mode & FMODE_WRITE) && 2696 (file->f_flags & O_TRUNC)) 2697 ftrace_clear_event_pids(tr, TRACE_PIDS); 2698 2699 ret = ftrace_event_open(inode, file, seq_ops); 2700 if (ret < 0) 2701 trace_array_put(tr); 2702 return ret; 2703 } 2704 2705 static int 2706 ftrace_event_set_npid_open(struct inode *inode, struct file *file) 2707 { 2708 const struct seq_operations *seq_ops = &show_set_no_pid_seq_ops; 2709 struct trace_array *tr = inode->i_private; 2710 int ret; 2711 2712 ret = tracing_check_open_get_tr(tr); 2713 if (ret) 2714 return ret; 2715 2716 if ((file->f_mode & FMODE_WRITE) && 2717 (file->f_flags & O_TRUNC)) 2718 ftrace_clear_event_pids(tr, TRACE_NO_PIDS); 2719 2720 ret = ftrace_event_open(inode, file, seq_ops); 2721 if (ret < 0) 2722 trace_array_put(tr); 2723 return ret; 2724 } 2725 2726 static struct event_subsystem * 2727 create_new_subsystem(const char *name) 2728 { 2729 struct event_subsystem *system; 2730 2731 /* need to create new entry */ 2732 system = kmalloc(sizeof(*system), GFP_KERNEL); 2733 if (!system) 2734 return NULL; 2735 2736 system->ref_count = 1; 2737 2738 /* Only allocate if dynamic (kprobes and modules) */ 2739 system->name = kstrdup_const(name, GFP_KERNEL); 2740 if (!system->name) 2741 goto out_free; 2742 2743 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL); 2744 if (!system->filter) 2745 goto out_free; 2746 2747 list_add(&system->list, &event_subsystems); 2748 2749 return system; 2750 2751 out_free: 2752 kfree_const(system->name); 2753 kfree(system); 2754 return NULL; 2755 } 2756 2757 static int system_callback(const char *name, umode_t *mode, void **data, 2758 const struct file_operations **fops) 2759 { 2760 if (strcmp(name, "filter") == 0) 2761 *fops = &ftrace_subsystem_filter_fops; 2762 2763 else if (strcmp(name, "enable") == 0) 2764 *fops = &ftrace_system_enable_fops; 2765 2766 else 2767 return 0; 2768 2769 *mode = TRACE_MODE_WRITE; 2770 return 1; 2771 } 2772 2773 static struct eventfs_inode * 2774 event_subsystem_dir(struct trace_array *tr, const char *name, 2775 struct trace_event_file *file, struct eventfs_inode *parent) 2776 { 2777 struct event_subsystem *system, *iter; 2778 struct trace_subsystem_dir *dir; 2779 struct eventfs_inode *ei; 2780 int nr_entries; 2781 static struct eventfs_entry system_entries[] = { 2782 { 2783 .name = "filter", 2784 .callback = system_callback, 2785 }, 2786 { 2787 .name = "enable", 2788 .callback = system_callback, 2789 } 2790 }; 2791 2792 /* First see if we did not already create this dir */ 2793 list_for_each_entry(dir, &tr->systems, list) { 2794 system = dir->subsystem; 2795 if (strcmp(system->name, name) == 0) { 2796 dir->nr_events++; 2797 file->system = dir; 2798 return dir->ei; 2799 } 2800 } 2801 2802 /* Now see if the system itself exists. */ 2803 system = NULL; 2804 list_for_each_entry(iter, &event_subsystems, list) { 2805 if (strcmp(iter->name, name) == 0) { 2806 system = iter; 2807 break; 2808 } 2809 } 2810 2811 dir = kmalloc(sizeof(*dir), GFP_KERNEL); 2812 if (!dir) 2813 goto out_fail; 2814 2815 if (!system) { 2816 system = create_new_subsystem(name); 2817 if (!system) 2818 goto out_free; 2819 } else 2820 __get_system(system); 2821 2822 /* ftrace only has directories no files */ 2823 if (strcmp(name, "ftrace") == 0) 2824 nr_entries = 0; 2825 else 2826 nr_entries = ARRAY_SIZE(system_entries); 2827 2828 ei = eventfs_create_dir(name, parent, system_entries, nr_entries, dir); 2829 if (IS_ERR(ei)) { 2830 pr_warn("Failed to create system directory %s\n", name); 2831 __put_system(system); 2832 goto out_free; 2833 } 2834 2835 dir->ei = ei; 2836 dir->tr = tr; 2837 dir->ref_count = 1; 2838 dir->nr_events = 1; 2839 dir->subsystem = system; 2840 file->system = dir; 2841 2842 list_add(&dir->list, &tr->systems); 2843 2844 return dir->ei; 2845 2846 out_free: 2847 kfree(dir); 2848 out_fail: 2849 /* Only print this message if failed on memory allocation */ 2850 if (!dir || !system) 2851 pr_warn("No memory to create event subsystem %s\n", name); 2852 return NULL; 2853 } 2854 2855 static int 2856 event_define_fields(struct trace_event_call *call) 2857 { 2858 struct list_head *head; 2859 int ret = 0; 2860 2861 /* 2862 * Other events may have the same class. Only update 2863 * the fields if they are not already defined. 2864 */ 2865 head = trace_get_fields(call); 2866 if (list_empty(head)) { 2867 struct trace_event_fields *field = call->class->fields_array; 2868 unsigned int offset = sizeof(struct trace_entry); 2869 2870 for (; field->type; field++) { 2871 if (field->type == TRACE_FUNCTION_TYPE) { 2872 field->define_fields(call); 2873 break; 2874 } 2875 2876 offset = ALIGN(offset, field->align); 2877 ret = trace_define_field_ext(call, field->type, field->name, 2878 offset, field->size, 2879 field->is_signed, field->filter_type, 2880 field->len, field->needs_test); 2881 if (WARN_ON_ONCE(ret)) { 2882 pr_err("error code is %d\n", ret); 2883 break; 2884 } 2885 2886 offset += field->size; 2887 } 2888 } 2889 2890 return ret; 2891 } 2892 2893 static int event_callback(const char *name, umode_t *mode, void **data, 2894 const struct file_operations **fops) 2895 { 2896 struct trace_event_file *file = *data; 2897 struct trace_event_call *call = file->event_call; 2898 2899 if (strcmp(name, "format") == 0) { 2900 *mode = TRACE_MODE_READ; 2901 *fops = &ftrace_event_format_fops; 2902 return 1; 2903 } 2904 2905 /* 2906 * Only event directories that can be enabled should have 2907 * triggers or filters, with the exception of the "print" 2908 * event that can have a "trigger" file. 2909 */ 2910 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) { 2911 if (call->class->reg && strcmp(name, "enable") == 0) { 2912 *mode = TRACE_MODE_WRITE; 2913 *fops = &ftrace_enable_fops; 2914 return 1; 2915 } 2916 2917 if (strcmp(name, "filter") == 0) { 2918 *mode = TRACE_MODE_WRITE; 2919 *fops = &ftrace_event_filter_fops; 2920 return 1; 2921 } 2922 } 2923 2924 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) || 2925 strcmp(trace_event_name(call), "print") == 0) { 2926 if (strcmp(name, "trigger") == 0) { 2927 *mode = TRACE_MODE_WRITE; 2928 *fops = &event_trigger_fops; 2929 return 1; 2930 } 2931 } 2932 2933 #ifdef CONFIG_PERF_EVENTS 2934 if (call->event.type && call->class->reg && 2935 strcmp(name, "id") == 0) { 2936 *mode = TRACE_MODE_READ; 2937 *data = (void *)(long)call->event.type; 2938 *fops = &ftrace_event_id_fops; 2939 return 1; 2940 } 2941 #endif 2942 2943 #ifdef CONFIG_HIST_TRIGGERS 2944 if (strcmp(name, "hist") == 0) { 2945 *mode = TRACE_MODE_READ; 2946 *fops = &event_hist_fops; 2947 return 1; 2948 } 2949 #endif 2950 #ifdef CONFIG_HIST_TRIGGERS_DEBUG 2951 if (strcmp(name, "hist_debug") == 0) { 2952 *mode = TRACE_MODE_READ; 2953 *fops = &event_hist_debug_fops; 2954 return 1; 2955 } 2956 #endif 2957 #ifdef CONFIG_TRACE_EVENT_INJECT 2958 if (call->event.type && call->class->reg && 2959 strcmp(name, "inject") == 0) { 2960 *mode = 0200; 2961 *fops = &event_inject_fops; 2962 return 1; 2963 } 2964 #endif 2965 return 0; 2966 } 2967 2968 /* The file is incremented on creation and freeing the enable file decrements it */ 2969 static void event_release(const char *name, void *data) 2970 { 2971 struct trace_event_file *file = data; 2972 2973 event_file_put(file); 2974 } 2975 2976 static int 2977 event_create_dir(struct eventfs_inode *parent, struct trace_event_file *file) 2978 { 2979 struct trace_event_call *call = file->event_call; 2980 struct trace_array *tr = file->tr; 2981 struct eventfs_inode *e_events; 2982 struct eventfs_inode *ei; 2983 const char *name; 2984 int nr_entries; 2985 int ret; 2986 static struct eventfs_entry event_entries[] = { 2987 { 2988 .name = "enable", 2989 .callback = event_callback, 2990 .release = event_release, 2991 }, 2992 { 2993 .name = "filter", 2994 .callback = event_callback, 2995 }, 2996 { 2997 .name = "trigger", 2998 .callback = event_callback, 2999 }, 3000 { 3001 .name = "format", 3002 .callback = event_callback, 3003 }, 3004 #ifdef CONFIG_PERF_EVENTS 3005 { 3006 .name = "id", 3007 .callback = event_callback, 3008 }, 3009 #endif 3010 #ifdef CONFIG_HIST_TRIGGERS 3011 { 3012 .name = "hist", 3013 .callback = event_callback, 3014 }, 3015 #endif 3016 #ifdef CONFIG_HIST_TRIGGERS_DEBUG 3017 { 3018 .name = "hist_debug", 3019 .callback = event_callback, 3020 }, 3021 #endif 3022 #ifdef CONFIG_TRACE_EVENT_INJECT 3023 { 3024 .name = "inject", 3025 .callback = event_callback, 3026 }, 3027 #endif 3028 }; 3029 3030 /* 3031 * If the trace point header did not define TRACE_SYSTEM 3032 * then the system would be called "TRACE_SYSTEM". This should 3033 * never happen. 3034 */ 3035 if (WARN_ON_ONCE(strcmp(call->class->system, TRACE_SYSTEM) == 0)) 3036 return -ENODEV; 3037 3038 e_events = event_subsystem_dir(tr, call->class->system, file, parent); 3039 if (!e_events) 3040 return -ENOMEM; 3041 3042 nr_entries = ARRAY_SIZE(event_entries); 3043 3044 name = trace_event_name(call); 3045 ei = eventfs_create_dir(name, e_events, event_entries, nr_entries, file); 3046 if (IS_ERR(ei)) { 3047 pr_warn("Could not create tracefs '%s' directory\n", name); 3048 return -1; 3049 } 3050 3051 file->ei = ei; 3052 3053 ret = event_define_fields(call); 3054 if (ret < 0) { 3055 pr_warn("Could not initialize trace point events/%s\n", name); 3056 return ret; 3057 } 3058 3059 /* Gets decremented on freeing of the "enable" file */ 3060 event_file_get(file); 3061 3062 return 0; 3063 } 3064 3065 static void remove_event_from_tracers(struct trace_event_call *call) 3066 { 3067 struct trace_event_file *file; 3068 struct trace_array *tr; 3069 3070 do_for_each_event_file_safe(tr, file) { 3071 if (file->event_call != call) 3072 continue; 3073 3074 remove_event_file_dir(file); 3075 /* 3076 * The do_for_each_event_file_safe() is 3077 * a double loop. After finding the call for this 3078 * trace_array, we use break to jump to the next 3079 * trace_array. 3080 */ 3081 break; 3082 } while_for_each_event_file(); 3083 } 3084 3085 static void event_remove(struct trace_event_call *call) 3086 { 3087 struct trace_array *tr; 3088 struct trace_event_file *file; 3089 3090 do_for_each_event_file(tr, file) { 3091 if (file->event_call != call) 3092 continue; 3093 3094 if (file->flags & EVENT_FILE_FL_WAS_ENABLED) 3095 tr->clear_trace = true; 3096 3097 ftrace_event_enable_disable(file, 0); 3098 /* 3099 * The do_for_each_event_file() is 3100 * a double loop. After finding the call for this 3101 * trace_array, we use break to jump to the next 3102 * trace_array. 3103 */ 3104 break; 3105 } while_for_each_event_file(); 3106 3107 if (call->event.funcs) 3108 __unregister_trace_event(&call->event); 3109 remove_event_from_tracers(call); 3110 list_del(&call->list); 3111 } 3112 3113 static int event_init(struct trace_event_call *call) 3114 { 3115 int ret = 0; 3116 const char *name; 3117 3118 name = trace_event_name(call); 3119 if (WARN_ON(!name)) 3120 return -EINVAL; 3121 3122 if (call->class->raw_init) { 3123 ret = call->class->raw_init(call); 3124 if (ret < 0 && ret != -ENOSYS) 3125 pr_warn("Could not initialize trace events/%s\n", name); 3126 } 3127 3128 return ret; 3129 } 3130 3131 static int 3132 __register_event(struct trace_event_call *call, struct module *mod) 3133 { 3134 int ret; 3135 3136 ret = event_init(call); 3137 if (ret < 0) 3138 return ret; 3139 3140 down_write(&trace_event_sem); 3141 list_add(&call->list, &ftrace_events); 3142 up_write(&trace_event_sem); 3143 3144 if (call->flags & TRACE_EVENT_FL_DYNAMIC) 3145 atomic_set(&call->refcnt, 0); 3146 else 3147 call->module = mod; 3148 3149 return 0; 3150 } 3151 3152 static char *eval_replace(char *ptr, struct trace_eval_map *map, int len) 3153 { 3154 int rlen; 3155 int elen; 3156 3157 /* Find the length of the eval value as a string */ 3158 elen = snprintf(ptr, 0, "%ld", map->eval_value); 3159 /* Make sure there's enough room to replace the string with the value */ 3160 if (len < elen) 3161 return NULL; 3162 3163 snprintf(ptr, elen + 1, "%ld", map->eval_value); 3164 3165 /* Get the rest of the string of ptr */ 3166 rlen = strlen(ptr + len); 3167 memmove(ptr + elen, ptr + len, rlen); 3168 /* Make sure we end the new string */ 3169 ptr[elen + rlen] = 0; 3170 3171 return ptr + elen; 3172 } 3173 3174 static void update_event_printk(struct trace_event_call *call, 3175 struct trace_eval_map *map) 3176 { 3177 char *ptr; 3178 int quote = 0; 3179 int len = strlen(map->eval_string); 3180 3181 for (ptr = call->print_fmt; *ptr; ptr++) { 3182 if (*ptr == '\\') { 3183 ptr++; 3184 /* paranoid */ 3185 if (!*ptr) 3186 break; 3187 continue; 3188 } 3189 if (*ptr == '"') { 3190 quote ^= 1; 3191 continue; 3192 } 3193 if (quote) 3194 continue; 3195 if (isdigit(*ptr)) { 3196 /* skip numbers */ 3197 do { 3198 ptr++; 3199 /* Check for alpha chars like ULL */ 3200 } while (isalnum(*ptr)); 3201 if (!*ptr) 3202 break; 3203 /* 3204 * A number must have some kind of delimiter after 3205 * it, and we can ignore that too. 3206 */ 3207 continue; 3208 } 3209 if (isalpha(*ptr) || *ptr == '_') { 3210 if (strncmp(map->eval_string, ptr, len) == 0 && 3211 !isalnum(ptr[len]) && ptr[len] != '_') { 3212 ptr = eval_replace(ptr, map, len); 3213 /* enum/sizeof string smaller than value */ 3214 if (WARN_ON_ONCE(!ptr)) 3215 return; 3216 /* 3217 * No need to decrement here, as eval_replace() 3218 * returns the pointer to the character passed 3219 * the eval, and two evals can not be placed 3220 * back to back without something in between. 3221 * We can skip that something in between. 3222 */ 3223 continue; 3224 } 3225 skip_more: 3226 do { 3227 ptr++; 3228 } while (isalnum(*ptr) || *ptr == '_'); 3229 if (!*ptr) 3230 break; 3231 /* 3232 * If what comes after this variable is a '.' or 3233 * '->' then we can continue to ignore that string. 3234 */ 3235 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) { 3236 ptr += *ptr == '.' ? 1 : 2; 3237 if (!*ptr) 3238 break; 3239 goto skip_more; 3240 } 3241 /* 3242 * Once again, we can skip the delimiter that came 3243 * after the string. 3244 */ 3245 continue; 3246 } 3247 } 3248 } 3249 3250 static void add_str_to_module(struct module *module, char *str) 3251 { 3252 struct module_string *modstr; 3253 3254 modstr = kmalloc(sizeof(*modstr), GFP_KERNEL); 3255 3256 /* 3257 * If we failed to allocate memory here, then we'll just 3258 * let the str memory leak when the module is removed. 3259 * If this fails to allocate, there's worse problems than 3260 * a leaked string on module removal. 3261 */ 3262 if (WARN_ON_ONCE(!modstr)) 3263 return; 3264 3265 modstr->module = module; 3266 modstr->str = str; 3267 3268 list_add(&modstr->next, &module_strings); 3269 } 3270 3271 #define ATTRIBUTE_STR "__attribute__(" 3272 #define ATTRIBUTE_STR_LEN (sizeof(ATTRIBUTE_STR) - 1) 3273 3274 /* Remove all __attribute__() from @type. Return allocated string or @type. */ 3275 static char *sanitize_field_type(const char *type) 3276 { 3277 char *attr, *tmp, *next, *ret = (char *)type; 3278 int depth; 3279 3280 next = (char *)type; 3281 while ((attr = strstr(next, ATTRIBUTE_STR))) { 3282 /* Retry if "__attribute__(" is a part of another word. */ 3283 if (attr != next && !isspace(attr[-1])) { 3284 next = attr + ATTRIBUTE_STR_LEN; 3285 continue; 3286 } 3287 3288 if (ret == type) { 3289 ret = kstrdup(type, GFP_KERNEL); 3290 if (WARN_ON_ONCE(!ret)) 3291 return NULL; 3292 attr = ret + (attr - type); 3293 } 3294 3295 /* the ATTRIBUTE_STR already has the first '(' */ 3296 depth = 1; 3297 next = attr + ATTRIBUTE_STR_LEN; 3298 do { 3299 tmp = strpbrk(next, "()"); 3300 /* There is unbalanced parentheses */ 3301 if (WARN_ON_ONCE(!tmp)) { 3302 kfree(ret); 3303 return (char *)type; 3304 } 3305 3306 if (*tmp == '(') 3307 depth++; 3308 else 3309 depth--; 3310 next = tmp + 1; 3311 } while (depth > 0); 3312 next = skip_spaces(next); 3313 strcpy(attr, next); 3314 next = attr; 3315 } 3316 return ret; 3317 } 3318 3319 static char *find_replacable_eval(const char *type, const char *eval_string, 3320 int len) 3321 { 3322 char *ptr; 3323 3324 if (!eval_string) 3325 return NULL; 3326 3327 ptr = strchr(type, '['); 3328 if (!ptr) 3329 return NULL; 3330 ptr++; 3331 3332 if (!isalpha(*ptr) && *ptr != '_') 3333 return NULL; 3334 3335 if (strncmp(eval_string, ptr, len) != 0) 3336 return NULL; 3337 3338 return ptr; 3339 } 3340 3341 static void update_event_fields(struct trace_event_call *call, 3342 struct trace_eval_map *map) 3343 { 3344 struct ftrace_event_field *field; 3345 const char *eval_string = NULL; 3346 struct list_head *head; 3347 int len = 0; 3348 char *ptr; 3349 char *str; 3350 3351 /* Dynamic events should never have field maps */ 3352 if (call->flags & TRACE_EVENT_FL_DYNAMIC) 3353 return; 3354 3355 if (map) { 3356 eval_string = map->eval_string; 3357 len = strlen(map->eval_string); 3358 } 3359 3360 head = trace_get_fields(call); 3361 list_for_each_entry(field, head, link) { 3362 str = sanitize_field_type(field->type); 3363 if (!str) 3364 return; 3365 3366 ptr = find_replacable_eval(str, eval_string, len); 3367 if (ptr) { 3368 if (str == field->type) { 3369 str = kstrdup(field->type, GFP_KERNEL); 3370 if (WARN_ON_ONCE(!str)) 3371 return; 3372 ptr = str + (ptr - field->type); 3373 } 3374 3375 ptr = eval_replace(ptr, map, len); 3376 /* enum/sizeof string smaller than value */ 3377 if (WARN_ON_ONCE(!ptr)) { 3378 kfree(str); 3379 continue; 3380 } 3381 } 3382 3383 if (str == field->type) 3384 continue; 3385 /* 3386 * If the event is part of a module, then we need to free the string 3387 * when the module is removed. Otherwise, it will stay allocated 3388 * until a reboot. 3389 */ 3390 if (call->module) 3391 add_str_to_module(call->module, str); 3392 3393 field->type = str; 3394 if (field->filter_type == FILTER_OTHER) 3395 field->filter_type = filter_assign_type(field->type); 3396 } 3397 } 3398 3399 /* Update all events for replacing eval and sanitizing */ 3400 void trace_event_update_all(struct trace_eval_map **map, int len) 3401 { 3402 struct trace_event_call *call, *p; 3403 const char *last_system = NULL; 3404 bool first = false; 3405 bool updated; 3406 int last_i; 3407 int i; 3408 3409 down_write(&trace_event_sem); 3410 list_for_each_entry_safe(call, p, &ftrace_events, list) { 3411 /* events are usually grouped together with systems */ 3412 if (!last_system || call->class->system != last_system) { 3413 first = true; 3414 last_i = 0; 3415 last_system = call->class->system; 3416 } 3417 3418 updated = false; 3419 /* 3420 * Since calls are grouped by systems, the likelihood that the 3421 * next call in the iteration belongs to the same system as the 3422 * previous call is high. As an optimization, we skip searching 3423 * for a map[] that matches the call's system if the last call 3424 * was from the same system. That's what last_i is for. If the 3425 * call has the same system as the previous call, then last_i 3426 * will be the index of the first map[] that has a matching 3427 * system. 3428 */ 3429 for (i = last_i; i < len; i++) { 3430 if (call->class->system == map[i]->system) { 3431 /* Save the first system if need be */ 3432 if (first) { 3433 last_i = i; 3434 first = false; 3435 } 3436 update_event_printk(call, map[i]); 3437 update_event_fields(call, map[i]); 3438 updated = true; 3439 } 3440 } 3441 /* If not updated yet, update field for sanitizing. */ 3442 if (!updated) 3443 update_event_fields(call, NULL); 3444 cond_resched(); 3445 } 3446 up_write(&trace_event_sem); 3447 } 3448 3449 static bool event_in_systems(struct trace_event_call *call, 3450 const char *systems) 3451 { 3452 const char *system; 3453 const char *p; 3454 3455 if (!systems) 3456 return true; 3457 3458 system = call->class->system; 3459 p = strstr(systems, system); 3460 if (!p) 3461 return false; 3462 3463 if (p != systems && !isspace(*(p - 1)) && *(p - 1) != ',') 3464 return false; 3465 3466 p += strlen(system); 3467 return !*p || isspace(*p) || *p == ','; 3468 } 3469 3470 #ifdef CONFIG_HIST_TRIGGERS 3471 /* 3472 * Wake up waiter on the hist_poll_wq from irq_work because the hist trigger 3473 * may happen in any context. 3474 */ 3475 static void hist_poll_event_irq_work(struct irq_work *work) 3476 { 3477 wake_up_all(&hist_poll_wq); 3478 } 3479 3480 DEFINE_IRQ_WORK(hist_poll_work, hist_poll_event_irq_work); 3481 DECLARE_WAIT_QUEUE_HEAD(hist_poll_wq); 3482 #endif 3483 3484 static struct trace_event_file * 3485 trace_create_new_event(struct trace_event_call *call, 3486 struct trace_array *tr) 3487 { 3488 struct trace_pid_list *no_pid_list; 3489 struct trace_pid_list *pid_list; 3490 struct trace_event_file *file; 3491 unsigned int first; 3492 3493 if (!event_in_systems(call, tr->system_names)) 3494 return NULL; 3495 3496 file = kmem_cache_alloc(file_cachep, GFP_TRACE); 3497 if (!file) 3498 return ERR_PTR(-ENOMEM); 3499 3500 pid_list = rcu_dereference_protected(tr->filtered_pids, 3501 lockdep_is_held(&event_mutex)); 3502 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids, 3503 lockdep_is_held(&event_mutex)); 3504 3505 if (!trace_pid_list_first(pid_list, &first) || 3506 !trace_pid_list_first(no_pid_list, &first)) 3507 file->flags |= EVENT_FILE_FL_PID_FILTER; 3508 3509 file->event_call = call; 3510 file->tr = tr; 3511 atomic_set(&file->sm_ref, 0); 3512 atomic_set(&file->tm_ref, 0); 3513 INIT_LIST_HEAD(&file->triggers); 3514 list_add(&file->list, &tr->events); 3515 refcount_set(&file->ref, 1); 3516 3517 return file; 3518 } 3519 3520 #define MAX_BOOT_TRIGGERS 32 3521 3522 static struct boot_triggers { 3523 const char *event; 3524 char *trigger; 3525 } bootup_triggers[MAX_BOOT_TRIGGERS]; 3526 3527 static char bootup_trigger_buf[COMMAND_LINE_SIZE]; 3528 static int nr_boot_triggers; 3529 3530 static __init int setup_trace_triggers(char *str) 3531 { 3532 char *trigger; 3533 char *buf; 3534 int i; 3535 3536 strscpy(bootup_trigger_buf, str, COMMAND_LINE_SIZE); 3537 trace_set_ring_buffer_expanded(NULL); 3538 disable_tracing_selftest("running event triggers"); 3539 3540 buf = bootup_trigger_buf; 3541 for (i = 0; i < MAX_BOOT_TRIGGERS; i++) { 3542 trigger = strsep(&buf, ","); 3543 if (!trigger) 3544 break; 3545 bootup_triggers[i].event = strsep(&trigger, "."); 3546 bootup_triggers[i].trigger = trigger; 3547 if (!bootup_triggers[i].trigger) 3548 break; 3549 } 3550 3551 nr_boot_triggers = i; 3552 return 1; 3553 } 3554 __setup("trace_trigger=", setup_trace_triggers); 3555 3556 /* Add an event to a trace directory */ 3557 static int 3558 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr) 3559 { 3560 struct trace_event_file *file; 3561 3562 file = trace_create_new_event(call, tr); 3563 /* 3564 * trace_create_new_event() returns ERR_PTR(-ENOMEM) if failed 3565 * allocation, or NULL if the event is not part of the tr->system_names. 3566 * When the event is not part of the tr->system_names, return zero, not 3567 * an error. 3568 */ 3569 if (!file) 3570 return 0; 3571 3572 if (IS_ERR(file)) 3573 return PTR_ERR(file); 3574 3575 if (eventdir_initialized) 3576 return event_create_dir(tr->event_dir, file); 3577 else 3578 return event_define_fields(call); 3579 } 3580 3581 static void trace_early_triggers(struct trace_event_file *file, const char *name) 3582 { 3583 int ret; 3584 int i; 3585 3586 for (i = 0; i < nr_boot_triggers; i++) { 3587 if (strcmp(name, bootup_triggers[i].event)) 3588 continue; 3589 mutex_lock(&event_mutex); 3590 ret = trigger_process_regex(file, bootup_triggers[i].trigger); 3591 mutex_unlock(&event_mutex); 3592 if (ret) 3593 pr_err("Failed to register trigger '%s' on event %s\n", 3594 bootup_triggers[i].trigger, 3595 bootup_triggers[i].event); 3596 } 3597 } 3598 3599 /* 3600 * Just create a descriptor for early init. A descriptor is required 3601 * for enabling events at boot. We want to enable events before 3602 * the filesystem is initialized. 3603 */ 3604 static int 3605 __trace_early_add_new_event(struct trace_event_call *call, 3606 struct trace_array *tr) 3607 { 3608 struct trace_event_file *file; 3609 int ret; 3610 3611 file = trace_create_new_event(call, tr); 3612 /* 3613 * trace_create_new_event() returns ERR_PTR(-ENOMEM) if failed 3614 * allocation, or NULL if the event is not part of the tr->system_names. 3615 * When the event is not part of the tr->system_names, return zero, not 3616 * an error. 3617 */ 3618 if (!file) 3619 return 0; 3620 3621 if (IS_ERR(file)) 3622 return PTR_ERR(file); 3623 3624 ret = event_define_fields(call); 3625 if (ret) 3626 return ret; 3627 3628 trace_early_triggers(file, trace_event_name(call)); 3629 3630 return 0; 3631 } 3632 3633 struct ftrace_module_file_ops; 3634 static void __add_event_to_tracers(struct trace_event_call *call); 3635 3636 /* Add an additional event_call dynamically */ 3637 int trace_add_event_call(struct trace_event_call *call) 3638 { 3639 int ret; 3640 lockdep_assert_held(&event_mutex); 3641 3642 guard(mutex)(&trace_types_lock); 3643 3644 ret = __register_event(call, NULL); 3645 if (ret < 0) 3646 return ret; 3647 3648 __add_event_to_tracers(call); 3649 return ret; 3650 } 3651 EXPORT_SYMBOL_GPL(trace_add_event_call); 3652 3653 /* 3654 * Must be called under locking of trace_types_lock, event_mutex and 3655 * trace_event_sem. 3656 */ 3657 static void __trace_remove_event_call(struct trace_event_call *call) 3658 { 3659 event_remove(call); 3660 trace_destroy_fields(call); 3661 } 3662 3663 static int probe_remove_event_call(struct trace_event_call *call) 3664 { 3665 struct trace_array *tr; 3666 struct trace_event_file *file; 3667 3668 #ifdef CONFIG_PERF_EVENTS 3669 if (call->perf_refcount) 3670 return -EBUSY; 3671 #endif 3672 do_for_each_event_file(tr, file) { 3673 if (file->event_call != call) 3674 continue; 3675 /* 3676 * We can't rely on ftrace_event_enable_disable(enable => 0) 3677 * we are going to do, soft mode can suppress 3678 * TRACE_REG_UNREGISTER. 3679 */ 3680 if (file->flags & EVENT_FILE_FL_ENABLED) 3681 goto busy; 3682 3683 if (file->flags & EVENT_FILE_FL_WAS_ENABLED) 3684 tr->clear_trace = true; 3685 /* 3686 * The do_for_each_event_file_safe() is 3687 * a double loop. After finding the call for this 3688 * trace_array, we use break to jump to the next 3689 * trace_array. 3690 */ 3691 break; 3692 } while_for_each_event_file(); 3693 3694 __trace_remove_event_call(call); 3695 3696 return 0; 3697 busy: 3698 /* No need to clear the trace now */ 3699 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 3700 tr->clear_trace = false; 3701 } 3702 return -EBUSY; 3703 } 3704 3705 /* Remove an event_call */ 3706 int trace_remove_event_call(struct trace_event_call *call) 3707 { 3708 int ret; 3709 3710 lockdep_assert_held(&event_mutex); 3711 3712 mutex_lock(&trace_types_lock); 3713 down_write(&trace_event_sem); 3714 ret = probe_remove_event_call(call); 3715 up_write(&trace_event_sem); 3716 mutex_unlock(&trace_types_lock); 3717 3718 return ret; 3719 } 3720 EXPORT_SYMBOL_GPL(trace_remove_event_call); 3721 3722 #define for_each_event(event, start, end) \ 3723 for (event = start; \ 3724 (unsigned long)event < (unsigned long)end; \ 3725 event++) 3726 3727 #ifdef CONFIG_MODULES 3728 static void update_mod_cache(struct trace_array *tr, struct module *mod) 3729 { 3730 struct event_mod_load *event_mod, *n; 3731 3732 list_for_each_entry_safe(event_mod, n, &tr->mod_events, list) { 3733 if (strcmp(event_mod->module, mod->name) != 0) 3734 continue; 3735 3736 __ftrace_set_clr_event_nolock(tr, event_mod->match, 3737 event_mod->system, 3738 event_mod->event, 1, mod->name); 3739 free_event_mod(event_mod); 3740 } 3741 } 3742 3743 static void update_cache_events(struct module *mod) 3744 { 3745 struct trace_array *tr; 3746 3747 list_for_each_entry(tr, &ftrace_trace_arrays, list) 3748 update_mod_cache(tr, mod); 3749 } 3750 3751 static void trace_module_add_events(struct module *mod) 3752 { 3753 struct trace_event_call **call, **start, **end; 3754 3755 if (!mod->num_trace_events) 3756 return; 3757 3758 /* Don't add infrastructure for mods without tracepoints */ 3759 if (trace_module_has_bad_taint(mod)) { 3760 pr_err("%s: module has bad taint, not creating trace events\n", 3761 mod->name); 3762 return; 3763 } 3764 3765 start = mod->trace_events; 3766 end = mod->trace_events + mod->num_trace_events; 3767 3768 for_each_event(call, start, end) { 3769 __register_event(*call, mod); 3770 __add_event_to_tracers(*call); 3771 } 3772 3773 update_cache_events(mod); 3774 } 3775 3776 static void trace_module_remove_events(struct module *mod) 3777 { 3778 struct trace_event_call *call, *p; 3779 struct module_string *modstr, *m; 3780 3781 down_write(&trace_event_sem); 3782 list_for_each_entry_safe(call, p, &ftrace_events, list) { 3783 if ((call->flags & TRACE_EVENT_FL_DYNAMIC) || !call->module) 3784 continue; 3785 if (call->module == mod) 3786 __trace_remove_event_call(call); 3787 } 3788 /* Check for any strings allocated for this module */ 3789 list_for_each_entry_safe(modstr, m, &module_strings, next) { 3790 if (modstr->module != mod) 3791 continue; 3792 list_del(&modstr->next); 3793 kfree(modstr->str); 3794 kfree(modstr); 3795 } 3796 up_write(&trace_event_sem); 3797 3798 /* 3799 * It is safest to reset the ring buffer if the module being unloaded 3800 * registered any events that were used. The only worry is if 3801 * a new module gets loaded, and takes on the same id as the events 3802 * of this module. When printing out the buffer, traced events left 3803 * over from this module may be passed to the new module events and 3804 * unexpected results may occur. 3805 */ 3806 tracing_reset_all_online_cpus_unlocked(); 3807 } 3808 3809 static int trace_module_notify(struct notifier_block *self, 3810 unsigned long val, void *data) 3811 { 3812 struct module *mod = data; 3813 3814 mutex_lock(&event_mutex); 3815 mutex_lock(&trace_types_lock); 3816 switch (val) { 3817 case MODULE_STATE_COMING: 3818 trace_module_add_events(mod); 3819 break; 3820 case MODULE_STATE_GOING: 3821 trace_module_remove_events(mod); 3822 break; 3823 } 3824 mutex_unlock(&trace_types_lock); 3825 mutex_unlock(&event_mutex); 3826 3827 return NOTIFY_OK; 3828 } 3829 3830 static struct notifier_block trace_module_nb = { 3831 .notifier_call = trace_module_notify, 3832 .priority = 1, /* higher than trace.c module notify */ 3833 }; 3834 #endif /* CONFIG_MODULES */ 3835 3836 /* Create a new event directory structure for a trace directory. */ 3837 static void 3838 __trace_add_event_dirs(struct trace_array *tr) 3839 { 3840 struct trace_event_call *call; 3841 int ret; 3842 3843 lockdep_assert_held(&trace_event_sem); 3844 3845 list_for_each_entry(call, &ftrace_events, list) { 3846 ret = __trace_add_new_event(call, tr); 3847 if (ret < 0) 3848 pr_warn("Could not create directory for event %s\n", 3849 trace_event_name(call)); 3850 } 3851 } 3852 3853 /* Returns any file that matches the system and event */ 3854 struct trace_event_file * 3855 __find_event_file(struct trace_array *tr, const char *system, const char *event) 3856 { 3857 struct trace_event_file *file; 3858 struct trace_event_call *call; 3859 const char *name; 3860 3861 list_for_each_entry(file, &tr->events, list) { 3862 3863 call = file->event_call; 3864 name = trace_event_name(call); 3865 3866 if (!name || !call->class) 3867 continue; 3868 3869 if (strcmp(event, name) == 0 && 3870 strcmp(system, call->class->system) == 0) 3871 return file; 3872 } 3873 return NULL; 3874 } 3875 3876 /* Returns valid trace event files that match system and event */ 3877 struct trace_event_file * 3878 find_event_file(struct trace_array *tr, const char *system, const char *event) 3879 { 3880 struct trace_event_file *file; 3881 3882 file = __find_event_file(tr, system, event); 3883 if (!file || !file->event_call->class->reg || 3884 file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) 3885 return NULL; 3886 3887 return file; 3888 } 3889 3890 /** 3891 * trace_get_event_file - Find and return a trace event file 3892 * @instance: The name of the trace instance containing the event 3893 * @system: The name of the system containing the event 3894 * @event: The name of the event 3895 * 3896 * Return a trace event file given the trace instance name, trace 3897 * system, and trace event name. If the instance name is NULL, it 3898 * refers to the top-level trace array. 3899 * 3900 * This function will look it up and return it if found, after calling 3901 * trace_array_get() to prevent the instance from going away, and 3902 * increment the event's module refcount to prevent it from being 3903 * removed. 3904 * 3905 * To release the file, call trace_put_event_file(), which will call 3906 * trace_array_put() and decrement the event's module refcount. 3907 * 3908 * Return: The trace event on success, ERR_PTR otherwise. 3909 */ 3910 struct trace_event_file *trace_get_event_file(const char *instance, 3911 const char *system, 3912 const char *event) 3913 { 3914 struct trace_array *tr = top_trace_array(); 3915 struct trace_event_file *file = NULL; 3916 int ret = -EINVAL; 3917 3918 if (instance) { 3919 tr = trace_array_find_get(instance); 3920 if (!tr) 3921 return ERR_PTR(-ENOENT); 3922 } else { 3923 ret = trace_array_get(tr); 3924 if (ret) 3925 return ERR_PTR(ret); 3926 } 3927 3928 guard(mutex)(&event_mutex); 3929 3930 file = find_event_file(tr, system, event); 3931 if (!file) { 3932 trace_array_put(tr); 3933 return ERR_PTR(-EINVAL); 3934 } 3935 3936 /* Don't let event modules unload while in use */ 3937 ret = trace_event_try_get_ref(file->event_call); 3938 if (!ret) { 3939 trace_array_put(tr); 3940 return ERR_PTR(-EBUSY); 3941 } 3942 3943 return file; 3944 } 3945 EXPORT_SYMBOL_GPL(trace_get_event_file); 3946 3947 /** 3948 * trace_put_event_file - Release a file from trace_get_event_file() 3949 * @file: The trace event file 3950 * 3951 * If a file was retrieved using trace_get_event_file(), this should 3952 * be called when it's no longer needed. It will cancel the previous 3953 * trace_array_get() called by that function, and decrement the 3954 * event's module refcount. 3955 */ 3956 void trace_put_event_file(struct trace_event_file *file) 3957 { 3958 mutex_lock(&event_mutex); 3959 trace_event_put_ref(file->event_call); 3960 mutex_unlock(&event_mutex); 3961 3962 trace_array_put(file->tr); 3963 } 3964 EXPORT_SYMBOL_GPL(trace_put_event_file); 3965 3966 #ifdef CONFIG_DYNAMIC_FTRACE 3967 3968 /* Avoid typos */ 3969 #define ENABLE_EVENT_STR "enable_event" 3970 #define DISABLE_EVENT_STR "disable_event" 3971 3972 struct event_probe_data { 3973 struct trace_event_file *file; 3974 unsigned long count; 3975 int ref; 3976 bool enable; 3977 }; 3978 3979 static void update_event_probe(struct event_probe_data *data) 3980 { 3981 if (data->enable) 3982 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags); 3983 else 3984 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags); 3985 } 3986 3987 static void 3988 event_enable_probe(unsigned long ip, unsigned long parent_ip, 3989 struct trace_array *tr, struct ftrace_probe_ops *ops, 3990 void *data) 3991 { 3992 struct ftrace_func_mapper *mapper = data; 3993 struct event_probe_data *edata; 3994 void **pdata; 3995 3996 pdata = ftrace_func_mapper_find_ip(mapper, ip); 3997 if (!pdata || !*pdata) 3998 return; 3999 4000 edata = *pdata; 4001 update_event_probe(edata); 4002 } 4003 4004 static void 4005 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, 4006 struct trace_array *tr, struct ftrace_probe_ops *ops, 4007 void *data) 4008 { 4009 struct ftrace_func_mapper *mapper = data; 4010 struct event_probe_data *edata; 4011 void **pdata; 4012 4013 pdata = ftrace_func_mapper_find_ip(mapper, ip); 4014 if (!pdata || !*pdata) 4015 return; 4016 4017 edata = *pdata; 4018 4019 if (!edata->count) 4020 return; 4021 4022 /* Skip if the event is in a state we want to switch to */ 4023 if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED)) 4024 return; 4025 4026 if (edata->count != -1) 4027 (edata->count)--; 4028 4029 update_event_probe(edata); 4030 } 4031 4032 static int 4033 event_enable_print(struct seq_file *m, unsigned long ip, 4034 struct ftrace_probe_ops *ops, void *data) 4035 { 4036 struct ftrace_func_mapper *mapper = data; 4037 struct event_probe_data *edata; 4038 void **pdata; 4039 4040 pdata = ftrace_func_mapper_find_ip(mapper, ip); 4041 4042 if (WARN_ON_ONCE(!pdata || !*pdata)) 4043 return 0; 4044 4045 edata = *pdata; 4046 4047 seq_printf(m, "%ps:", (void *)ip); 4048 4049 seq_printf(m, "%s:%s:%s", 4050 edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR, 4051 edata->file->event_call->class->system, 4052 trace_event_name(edata->file->event_call)); 4053 4054 if (edata->count == -1) 4055 seq_puts(m, ":unlimited\n"); 4056 else 4057 seq_printf(m, ":count=%ld\n", edata->count); 4058 4059 return 0; 4060 } 4061 4062 static int 4063 event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr, 4064 unsigned long ip, void *init_data, void **data) 4065 { 4066 struct ftrace_func_mapper *mapper = *data; 4067 struct event_probe_data *edata = init_data; 4068 int ret; 4069 4070 if (!mapper) { 4071 mapper = allocate_ftrace_func_mapper(); 4072 if (!mapper) 4073 return -ENODEV; 4074 *data = mapper; 4075 } 4076 4077 ret = ftrace_func_mapper_add_ip(mapper, ip, edata); 4078 if (ret < 0) 4079 return ret; 4080 4081 edata->ref++; 4082 4083 return 0; 4084 } 4085 4086 static int free_probe_data(void *data) 4087 { 4088 struct event_probe_data *edata = data; 4089 4090 edata->ref--; 4091 if (!edata->ref) { 4092 /* Remove soft mode */ 4093 __ftrace_event_enable_disable(edata->file, 0, 1); 4094 trace_event_put_ref(edata->file->event_call); 4095 kfree(edata); 4096 } 4097 return 0; 4098 } 4099 4100 static void 4101 event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr, 4102 unsigned long ip, void *data) 4103 { 4104 struct ftrace_func_mapper *mapper = data; 4105 struct event_probe_data *edata; 4106 4107 if (!ip) { 4108 if (!mapper) 4109 return; 4110 free_ftrace_func_mapper(mapper, free_probe_data); 4111 return; 4112 } 4113 4114 edata = ftrace_func_mapper_remove_ip(mapper, ip); 4115 4116 if (WARN_ON_ONCE(!edata)) 4117 return; 4118 4119 if (WARN_ON_ONCE(edata->ref <= 0)) 4120 return; 4121 4122 free_probe_data(edata); 4123 } 4124 4125 static struct ftrace_probe_ops event_enable_probe_ops = { 4126 .func = event_enable_probe, 4127 .print = event_enable_print, 4128 .init = event_enable_init, 4129 .free = event_enable_free, 4130 }; 4131 4132 static struct ftrace_probe_ops event_enable_count_probe_ops = { 4133 .func = event_enable_count_probe, 4134 .print = event_enable_print, 4135 .init = event_enable_init, 4136 .free = event_enable_free, 4137 }; 4138 4139 static struct ftrace_probe_ops event_disable_probe_ops = { 4140 .func = event_enable_probe, 4141 .print = event_enable_print, 4142 .init = event_enable_init, 4143 .free = event_enable_free, 4144 }; 4145 4146 static struct ftrace_probe_ops event_disable_count_probe_ops = { 4147 .func = event_enable_count_probe, 4148 .print = event_enable_print, 4149 .init = event_enable_init, 4150 .free = event_enable_free, 4151 }; 4152 4153 static int 4154 event_enable_func(struct trace_array *tr, struct ftrace_hash *hash, 4155 char *glob, char *cmd, char *param, int enabled) 4156 { 4157 struct trace_event_file *file; 4158 struct ftrace_probe_ops *ops; 4159 struct event_probe_data *data; 4160 unsigned long count = -1; 4161 const char *system; 4162 const char *event; 4163 char *number; 4164 bool enable; 4165 int ret; 4166 4167 if (!tr) 4168 return -ENODEV; 4169 4170 /* hash funcs only work with set_ftrace_filter */ 4171 if (!enabled || !param) 4172 return -EINVAL; 4173 4174 system = strsep(¶m, ":"); 4175 if (!param) 4176 return -EINVAL; 4177 4178 event = strsep(¶m, ":"); 4179 4180 guard(mutex)(&event_mutex); 4181 4182 file = find_event_file(tr, system, event); 4183 if (!file) 4184 return -EINVAL; 4185 4186 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0; 4187 4188 if (enable) 4189 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops; 4190 else 4191 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops; 4192 4193 if (glob[0] == '!') 4194 return unregister_ftrace_function_probe_func(glob+1, tr, ops); 4195 4196 if (param) { 4197 number = strsep(¶m, ":"); 4198 4199 if (!strlen(number)) 4200 return -EINVAL; 4201 4202 /* 4203 * We use the callback data field (which is a pointer) 4204 * as our counter. 4205 */ 4206 ret = kstrtoul(number, 0, &count); 4207 if (ret) 4208 return ret; 4209 } 4210 4211 /* Don't let event modules unload while probe registered */ 4212 ret = trace_event_try_get_ref(file->event_call); 4213 if (!ret) 4214 return -EBUSY; 4215 4216 ret = __ftrace_event_enable_disable(file, 1, 1); 4217 if (ret < 0) 4218 goto out_put; 4219 4220 ret = -ENOMEM; 4221 data = kzalloc(sizeof(*data), GFP_KERNEL); 4222 if (!data) 4223 goto out_put; 4224 4225 data->enable = enable; 4226 data->count = count; 4227 data->file = file; 4228 4229 ret = register_ftrace_function_probe(glob, tr, ops, data); 4230 /* 4231 * The above returns on success the # of functions enabled, 4232 * but if it didn't find any functions it returns zero. 4233 * Consider no functions a failure too. 4234 */ 4235 4236 /* Just return zero, not the number of enabled functions */ 4237 if (ret > 0) 4238 return 0; 4239 4240 kfree(data); 4241 4242 if (!ret) 4243 ret = -ENOENT; 4244 4245 __ftrace_event_enable_disable(file, 0, 1); 4246 out_put: 4247 trace_event_put_ref(file->event_call); 4248 return ret; 4249 } 4250 4251 static struct ftrace_func_command event_enable_cmd = { 4252 .name = ENABLE_EVENT_STR, 4253 .func = event_enable_func, 4254 }; 4255 4256 static struct ftrace_func_command event_disable_cmd = { 4257 .name = DISABLE_EVENT_STR, 4258 .func = event_enable_func, 4259 }; 4260 4261 static __init int register_event_cmds(void) 4262 { 4263 int ret; 4264 4265 ret = register_ftrace_command(&event_enable_cmd); 4266 if (WARN_ON(ret < 0)) 4267 return ret; 4268 ret = register_ftrace_command(&event_disable_cmd); 4269 if (WARN_ON(ret < 0)) 4270 unregister_ftrace_command(&event_enable_cmd); 4271 return ret; 4272 } 4273 #else 4274 static inline int register_event_cmds(void) { return 0; } 4275 #endif /* CONFIG_DYNAMIC_FTRACE */ 4276 4277 /* 4278 * The top level array and trace arrays created by boot-time tracing 4279 * have already had its trace_event_file descriptors created in order 4280 * to allow for early events to be recorded. 4281 * This function is called after the tracefs has been initialized, 4282 * and we now have to create the files associated to the events. 4283 */ 4284 static void __trace_early_add_event_dirs(struct trace_array *tr) 4285 { 4286 struct trace_event_file *file; 4287 int ret; 4288 4289 4290 list_for_each_entry(file, &tr->events, list) { 4291 ret = event_create_dir(tr->event_dir, file); 4292 if (ret < 0) 4293 pr_warn("Could not create directory for event %s\n", 4294 trace_event_name(file->event_call)); 4295 } 4296 } 4297 4298 /* 4299 * For early boot up, the top trace array and the trace arrays created 4300 * by boot-time tracing require to have a list of events that can be 4301 * enabled. This must be done before the filesystem is set up in order 4302 * to allow events to be traced early. 4303 */ 4304 void __trace_early_add_events(struct trace_array *tr) 4305 { 4306 struct trace_event_call *call; 4307 int ret; 4308 4309 list_for_each_entry(call, &ftrace_events, list) { 4310 /* Early boot up should not have any modules loaded */ 4311 if (!(call->flags & TRACE_EVENT_FL_DYNAMIC) && 4312 WARN_ON_ONCE(call->module)) 4313 continue; 4314 4315 ret = __trace_early_add_new_event(call, tr); 4316 if (ret < 0) 4317 pr_warn("Could not create early event %s\n", 4318 trace_event_name(call)); 4319 } 4320 } 4321 4322 /* Remove the event directory structure for a trace directory. */ 4323 static void 4324 __trace_remove_event_dirs(struct trace_array *tr) 4325 { 4326 struct trace_event_file *file, *next; 4327 4328 list_for_each_entry_safe(file, next, &tr->events, list) 4329 remove_event_file_dir(file); 4330 } 4331 4332 static void __add_event_to_tracers(struct trace_event_call *call) 4333 { 4334 struct trace_array *tr; 4335 4336 list_for_each_entry(tr, &ftrace_trace_arrays, list) 4337 __trace_add_new_event(call, tr); 4338 } 4339 4340 extern struct trace_event_call *__start_ftrace_events[]; 4341 extern struct trace_event_call *__stop_ftrace_events[]; 4342 4343 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata; 4344 4345 static __init int setup_trace_event(char *str) 4346 { 4347 strscpy(bootup_event_buf, str, COMMAND_LINE_SIZE); 4348 trace_set_ring_buffer_expanded(NULL); 4349 disable_tracing_selftest("running event tracing"); 4350 4351 return 1; 4352 } 4353 __setup("trace_event=", setup_trace_event); 4354 4355 static int events_callback(const char *name, umode_t *mode, void **data, 4356 const struct file_operations **fops) 4357 { 4358 if (strcmp(name, "enable") == 0) { 4359 *mode = TRACE_MODE_WRITE; 4360 *fops = &ftrace_tr_enable_fops; 4361 return 1; 4362 } 4363 4364 if (strcmp(name, "header_page") == 0) { 4365 *mode = TRACE_MODE_READ; 4366 *fops = &ftrace_show_header_page_fops; 4367 4368 } else if (strcmp(name, "header_event") == 0) { 4369 *mode = TRACE_MODE_READ; 4370 *fops = &ftrace_show_header_event_fops; 4371 } else 4372 return 0; 4373 4374 return 1; 4375 } 4376 4377 /* Expects to have event_mutex held when called */ 4378 static int 4379 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr) 4380 { 4381 struct eventfs_inode *e_events; 4382 struct dentry *entry; 4383 int nr_entries; 4384 static struct eventfs_entry events_entries[] = { 4385 { 4386 .name = "enable", 4387 .callback = events_callback, 4388 }, 4389 { 4390 .name = "header_page", 4391 .callback = events_callback, 4392 }, 4393 { 4394 .name = "header_event", 4395 .callback = events_callback, 4396 }, 4397 }; 4398 4399 entry = trace_create_file("set_event", TRACE_MODE_WRITE, parent, 4400 tr, &ftrace_set_event_fops); 4401 if (!entry) 4402 return -ENOMEM; 4403 4404 nr_entries = ARRAY_SIZE(events_entries); 4405 4406 e_events = eventfs_create_events_dir("events", parent, events_entries, 4407 nr_entries, tr); 4408 if (IS_ERR(e_events)) { 4409 pr_warn("Could not create tracefs 'events' directory\n"); 4410 return -ENOMEM; 4411 } 4412 4413 /* There are not as crucial, just warn if they are not created */ 4414 4415 trace_create_file("set_event_pid", TRACE_MODE_WRITE, parent, 4416 tr, &ftrace_set_event_pid_fops); 4417 4418 trace_create_file("set_event_notrace_pid", 4419 TRACE_MODE_WRITE, parent, tr, 4420 &ftrace_set_event_notrace_pid_fops); 4421 4422 tr->event_dir = e_events; 4423 4424 return 0; 4425 } 4426 4427 /** 4428 * event_trace_add_tracer - add a instance of a trace_array to events 4429 * @parent: The parent dentry to place the files/directories for events in 4430 * @tr: The trace array associated with these events 4431 * 4432 * When a new instance is created, it needs to set up its events 4433 * directory, as well as other files associated with events. It also 4434 * creates the event hierarchy in the @parent/events directory. 4435 * 4436 * Returns 0 on success. 4437 * 4438 * Must be called with event_mutex held. 4439 */ 4440 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr) 4441 { 4442 int ret; 4443 4444 lockdep_assert_held(&event_mutex); 4445 4446 ret = create_event_toplevel_files(parent, tr); 4447 if (ret) 4448 goto out; 4449 4450 down_write(&trace_event_sem); 4451 /* If tr already has the event list, it is initialized in early boot. */ 4452 if (unlikely(!list_empty(&tr->events))) 4453 __trace_early_add_event_dirs(tr); 4454 else 4455 __trace_add_event_dirs(tr); 4456 up_write(&trace_event_sem); 4457 4458 out: 4459 return ret; 4460 } 4461 4462 /* 4463 * The top trace array already had its file descriptors created. 4464 * Now the files themselves need to be created. 4465 */ 4466 static __init int 4467 early_event_add_tracer(struct dentry *parent, struct trace_array *tr) 4468 { 4469 int ret; 4470 4471 guard(mutex)(&event_mutex); 4472 4473 ret = create_event_toplevel_files(parent, tr); 4474 if (ret) 4475 return ret; 4476 4477 down_write(&trace_event_sem); 4478 __trace_early_add_event_dirs(tr); 4479 up_write(&trace_event_sem); 4480 4481 return 0; 4482 } 4483 4484 /* Must be called with event_mutex held */ 4485 int event_trace_del_tracer(struct trace_array *tr) 4486 { 4487 lockdep_assert_held(&event_mutex); 4488 4489 /* Disable any event triggers and associated soft-disabled events */ 4490 clear_event_triggers(tr); 4491 4492 /* Clear the pid list */ 4493 __ftrace_clear_event_pids(tr, TRACE_PIDS | TRACE_NO_PIDS); 4494 4495 /* Disable any running events */ 4496 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0, NULL); 4497 4498 /* Make sure no more events are being executed */ 4499 tracepoint_synchronize_unregister(); 4500 4501 down_write(&trace_event_sem); 4502 __trace_remove_event_dirs(tr); 4503 eventfs_remove_events_dir(tr->event_dir); 4504 up_write(&trace_event_sem); 4505 4506 tr->event_dir = NULL; 4507 4508 return 0; 4509 } 4510 4511 static __init int event_trace_memsetup(void) 4512 { 4513 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC); 4514 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC); 4515 return 0; 4516 } 4517 4518 __init void 4519 early_enable_events(struct trace_array *tr, char *buf, bool disable_first) 4520 { 4521 char *token; 4522 int ret; 4523 4524 while (true) { 4525 token = strsep(&buf, ","); 4526 4527 if (!token) 4528 break; 4529 4530 if (*token) { 4531 /* Restarting syscalls requires that we stop them first */ 4532 if (disable_first) 4533 ftrace_set_clr_event(tr, token, 0); 4534 4535 ret = ftrace_set_clr_event(tr, token, 1); 4536 if (ret) 4537 pr_warn("Failed to enable trace event: %s\n", token); 4538 } 4539 4540 /* Put back the comma to allow this to be called again */ 4541 if (buf) 4542 *(buf - 1) = ','; 4543 } 4544 } 4545 4546 static __init int event_trace_enable(void) 4547 { 4548 struct trace_array *tr = top_trace_array(); 4549 struct trace_event_call **iter, *call; 4550 int ret; 4551 4552 if (!tr) 4553 return -ENODEV; 4554 4555 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) { 4556 4557 call = *iter; 4558 ret = event_init(call); 4559 if (!ret) 4560 list_add(&call->list, &ftrace_events); 4561 } 4562 4563 register_trigger_cmds(); 4564 4565 /* 4566 * We need the top trace array to have a working set of trace 4567 * points at early init, before the debug files and directories 4568 * are created. Create the file entries now, and attach them 4569 * to the actual file dentries later. 4570 */ 4571 __trace_early_add_events(tr); 4572 4573 early_enable_events(tr, bootup_event_buf, false); 4574 4575 trace_printk_start_comm(); 4576 4577 register_event_cmds(); 4578 4579 4580 return 0; 4581 } 4582 4583 /* 4584 * event_trace_enable() is called from trace_event_init() first to 4585 * initialize events and perhaps start any events that are on the 4586 * command line. Unfortunately, there are some events that will not 4587 * start this early, like the system call tracepoints that need 4588 * to set the %SYSCALL_WORK_SYSCALL_TRACEPOINT flag of pid 1. But 4589 * event_trace_enable() is called before pid 1 starts, and this flag 4590 * is never set, making the syscall tracepoint never get reached, but 4591 * the event is enabled regardless (and not doing anything). 4592 */ 4593 static __init int event_trace_enable_again(void) 4594 { 4595 struct trace_array *tr; 4596 4597 tr = top_trace_array(); 4598 if (!tr) 4599 return -ENODEV; 4600 4601 early_enable_events(tr, bootup_event_buf, true); 4602 4603 return 0; 4604 } 4605 4606 early_initcall(event_trace_enable_again); 4607 4608 /* Init fields which doesn't related to the tracefs */ 4609 static __init int event_trace_init_fields(void) 4610 { 4611 if (trace_define_generic_fields()) 4612 pr_warn("tracing: Failed to allocated generic fields"); 4613 4614 if (trace_define_common_fields()) 4615 pr_warn("tracing: Failed to allocate common fields"); 4616 4617 return 0; 4618 } 4619 4620 __init int event_trace_init(void) 4621 { 4622 struct trace_array *tr; 4623 int ret; 4624 4625 tr = top_trace_array(); 4626 if (!tr) 4627 return -ENODEV; 4628 4629 trace_create_file("available_events", TRACE_MODE_READ, 4630 NULL, tr, &ftrace_avail_fops); 4631 4632 ret = early_event_add_tracer(NULL, tr); 4633 if (ret) 4634 return ret; 4635 4636 #ifdef CONFIG_MODULES 4637 ret = register_module_notifier(&trace_module_nb); 4638 if (ret) 4639 pr_warn("Failed to register trace events module notifier\n"); 4640 #endif 4641 4642 eventdir_initialized = true; 4643 4644 return 0; 4645 } 4646 4647 void __init trace_event_init(void) 4648 { 4649 event_trace_memsetup(); 4650 init_ftrace_syscalls(); 4651 event_trace_enable(); 4652 event_trace_init_fields(); 4653 } 4654 4655 #ifdef CONFIG_EVENT_TRACE_STARTUP_TEST 4656 4657 static DEFINE_SPINLOCK(test_spinlock); 4658 static DEFINE_SPINLOCK(test_spinlock_irq); 4659 static DEFINE_MUTEX(test_mutex); 4660 4661 static __init void test_work(struct work_struct *dummy) 4662 { 4663 spin_lock(&test_spinlock); 4664 spin_lock_irq(&test_spinlock_irq); 4665 udelay(1); 4666 spin_unlock_irq(&test_spinlock_irq); 4667 spin_unlock(&test_spinlock); 4668 4669 mutex_lock(&test_mutex); 4670 msleep(1); 4671 mutex_unlock(&test_mutex); 4672 } 4673 4674 static __init int event_test_thread(void *unused) 4675 { 4676 void *test_malloc; 4677 4678 test_malloc = kmalloc(1234, GFP_KERNEL); 4679 if (!test_malloc) 4680 pr_info("failed to kmalloc\n"); 4681 4682 schedule_on_each_cpu(test_work); 4683 4684 kfree(test_malloc); 4685 4686 set_current_state(TASK_INTERRUPTIBLE); 4687 while (!kthread_should_stop()) { 4688 schedule(); 4689 set_current_state(TASK_INTERRUPTIBLE); 4690 } 4691 __set_current_state(TASK_RUNNING); 4692 4693 return 0; 4694 } 4695 4696 /* 4697 * Do various things that may trigger events. 4698 */ 4699 static __init void event_test_stuff(void) 4700 { 4701 struct task_struct *test_thread; 4702 4703 test_thread = kthread_run(event_test_thread, NULL, "test-events"); 4704 msleep(1); 4705 kthread_stop(test_thread); 4706 } 4707 4708 /* 4709 * For every trace event defined, we will test each trace point separately, 4710 * and then by groups, and finally all trace points. 4711 */ 4712 static __init void event_trace_self_tests(void) 4713 { 4714 struct trace_subsystem_dir *dir; 4715 struct trace_event_file *file; 4716 struct trace_event_call *call; 4717 struct event_subsystem *system; 4718 struct trace_array *tr; 4719 int ret; 4720 4721 tr = top_trace_array(); 4722 if (!tr) 4723 return; 4724 4725 pr_info("Running tests on trace events:\n"); 4726 4727 list_for_each_entry(file, &tr->events, list) { 4728 4729 call = file->event_call; 4730 4731 /* Only test those that have a probe */ 4732 if (!call->class || !call->class->probe) 4733 continue; 4734 4735 /* 4736 * Testing syscall events here is pretty useless, but 4737 * we still do it if configured. But this is time consuming. 4738 * What we really need is a user thread to perform the 4739 * syscalls as we test. 4740 */ 4741 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS 4742 if (call->class->system && 4743 strcmp(call->class->system, "syscalls") == 0) 4744 continue; 4745 #endif 4746 4747 pr_info("Testing event %s: ", trace_event_name(call)); 4748 4749 /* 4750 * If an event is already enabled, someone is using 4751 * it and the self test should not be on. 4752 */ 4753 if (file->flags & EVENT_FILE_FL_ENABLED) { 4754 pr_warn("Enabled event during self test!\n"); 4755 WARN_ON_ONCE(1); 4756 continue; 4757 } 4758 4759 ftrace_event_enable_disable(file, 1); 4760 event_test_stuff(); 4761 ftrace_event_enable_disable(file, 0); 4762 4763 pr_cont("OK\n"); 4764 } 4765 4766 /* Now test at the sub system level */ 4767 4768 pr_info("Running tests on trace event systems:\n"); 4769 4770 list_for_each_entry(dir, &tr->systems, list) { 4771 4772 system = dir->subsystem; 4773 4774 /* the ftrace system is special, skip it */ 4775 if (strcmp(system->name, "ftrace") == 0) 4776 continue; 4777 4778 pr_info("Testing event system %s: ", system->name); 4779 4780 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1, NULL); 4781 if (WARN_ON_ONCE(ret)) { 4782 pr_warn("error enabling system %s\n", 4783 system->name); 4784 continue; 4785 } 4786 4787 event_test_stuff(); 4788 4789 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0, NULL); 4790 if (WARN_ON_ONCE(ret)) { 4791 pr_warn("error disabling system %s\n", 4792 system->name); 4793 continue; 4794 } 4795 4796 pr_cont("OK\n"); 4797 } 4798 4799 /* Test with all events enabled */ 4800 4801 pr_info("Running tests on all trace events:\n"); 4802 pr_info("Testing all events: "); 4803 4804 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1, NULL); 4805 if (WARN_ON_ONCE(ret)) { 4806 pr_warn("error enabling all events\n"); 4807 return; 4808 } 4809 4810 event_test_stuff(); 4811 4812 /* reset sysname */ 4813 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0, NULL); 4814 if (WARN_ON_ONCE(ret)) { 4815 pr_warn("error disabling all events\n"); 4816 return; 4817 } 4818 4819 pr_cont("OK\n"); 4820 } 4821 4822 #ifdef CONFIG_FUNCTION_TRACER 4823 4824 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable); 4825 4826 static struct trace_event_file event_trace_file __initdata; 4827 4828 static void __init 4829 function_test_events_call(unsigned long ip, unsigned long parent_ip, 4830 struct ftrace_ops *op, struct ftrace_regs *regs) 4831 { 4832 struct trace_buffer *buffer; 4833 struct ring_buffer_event *event; 4834 struct ftrace_entry *entry; 4835 unsigned int trace_ctx; 4836 long disabled; 4837 int cpu; 4838 4839 trace_ctx = tracing_gen_ctx(); 4840 preempt_disable_notrace(); 4841 cpu = raw_smp_processor_id(); 4842 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu)); 4843 4844 if (disabled != 1) 4845 goto out; 4846 4847 event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file, 4848 TRACE_FN, sizeof(*entry), 4849 trace_ctx); 4850 if (!event) 4851 goto out; 4852 entry = ring_buffer_event_data(event); 4853 entry->ip = ip; 4854 entry->parent_ip = parent_ip; 4855 4856 event_trigger_unlock_commit(&event_trace_file, buffer, event, 4857 entry, trace_ctx); 4858 out: 4859 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu)); 4860 preempt_enable_notrace(); 4861 } 4862 4863 static struct ftrace_ops trace_ops __initdata = 4864 { 4865 .func = function_test_events_call, 4866 }; 4867 4868 static __init void event_trace_self_test_with_function(void) 4869 { 4870 int ret; 4871 4872 event_trace_file.tr = top_trace_array(); 4873 if (WARN_ON(!event_trace_file.tr)) 4874 return; 4875 4876 ret = register_ftrace_function(&trace_ops); 4877 if (WARN_ON(ret < 0)) { 4878 pr_info("Failed to enable function tracer for event tests\n"); 4879 return; 4880 } 4881 pr_info("Running tests again, along with the function tracer\n"); 4882 event_trace_self_tests(); 4883 unregister_ftrace_function(&trace_ops); 4884 } 4885 #else 4886 static __init void event_trace_self_test_with_function(void) 4887 { 4888 } 4889 #endif 4890 4891 static __init int event_trace_self_tests_init(void) 4892 { 4893 if (!tracing_selftest_disabled) { 4894 event_trace_self_tests(); 4895 event_trace_self_test_with_function(); 4896 } 4897 4898 return 0; 4899 } 4900 4901 late_initcall(event_trace_self_tests_init); 4902 4903 #endif 4904