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