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