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